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