has_groups.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module HasGroups
  3. extend ActiveSupport::Concern
  4. included do
  5. before_destroy :destroy_group_relations
  6. attr_accessor :group_access_buffer
  7. after_save :process_group_access_buffer
  8. # add association to Group, too but ignore it in asset output
  9. Group.has_many group_through_identifier
  10. Group.has_many model_name.collection.to_sym, through: group_through_identifier, after_add: :cache_update, after_remove: :cache_update, dependent: :destroy
  11. Group.association_attributes_ignored group_through_identifier
  12. association_attributes_ignored :groups, group_through_identifier
  13. has_many group_through_identifier
  14. has_many :groups, through: group_through_identifier do
  15. # A helper to join the :through table into the result of groups to access :through attributes
  16. #
  17. # @param [String, Array<String>] access Limiting to one or more access verbs. 'full' gets added automatically
  18. #
  19. # @example All access groups
  20. # user.groups.access
  21. # #=> [#<Group id: 1, access="read", ...>, ...]
  22. #
  23. # @example Groups for given access(es) plus 'full'
  24. # user.groups.access('read')
  25. # #=> [#<Group id: 1, access="full", ...>, ...]
  26. #
  27. # @example Groups for given access(es)es plus 'full'
  28. # user.groups.access('read', 'change')
  29. # #=> [#<Group id: 1, access="full", ...>, ...]
  30. #
  31. # @return [ActiveRecord::AssociationRelation<[<Group]>] List of Groups with :through attributes
  32. def access(*access)
  33. table_name = proxy_association.owner.class.group_through.table_name
  34. query = select("#{ActiveRecord::Base.connection.quote_table_name('groups')}.*, #{ActiveRecord::Base.connection.quote_table_name(table_name)}.*")
  35. return query if access.blank?
  36. access.push('full') if access.exclude?('full')
  37. query.where("#{table_name}.access" => access)
  38. end
  39. end
  40. end
  41. # Checks a given Group( ID) for given access(es) for the instance.
  42. # Checks indirect access via Roles if instance has Roles, too.
  43. #
  44. # @example Group ID param
  45. # user.group_access?(1, 'read')
  46. # #=> true
  47. #
  48. # @example Group param
  49. # user.group_access?(group, 'read')
  50. # #=> true
  51. #
  52. # @example Access list
  53. # user.group_access?(group, ['read', 'create'])
  54. # #=> true
  55. #
  56. # @return [Boolean]
  57. def group_access?(group, access)
  58. Auth::RequestCache.fetch_value("group_access/#{cache_key_with_version}/#{group.to_param}/#{access}") do
  59. group_access_uncached?(group, access)
  60. end
  61. end
  62. def group_access_uncached?(group, access)
  63. return false if !active?
  64. return false if !groups_access_permission?
  65. access = Array(access).map(&:to_sym) | [:full]
  66. # check direct access
  67. return true if group_through.klass.eager_load(:group).exists?(
  68. group_through.foreign_key => id,
  69. group_id: group,
  70. access: access,
  71. groups: {
  72. active: true
  73. }
  74. )
  75. # check indirect access through Roles if possible
  76. return false if !respond_to?(:role_access?)
  77. role_access?(group, access)
  78. end
  79. # Lists the Group IDs the instance has the given access(es) plus 'full' to.
  80. # Adds indirect accessable Group IDs via Roles if instance has Roles, too.
  81. #
  82. # @example Single access
  83. # user.group_ids_access('read')
  84. # #=> [1, 3, ...]
  85. #
  86. # @example Access list
  87. # user.group_ids_access(['read', 'create'])
  88. # #=> [1, 3, ...]
  89. #
  90. # @return [Array<Integer>] Group IDs the instance has the given access(es) to.
  91. def group_ids_access(access)
  92. return [] if !active?
  93. return [] if !groups_access_permission?
  94. access = Array(access).map(&:to_sym) | [:full]
  95. foreign_key = group_through.foreign_key
  96. klass = group_through.klass
  97. # check direct access
  98. ids = klass.eager_load(:group).where(foreign_key => id, access: access, groups: { active: true }).pluck(:group_id)
  99. ids ||= []
  100. # check indirect access through roles if possible
  101. return ids if !respond_to?(:role_ids)
  102. role_group_ids = RoleGroup.eager_load(:group).where(role_id: role_ids, access: access, groups: { active: true }).pluck(:group_id)
  103. # combines and removes duplicates
  104. # and returns them in one statement
  105. ids | role_group_ids
  106. end
  107. # Lists Groups the instance has the given access(es) plus 'full' to.
  108. # Adds indirect accessable Groups via Roles if instance has Roles, too.
  109. #
  110. # @example Single access
  111. # user.groups_access('read')
  112. # #=> [#<Group id: 1, access="read", ...>, ...]
  113. #
  114. # @example Access list
  115. # user.groups_access(['read', 'create'])
  116. # #=> [#<Group id: 1, access="read", ...>, ...]
  117. #
  118. # @return [Array<Group>] Groups the instance has the given access(es) to.
  119. def groups_access(access)
  120. group_ids = group_ids_access(access)
  121. Group.where(id: group_ids)
  122. end
  123. # Returns a map of Group name to access
  124. #
  125. # @example
  126. # user.group_names_access_map
  127. # #=> {'Users' => 'full', 'Support' => ['read', 'change']}
  128. #
  129. # @return [Hash<String=>String,Array<String>>] The map of Group name to access
  130. def group_names_access_map
  131. groups_access_map(:name)
  132. end
  133. # Stores a map of Group ID to access. Deletes all other relations.
  134. #
  135. # @example
  136. # user.group_names_access_map = {'Users' => 'full', 'Support' => ['read', 'change']}
  137. # #=> {'Users' => 'full', 'Support' => ['read', 'change']}
  138. #
  139. # @return [Hash<String=>String,Array<String>>] The given map
  140. def group_names_access_map=(name_access_map)
  141. groups_access_map_store(name_access_map) do |group_name|
  142. Group.where(name: group_name).pick(:id)
  143. end
  144. end
  145. # Returns a map of Group ID to access
  146. #
  147. # @example
  148. # user.group_ids_access_map
  149. # #=> {1 => 'full', 42 => ['read', 'change']}
  150. #
  151. # @return [Hash<Integer=>String,Array<String>>] The map of Group ID to access
  152. def group_ids_access_map
  153. groups_access_map(:id)
  154. end
  155. # Stores a map of Group ID to access. Deletes all other relations.
  156. #
  157. # @example
  158. # user.group_ids_access_map = {1 => 'full', 42 => ['read', 'change']}
  159. # #=> {1 => 'full', 42 => ['read', 'change']}
  160. #
  161. # @return [Hash<Integer=>String,Array<String>>] The given map
  162. def group_ids_access_map=(id_access_map)
  163. groups_access_map_store(id_access_map)
  164. end
  165. # An alias to .groups class method
  166. def group_through
  167. @group_through ||= self.class.group_through
  168. end
  169. # Checks if the instance has general permission to Group access.
  170. #
  171. # @example
  172. # customer_user.groups_access_permission?
  173. # #=> false
  174. #
  175. # @return [Boolean]
  176. def groups_access_permission?
  177. return true if !respond_to?(:permissions?)
  178. permissions?('ticket.agent')
  179. end
  180. private
  181. def groups_access_map(key)
  182. return {} if !active?
  183. return {} if !groups_access_permission?
  184. groups.access.where(active: true).pluck(key, :access).each_with_object({}) do |entry, hash|
  185. hash[ entry[0] ] ||= []
  186. hash[ entry[0] ].push(entry[1])
  187. end
  188. end
  189. def groups_access_map_store(map)
  190. fill_group_access_buffer do
  191. Hash(map).each do |group_identifier, accesses|
  192. # use given key as identifier or look it up
  193. # via the given block which returns the identifier
  194. group_id = block_given? ? yield(group_identifier) : group_identifier
  195. Array(accesses).each do |access|
  196. push_group_access_buffer(
  197. group_id: group_id,
  198. access: access
  199. )
  200. end
  201. end
  202. end
  203. end
  204. def fill_group_access_buffer
  205. @group_access_buffer = []
  206. yield
  207. process_group_access_buffer if id
  208. end
  209. def push_group_access_buffer(entry)
  210. @group_access_buffer.push(entry)
  211. end
  212. def flush_group_access_buffer
  213. # group_access_buffer is at least an empty Array
  214. # if changes to the map were performed
  215. # otherwise it's just an update of other attributes
  216. return if group_access_buffer.nil?
  217. yield
  218. self.group_access_buffer = nil
  219. end
  220. def process_group_access_buffer
  221. flush_group_access_buffer do
  222. destroy_group_relations
  223. break if group_access_buffer.blank?
  224. foreign_key = group_through.foreign_key
  225. entries = group_access_buffer.collect do |entry|
  226. entry[foreign_key] = id
  227. entry
  228. end
  229. group_through.klass.create!(entries)
  230. end
  231. true
  232. end
  233. def destroy_group_relations
  234. group_through.klass.where(group_through.foreign_key => id).destroy_all
  235. end
  236. # methods defined here are going to extend the class, not the instance of it
  237. class_methods do
  238. # Lists IDs of instances having the given access(es) to the given Group.
  239. #
  240. # @example Group ID param
  241. # User.group_access_ids(1, 'read')
  242. # #=> [1, 3, ...]
  243. #
  244. # @example Group param
  245. # User.group_access_ids(group, 'read')
  246. # #=> [1, 3, ...]
  247. #
  248. # @example Access list
  249. # User.group_access_ids(group, ['read', 'create'])
  250. # #=> [1, 3, ...]
  251. #
  252. # @return [Array<Integer>]
  253. def group_access_ids(group_id, access)
  254. group_access(group_id, access).collect(&:id)
  255. end
  256. # Lists instances having the given access(es) to the given Group.
  257. #
  258. # @example Group ID param
  259. # User.group_access(1, 'read')
  260. # #=> [#<User id: 1, ...>, ...]
  261. #
  262. # @example Group param
  263. # User.group_access(group, 'read')
  264. # #=> [#<User id: 1, ...>, ...]
  265. #
  266. # @example Access list
  267. # User.group_access(group, ['read', 'create'])
  268. # #=> [#<User id: 1, ...>, ...]
  269. #
  270. # @return [Array<Class>]
  271. def group_access(group, access)
  272. access = Array(access).map(&:to_sym) | [:full]
  273. # check direct access
  274. instances = Permission.join_with(self, 'ticket.agent').joins(group_through.name)
  275. .where(group_through.table_name => { group_id: group, access: access }, active: true)
  276. # check indirect access through roles if possible
  277. return instances if !respond_to?(:role_access)
  278. # combines and removes duplicates
  279. # and returns them in one statement
  280. instances | role_access(group, access)
  281. end
  282. # The reflection instance containing the association data
  283. #
  284. # @example
  285. # User.group_through
  286. # #=> <ActiveRecord::Reflection::HasManyReflection:0x007fd2f5785440 @name=:user_groups, ...>
  287. #
  288. # @return [ActiveRecord::Reflection::HasManyReflection] The given map
  289. def group_through
  290. @group_through ||= reflect_on_association(group_through_identifier)
  291. end
  292. # The identifier of the has_many :through relation
  293. #
  294. # @example
  295. # User.group_through_identifier
  296. # #=> :user_groups
  297. #
  298. # @return [Symbol] The relation identifier
  299. def group_through_identifier
  300. :"#{name.downcase}_groups"
  301. end
  302. end
  303. end