@@ -1,96 +1,96 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2012 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2012 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 | has_many :members, :foreign_key => 'user_id', :dependent => :destroy |
|
21 | has_many :members, :foreign_key => 'user_id', :dependent => :destroy | |
22 | 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" |
|
22 | 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" | |
23 | has_many :projects, :through => :memberships |
|
23 | has_many :projects, :through => :memberships | |
24 | has_many :issue_categories, :foreign_key => 'assigned_to_id', :dependent => :nullify |
|
24 | has_many :issue_categories, :foreign_key => 'assigned_to_id', :dependent => :nullify | |
25 |
|
25 | |||
26 | # Groups and active users |
|
26 | # Groups and active users | |
27 | scope :active, :conditions => "#{Principal.table_name}.status = 1" |
|
27 | scope :active, :conditions => "#{Principal.table_name}.status = 1" | |
28 |
|
28 | |||
29 | scope :like, lambda {|q| |
|
29 | scope :like, lambda {|q| | |
30 | q = q.to_s |
|
30 | q = q.to_s | |
31 | if q.blank? |
|
31 | if q.blank? | |
32 | where({}) |
|
32 | where({}) | |
33 | else |
|
33 | else | |
34 | pattern = "%#{q}%" |
|
34 | pattern = "%#{q}%" | |
35 | sql = %w(login firstname lastname mail).map {|column| "LOWER(#{table_name}.#{column}) LIKE LOWER(:p)"}.join(" OR ") |
|
35 | sql = %w(login firstname lastname mail).map {|column| "LOWER(#{table_name}.#{column}) LIKE LOWER(:p)"}.join(" OR ") | |
36 | params = {:p => pattern} |
|
36 | params = {:p => pattern} | |
37 | if q =~ /^(.+)\s+(.+)$/ |
|
37 | if q =~ /^(.+)\s+(.+)$/ | |
38 | a, b = "#{$1}%", "#{$2}%" |
|
38 | a, b = "#{$1}%", "#{$2}%" | |
39 | sql << " OR (LOWER(#{table_name}.firstname) LIKE LOWER(:a) AND LOWER(#{table_name}.lastname) LIKE LOWER(:b))" |
|
39 | sql << " OR (LOWER(#{table_name}.firstname) LIKE LOWER(:a) AND LOWER(#{table_name}.lastname) LIKE LOWER(:b))" | |
40 | sql << " OR (LOWER(#{table_name}.firstname) LIKE LOWER(:b) AND LOWER(#{table_name}.lastname) LIKE LOWER(:a))" |
|
40 | sql << " OR (LOWER(#{table_name}.firstname) LIKE LOWER(:b) AND LOWER(#{table_name}.lastname) LIKE LOWER(:a))" | |
41 | params.merge!(:a => a, :b => b) |
|
41 | params.merge!(:a => a, :b => b) | |
42 | end |
|
42 | end | |
43 | where(sql, params) |
|
43 | where(sql, params) | |
44 | end |
|
44 | end | |
45 | } |
|
45 | } | |
46 |
|
46 | |||
47 | # Principals that are members of a collection of projects |
|
47 | # Principals that are members of a collection of projects | |
48 | scope :member_of, lambda {|projects| |
|
48 | scope :member_of, lambda {|projects| | |
49 | projects = [projects] unless projects.is_a?(Array) |
|
49 | projects = [projects] unless projects.is_a?(Array) | |
50 | if projects.empty? |
|
50 | if projects.empty? | |
51 | {:conditions => "1=0"} |
|
51 | where("1=0") | |
52 | else |
|
52 | else | |
53 | ids = projects.map(&:id) |
|
53 | ids = projects.map(&:id) | |
54 |
|
|
54 | where("#{Principal.table_name}.status = 1 AND #{Principal.table_name}.id IN (SELECT DISTINCT user_id FROM #{Member.table_name} WHERE project_id IN (?))", ids) | |
55 | end |
|
55 | end | |
56 | } |
|
56 | } | |
57 | # Principals that are not members of projects |
|
57 | # Principals that are not members of projects | |
58 | scope :not_member_of, lambda {|projects| |
|
58 | scope :not_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 | {:conditions => "1=0"} |
|
61 | where("1=0") | |
62 | else |
|
62 | else | |
63 | ids = projects.map(&:id) |
|
63 | ids = projects.map(&:id) | |
64 |
|
|
64 | where("#{Principal.table_name}.id NOT IN (SELECT DISTINCT user_id FROM #{Member.table_name} WHERE project_id IN (?))", ids) | |
65 | end |
|
65 | end | |
66 | } |
|
66 | } | |
67 |
|
67 | |||
68 | before_create :set_default_empty_values |
|
68 | before_create :set_default_empty_values | |
69 |
|
69 | |||
70 | def name(formatter = nil) |
|
70 | def name(formatter = nil) | |
71 | to_s |
|
71 | to_s | |
72 | end |
|
72 | end | |
73 |
|
73 | |||
74 | def <=>(principal) |
|
74 | def <=>(principal) | |
75 | if principal.nil? |
|
75 | if principal.nil? | |
76 | -1 |
|
76 | -1 | |
77 | elsif self.class.name == principal.class.name |
|
77 | elsif self.class.name == principal.class.name | |
78 | self.to_s.downcase <=> principal.to_s.downcase |
|
78 | self.to_s.downcase <=> principal.to_s.downcase | |
79 | else |
|
79 | else | |
80 | # groups after users |
|
80 | # groups after users | |
81 | principal.class.name <=> self.class.name |
|
81 | principal.class.name <=> self.class.name | |
82 | end |
|
82 | end | |
83 | end |
|
83 | end | |
84 |
|
84 | |||
85 | protected |
|
85 | protected | |
86 |
|
86 | |||
87 | # Make sure we don't try to insert NULL values (see #4632) |
|
87 | # Make sure we don't try to insert NULL values (see #4632) | |
88 | def set_default_empty_values |
|
88 | def set_default_empty_values | |
89 | self.login ||= '' |
|
89 | self.login ||= '' | |
90 | self.hashed_password ||= '' |
|
90 | self.hashed_password ||= '' | |
91 | self.firstname ||= '' |
|
91 | self.firstname ||= '' | |
92 | self.lastname ||= '' |
|
92 | self.lastname ||= '' | |
93 | self.mail ||= '' |
|
93 | self.mail ||= '' | |
94 | true |
|
94 | true | |
95 | end |
|
95 | end | |
96 | end |
|
96 | end |
General Comments 0
You need to be logged in to leave comments.
Login now