has_groups.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. # Copyright (C) 2012-2023 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_id, access)
  58. return false if !active?
  59. return false if !groups_access_permission?
  60. group_id = self.class.ensure_group_id_parameter(group_id)
  61. access = Array(access).map(&:to_sym) | [:full]
  62. # check direct access
  63. return true if group_through.klass.eager_load(:group).exists?(
  64. group_through.foreign_key => id,
  65. group_id: group_id,
  66. access: access,
  67. groups: {
  68. active: true
  69. }
  70. )
  71. # check indirect access through Roles if possible
  72. return false if !respond_to?(:role_access?)
  73. role_access?(group_id, access)
  74. end
  75. # Lists the Group IDs the instance has the given access(es) plus 'full' to.
  76. # Adds indirect accessable Group IDs via Roles if instance has Roles, too.
  77. #
  78. # @example Single access
  79. # user.group_ids_access('read')
  80. # #=> [1, 3, ...]
  81. #
  82. # @example Access list
  83. # user.group_ids_access(['read', 'create'])
  84. # #=> [1, 3, ...]
  85. #
  86. # @return [Array<Integer>] Group IDs the instance has the given access(es) to.
  87. def group_ids_access(access)
  88. return [] if !active?
  89. return [] if !groups_access_permission?
  90. access = Array(access).map(&:to_sym) | [:full]
  91. foreign_key = group_through.foreign_key
  92. klass = group_through.klass
  93. # check direct access
  94. ids = klass.eager_load(:group).where(foreign_key => id, access: access, groups: { active: true }).pluck(:group_id)
  95. ids ||= []
  96. # check indirect access through roles if possible
  97. return ids if !respond_to?(:role_ids)
  98. role_group_ids = RoleGroup.eager_load(:group).where(role_id: role_ids, access: access, groups: { active: true }).pluck(:group_id)
  99. # combines and removes duplicates
  100. # and returns them in one statement
  101. ids | role_group_ids
  102. end
  103. # Lists Groups the instance has the given access(es) plus 'full' to.
  104. # Adds indirect accessable Groups via Roles if instance has Roles, too.
  105. #
  106. # @example Single access
  107. # user.groups_access('read')
  108. # #=> [#<Group id: 1, access="read", ...>, ...]
  109. #
  110. # @example Access list
  111. # user.groups_access(['read', 'create'])
  112. # #=> [#<Group id: 1, access="read", ...>, ...]
  113. #
  114. # @return [Array<Group>] Groups the instance has the given access(es) to.
  115. def groups_access(access)
  116. group_ids = group_ids_access(access)
  117. Group.where(id: group_ids)
  118. end
  119. # Returns a map of Group name to access
  120. #
  121. # @example
  122. # user.group_names_access_map
  123. # #=> {'Users' => 'full', 'Support' => ['read', 'change']}
  124. #
  125. # @return [Hash<String=>String,Array<String>>] The map of Group name to access
  126. def group_names_access_map
  127. groups_access_map(:name)
  128. end
  129. # Stores a map of Group ID to access. Deletes all other relations.
  130. #
  131. # @example
  132. # user.group_names_access_map = {'Users' => 'full', 'Support' => ['read', 'change']}
  133. # #=> {'Users' => 'full', 'Support' => ['read', 'change']}
  134. #
  135. # @return [Hash<String=>String,Array<String>>] The given map
  136. def group_names_access_map=(name_access_map)
  137. groups_access_map_store(name_access_map) do |group_name|
  138. Group.where(name: group_name).pick(:id)
  139. end
  140. end
  141. # Returns a map of Group ID to access
  142. #
  143. # @example
  144. # user.group_ids_access_map
  145. # #=> {1 => 'full', 42 => ['read', 'change']}
  146. #
  147. # @return [Hash<Integer=>String,Array<String>>] The map of Group ID to access
  148. def group_ids_access_map
  149. groups_access_map(:id)
  150. end
  151. # Stores a map of Group ID to access. Deletes all other relations.
  152. #
  153. # @example
  154. # user.group_ids_access_map = {1 => 'full', 42 => ['read', 'change']}
  155. # #=> {1 => 'full', 42 => ['read', 'change']}
  156. #
  157. # @return [Hash<Integer=>String,Array<String>>] The given map
  158. def group_ids_access_map=(id_access_map)
  159. groups_access_map_store(id_access_map)
  160. end
  161. # An alias to .groups class method
  162. def group_through
  163. @group_through ||= self.class.group_through
  164. end
  165. # Checks if the instance has general permission to Group access.
  166. #
  167. # @example
  168. # customer_user.groups_access_permission?
  169. # #=> false
  170. #
  171. # @return [Boolean]
  172. def groups_access_permission?
  173. return true if !respond_to?(:permissions?)
  174. permissions?('ticket.agent')
  175. end
  176. private
  177. def groups_access_map(key)
  178. return {} if !active?
  179. return {} if !groups_access_permission?
  180. groups.access.where(active: true).pluck(key, :access).each_with_object({}) do |entry, hash|
  181. hash[ entry[0] ] ||= []
  182. hash[ entry[0] ].push(entry[1])
  183. end
  184. end
  185. def groups_access_map_store(map)
  186. fill_group_access_buffer do
  187. Hash(map).each do |group_identifier, accesses|
  188. # use given key as identifier or look it up
  189. # via the given block which returns the identifier
  190. group_id = block_given? ? yield(group_identifier) : group_identifier
  191. Array(accesses).each do |access|
  192. push_group_access_buffer(
  193. group_id: group_id,
  194. access: access
  195. )
  196. end
  197. end
  198. end
  199. end
  200. def fill_group_access_buffer
  201. @group_access_buffer = []
  202. yield
  203. process_group_access_buffer if id
  204. end
  205. def push_group_access_buffer(entry)
  206. @group_access_buffer.push(entry)
  207. end
  208. def flush_group_access_buffer
  209. # group_access_buffer is at least an empty Array
  210. # if changes to the map were performed
  211. # otherwise it's just an update of other attributes
  212. return if group_access_buffer.nil?
  213. yield
  214. self.group_access_buffer = nil
  215. end
  216. def process_group_access_buffer
  217. flush_group_access_buffer do
  218. destroy_group_relations
  219. break if group_access_buffer.blank?
  220. foreign_key = group_through.foreign_key
  221. entries = group_access_buffer.collect do |entry|
  222. entry[foreign_key] = id
  223. entry
  224. end
  225. group_through.klass.create!(entries)
  226. end
  227. true
  228. end
  229. def destroy_group_relations
  230. group_through.klass.where(group_through.foreign_key => id).destroy_all
  231. end
  232. # methods defined here are going to extend the class, not the instance of it
  233. class_methods do
  234. # Lists IDs of instances having the given access(es) to the given Group.
  235. #
  236. # @example Group ID param
  237. # User.group_access_ids(1, 'read')
  238. # #=> [1, 3, ...]
  239. #
  240. # @example Group param
  241. # User.group_access_ids(group, 'read')
  242. # #=> [1, 3, ...]
  243. #
  244. # @example Access list
  245. # User.group_access_ids(group, ['read', 'create'])
  246. # #=> [1, 3, ...]
  247. #
  248. # @return [Array<Integer>]
  249. def group_access_ids(group_id, access)
  250. group_access(group_id, access).collect(&:id)
  251. end
  252. # Lists instances having the given access(es) to the given Group.
  253. #
  254. # @example Group ID param
  255. # User.group_access(1, 'read')
  256. # #=> [#<User id: 1, ...>, ...]
  257. #
  258. # @example Group param
  259. # User.group_access(group, 'read')
  260. # #=> [#<User id: 1, ...>, ...]
  261. #
  262. # @example Access list
  263. # User.group_access(group, ['read', 'create'])
  264. # #=> [#<User id: 1, ...>, ...]
  265. #
  266. # @return [Array<Class>]
  267. def group_access(group_id, access)
  268. group_id = ensure_group_id_parameter(group_id)
  269. access = Array(access).map(&:to_sym) | [:full]
  270. # check direct access
  271. instances = Permission.join_with(self, 'ticket.agent').joins(group_through.name)
  272. .where(group_through.table_name => { group_id: group_id, access: access }, active: true)
  273. # check indirect access through roles if possible
  274. return instances if !respond_to?(:role_access)
  275. # combines and removes duplicates
  276. # and returns them in one statement
  277. instances | role_access(group_id, access)
  278. end
  279. # The reflection instance containing the association data
  280. #
  281. # @example
  282. # User.group_through
  283. # #=> <ActiveRecord::Reflection::HasManyReflection:0x007fd2f5785440 @name=:user_groups, ...>
  284. #
  285. # @return [ActiveRecord::Reflection::HasManyReflection] The given map
  286. def group_through
  287. @group_through ||= reflect_on_association(group_through_identifier)
  288. end
  289. # The identifier of the has_many :through relation
  290. #
  291. # @example
  292. # User.group_through_identifier
  293. # #=> :user_groups
  294. #
  295. # @return [Symbol] The relation identifier
  296. def group_through_identifier
  297. :"#{name.downcase}_groups"
  298. end
  299. def ensure_group_id_parameter(group_or_id)
  300. return group_or_id if group_or_id.is_a?(Integer)
  301. group_or_id.id
  302. end
  303. end
  304. end