2009-05-10 14:54:31 +04:00
|
|
|
# Redmine - project management software
|
2013-01-12 13:29:31 +04:00
|
|
|
# Copyright (C) 2006-2013 Jean-Philippe Lang
|
2009-05-10 14:54:31 +04: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:53:41 +04:00
|
|
|
#
|
2009-05-10 14:54:31 +04: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:53:41 +04:00
|
|
|
#
|
2009-05-10 14:54:31 +04: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 MemberRole < ActiveRecord::Base
|
|
|
|
belongs_to :member
|
|
|
|
belongs_to :role
|
2011-08-21 05:53:41 +04:00
|
|
|
|
2009-09-12 12:36:46 +04:00
|
|
|
after_destroy :remove_member_if_empty
|
|
|
|
|
2013-02-02 16:50:45 +04:00
|
|
|
after_create :add_role_to_group_users, :add_role_to_subprojects
|
|
|
|
after_destroy :remove_inherited_roles
|
2011-08-21 05:53:41 +04:00
|
|
|
|
2009-05-10 14:54:31 +04:00
|
|
|
validates_presence_of :role
|
2011-12-04 09:21:20 +04:00
|
|
|
validate :validate_role_member
|
2011-08-21 05:53:41 +04:00
|
|
|
|
2011-12-04 09:21:20 +04:00
|
|
|
def validate_role_member
|
2009-05-10 14:54:31 +04:00
|
|
|
errors.add :role_id, :invalid if role && !role.member?
|
|
|
|
end
|
2011-08-21 05:53:41 +04:00
|
|
|
|
2009-12-26 19:14:55 +03:00
|
|
|
def inherited?
|
|
|
|
!inherited_from.nil?
|
|
|
|
end
|
2011-08-21 05:53:41 +04:00
|
|
|
|
2009-09-12 12:36:46 +04:00
|
|
|
private
|
2011-08-21 05:53:41 +04:00
|
|
|
|
2009-09-12 12:36:46 +04:00
|
|
|
def remove_member_if_empty
|
|
|
|
if member.roles.empty?
|
|
|
|
member.destroy
|
|
|
|
end
|
|
|
|
end
|
2011-08-21 05:53:41 +04:00
|
|
|
|
2009-09-12 12:36:46 +04:00
|
|
|
def add_role_to_group_users
|
2013-02-02 16:50:45 +04:00
|
|
|
if member.principal.is_a?(Group) && !inherited?
|
2009-09-12 12:36:46 +04:00
|
|
|
member.principal.users.each do |user|
|
2013-02-02 16:50:45 +04:00
|
|
|
user_member = Member.find_or_new(member.project_id, user.id)
|
2009-09-12 12:36:46 +04:00
|
|
|
user_member.member_roles << MemberRole.new(:role => role, :inherited_from => id)
|
|
|
|
user_member.save!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-08-21 05:53:41 +04:00
|
|
|
|
2013-02-02 16:50:45 +04:00
|
|
|
def add_role_to_subprojects
|
|
|
|
member.project.children.each do |subproject|
|
|
|
|
if subproject.inherit_members?
|
|
|
|
child_member = Member.find_or_new(subproject.id, member.user_id)
|
|
|
|
child_member.member_roles << MemberRole.new(:role => role, :inherited_from => id)
|
|
|
|
child_member.save!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_inherited_roles
|
2012-12-03 00:23:48 +04:00
|
|
|
MemberRole.where(:inherited_from => id).all.group_by(&:member).each do |member, member_roles|
|
2009-12-13 15:39:22 +03:00
|
|
|
member_roles.each(&:destroy)
|
|
|
|
end
|
2009-09-12 12:36:46 +04:00
|
|
|
end
|
2009-05-10 14:54:31 +04:00
|
|
|
end
|