您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

member_role.rb 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2021 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 < ActiveRecord::Base
  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 if role && !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.empty?
  45. member.destroy
  46. end
  47. end
  48. def add_role_to_group_users
  49. if member.principal.is_a?(Group) && !inherited?
  50. member.principal.users.each do |user|
  51. user_member = Member.find_or_new(member.project_id, user.id)
  52. user_member.member_roles << MemberRole.new(:role => role, :inherited_from => id)
  53. user_member.save!
  54. end
  55. end
  56. end
  57. def add_role_to_subprojects
  58. member.project.children.each do |subproject|
  59. if subproject.inherit_members?
  60. child_member = Member.find_or_new(subproject.id, member.user_id)
  61. child_member.member_roles << MemberRole.new(:role => role, :inherited_from => id)
  62. child_member.save!
  63. end
  64. end
  65. end
  66. def remove_inherited_roles
  67. MemberRole.where(:inherited_from => id).destroy_all
  68. end
  69. end