Chris@0
|
1 # Redmine - project management software
|
Chris@1115
|
2 # Copyright (C) 2006-2012 Jean-Philippe Lang
|
Chris@0
|
3 #
|
Chris@0
|
4 # This program is free software; you can redistribute it and/or
|
Chris@0
|
5 # modify it under the terms of the GNU General Public License
|
Chris@0
|
6 # as published by the Free Software Foundation; either version 2
|
Chris@0
|
7 # of the License, or (at your option) any later version.
|
Chris@909
|
8 #
|
Chris@0
|
9 # This program is distributed in the hope that it will be useful,
|
Chris@0
|
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@0
|
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@0
|
12 # GNU General Public License for more details.
|
Chris@909
|
13 #
|
Chris@0
|
14 # You should have received a copy of the GNU General Public License
|
Chris@0
|
15 # along with this program; if not, write to the Free Software
|
Chris@0
|
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
Chris@0
|
17
|
Chris@0
|
18 class Group < Principal
|
Chris@1115
|
19 include Redmine::SafeAttributes
|
Chris@1115
|
20
|
Chris@0
|
21 has_and_belongs_to_many :users, :after_add => :user_added,
|
Chris@0
|
22 :after_remove => :user_removed
|
Chris@909
|
23
|
Chris@0
|
24 acts_as_customizable
|
Chris@909
|
25
|
Chris@0
|
26 validates_presence_of :lastname
|
Chris@0
|
27 validates_uniqueness_of :lastname, :case_sensitive => false
|
Chris@0
|
28 validates_length_of :lastname, :maximum => 30
|
Chris@909
|
29
|
Chris@909
|
30 before_destroy :remove_references_before_destroy
|
Chris@909
|
31
|
Chris@1115
|
32 scope :sorted, order("#{table_name}.lastname ASC")
|
Chris@1115
|
33
|
Chris@1115
|
34 safe_attributes 'name',
|
Chris@1115
|
35 'user_ids',
|
Chris@1115
|
36 'custom_field_values',
|
Chris@1115
|
37 'custom_fields',
|
Chris@1115
|
38 :if => lambda {|group, user| user.admin?}
|
Chris@1115
|
39
|
Chris@0
|
40 def to_s
|
Chris@0
|
41 lastname.to_s
|
Chris@0
|
42 end
|
Chris@909
|
43
|
Chris@1115
|
44 def name
|
Chris@1115
|
45 lastname
|
Chris@1115
|
46 end
|
Chris@1115
|
47
|
Chris@1115
|
48 def name=(arg)
|
Chris@1115
|
49 self.lastname = arg
|
Chris@1115
|
50 end
|
Chris@909
|
51
|
Chris@0
|
52 def user_added(user)
|
Chris@0
|
53 members.each do |member|
|
Chris@119
|
54 next if member.project.nil?
|
Chris@0
|
55 user_member = Member.find_by_project_id_and_user_id(member.project_id, user.id) || Member.new(:project_id => member.project_id, :user_id => user.id)
|
Chris@0
|
56 member.member_roles.each do |member_role|
|
Chris@0
|
57 user_member.member_roles << MemberRole.new(:role => member_role.role, :inherited_from => member_role.id)
|
Chris@0
|
58 end
|
Chris@0
|
59 user_member.save!
|
Chris@0
|
60 end
|
Chris@0
|
61 end
|
Chris@909
|
62
|
Chris@0
|
63 def user_removed(user)
|
Chris@0
|
64 members.each do |member|
|
Chris@0
|
65 MemberRole.find(:all, :include => :member,
|
Chris@0
|
66 :conditions => ["#{Member.table_name}.user_id = ? AND #{MemberRole.table_name}.inherited_from IN (?)", user.id, member.member_role_ids]).each(&:destroy)
|
Chris@0
|
67 end
|
Chris@0
|
68 end
|
Chris@909
|
69
|
Chris@1115
|
70 def self.human_attribute_name(attribute_key_name, *args)
|
Chris@1115
|
71 attr_name = attribute_key_name.to_s
|
Chris@909
|
72 if attr_name == 'lastname'
|
Chris@909
|
73 attr_name = "name"
|
Chris@909
|
74 end
|
Chris@1115
|
75 super(attr_name, *args)
|
Chris@909
|
76 end
|
Chris@909
|
77
|
Chris@909
|
78 private
|
Chris@909
|
79
|
Chris@909
|
80 # Removes references that are not handled by associations
|
Chris@909
|
81 def remove_references_before_destroy
|
Chris@909
|
82 return if self.id.nil?
|
Chris@909
|
83
|
Chris@909
|
84 Issue.update_all 'assigned_to_id = NULL', ['assigned_to_id = ?', id]
|
Chris@909
|
85 end
|
Chris@0
|
86 end
|