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