has_roles.rb 2.9 KB

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