You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2017 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. class MemberRole < ActiveRecord::Base
  18. belongs_to :member
  19. belongs_to :role
  20. after_destroy :remove_member_if_empty
  21. after_create :add_role_to_group_users, :add_role_to_subprojects
  22. after_destroy :remove_inherited_roles
  23. validates_presence_of :role
  24. validate :validate_role_member
  25. def validate_role_member
  26. errors.add :role_id, :invalid if role && !role.member?
  27. end
  28. def inherited?
  29. !inherited_from.nil?
  30. end
  31. # Destroys the MemberRole without destroying its Member if it doesn't have
  32. # any other roles
  33. def destroy_without_member_removal
  34. @member_removal = false
  35. destroy
  36. end
  37. private
  38. def remove_member_if_empty
  39. if @member_removal != false && member.roles.empty?
  40. member.destroy
  41. end
  42. end
  43. def add_role_to_group_users
  44. if member.principal.is_a?(Group) && !inherited?
  45. member.principal.users.each do |user|
  46. user_member = Member.find_or_new(member.project_id, user.id)
  47. user_member.member_roles << MemberRole.new(:role => role, :inherited_from => id)
  48. user_member.save!
  49. end
  50. end
  51. end
  52. def add_role_to_subprojects
  53. member.project.children.each do |subproject|
  54. if subproject.inherit_members?
  55. child_member = Member.find_or_new(subproject.id, member.user_id)
  56. child_member.member_roles << MemberRole.new(:role => role, :inherited_from => id)
  57. child_member.save!
  58. end
  59. end
  60. end
  61. def remove_inherited_roles
  62. MemberRole.where(:inherited_from => id).destroy_all
  63. end
  64. end