Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Hyrax: Assigning Sipity::EntitySpecificResponsibiilty
module Bison
module Workflow
class AssignEntityWorkflowPermissions
def self.call(target:, **)
entity = PowerConverter.convert(target, to: :sipity_entity)
users = User.where(email: target.edit_users)
user_agents = find_or_create_user_agents(users)
group_agents = find_or_create_group_agents(target.edit_groups.select { |g| g =~ /curator$/ })
# Remove permission for agents that no longer have access.
remove_agents = existing_entity_level_agents(entity) - user_agents - group_agents
Sipity::EntitySpecificResponsibility.where(entity: entity, agent: remove_agents).destroy_all
# Assign "deposting" and "self_depositing" roles to users with edit access.
associate_workflow_role_at_entity_level(
agents: user_agents,
role: find_or_create_workflow_role('depositing', entity.workflow),
entity: entity
)
associate_workflow_role_at_entity_level(
agents: find_or_create_user_agents(users.select { |u| !u.student? }),
role: find_or_create_workflow_role('self_depositing', entity.workflow),
entity: entity
)
# Assign "curating" role to curator groups with edit access.
associate_workflow_role_at_entity_level(
agents: group_agents,
role: find_or_create_workflow_role('curating', entity.workflow),
entity: entity
)
end
private_class_method def self.associate_workflow_role_at_entity_level(agents:, role:, entity:)
agents.each do |agent|
Sipity::EntitySpecificResponsibility.find_or_create_by!(workflow_role: role, entity: entity, agent: agent)
end
end
private_class_method def self.existing_entity_level_agents(entity)
Sipity::EntitySpecificResponsibility.where(entity: entity).all.map(&:agent)
end
# Find or create a Sipity::WorkflowRole object.
# @param role A workflow role String.
# @param workflow A Sipity::Workflow object.
private_class_method def self.find_or_create_workflow_role(role, workflow)
# This should be thread-safe since all Sipity::WorkflowRole are created
# when the workflow is initialized.
Sipity::WorkflowRole.find_or_create_by!(
role: PowerConverter.convert_to_sipity_role(role),
workflow: workflow
)
end
# Find or create a Sipity::Agent object for a group/role.
# @param groups A list of Strings (group names)
private_class_method def self.find_or_create_group_agents(groups)
# This should probably be done in a thread safe way, but for now
# we just assume the Sipity::Agent for the group/role already exists.
groups.map { |g| PowerConverter.convert_to_sipity_agent(Hyrax::Group.new(g)) }
end
# Find or create a Sipity::Agent object for a user.
# @param users A list of ::User objects.
private_class_method def self.find_or_create_user_agents(users)
# Thread-safe way to find or create a Sipity::Agent for the users.
users.map(&:to_sipity_agent)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.