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.

member_role.rb 2.5KB

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