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