has_roles.rb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module HasRoles
  3. extend ActiveSupport::Concern
  4. included do
  5. attr_accessor :reset_notification_config_before_save
  6. has_and_belongs_to_many :roles,
  7. before_add: %i[validate_agent_limit_by_role validate_roles],
  8. after_add: %i[cache_update role_check_preference_notifications_default],
  9. before_remove: :last_admin_check_by_role,
  10. after_remove: %i[cache_update]
  11. end
  12. # Checks a given Group( ID) for given access(es) for the instance associated roles.
  13. #
  14. # @example Group ID param
  15. # user.role_access?(1, 'read')
  16. # #=> true
  17. #
  18. # @example Group param
  19. # user.role_access?(group, 'read')
  20. # #=> true
  21. #
  22. # @example Access list
  23. # user.role_access?(group, ['read', 'create'])
  24. # #=> true
  25. #
  26. # @return [Boolean]
  27. def role_access?(group_id, access)
  28. return false if !groups_access_permission?
  29. group_id = self.class.ensure_group_id_parameter(group_id)
  30. access = Array(access).map(&:to_sym) | [:full]
  31. RoleGroup.eager_load(:group, :role).exists?(
  32. role_id: roles.pluck(:id),
  33. group_id: group_id,
  34. access: access,
  35. groups: {
  36. active: true
  37. },
  38. roles: {
  39. active: true
  40. }
  41. )
  42. end
  43. def role_check_preference_notifications_default(new_role)
  44. return true if preferences.dig(:notification_config, :matrix)
  45. # Check if the new role for the user has "ticket.agent" permission.
  46. return if new_role.permissions.none? { |permission| permission.name == 'ticket.agent' }
  47. fill_notification_config_preferences
  48. self.reset_notification_config_before_save = true
  49. save if persisted?
  50. true
  51. end
  52. # methods defined here are going to extend the class, not the instance of it
  53. class_methods do
  54. # Lists instances having the given access(es) to the given Group through Roles.
  55. #
  56. # @example Group ID param
  57. # User.role_access(1, 'read')
  58. # #=> [1, 3, ...]
  59. #
  60. # @example Group param
  61. # User.role_access(group, 'read')
  62. # #=> [1, 3, ...]
  63. #
  64. # @example Access list
  65. # User.role_access(group, ['read', 'create'])
  66. # #=> [1, 3, ...]
  67. #
  68. # @return [Array<Integer>]
  69. def role_access(group_id, access)
  70. group_id = ensure_group_id_parameter(group_id)
  71. access = Array(access).map(&:to_sym) | [:full]
  72. role_ids = RoleGroup.eager_load(:role).where(group_id: group_id, access: access, roles: { active: true }).pluck(:role_id)
  73. join_table = reflect_on_association(:roles).join_table
  74. Permission.join_with(self, 'ticket.agent').joins(:roles).where(active: true, join_table => { role_id: role_ids }).distinct
  75. end
  76. # Lists IDs of instances having the given access(es) to the given Group through Roles.
  77. #
  78. # @example Group ID param
  79. # User.role_access_ids(1, 'read')
  80. # #=> [1, 3, ...]
  81. #
  82. # @example Group param
  83. # User.role_access_ids(group, 'read')
  84. # #=> [1, 3, ...]
  85. #
  86. # @example Access list
  87. # User.role_access_ids(group, ['read', 'create'])
  88. # #=> [1, 3, ...]
  89. #
  90. # @return [Array<Integer>]
  91. def role_access_ids(group_id, access)
  92. role_access(group_id, access).collect(&:id)
  93. end
  94. def ensure_group_id_parameter(group_or_id)
  95. return group_or_id if group_or_id.is_a?(Integer)
  96. group_or_id.id
  97. end
  98. end
  99. end