app/services/hyrax/collections/permissions_create_service.rb in hyrax-2.1.0.rc2 vs app/services/hyrax/collections/permissions_create_service.rb in hyrax-2.1.0.rc3
- old
+ new
@@ -15,9 +15,32 @@
PermissionTemplate.create!(source_id: collection.id,
access_grants_attributes: access_grants.uniq)
collection.reset_access_controls!
end
+ # @api public
+ #
+ # Add access grants to a collection
+ #
+ # @param collection_id [String] id of a collection
+ # @param grants [Array<Hash>] array of grants to add to the collection
+ # @example grants
+ # [ { agent_type: Hyrax::PermissionTemplateAccess::GROUP,
+ # agent_id: 'my_group_name',
+ # access: Hyrax::PermissionTemplateAccess::DEPOSIT } ]
+ # @see Hyrax::PermissionTemplateAccess for valid values for agent_type and access
+ def self.add_access(collection_id:, grants:)
+ collection = Collection.find(collection_id)
+ template = Hyrax::PermissionTemplate.find_by!(source_id: collection_id)
+ grants.each do |grant|
+ Hyrax::PermissionTemplateAccess.find_or_create_by(permission_template_id: template.id,
+ agent_type: grant[:agent_type],
+ agent_id: grant[:agent_id],
+ access: grant[:access])
+ end
+ collection.reset_access_controls!
+ end
+
# @api private
#
# Gather the default permissions needed for a new collection
#
# @param collection_type [CollectionType] the collection type of the new collection