2009-05-10 14:54:31 +04:00
|
|
|
# Redmine - project management software
|
2014-01-30 02:45:39 +04:00
|
|
|
# Copyright (C) 2006-2014 Jean-Philippe Lang
|
2007-03-12 20:59:02 +03:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# of the License, or (at your option) any later version.
|
2011-08-21 05:54:24 +04:00
|
|
|
#
|
2007-03-12 20:59:02 +03:00
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
2011-08-21 05:54:24 +04:00
|
|
|
#
|
2007-03-12 20:59:02 +03:00
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
class Member < ActiveRecord::Base
|
|
|
|
belongs_to :user
|
2009-09-12 12:36:46 +04:00
|
|
|
belongs_to :principal, :foreign_key => 'user_id'
|
|
|
|
has_many :member_roles, :dependent => :destroy
|
2009-05-10 14:54:31 +04:00
|
|
|
has_many :roles, :through => :member_roles
|
2007-03-12 20:59:02 +03:00
|
|
|
belongs_to :project
|
|
|
|
|
2009-09-12 12:36:46 +04:00
|
|
|
validates_presence_of :principal, :project
|
2007-03-12 20:59:02 +03:00
|
|
|
validates_uniqueness_of :user_id, :scope => :project_id
|
2011-12-03 18:33:49 +04:00
|
|
|
validate :validate_role
|
2009-12-13 15:39:22 +03:00
|
|
|
|
2011-12-03 20:41:23 +04:00
|
|
|
before_destroy :set_issue_category_nil
|
2011-08-21 05:54:24 +04:00
|
|
|
|
2011-12-08 19:12:02 +04:00
|
|
|
def role
|
|
|
|
end
|
|
|
|
|
|
|
|
def role=
|
|
|
|
end
|
|
|
|
|
2007-03-12 20:59:02 +03:00
|
|
|
def name
|
2007-06-29 21:27:27 +04:00
|
|
|
self.user.name
|
2006-07-29 23:54:22 +04:00
|
|
|
end
|
2011-08-21 05:54:24 +04:00
|
|
|
|
2009-09-12 12:36:46 +04:00
|
|
|
alias :base_role_ids= :role_ids=
|
|
|
|
def role_ids=(arg)
|
|
|
|
ids = (arg || []).collect(&:to_i) - [0]
|
|
|
|
# Keep inherited roles
|
|
|
|
ids += member_roles.select {|mr| !mr.inherited_from.nil?}.collect(&:role_id)
|
2011-08-21 05:54:24 +04:00
|
|
|
|
2009-09-12 12:36:46 +04:00
|
|
|
new_role_ids = ids - role_ids
|
|
|
|
# Add new roles
|
|
|
|
new_role_ids.each {|id| member_roles << MemberRole.new(:role_id => id) }
|
|
|
|
# Remove roles (Rails' #role_ids= will not trigger MemberRole#on_destroy)
|
2009-12-13 15:39:22 +03:00
|
|
|
member_roles_to_destroy = member_roles.select {|mr| !ids.include?(mr.role_id)}
|
|
|
|
if member_roles_to_destroy.any?
|
|
|
|
member_roles_to_destroy.each(&:destroy)
|
|
|
|
end
|
2009-03-28 15:07:05 +03:00
|
|
|
end
|
2011-08-21 05:54:24 +04:00
|
|
|
|
2008-01-25 13:31:06 +03:00
|
|
|
def <=>(member)
|
2009-05-10 14:54:31 +04:00
|
|
|
a, b = roles.sort.first, member.roles.sort.first
|
2012-02-12 18:54:30 +04:00
|
|
|
if a == b
|
|
|
|
if principal
|
|
|
|
principal <=> member.principal
|
|
|
|
else
|
|
|
|
1
|
|
|
|
end
|
|
|
|
elsif a
|
|
|
|
a <=> b
|
|
|
|
else
|
|
|
|
1
|
|
|
|
end
|
2009-09-12 12:36:46 +04:00
|
|
|
end
|
2011-08-21 05:54:24 +04:00
|
|
|
|
2009-09-12 12:36:46 +04:00
|
|
|
def deletable?
|
|
|
|
member_roles.detect {|mr| mr.inherited_from}.nil?
|
2008-01-25 13:31:06 +03:00
|
|
|
end
|
2011-08-21 05:54:24 +04:00
|
|
|
|
2009-12-26 18:46:12 +03:00
|
|
|
def include?(user)
|
|
|
|
if principal.is_a?(Group)
|
|
|
|
!user.nil? && user.groups.include?(principal)
|
|
|
|
else
|
|
|
|
self.user == user
|
|
|
|
end
|
|
|
|
end
|
2011-08-21 05:54:24 +04:00
|
|
|
|
2011-12-03 20:41:23 +04:00
|
|
|
def set_issue_category_nil
|
2009-09-12 12:36:46 +04:00
|
|
|
if user
|
|
|
|
# remove category based auto assignments for this member
|
2014-01-08 08:58:13 +04:00
|
|
|
IssueCategory.where(["project_id = ? AND assigned_to_id = ?", project.id, user.id]).
|
|
|
|
update_all("assigned_to_id = NULL")
|
2009-09-12 12:36:46 +04:00
|
|
|
end
|
2007-06-29 21:21:37 +04:00
|
|
|
end
|
2010-03-18 18:49:11 +03:00
|
|
|
|
2014-04-07 12:07:25 +04:00
|
|
|
# Find or initialize a Member with an id, attributes, and for a Principal
|
2010-03-18 18:49:11 +03:00
|
|
|
def self.edit_membership(id, new_attributes, principal=nil)
|
|
|
|
@membership = id.present? ? Member.find(id) : Member.new(:principal => principal)
|
|
|
|
@membership.attributes = new_attributes
|
|
|
|
@membership
|
|
|
|
end
|
2011-08-21 05:54:24 +04:00
|
|
|
|
2013-02-02 16:50:45 +04:00
|
|
|
# Finds or initilizes a Member for the given project and principal
|
|
|
|
def self.find_or_new(project, principal)
|
|
|
|
project_id = project.is_a?(Project) ? project.id : project
|
|
|
|
principal_id = principal.is_a?(Principal) ? principal.id : principal
|
|
|
|
|
|
|
|
member = Member.find_by_project_id_and_user_id(project_id, principal_id)
|
|
|
|
member ||= Member.new(:project_id => project_id, :user_id => principal_id)
|
|
|
|
member
|
|
|
|
end
|
|
|
|
|
2009-05-10 14:54:31 +04:00
|
|
|
protected
|
2011-08-21 05:54:24 +04:00
|
|
|
|
2011-12-03 18:33:49 +04:00
|
|
|
def validate_role
|
2011-12-08 17:37:27 +04:00
|
|
|
errors.add_on_empty :role if member_roles.empty? && roles.empty?
|
2009-05-10 14:54:31 +04:00
|
|
|
end
|
2006-06-28 22:11:03 +04:00
|
|
|
end
|