Chris@0
|
1 # Redmine - project management software
|
Chris@909
|
2 # Copyright (C) 2006-2011 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 Member < ActiveRecord::Base
|
Chris@0
|
19 belongs_to :user
|
Chris@0
|
20 belongs_to :principal, :foreign_key => 'user_id'
|
Chris@0
|
21 has_many :member_roles, :dependent => :destroy
|
Chris@0
|
22 has_many :roles, :through => :member_roles
|
Chris@0
|
23 belongs_to :project
|
Chris@0
|
24
|
Chris@0
|
25 validates_presence_of :principal, :project
|
Chris@0
|
26 validates_uniqueness_of :user_id, :scope => :project_id
|
Chris@0
|
27
|
Chris@0
|
28 after_destroy :unwatch_from_permission_change
|
Chris@909
|
29
|
Chris@0
|
30 def name
|
Chris@0
|
31 self.user.name
|
Chris@0
|
32 end
|
Chris@909
|
33
|
Chris@0
|
34 alias :base_role_ids= :role_ids=
|
Chris@0
|
35 def role_ids=(arg)
|
Chris@0
|
36 ids = (arg || []).collect(&:to_i) - [0]
|
Chris@0
|
37 # Keep inherited roles
|
Chris@0
|
38 ids += member_roles.select {|mr| !mr.inherited_from.nil?}.collect(&:role_id)
|
Chris@909
|
39
|
Chris@0
|
40 new_role_ids = ids - role_ids
|
Chris@0
|
41 # Add new roles
|
Chris@0
|
42 new_role_ids.each {|id| member_roles << MemberRole.new(:role_id => id) }
|
Chris@0
|
43 # Remove roles (Rails' #role_ids= will not trigger MemberRole#on_destroy)
|
Chris@0
|
44 member_roles_to_destroy = member_roles.select {|mr| !ids.include?(mr.role_id)}
|
Chris@0
|
45 if member_roles_to_destroy.any?
|
Chris@0
|
46 member_roles_to_destroy.each(&:destroy)
|
Chris@0
|
47 unwatch_from_permission_change
|
Chris@0
|
48 end
|
Chris@0
|
49 end
|
Chris@909
|
50
|
Chris@0
|
51 def <=>(member)
|
Chris@0
|
52 a, b = roles.sort.first, member.roles.sort.first
|
Chris@929
|
53 if a == b
|
Chris@929
|
54 if principal
|
Chris@929
|
55 principal <=> member.principal
|
Chris@929
|
56 else
|
Chris@929
|
57 1
|
Chris@929
|
58 end
|
Chris@929
|
59 elsif a
|
Chris@929
|
60 a <=> b
|
Chris@929
|
61 else
|
Chris@929
|
62 1
|
Chris@929
|
63 end
|
Chris@0
|
64 end
|
Chris@909
|
65
|
Chris@0
|
66 def deletable?
|
Chris@0
|
67 member_roles.detect {|mr| mr.inherited_from}.nil?
|
Chris@0
|
68 end
|
Chris@909
|
69
|
Chris@0
|
70 def include?(user)
|
Chris@0
|
71 if principal.is_a?(Group)
|
Chris@0
|
72 !user.nil? && user.groups.include?(principal)
|
Chris@0
|
73 else
|
Chris@0
|
74 self.user == user
|
Chris@0
|
75 end
|
Chris@0
|
76 end
|
Chris@909
|
77
|
Chris@0
|
78 def before_destroy
|
Chris@0
|
79 if user
|
Chris@0
|
80 # remove category based auto assignments for this member
|
Chris@0
|
81 IssueCategory.update_all "assigned_to_id = NULL", ["project_id = ? AND assigned_to_id = ?", project.id, user.id]
|
Chris@0
|
82 end
|
Chris@0
|
83 end
|
Chris@0
|
84
|
Chris@0
|
85 # Find or initilize a Member with an id, attributes, and for a Principal
|
Chris@0
|
86 def self.edit_membership(id, new_attributes, principal=nil)
|
Chris@0
|
87 @membership = id.present? ? Member.find(id) : Member.new(:principal => principal)
|
Chris@0
|
88 @membership.attributes = new_attributes
|
Chris@0
|
89 @membership
|
Chris@0
|
90 end
|
Chris@909
|
91
|
Chris@0
|
92 protected
|
Chris@909
|
93
|
Chris@0
|
94 def validate
|
Chris@14
|
95 errors.add_on_empty :role if member_roles.empty? && roles.empty?
|
Chris@0
|
96 end
|
Chris@909
|
97
|
Chris@0
|
98 private
|
Chris@909
|
99
|
Chris@0
|
100 # Unwatch things that the user is no longer allowed to view inside project
|
Chris@0
|
101 def unwatch_from_permission_change
|
Chris@0
|
102 if user
|
Chris@0
|
103 Watcher.prune(:user => user, :project => project)
|
Chris@0
|
104 end
|
Chris@0
|
105 end
|
Chris@0
|
106 end
|