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