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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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(
  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.merge!(:"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. before_create :set_default_empty_values
  103. before_destroy :nullify_projects_default_assigned_to
  104. def reload(*args)
  105. @project_ids = nil
  106. super
  107. end
  108. def name(formatter = nil)
  109. to_s
  110. end
  111. def mail=(*args)
  112. nil
  113. end
  114. def mail
  115. nil
  116. end
  117. def visible?(user=User.current)
  118. Principal.visible(user).find_by(:id => id) == self
  119. end
  120. # Returns true if the principal is a member of project
  121. def member_of?(project)
  122. project.is_a?(Project) && project_ids.include?(project.id)
  123. end
  124. # Returns an array of the project ids that the principal is a member of
  125. def project_ids
  126. @project_ids ||= super.freeze
  127. end
  128. def <=>(principal)
  129. if principal.nil?
  130. -1
  131. elsif self.class.name == principal.class.name
  132. self.to_s.casecmp(principal.to_s)
  133. else
  134. # groups after users
  135. principal.class.name <=> self.class.name
  136. end
  137. end
  138. # Returns an array of fields names than can be used to make an order statement for principals.
  139. # Users are sorted before Groups.
  140. # Examples:
  141. def self.fields_for_order_statement(table=nil)
  142. table ||= table_name
  143. columns = ['type DESC'] + (User.name_formatter[:order] - ['id']) + ['lastname', 'id']
  144. columns.uniq.map {|field| "#{table}.#{field}"}
  145. end
  146. # Returns the principal that matches the keyword among principals
  147. def self.detect_by_keyword(principals, keyword)
  148. keyword = keyword.to_s
  149. return nil if keyword.blank?
  150. principal = nil
  151. principal ||= principals.detect {|a| keyword.casecmp(a.login.to_s) == 0}
  152. principal ||= principals.detect {|a| keyword.casecmp(a.mail.to_s) == 0}
  153. if principal.nil? && / /.match?(keyword)
  154. firstname, lastname = *(keyword.split) # "First Last Throwaway"
  155. principal ||=
  156. principals.detect {|a|
  157. a.is_a?(User) &&
  158. firstname.casecmp(a.firstname.to_s) == 0 &&
  159. lastname.casecmp(a.lastname.to_s) == 0
  160. }
  161. end
  162. if principal.nil?
  163. principal ||= principals.detect {|a| keyword.casecmp(a.name) == 0}
  164. end
  165. principal
  166. end
  167. def nullify_projects_default_assigned_to
  168. Project.where(default_assigned_to: self).update_all(default_assigned_to_id: nil)
  169. end
  170. protected
  171. # Make sure we don't try to insert NULL values (see #4632)
  172. def set_default_empty_values
  173. self.login ||= ''
  174. self.hashed_password ||= ''
  175. self.firstname ||= ''
  176. self.lastname ||= ''
  177. true
  178. end
  179. def validate_status
  180. if status_changed? && self.class.valid_statuses.present?
  181. unless self.class.valid_statuses.include?(status)
  182. errors.add :status, :invalid
  183. end
  184. end
  185. end
  186. end
  187. require_dependency "user"
  188. require_dependency "group"