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