annotate .svn/pristine/48/4881b5ad6e4efa4165188246776dbe492c56e6df.svn-base @ 1295:622f24f53b42 redmine-2.3

Update to Redmine SVN revision 11972 on 2.3-stable branch
author Chris Cannam
date Fri, 14 Jun 2013 09:02:21 +0100
parents
children
rev   line source
Chris@1295 1 # Redmine - project management software
Chris@1295 2 # Copyright (C) 2006-2013 Jean-Philippe Lang
Chris@1295 3 #
Chris@1295 4 # This program is free software; you can redistribute it and/or
Chris@1295 5 # modify it under the terms of the GNU General Public License
Chris@1295 6 # as published by the Free Software Foundation; either version 2
Chris@1295 7 # of the License, or (at your option) any later version.
Chris@1295 8 #
Chris@1295 9 # This program is distributed in the hope that it will be useful,
Chris@1295 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1295 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1295 12 # GNU General Public License for more details.
Chris@1295 13 #
Chris@1295 14 # You should have received a copy of the GNU General Public License
Chris@1295 15 # along with this program; if not, write to the Free Software
Chris@1295 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1295 17
Chris@1295 18 class Principal < ActiveRecord::Base
Chris@1295 19 self.table_name = "#{table_name_prefix}users#{table_name_suffix}"
Chris@1295 20
Chris@1295 21 # Account statuses
Chris@1295 22 STATUS_ANONYMOUS = 0
Chris@1295 23 STATUS_ACTIVE = 1
Chris@1295 24 STATUS_REGISTERED = 2
Chris@1295 25 STATUS_LOCKED = 3
Chris@1295 26
Chris@1295 27 has_many :members, :foreign_key => 'user_id', :dependent => :destroy
Chris@1295 28 has_many :memberships, :class_name => 'Member', :foreign_key => 'user_id', :include => [ :project, :roles ], :conditions => "#{Project.table_name}.status<>#{Project::STATUS_ARCHIVED}", :order => "#{Project.table_name}.name"
Chris@1295 29 has_many :projects, :through => :memberships
Chris@1295 30 has_many :issue_categories, :foreign_key => 'assigned_to_id', :dependent => :nullify
Chris@1295 31
Chris@1295 32 # Groups and active users
Chris@1295 33 scope :active, lambda { where(:status => STATUS_ACTIVE) }
Chris@1295 34
Chris@1295 35 scope :like, lambda {|q|
Chris@1295 36 q = q.to_s
Chris@1295 37 if q.blank?
Chris@1295 38 where({})
Chris@1295 39 else
Chris@1295 40 pattern = "%#{q}%"
Chris@1295 41 sql = %w(login firstname lastname mail).map {|column| "LOWER(#{table_name}.#{column}) LIKE LOWER(:p)"}.join(" OR ")
Chris@1295 42 params = {:p => pattern}
Chris@1295 43 if q =~ /^(.+)\s+(.+)$/
Chris@1295 44 a, b = "#{$1}%", "#{$2}%"
Chris@1295 45 sql << " OR (LOWER(#{table_name}.firstname) LIKE LOWER(:a) AND LOWER(#{table_name}.lastname) LIKE LOWER(:b))"
Chris@1295 46 sql << " OR (LOWER(#{table_name}.firstname) LIKE LOWER(:b) AND LOWER(#{table_name}.lastname) LIKE LOWER(:a))"
Chris@1295 47 params.merge!(:a => a, :b => b)
Chris@1295 48 end
Chris@1295 49 where(sql, params)
Chris@1295 50 end
Chris@1295 51 }
Chris@1295 52
Chris@1295 53 # Principals that are members of a collection of projects
Chris@1295 54 scope :member_of, lambda {|projects|
Chris@1295 55 projects = [projects] unless projects.is_a?(Array)
Chris@1295 56 if projects.empty?
Chris@1295 57 where("1=0")
Chris@1295 58 else
Chris@1295 59 ids = projects.map(&:id)
Chris@1295 60 active.uniq.joins(:members).where("#{Member.table_name}.project_id IN (?)", ids)
Chris@1295 61 end
Chris@1295 62 }
Chris@1295 63 # Principals that are not members of projects
Chris@1295 64 scope :not_member_of, lambda {|projects|
Chris@1295 65 projects = [projects] unless projects.is_a?(Array)
Chris@1295 66 if projects.empty?
Chris@1295 67 where("1=0")
Chris@1295 68 else
Chris@1295 69 ids = projects.map(&:id)
Chris@1295 70 where("#{Principal.table_name}.id NOT IN (SELECT DISTINCT user_id FROM #{Member.table_name} WHERE project_id IN (?))", ids)
Chris@1295 71 end
Chris@1295 72 }
Chris@1295 73 scope :sorted, lambda { order(*Principal.fields_for_order_statement)}
Chris@1295 74
Chris@1295 75 before_create :set_default_empty_values
Chris@1295 76
Chris@1295 77 def name(formatter = nil)
Chris@1295 78 to_s
Chris@1295 79 end
Chris@1295 80
Chris@1295 81 def <=>(principal)
Chris@1295 82 if principal.nil?
Chris@1295 83 -1
Chris@1295 84 elsif self.class.name == principal.class.name
Chris@1295 85 self.to_s.downcase <=> principal.to_s.downcase
Chris@1295 86 else
Chris@1295 87 # groups after users
Chris@1295 88 principal.class.name <=> self.class.name
Chris@1295 89 end
Chris@1295 90 end
Chris@1295 91
Chris@1295 92 # Returns an array of fields names than can be used to make an order statement for principals.
Chris@1295 93 # Users are sorted before Groups.
Chris@1295 94 # Examples:
Chris@1295 95 def self.fields_for_order_statement(table=nil)
Chris@1295 96 table ||= table_name
Chris@1295 97 columns = ['type DESC'] + (User.name_formatter[:order] - ['id']) + ['lastname', 'id']
Chris@1295 98 columns.uniq.map {|field| "#{table}.#{field}"}
Chris@1295 99 end
Chris@1295 100
Chris@1295 101 protected
Chris@1295 102
Chris@1295 103 # Make sure we don't try to insert NULL values (see #4632)
Chris@1295 104 def set_default_empty_values
Chris@1295 105 self.login ||= ''
Chris@1295 106 self.hashed_password ||= ''
Chris@1295 107 self.firstname ||= ''
Chris@1295 108 self.lastname ||= ''
Chris@1295 109 self.mail ||= ''
Chris@1295 110 true
Chris@1295 111 end
Chris@1295 112 end