You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

principal.rb 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2017 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  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
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. class Principal < ActiveRecord::Base
  18. self.table_name = "#{table_name_prefix}users#{table_name_suffix}"
  19. # Account statuses
  20. STATUS_ANONYMOUS = 0
  21. STATUS_ACTIVE = 1
  22. STATUS_REGISTERED = 2
  23. STATUS_LOCKED = 3
  24. class_attribute :valid_statuses
  25. has_many :members, :foreign_key => 'user_id', :dependent => :destroy
  26. has_many :memberships,
  27. lambda {joins(:project).where.not(:projects => {:status => Project::STATUS_ARCHIVED})},
  28. :class_name => 'Member',
  29. :foreign_key => 'user_id'
  30. has_many :projects, :through => :memberships
  31. has_many :issue_categories, :foreign_key => 'assigned_to_id', :dependent => :nullify
  32. validate :validate_status
  33. # Groups and active users
  34. scope :active, lambda { where(:status => STATUS_ACTIVE) }
  35. scope :visible, lambda {|*args|
  36. user = args.first || User.current
  37. if user.admin?
  38. all
  39. else
  40. view_all_active = false
  41. if user.memberships.to_a.any?
  42. view_all_active = user.memberships.any? {|m| m.roles.any? {|r| r.users_visibility == 'all'}}
  43. else
  44. view_all_active = user.builtin_role.users_visibility == 'all'
  45. end
  46. if view_all_active
  47. active
  48. else
  49. # self and members of visible projects
  50. active.where("#{table_name}.id = ? OR #{table_name}.id IN (SELECT user_id FROM #{Member.table_name} WHERE project_id IN (?))",
  51. user.id, user.visible_project_ids
  52. )
  53. end
  54. end
  55. }
  56. scope :like, lambda {|q|
  57. q = q.to_s
  58. if q.blank?
  59. where({})
  60. else
  61. pattern = "%#{q}%"
  62. sql = "LOWER(#{table_name}.login) LIKE LOWER(:p)"
  63. sql << " OR #{table_name}.id IN (SELECT user_id FROM #{EmailAddress.table_name} WHERE LOWER(address) LIKE LOWER(:p))"
  64. params = {:p => pattern}
  65. tokens = q.split(/\s+/).reject(&:blank?).map { |token| "%#{token}%" }
  66. if tokens.present?
  67. sql << ' OR ('
  68. sql << tokens.map.with_index do |token, index|
  69. params.merge!(:"token_#{index}" => token)
  70. "(LOWER(#{table_name}.firstname) LIKE LOWER(:token_#{index}) OR LOWER(#{table_name}.lastname) LIKE LOWER(:token_#{index}))"
  71. end.join(' AND ')
  72. sql << ')'
  73. end
  74. where(sql, params)
  75. end
  76. }
  77. # Principals that are members of a collection of projects
  78. scope :member_of, lambda {|projects|
  79. projects = [projects] if projects.is_a?(Project)
  80. if projects.blank?
  81. where("1=0")
  82. else
  83. ids = projects.map(&:id)
  84. # include active and locked users
  85. where(:status => [STATUS_LOCKED, STATUS_ACTIVE]).
  86. where("#{Principal.table_name}.id IN (SELECT DISTINCT user_id FROM #{Member.table_name} WHERE project_id IN (?))", ids)
  87. end
  88. }
  89. # Principals that are not members of projects
  90. scope :not_member_of, lambda {|projects|
  91. projects = [projects] unless projects.is_a?(Array)
  92. if projects.empty?
  93. where("1=0")
  94. else
  95. ids = projects.map(&:id)
  96. where("#{Principal.table_name}.id NOT IN (SELECT DISTINCT user_id FROM #{Member.table_name} WHERE project_id IN (?))", ids)
  97. end
  98. }
  99. scope :sorted, lambda { order(*Principal.fields_for_order_statement)}
  100. before_create :set_default_empty_values
  101. before_destroy :nullify_projects_default_assigned_to
  102. def reload(*args)
  103. @project_ids = nil
  104. super
  105. end
  106. def name(formatter = nil)
  107. to_s
  108. end
  109. def mail=(*args)
  110. nil
  111. end
  112. def mail
  113. nil
  114. end
  115. def visible?(user=User.current)
  116. Principal.visible(user).find_by(:id => id) == self
  117. end
  118. # Returns true if the principal is a member of project
  119. def member_of?(project)
  120. project.is_a?(Project) && project_ids.include?(project.id)
  121. end
  122. # Returns an array of the project ids that the principal is a member of
  123. def project_ids
  124. @project_ids ||= super.freeze
  125. end
  126. def <=>(principal)
  127. if principal.nil?
  128. -1
  129. elsif self.class.name == principal.class.name
  130. self.to_s.casecmp(principal.to_s)
  131. else
  132. # groups after users
  133. principal.class.name <=> self.class.name
  134. end
  135. end
  136. # Returns an array of fields names than can be used to make an order statement for principals.
  137. # Users are sorted before Groups.
  138. # Examples:
  139. def self.fields_for_order_statement(table=nil)
  140. table ||= table_name
  141. columns = ['type DESC'] + (User.name_formatter[:order] - ['id']) + ['lastname', 'id']
  142. columns.uniq.map {|field| "#{table}.#{field}"}
  143. end
  144. # Returns the principal that matches the keyword among principals
  145. def self.detect_by_keyword(principals, keyword)
  146. keyword = keyword.to_s
  147. return nil if keyword.blank?
  148. principal = nil
  149. principal ||= principals.detect {|a| keyword.casecmp(a.login.to_s) == 0}
  150. principal ||= principals.detect {|a| keyword.casecmp(a.mail.to_s) == 0}
  151. if principal.nil? && keyword.match(/ /)
  152. firstname, lastname = *(keyword.split) # "First Last Throwaway"
  153. principal ||= principals.detect {|a|
  154. a.is_a?(User) &&
  155. firstname.casecmp(a.firstname.to_s) == 0 &&
  156. lastname.casecmp(a.lastname.to_s) == 0
  157. }
  158. end
  159. if principal.nil?
  160. principal ||= principals.detect {|a| keyword.casecmp(a.name) == 0}
  161. end
  162. principal
  163. end
  164. def nullify_projects_default_assigned_to
  165. Project.where(default_assigned_to: self).update_all(default_assigned_to_id: nil)
  166. end
  167. protected
  168. # Make sure we don't try to insert NULL values (see #4632)
  169. def set_default_empty_values
  170. self.login ||= ''
  171. self.hashed_password ||= ''
  172. self.firstname ||= ''
  173. self.lastname ||= ''
  174. true
  175. end
  176. def validate_status
  177. if status_changed? && self.class.valid_statuses.present?
  178. unless self.class.valid_statuses.include?(status)
  179. errors.add :status, :invalid
  180. end
  181. end
  182. end
  183. end
  184. require_dependency "user"
  185. require_dependency "group"