has_roles.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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, access)
  28. return false if !groups_access_permission?
  29. access = Array(access).map(&:to_sym) | [:full]
  30. RoleGroup.eager_load(:group, :role).exists?(
  31. role_id: roles.pluck(:id),
  32. group_id: group,
  33. access: access,
  34. groups: {
  35. active: true
  36. },
  37. roles: {
  38. active: true
  39. }
  40. )
  41. end
  42. def role_check_preference_notifications_default(new_role)
  43. return true if preferences.dig(:notification_config, :matrix)
  44. # Check if the new role for the user has "ticket.agent" permission.
  45. return if new_role.permissions.none? { |permission| permission.name == 'ticket.agent' }
  46. fill_notification_config_preferences
  47. self.reset_notification_config_before_save = true
  48. save if persisted?
  49. true
  50. end
  51. # methods defined here are going to extend the class, not the instance of it
  52. class_methods do
  53. # Lists instances having the given access(es) to the given Group through Roles.
  54. #
  55. # @example Group ID param
  56. # User.role_access(1, 'read')
  57. # #=> [1, 3, ...]
  58. #
  59. # @example Group param
  60. # User.role_access(group, 'read')
  61. # #=> [1, 3, ...]
  62. #
  63. # @example Access list
  64. # User.role_access(group, ['read', 'create'])
  65. # #=> [1, 3, ...]
  66. #
  67. # @return [Array<Integer>]
  68. def role_access(group, access)
  69. access = Array(access).map(&:to_sym) | [:full]
  70. role_ids = RoleGroup.eager_load(:role).where(group_id: group, access: access, roles: { active: true }).pluck(:role_id)
  71. join_table = reflect_on_association(:roles).join_table
  72. Permission.join_with(self, 'ticket.agent').joins(:roles).where(active: true, join_table => { role_id: role_ids }).distinct
  73. end
  74. # Lists IDs of instances having the given access(es) to the given Group through Roles.
  75. #
  76. # @example Group ID param
  77. # User.role_access_ids(1, 'read')
  78. # #=> [1, 3, ...]
  79. #
  80. # @example Group param
  81. # User.role_access_ids(group, 'read')
  82. # #=> [1, 3, ...]
  83. #
  84. # @example Access list
  85. # User.role_access_ids(group, ['read', 'create'])
  86. # #=> [1, 3, ...]
  87. #
  88. # @return [Array<Integer>]
  89. def role_access_ids(group_id, access)
  90. role_access(group_id, access).collect(&:id)
  91. end
  92. end
  93. end