Module: Sipity::Commands::PermissionCommands

Defined in:
app/repositories/sipity/commands/permission_commands.rb

Overview

Commands related to the permission model

TODO: Need to come up with a better way of handling this. Exposing module functions and instance methods is a bit insane. It works, but increases coupling. Possible solution, module provides a means for delegation to a proper class? The design goal is to provide a way for accessing repository services in other contexts. So the question is: How is that different from module functions and instance methods via mixin? Something to think about.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.grant_creating_user_permission_for!(entity:, user: nil, group: nil, actor: nil) ⇒ Object



34
35
36
37
38
39
40
# File 'app/repositories/sipity/commands/permission_commands.rb', line 34

def grant_creating_user_permission_for!(entity:, user: nil, group: nil, actor: nil)
  # REVIEW: Does the constant even make sense on the data structure? Or
  #   is it more relevant here?
  role = Models::Permission::CREATING_USER
  actors = [user, group, actor]
  grant_permission_for!(entity: entity, actors: actors, role: role)
end

.grant_groups_permission_to_entity_for_role!(entity:, roles:) ⇒ Object

Responsible for finding the groups that are assigned the given role for the given entity's sip type.

Raises:

  • Exception if for any of the given roles, no group could be found



20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/repositories/sipity/commands/permission_commands.rb', line 20

def grant_groups_permission_to_entity_for_role!(entity:, roles:)
  # TODO: Extract this map of roles to groups; Will we need roles by
  #   sip type?
  map = { 'etd_reviewer' => 'graduate_school', 'cataloger' => 'library_cataloging' }
  Array.wrap(roles).each do |role|
    group_names = map.fetch(role.to_s)
    Array.wrap(group_names).each do |group_name|
      group = Models::Group.find_or_create_by!(name: group_name)
      grant_permission_for!(entity: entity, role: role, actors: group)
    end
  end
end

.grant_permission_for!(entity:, actors:, role:) ⇒ Object (private)



43
44
45
# File 'app/repositories/sipity/commands/permission_commands.rb', line 43

def grant_permission_for!(entity:, actors:, role:)
  Array.wrap(actors).flatten.compact.each { |an_actor| Models::Permission.create!(entity: entity, actor: an_actor, role: role) }
end

Instance Method Details

#grant_creating_user_permission_for!(entity:, user: nil, group: nil, actor: nil) ⇒ Object (private)



34
35
36
37
38
39
40
# File 'app/repositories/sipity/commands/permission_commands.rb', line 34

def grant_creating_user_permission_for!(entity:, user: nil, group: nil, actor: nil)
  # REVIEW: Does the constant even make sense on the data structure? Or
  #   is it more relevant here?
  role = Models::Permission::CREATING_USER
  actors = [user, group, actor]
  grant_permission_for!(entity: entity, actors: actors, role: role)
end

#grant_groups_permission_to_entity_for_role!(entity:, roles:) ⇒ Object (private)

Responsible for finding the groups that are assigned the given role for the given entity's sip type.

Raises:

  • Exception if for any of the given roles, no group could be found



20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/repositories/sipity/commands/permission_commands.rb', line 20

def grant_groups_permission_to_entity_for_role!(entity:, roles:)
  # TODO: Extract this map of roles to groups; Will we need roles by
  #   sip type?
  map = { 'etd_reviewer' => 'graduate_school', 'cataloger' => 'library_cataloging' }
  Array.wrap(roles).each do |role|
    group_names = map.fetch(role.to_s)
    Array.wrap(group_names).each do |group_name|
      group = Models::Group.find_or_create_by!(name: group_name)
      grant_permission_for!(entity: entity, role: role, actors: group)
    end
  end
end

#grant_permission_for!(entity:, actors:, role:) ⇒ Object (private)



43
44
45
# File 'app/repositories/sipity/commands/permission_commands.rb', line 43

def grant_permission_for!(entity:, actors:, role:)
  Array.wrap(actors).flatten.compact.each { |an_actor| Models::Permission.create!(entity: entity, actor: an_actor, role: role) }
end