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 3.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2011 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. set_table_name "#{table_name_prefix}users#{table_name_suffix}"
  19. has_many :members, :foreign_key => 'user_id', :dependent => :destroy
  20. has_many :memberships, :class_name => 'Member', :foreign_key => 'user_id', :include => [ :project, :roles ], :conditions => "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}", :order => "#{Project.table_name}.name"
  21. has_many :projects, :through => :memberships
  22. has_many :issue_categories, :foreign_key => 'assigned_to_id', :dependent => :nullify
  23. # Groups and active users
  24. named_scope :active, :conditions => "#{Principal.table_name}.status = 1"
  25. named_scope :like, lambda {|q|
  26. if q.blank?
  27. {}
  28. else
  29. q = q.to_s.downcase
  30. pattern = "%#{q}%"
  31. sql = "LOWER(login) LIKE :p OR LOWER(firstname) LIKE :p OR LOWER(lastname) LIKE :p OR LOWER(mail) LIKE :p"
  32. params = {:p => pattern}
  33. if q =~ /^(.+)\s+(.+)$/
  34. a, b = "#{$1}%", "#{$2}%"
  35. sql << " OR (LOWER(firstname) LIKE :a AND LOWER(lastname) LIKE :b) OR (LOWER(firstname) LIKE :b AND LOWER(lastname) LIKE :a)"
  36. params.merge!(:a => a, :b => b)
  37. end
  38. {:conditions => [sql, params]}
  39. end
  40. }
  41. # Principals that are members of a collection of projects
  42. named_scope :member_of, lambda {|projects|
  43. projects = [projects] unless projects.is_a?(Array)
  44. if projects.empty?
  45. {:conditions => "1=0"}
  46. else
  47. ids = projects.map(&:id)
  48. {:conditions => ["#{Principal.table_name}.status = 1 AND #{Principal.table_name}.id IN (SELECT DISTINCT user_id FROM #{Member.table_name} WHERE project_id IN (?))", ids]}
  49. end
  50. }
  51. # Principals that are not members of projects
  52. named_scope :not_member_of, lambda {|projects|
  53. projects = [projects] unless projects.is_a?(Array)
  54. if projects.empty?
  55. {:conditions => "1=0"}
  56. else
  57. ids = projects.map(&:id)
  58. {:conditions => ["#{Principal.table_name}.id NOT IN (SELECT DISTINCT user_id FROM #{Member.table_name} WHERE project_id IN (?))", ids]}
  59. end
  60. }
  61. before_create :set_default_empty_values
  62. def name(formatter = nil)
  63. to_s
  64. end
  65. def <=>(principal)
  66. if principal.nil?
  67. -1
  68. elsif self.class.name == principal.class.name
  69. self.to_s.downcase <=> principal.to_s.downcase
  70. else
  71. # groups after users
  72. principal.class.name <=> self.class.name
  73. end
  74. end
  75. protected
  76. # Make sure we don't try to insert NULL values (see #4632)
  77. def set_default_empty_values
  78. self.login ||= ''
  79. self.hashed_password ||= ''
  80. self.firstname ||= ''
  81. self.lastname ||= ''
  82. self.mail ||= ''
  83. true
  84. end
  85. end