has_groups.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://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.include?('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 = self.class.ensure_group_access_list_parameter(access)
  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 = self.class.ensure_group_access_list_parameter(access)
  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).pluck(:id).first
  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. cache_delete
  216. push_ticket_create_screen_background_job
  217. end
  218. def process_group_access_buffer
  219. flush_group_access_buffer do
  220. destroy_group_relations
  221. break if group_access_buffer.blank?
  222. foreign_key = group_through.foreign_key
  223. entries = group_access_buffer.collect do |entry|
  224. entry[foreign_key] = id
  225. entry
  226. end
  227. group_through.klass.create!(entries)
  228. end
  229. true
  230. end
  231. def destroy_group_relations
  232. group_through.klass.where(group_through.foreign_key => id).destroy_all
  233. end
  234. # methods defined here are going to extend the class, not the instance of it
  235. class_methods do
  236. # Lists IDs of instances having the given access(es) to the given Group.
  237. #
  238. # @example Group ID param
  239. # User.group_access_ids(1, 'read')
  240. # #=> [1, 3, ...]
  241. #
  242. # @example Group param
  243. # User.group_access_ids(group, 'read')
  244. # #=> [1, 3, ...]
  245. #
  246. # @example Access list
  247. # User.group_access_ids(group, ['read', 'create'])
  248. # #=> [1, 3, ...]
  249. #
  250. # @return [Array<Integer>]
  251. def group_access_ids(group_id, access)
  252. group_access(group_id, access).collect(&:id)
  253. end
  254. # Lists instances having the given access(es) to the given Group.
  255. #
  256. # @example Group ID param
  257. # User.group_access(1, 'read')
  258. # #=> [#<User id: 1, ...>, ...]
  259. #
  260. # @example Group param
  261. # User.group_access(group, 'read')
  262. # #=> [#<User id: 1, ...>, ...]
  263. #
  264. # @example Access list
  265. # User.group_access(group, ['read', 'create'])
  266. # #=> [#<User id: 1, ...>, ...]
  267. #
  268. # @return [Array<Class>]
  269. def group_access(group_id, access)
  270. group_id = ensure_group_id_parameter(group_id)
  271. access = ensure_group_access_list_parameter(access)
  272. # check direct access
  273. instances = joins(group_through.name)
  274. .where( group_through.table_name => { group_id: group_id, access: access }, active: true )
  275. if method_defined?(:permissions?)
  276. permissions = Permission.with_parents('ticket.agent')
  277. instances = instances
  278. .joins(roles: :permissions)
  279. .where(roles: { active: true }, permissions: { name: permissions, active: true })
  280. end
  281. # check indirect access through roles if possible
  282. return instances if !respond_to?(:role_access)
  283. # combines and removes duplicates
  284. # and returns them in one statement
  285. instances | role_access(group_id, access)
  286. end
  287. # The reflection instance containing the association data
  288. #
  289. # @example
  290. # User.group_through
  291. # #=> <ActiveRecord::Reflection::HasManyReflection:0x007fd2f5785440 @name=:user_groups, ...>
  292. #
  293. # @return [ActiveRecord::Reflection::HasManyReflection] The given map
  294. def group_through
  295. @group_through ||= reflect_on_association(group_through_identifier)
  296. end
  297. # The identifier of the has_many :through relation
  298. #
  299. # @example
  300. # User.group_through_identifier
  301. # #=> :user_groups
  302. #
  303. # @return [Symbol] The relation identifier
  304. def group_through_identifier
  305. "#{name.downcase}_groups".to_sym
  306. end
  307. def ensure_group_id_parameter(group_or_id)
  308. return group_or_id if group_or_id.is_a?(Integer)
  309. group_or_id.id
  310. end
  311. def ensure_group_access_list_parameter(access)
  312. access = [access] if access.is_a?(String)
  313. access.push('full') if !access.include?('full')
  314. access
  315. end
  316. end
  317. end