annotate .svn/pristine/1b/1bd16d41e7893f1a65ab6da01777dc7a3b6b060d.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

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