Chris@1494: # Redmine - project management software Chris@1494: # Copyright (C) 2006-2014 Jean-Philippe Lang Chris@1494: # Chris@1494: # This program is free software; you can redistribute it and/or Chris@1494: # modify it under the terms of the GNU General Public License Chris@1494: # as published by the Free Software Foundation; either version 2 Chris@1494: # of the License, or (at your option) any later version. Chris@1494: # Chris@1494: # This program is distributed in the hope that it will be useful, Chris@1494: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@1494: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@1494: # GNU General Public License for more details. Chris@1494: # Chris@1494: # You should have received a copy of the GNU General Public License Chris@1494: # along with this program; if not, write to the Free Software Chris@1494: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@1494: Chris@1494: class Principal < ActiveRecord::Base Chris@1494: self.table_name = "#{table_name_prefix}users#{table_name_suffix}" Chris@1494: Chris@1494: # Account statuses Chris@1494: STATUS_ANONYMOUS = 0 Chris@1494: STATUS_ACTIVE = 1 Chris@1494: STATUS_REGISTERED = 2 Chris@1494: STATUS_LOCKED = 3 Chris@1494: Chris@1494: has_many :members, :foreign_key => 'user_id', :dependent => :destroy Chris@1494: 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: has_many :projects, :through => :memberships Chris@1494: has_many :issue_categories, :foreign_key => 'assigned_to_id', :dependent => :nullify Chris@1494: Chris@1494: # Groups and active users Chris@1494: scope :active, lambda { where(:status => STATUS_ACTIVE) } Chris@1494: Chris@1494: scope :like, lambda {|q| Chris@1494: q = q.to_s Chris@1494: if q.blank? Chris@1494: where({}) Chris@1494: else Chris@1494: pattern = "%#{q}%" Chris@1494: sql = %w(login firstname lastname mail).map {|column| "LOWER(#{table_name}.#{column}) LIKE LOWER(:p)"}.join(" OR ") Chris@1494: params = {:p => pattern} Chris@1494: if q =~ /^(.+)\s+(.+)$/ Chris@1494: a, b = "#{$1}%", "#{$2}%" Chris@1494: sql << " OR (LOWER(#{table_name}.firstname) LIKE LOWER(:a) AND LOWER(#{table_name}.lastname) LIKE LOWER(:b))" Chris@1494: sql << " OR (LOWER(#{table_name}.firstname) LIKE LOWER(:b) AND LOWER(#{table_name}.lastname) LIKE LOWER(:a))" Chris@1494: params.merge!(:a => a, :b => b) Chris@1494: end Chris@1494: where(sql, params) Chris@1494: end Chris@1494: } Chris@1494: Chris@1494: # Principals that are members of a collection of projects Chris@1494: scope :member_of, lambda {|projects| Chris@1494: projects = [projects] unless projects.is_a?(Array) Chris@1494: if projects.empty? Chris@1494: where("1=0") Chris@1494: else Chris@1494: ids = projects.map(&:id) Chris@1494: active.uniq.joins(:members).where("#{Member.table_name}.project_id IN (?)", ids) Chris@1494: end Chris@1494: } Chris@1494: # Principals that are not members of projects Chris@1494: scope :not_member_of, lambda {|projects| Chris@1494: projects = [projects] unless projects.is_a?(Array) Chris@1494: if projects.empty? Chris@1494: where("1=0") Chris@1494: else Chris@1494: ids = projects.map(&:id) Chris@1494: where("#{Principal.table_name}.id NOT IN (SELECT DISTINCT user_id FROM #{Member.table_name} WHERE project_id IN (?))", ids) Chris@1494: end Chris@1494: } Chris@1494: scope :sorted, lambda { order(*Principal.fields_for_order_statement)} Chris@1494: Chris@1494: before_create :set_default_empty_values Chris@1494: Chris@1494: def name(formatter = nil) Chris@1494: to_s Chris@1494: end Chris@1494: Chris@1494: def <=>(principal) Chris@1494: if principal.nil? Chris@1494: -1 Chris@1494: elsif self.class.name == principal.class.name Chris@1494: self.to_s.downcase <=> principal.to_s.downcase Chris@1494: else Chris@1494: # groups after users Chris@1494: principal.class.name <=> self.class.name Chris@1494: end Chris@1494: end Chris@1494: Chris@1494: # Returns an array of fields names than can be used to make an order statement for principals. Chris@1494: # Users are sorted before Groups. Chris@1494: # Examples: Chris@1494: def self.fields_for_order_statement(table=nil) Chris@1494: table ||= table_name Chris@1494: columns = ['type DESC'] + (User.name_formatter[:order] - ['id']) + ['lastname', 'id'] Chris@1494: columns.uniq.map {|field| "#{table}.#{field}"} Chris@1494: end Chris@1494: Chris@1494: protected Chris@1494: Chris@1494: # Make sure we don't try to insert NULL values (see #4632) Chris@1494: def set_default_empty_values Chris@1494: self.login ||= '' Chris@1494: self.hashed_password ||= '' Chris@1494: self.firstname ||= '' Chris@1494: self.lastname ||= '' Chris@1494: self.mail ||= '' Chris@1494: true Chris@1494: end Chris@1494: end