Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2020 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(
  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. }
  58. scope :like, lambda {|q|
  59. q = q.to_s
  60. if q.blank?
  61. where({})
  62. else
  63. pattern = "%#{q}%"
  64. sql = +"LOWER(#{table_name}.login) LIKE LOWER(:p)"
  65. sql << " OR #{table_name}.id IN (SELECT user_id FROM #{EmailAddress.table_name} WHERE LOWER(address) LIKE LOWER(:p))"
  66. params = {:p => pattern}
  67. tokens = q.split(/\s+/).reject(&:blank?).map { |token| "%#{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}) OR LOWER(#{table_name}.lastname) LIKE LOWER(:token_#{index}))"
  73. end.join(' AND ')
  74. sql << ')'
  75. end
  76. where(sql, params)
  77. end
  78. }
  79. # Principals that are members of a collection of projects
  80. scope :member_of, lambda {|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. }
  91. # Principals that are not members of projects
  92. scope :not_member_of, lambda {|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. }
  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. if principal.nil?
  132. -1
  133. elsif self.class.name == principal.class.name
  134. self.to_s.casecmp(principal.to_s)
  135. else
  136. # groups after users
  137. principal.class.name <=> self.class.name
  138. end
  139. end
  140. # Returns an array of fields names than can be used to make an order statement for principals.
  141. # Users are sorted before Groups.
  142. # Examples:
  143. def self.fields_for_order_statement(table=nil)
  144. table ||= table_name
  145. columns = ['type DESC'] + (User.name_formatter[:order] - ['id']) + ['lastname', 'id']
  146. columns.uniq.map {|field| "#{table}.#{field}"}
  147. end
  148. # Returns the principal that matches the keyword among principals
  149. def self.detect_by_keyword(principals, keyword)
  150. keyword = keyword.to_s
  151. return nil if keyword.blank?
  152. principal = nil
  153. principal ||= principals.detect {|a| keyword.casecmp(a.login.to_s) == 0}
  154. principal ||= principals.detect {|a| keyword.casecmp(a.mail.to_s) == 0}
  155. if principal.nil? && / /.match?(keyword)
  156. firstname, lastname = *(keyword.split) # "First Last Throwaway"
  157. principal ||=
  158. principals.detect {|a|
  159. a.is_a?(User) &&
  160. firstname.casecmp(a.firstname.to_s) == 0 &&
  161. lastname.casecmp(a.lastname.to_s) == 0
  162. }
  163. end
  164. if principal.nil?
  165. principal ||= principals.detect {|a| keyword.casecmp(a.name) == 0}
  166. end
  167. principal
  168. end
  169. def nullify_projects_default_assigned_to
  170. Project.where(default_assigned_to: self).update_all(default_assigned_to_id: nil)
  171. end
  172. protected
  173. # Make sure we don't try to insert NULL values (see #4632)
  174. def set_default_empty_values
  175. self.login ||= ''
  176. self.hashed_password ||= ''
  177. self.firstname ||= ''
  178. self.lastname ||= ''
  179. true
  180. end
  181. def validate_status
  182. if status_changed? && self.class.valid_statuses.present?
  183. unless self.class.valid_statuses.include?(status)
  184. errors.add :status, :invalid
  185. end
  186. end
  187. end
  188. end
  189. require_dependency "user"
  190. require_dependency "group"