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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2023 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 < ApplicationRecord
  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 do |*args|
  37. user = args.first || User.current
  38. if user.admin?
  39. all
  40. else
  41. view_all_active = false
  42. if user.memberships.any?
  43. view_all_active = User.where(id: user.id).joins(memberships: :roles).where("#{Role.table_name}.users_visibility = ?", 'all').any?
  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(
  52. "#{table_name}.id = ? OR #{table_name}.id IN (SELECT user_id FROM #{Member.table_name} WHERE project_id IN (?))",
  53. user.id, user.visible_project_ids
  54. )
  55. end
  56. end
  57. end)
  58. scope :like, (lambda do |q|
  59. q = q.to_s
  60. if q.blank?
  61. where({})
  62. else
  63. pattern = "%#{sanitize_sql_like q}%"
  64. sql = +"LOWER(#{table_name}.login) LIKE LOWER(:p) ESCAPE :s"
  65. sql << " OR #{table_name}.id IN (SELECT user_id FROM #{EmailAddress.table_name} WHERE LOWER(address) LIKE LOWER(:p) ESCAPE :s)"
  66. params = {:p => pattern, :s => '\\'}
  67. tokens = q.split(/\s+/).reject(&:blank?).map {|token| "%#{sanitize_sql_like token}%"}
  68. if tokens.present?
  69. sql << ' OR ('
  70. sql << tokens.map.with_index do |token, index|
  71. params[:"token_#{index}"] = token
  72. "(LOWER(#{table_name}.firstname) LIKE LOWER(:token_#{index}) ESCAPE :s OR LOWER(#{table_name}.lastname) LIKE LOWER(:token_#{index}) ESCAPE :s)"
  73. end.join(' AND ')
  74. sql << ')'
  75. end
  76. where(sql, params)
  77. end
  78. end)
  79. # Principals that are members of a collection of projects
  80. scope :member_of, (lambda do |projects|
  81. projects = [projects] if projects.is_a?(Project)
  82. if projects.blank?
  83. where("1=0")
  84. else
  85. ids = projects.map(&:id)
  86. # include active and locked users
  87. where(:status => [STATUS_LOCKED, STATUS_ACTIVE]).
  88. where("#{Principal.table_name}.id IN (SELECT DISTINCT user_id FROM #{Member.table_name} WHERE project_id IN (?))", ids)
  89. end
  90. end)
  91. # Principals that are not members of projects
  92. scope :not_member_of, (lambda do |projects|
  93. projects = [projects] unless projects.is_a?(Array)
  94. if projects.empty?
  95. where("1=0")
  96. else
  97. ids = projects.map(&:id)
  98. where("#{Principal.table_name}.id NOT IN (SELECT DISTINCT user_id FROM #{Member.table_name} WHERE project_id IN (?))", ids)
  99. end
  100. end)
  101. scope :sorted, lambda {order(*Principal.fields_for_order_statement)}
  102. # Principals that can be added as watchers
  103. scope :assignable_watchers, lambda {active.visible.where(:type => ['User', 'Group'])}
  104. before_create :set_default_empty_values
  105. before_destroy :nullify_projects_default_assigned_to
  106. def reload(*args)
  107. @project_ids = nil
  108. super
  109. end
  110. def name(formatter = nil)
  111. to_s
  112. end
  113. def mail=(*args)
  114. nil
  115. end
  116. def mail
  117. nil
  118. end
  119. def visible?(user=User.current)
  120. Principal.visible(user).find_by(:id => id) == self
  121. end
  122. # Returns true if the principal is a member of project
  123. def member_of?(project)
  124. project.is_a?(Project) && project_ids.include?(project.id)
  125. end
  126. # Returns an array of the project ids that the principal is a member of
  127. def project_ids
  128. @project_ids ||= super.freeze
  129. end
  130. def <=>(principal)
  131. # avoid an error when sorting members without roles (#10053)
  132. return -1 if principal.nil?
  133. return nil unless principal.is_a?(Principal)
  134. if self.class.name == principal.class.name
  135. self.to_s.casecmp(principal.to_s)
  136. else
  137. # groups after users
  138. principal.class.name <=> self.class.name
  139. end
  140. end
  141. # Returns an array of fields names than can be used to make an order statement for principals.
  142. # Users are sorted before Groups.
  143. # Examples:
  144. def self.fields_for_order_statement(table=nil)
  145. table ||= table_name
  146. columns = ['type DESC'] + (User.name_formatter[:order] - ['id']) + ['lastname', 'id']
  147. columns.uniq.map {|field| "#{table}.#{field}"}
  148. end
  149. # Returns the principal that matches the keyword among principals
  150. def self.detect_by_keyword(principals, keyword)
  151. keyword = keyword.to_s
  152. return nil if keyword.blank?
  153. principal = nil
  154. principal ||= principals.detect {|a| keyword.casecmp(a.login.to_s) == 0}
  155. principal ||= principals.detect {|a| keyword.casecmp(a.mail.to_s) == 0}
  156. if principal.nil? && keyword.include?(' ')
  157. firstname, lastname = *(keyword.split) # "First Last Throwaway"
  158. principal ||=
  159. principals.detect do |a|
  160. a.is_a?(User) &&
  161. firstname.casecmp(a.firstname.to_s) == 0 &&
  162. lastname.casecmp(a.lastname.to_s) == 0
  163. end
  164. end
  165. if principal.nil?
  166. principal ||= principals.detect {|a| keyword.casecmp(a.name) == 0}
  167. end
  168. principal
  169. end
  170. def nullify_projects_default_assigned_to
  171. Project.where(default_assigned_to: self).update_all(default_assigned_to_id: nil)
  172. end
  173. protected
  174. # Make sure we don't try to insert NULL values (see #4632)
  175. def set_default_empty_values
  176. self.login ||= ''
  177. self.hashed_password ||= ''
  178. self.firstname ||= ''
  179. self.lastname ||= ''
  180. true
  181. end
  182. def validate_status
  183. if status_changed? && self.class.valid_statuses.present?
  184. unless self.class.valid_statuses.include?(status)
  185. errors.add :status, :invalid
  186. end
  187. end
  188. end
  189. end