@@ -1,162 +1,162 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2015 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2015 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, |
|
28 | has_many :memberships, | |
29 | lambda {preload(:project, :roles). |
|
29 | lambda {preload(:project, :roles). | |
30 | joins(:project). |
|
30 | joins(:project). | |
31 | where("#{Project.table_name}.status<>#{Project::STATUS_ARCHIVED}"). |
|
31 | where("#{Project.table_name}.status<>#{Project::STATUS_ARCHIVED}"). | |
32 | order("#{Project.table_name}.name")}, |
|
32 | order("#{Project.table_name}.name")}, | |
33 | :class_name => 'Member', |
|
33 | :class_name => 'Member', | |
34 | :foreign_key => 'user_id' |
|
34 | :foreign_key => 'user_id' | |
35 | has_many :projects, :through => :memberships |
|
35 | has_many :projects, :through => :memberships | |
36 | has_many :issue_categories, :foreign_key => 'assigned_to_id', :dependent => :nullify |
|
36 | has_many :issue_categories, :foreign_key => 'assigned_to_id', :dependent => :nullify | |
37 |
|
37 | |||
38 | # Groups and active users |
|
38 | # Groups and active users | |
39 | scope :active, lambda { where(:status => STATUS_ACTIVE) } |
|
39 | scope :active, lambda { where(:status => STATUS_ACTIVE) } | |
40 |
|
40 | |||
41 | scope :visible, lambda {|*args| |
|
41 | scope :visible, lambda {|*args| | |
42 | user = args.first || User.current |
|
42 | user = args.first || User.current | |
43 |
|
43 | |||
44 | if user.admin? |
|
44 | if user.admin? | |
45 | all |
|
45 | all | |
46 | else |
|
46 | else | |
47 | view_all_active = false |
|
47 | view_all_active = false | |
48 | if user.memberships.to_a.any? |
|
48 | if user.memberships.to_a.any? | |
49 | view_all_active = user.memberships.any? {|m| m.roles.any? {|r| r.users_visibility == 'all'}} |
|
49 | view_all_active = user.memberships.any? {|m| m.roles.any? {|r| r.users_visibility == 'all'}} | |
50 | else |
|
50 | else | |
51 | view_all_active = user.builtin_role.users_visibility == 'all' |
|
51 | view_all_active = user.builtin_role.users_visibility == 'all' | |
52 | end |
|
52 | end | |
53 |
|
53 | |||
54 | if view_all_active |
|
54 | if view_all_active | |
55 | active |
|
55 | active | |
56 | else |
|
56 | else | |
57 | # self and members of visible projects |
|
57 | # self and members of visible projects | |
58 | active.where("#{table_name}.id = ? OR #{table_name}.id IN (SELECT user_id FROM #{Member.table_name} WHERE project_id IN (?))", |
|
58 | active.where("#{table_name}.id = ? OR #{table_name}.id IN (SELECT user_id FROM #{Member.table_name} WHERE project_id IN (?))", | |
59 | user.id, user.visible_project_ids |
|
59 | user.id, user.visible_project_ids | |
60 | ) |
|
60 | ) | |
61 | end |
|
61 | end | |
62 | end |
|
62 | end | |
63 | } |
|
63 | } | |
64 |
|
64 | |||
65 | scope :like, lambda {|q| |
|
65 | scope :like, lambda {|q| | |
66 | q = q.to_s |
|
66 | q = q.to_s | |
67 | if q.blank? |
|
67 | if q.blank? | |
68 | where({}) |
|
68 | where({}) | |
69 | else |
|
69 | else | |
70 | pattern = "%#{q}%" |
|
70 | pattern = "%#{q}%" | |
71 | sql = %w(login firstname lastname).map {|column| "LOWER(#{table_name}.#{column}) LIKE LOWER(:p)"}.join(" OR ") |
|
71 | sql = %w(login firstname lastname).map {|column| "LOWER(#{table_name}.#{column}) LIKE LOWER(:p)"}.join(" OR ") | |
72 | sql << " OR #{table_name}.id IN (SELECT user_id FROM #{EmailAddress.table_name} WHERE LOWER(address) LIKE LOWER(:p))" |
|
72 | sql << " OR #{table_name}.id IN (SELECT user_id FROM #{EmailAddress.table_name} WHERE LOWER(address) LIKE LOWER(:p))" | |
73 | params = {:p => pattern} |
|
73 | params = {:p => pattern} | |
74 | if q =~ /^(.+)\s+(.+)$/ |
|
74 | if q =~ /^(.+)\s+(.+)$/ | |
75 | a, b = "#{$1}%", "#{$2}%" |
|
75 | a, b = "#{$1}%", "#{$2}%" | |
76 | sql << " OR (LOWER(#{table_name}.firstname) LIKE LOWER(:a) AND LOWER(#{table_name}.lastname) LIKE LOWER(:b))" |
|
76 | sql << " OR (LOWER(#{table_name}.firstname) LIKE LOWER(:a) AND LOWER(#{table_name}.lastname) LIKE LOWER(:b))" | |
77 | sql << " OR (LOWER(#{table_name}.firstname) LIKE LOWER(:b) AND LOWER(#{table_name}.lastname) LIKE LOWER(:a))" |
|
77 | sql << " OR (LOWER(#{table_name}.firstname) LIKE LOWER(:b) AND LOWER(#{table_name}.lastname) LIKE LOWER(:a))" | |
78 | params.merge!(:a => a, :b => b) |
|
78 | params.merge!(:a => a, :b => b) | |
79 | end |
|
79 | end | |
80 | where(sql, params) |
|
80 | where(sql, params) | |
81 | end |
|
81 | end | |
82 | } |
|
82 | } | |
83 |
|
83 | |||
84 | # Principals that are members of a collection of projects |
|
84 | # Principals that are members of a collection of projects | |
85 | scope :member_of, lambda {|projects| |
|
85 | scope :member_of, lambda {|projects| | |
86 | projects = [projects] if projects.is_a?(Project) |
|
86 | projects = [projects] if projects.is_a?(Project) | |
87 | if projects.blank? |
|
87 | if projects.blank? | |
88 | where("1=0") |
|
88 | where("1=0") | |
89 | else |
|
89 | else | |
90 | ids = projects.map(&:id) |
|
90 | ids = projects.map(&:id) | |
91 | active.where("#{Principal.table_name}.id IN (SELECT DISTINCT user_id FROM #{Member.table_name} WHERE project_id IN (?))", ids) |
|
91 | active.where("#{Principal.table_name}.id IN (SELECT DISTINCT user_id FROM #{Member.table_name} WHERE project_id IN (?))", ids) | |
92 | end |
|
92 | end | |
93 | } |
|
93 | } | |
94 | # Principals that are not members of projects |
|
94 | # Principals that are not members of projects | |
95 | scope :not_member_of, lambda {|projects| |
|
95 | scope :not_member_of, lambda {|projects| | |
96 | projects = [projects] unless projects.is_a?(Array) |
|
96 | projects = [projects] unless projects.is_a?(Array) | |
97 | if projects.empty? |
|
97 | if projects.empty? | |
98 | where("1=0") |
|
98 | where("1=0") | |
99 | else |
|
99 | else | |
100 | ids = projects.map(&:id) |
|
100 | ids = projects.map(&:id) | |
101 | where("#{Principal.table_name}.id NOT IN (SELECT DISTINCT user_id FROM #{Member.table_name} WHERE project_id IN (?))", ids) |
|
101 | where("#{Principal.table_name}.id NOT IN (SELECT DISTINCT user_id FROM #{Member.table_name} WHERE project_id IN (?))", ids) | |
102 | end |
|
102 | end | |
103 | } |
|
103 | } | |
104 | scope :sorted, lambda { order(*Principal.fields_for_order_statement)} |
|
104 | scope :sorted, lambda { order(*Principal.fields_for_order_statement)} | |
105 |
|
105 | |||
106 | before_create :set_default_empty_values |
|
106 | before_create :set_default_empty_values | |
107 |
|
107 | |||
108 | def name(formatter = nil) |
|
108 | def name(formatter = nil) | |
109 | to_s |
|
109 | to_s | |
110 | end |
|
110 | end | |
111 |
|
111 | |||
112 | def mail=(*args) |
|
112 | def mail=(*args) | |
113 | nil |
|
113 | nil | |
114 | end |
|
114 | end | |
115 |
|
115 | |||
116 | def mail |
|
116 | def mail | |
117 | nil |
|
117 | nil | |
118 | end |
|
118 | end | |
119 |
|
119 | |||
120 | def visible?(user=User.current) |
|
120 | def visible?(user=User.current) | |
121 | Principal.visible(user).where(:id => id).first == self |
|
121 | Principal.visible(user).where(:id => id).first == self | |
122 | end |
|
122 | end | |
123 |
|
123 | |||
124 | # Return true if the principal is a member of project |
|
124 | # Return true if the principal is a member of project | |
125 | def member_of?(project) |
|
125 | def member_of?(project) | |
126 | projects.to_a.include?(project) |
|
126 | projects.to_a.include?(project) | |
127 | end |
|
127 | end | |
128 |
|
128 | |||
129 | def <=>(principal) |
|
129 | def <=>(principal) | |
130 | if principal.nil? |
|
130 | if principal.nil? | |
131 | -1 |
|
131 | -1 | |
132 | elsif self.class.name == principal.class.name |
|
132 | elsif self.class.name == principal.class.name | |
133 | self.to_s.downcase <=> principal.to_s.downcase |
|
133 | self.to_s.downcase <=> principal.to_s.downcase | |
134 | else |
|
134 | else | |
135 | # groups after users |
|
135 | # groups after users | |
136 | principal.class.name <=> self.class.name |
|
136 | principal.class.name <=> self.class.name | |
137 | end |
|
137 | end | |
138 | end |
|
138 | end | |
139 |
|
139 | |||
140 | # Returns an array of fields names than can be used to make an order statement for principals. |
|
140 | # Returns an array of fields names than can be used to make an order statement for principals. | |
141 | # Users are sorted before Groups. |
|
141 | # Users are sorted before Groups. | |
142 | # Examples: |
|
142 | # Examples: | |
143 | def self.fields_for_order_statement(table=nil) |
|
143 | def self.fields_for_order_statement(table=nil) | |
144 | table ||= table_name |
|
144 | table ||= table_name | |
145 | columns = ['type DESC'] + (User.name_formatter[:order] - ['id']) + ['lastname', 'id'] |
|
145 | columns = ['type DESC'] + (User.name_formatter[:order] - ['id']) + ['lastname', 'id'] | |
146 | columns.uniq.map {|field| "#{table}.#{field}"} |
|
146 | columns.uniq.map {|field| "#{table}.#{field}"} | |
147 | end |
|
147 | end | |
148 |
|
148 | |||
149 | protected |
|
149 | protected | |
150 |
|
150 | |||
151 | # Make sure we don't try to insert NULL values (see #4632) |
|
151 | # Make sure we don't try to insert NULL values (see #4632) | |
152 | def set_default_empty_values |
|
152 | def set_default_empty_values | |
153 | self.login ||= '' |
|
153 | self.login ||= '' | |
154 | self.hashed_password ||= '' |
|
154 | self.hashed_password ||= '' | |
155 | self.firstname ||= '' |
|
155 | self.firstname ||= '' | |
156 | self.lastname ||= '' |
|
156 | self.lastname ||= '' | |
157 | true |
|
157 | true | |
158 | end |
|
158 | end | |
159 | end |
|
159 | end | |
160 |
|
160 | |||
161 | require_dependency "user" |
|
161 | require_dependency "user" | |
162 | require_dependency "group" |
|
162 | require_dependency "group" |
General Comments 0
You need to be logged in to leave comments.
Login now