Chris@1296: # Redmine - project management software Chris@1296: # Copyright (C) 2006-2012 Jean-Philippe Lang Chris@1296: # Chris@1296: # This program is free software; you can redistribute it and/or Chris@1296: # modify it under the terms of the GNU General Public License Chris@1296: # as published by the Free Software Foundation; either version 2 Chris@1296: # of the License, or (at your option) any later version. Chris@1296: # Chris@1296: # This program is distributed in the hope that it will be useful, Chris@1296: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@1296: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@1296: # GNU General Public License for more details. Chris@1296: # Chris@1296: # You should have received a copy of the GNU General Public License Chris@1296: # along with this program; if not, write to the Free Software Chris@1296: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@1296: Chris@1296: class Member < ActiveRecord::Base Chris@1296: belongs_to :user Chris@1296: belongs_to :principal, :foreign_key => 'user_id' Chris@1296: has_many :member_roles, :dependent => :destroy Chris@1296: has_many :roles, :through => :member_roles Chris@1296: belongs_to :project Chris@1296: Chris@1296: validates_presence_of :principal, :project Chris@1296: validates_uniqueness_of :user_id, :scope => :project_id Chris@1296: validate :validate_role Chris@1296: Chris@1296: before_destroy :set_issue_category_nil Chris@1296: after_destroy :unwatch_from_permission_change Chris@1296: Chris@1296: def role Chris@1296: end Chris@1296: Chris@1296: def role= Chris@1296: end Chris@1296: Chris@1296: def name Chris@1296: self.user.name Chris@1296: end Chris@1296: Chris@1296: alias :base_role_ids= :role_ids= Chris@1296: def role_ids=(arg) Chris@1296: ids = (arg || []).collect(&:to_i) - [0] Chris@1296: # Keep inherited roles Chris@1296: ids += member_roles.select {|mr| !mr.inherited_from.nil?}.collect(&:role_id) Chris@1296: Chris@1296: new_role_ids = ids - role_ids Chris@1296: # Add new roles Chris@1296: new_role_ids.each {|id| member_roles << MemberRole.new(:role_id => id) } Chris@1296: # Remove roles (Rails' #role_ids= will not trigger MemberRole#on_destroy) Chris@1296: member_roles_to_destroy = member_roles.select {|mr| !ids.include?(mr.role_id)} Chris@1296: if member_roles_to_destroy.any? Chris@1296: member_roles_to_destroy.each(&:destroy) Chris@1296: unwatch_from_permission_change Chris@1296: end Chris@1296: end Chris@1296: Chris@1296: def <=>(member) Chris@1296: a, b = roles.sort.first, member.roles.sort.first Chris@1296: if a == b Chris@1296: if principal Chris@1296: principal <=> member.principal Chris@1296: else Chris@1296: 1 Chris@1296: end Chris@1296: elsif a Chris@1296: a <=> b Chris@1296: else Chris@1296: 1 Chris@1296: end Chris@1296: end Chris@1296: Chris@1296: def deletable? Chris@1296: member_roles.detect {|mr| mr.inherited_from}.nil? Chris@1296: end Chris@1296: Chris@1296: def include?(user) Chris@1296: if principal.is_a?(Group) Chris@1296: !user.nil? && user.groups.include?(principal) Chris@1296: else Chris@1296: self.user == user Chris@1296: end Chris@1296: end Chris@1296: Chris@1296: def set_issue_category_nil Chris@1296: if user Chris@1296: # remove category based auto assignments for this member Chris@1296: IssueCategory.update_all "assigned_to_id = NULL", ["project_id = ? AND assigned_to_id = ?", project.id, user.id] Chris@1296: end Chris@1296: end Chris@1296: Chris@1296: # Find or initilize a Member with an id, attributes, and for a Principal Chris@1296: def self.edit_membership(id, new_attributes, principal=nil) Chris@1296: @membership = id.present? ? Member.find(id) : Member.new(:principal => principal) Chris@1296: @membership.attributes = new_attributes Chris@1296: @membership Chris@1296: end Chris@1296: Chris@1296: protected Chris@1296: Chris@1296: def validate_role Chris@1296: errors.add_on_empty :role if member_roles.empty? && roles.empty? Chris@1296: end Chris@1296: Chris@1296: private Chris@1296: Chris@1296: # Unwatch things that the user is no longer allowed to view inside project Chris@1296: def unwatch_from_permission_change Chris@1296: if user Chris@1296: Watcher.prune(:user => user, :project => project) Chris@1296: end Chris@1296: end Chris@1296: end