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.6KB

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