Module: Sipity::Queries::PermissionQueries

Defined in:
app/repositories/sipity/queries/permission_queries.rb

Overview

Queries

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.emails_for_associated_users(roles:, entity:) ⇒ Object


7
8
9
# File 'app/repositories/sipity/queries/permission_queries.rb', line 7

def emails_for_associated_users(roles:, entity:)
  scope_users_by_entity_and_roles(roles: roles, entity: entity).pluck(:email)
end

.scope_entities_for_user_and_roles(entity_type:, user:, roles:) ⇒ Object

Note:

Welcome to the land of AREL.

Responsible for returning a scope for the entity type: That match the given :entity_type And for which the given :user has at least one of the given :roles.

If no roles are given, no entities will be returned.


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/repositories/sipity/queries/permission_queries.rb', line 71

def scope_entities_for_user_and_roles(entity_type:, user:, roles:)
  perm_table = Models::Permission.arel_table
  memb_table = Models::GroupMembership.arel_table
  group_class =  Models::Group
  actor_id = user.to_param

  subquery_entity_relation_to_user = perm_table.project(perm_table[:entity_id]).where(
    perm_table[:actor_id].eq(actor_id).
    and(perm_table[:actor_type].eq(user.class.base_class)).
    and(perm_table[:role].in(roles)).
    and(perm_table[:entity_type].eq(entity_type.base_class))
  )

  subquery_entity_relation_to_user_by_group_membership = perm_table.project(perm_table[:entity_id]).where(
    perm_table[:role].in(roles).
    and(perm_table[:entity_type].eq(entity_type.base_class)).
    and(perm_table[:actor_type].eq(group_class.base_class)).
    and(perm_table[:actor_id].in(memb_table.project(memb_table[:group_id]).where(memb_table[:user_id].eq(actor_id))))
  )

  entity_type.where(
    entity_type.arel_table[:id].in(subquery_entity_relation_to_user).
    or(entity_type.arel_table[:id].in(subquery_entity_relation_to_user_by_group_membership))
  )
end

.scope_users_by_entity_and_roles(roles:, entity:, user_class: User, group_class: Models::Group) ⇒ Object

Note:

Welcome to the land of AREL.

Responsible for returning a User scope: That will include all users That have one or more roles In which the user has a direction relation to the entity Or in which a relation to the entity can be inferred by group membership

If no roles are given, no entities will be returned.


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/repositories/sipity/queries/permission_queries.rb', line 27

def scope_users_by_entity_and_roles(roles:, entity:, user_class: User, group_class: Models::Group)
  user_table = user_class.arel_table
  perm_table = Models::Permission.arel_table
  memb_table = Models::GroupMembership.arel_table
  entity_id = entity.to_param

  subqyery_user_relation_entity = perm_table.project(perm_table[:actor_id]).where(
    perm_table[:actor_type].eq(user_class.base_class).
    and(perm_table[:role].in(roles)).
    and(perm_table[:entity_type].eq(entity.class.base_class)).
    and(perm_table[:entity_id].eq(entity_id))
  )

  subquery_user_relation_to_entity_by_group_membership = memb_table.project(memb_table[:user_id]).where(
    memb_table[:user_id].in(
      perm_table.project(perm_table[:actor_id]).where(
        perm_table[:actor_type].eq(group_class.base_class).
        and(perm_table[:role].in(roles)).
        and(perm_table[:entity_type].eq(entity.class.base_class)).
        and(perm_table[:entity_id].eq(entity_id))
      )
    )
  )
  user_class.where(
    user_table[:id].in(subquery_user_relation_to_entity_by_group_membership).
    or(user_table[:id].in(subqyery_user_relation_entity))
  )
end

Instance Method Details

#emails_for_associated_users(roles:, entity:) ⇒ Object (private)


7
8
9
# File 'app/repositories/sipity/queries/permission_queries.rb', line 7

def emails_for_associated_users(roles:, entity:)
  scope_users_by_entity_and_roles(roles: roles, entity: entity).pluck(:email)
end

#scope_entities_for_user_and_roles(entity_type:, user:, roles:) ⇒ Object (private)

Note:

Welcome to the land of AREL.

Responsible for returning a scope for the entity type: That match the given :entity_type And for which the given :user has at least one of the given :roles.

If no roles are given, no entities will be returned.


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/repositories/sipity/queries/permission_queries.rb', line 71

def scope_entities_for_user_and_roles(entity_type:, user:, roles:)
  perm_table = Models::Permission.arel_table
  memb_table = Models::GroupMembership.arel_table
  group_class =  Models::Group
  actor_id = user.to_param

  subquery_entity_relation_to_user = perm_table.project(perm_table[:entity_id]).where(
    perm_table[:actor_id].eq(actor_id).
    and(perm_table[:actor_type].eq(user.class.base_class)).
    and(perm_table[:role].in(roles)).
    and(perm_table[:entity_type].eq(entity_type.base_class))
  )

  subquery_entity_relation_to_user_by_group_membership = perm_table.project(perm_table[:entity_id]).where(
    perm_table[:role].in(roles).
    and(perm_table[:entity_type].eq(entity_type.base_class)).
    and(perm_table[:actor_type].eq(group_class.base_class)).
    and(perm_table[:actor_id].in(memb_table.project(memb_table[:group_id]).where(memb_table[:user_id].eq(actor_id))))
  )

  entity_type.where(
    entity_type.arel_table[:id].in(subquery_entity_relation_to_user).
    or(entity_type.arel_table[:id].in(subquery_entity_relation_to_user_by_group_membership))
  )
end

#scope_users_by_entity_and_roles(roles:, entity:, user_class: User, group_class: Models::Group) ⇒ Object (private)

Note:

Welcome to the land of AREL.

Responsible for returning a User scope: That will include all users That have one or more roles In which the user has a direction relation to the entity Or in which a relation to the entity can be inferred by group membership

If no roles are given, no entities will be returned.


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/repositories/sipity/queries/permission_queries.rb', line 27

def scope_users_by_entity_and_roles(roles:, entity:, user_class: User, group_class: Models::Group)
  user_table = user_class.arel_table
  perm_table = Models::Permission.arel_table
  memb_table = Models::GroupMembership.arel_table
  entity_id = entity.to_param

  subqyery_user_relation_entity = perm_table.project(perm_table[:actor_id]).where(
    perm_table[:actor_type].eq(user_class.base_class).
    and(perm_table[:role].in(roles)).
    and(perm_table[:entity_type].eq(entity.class.base_class)).
    and(perm_table[:entity_id].eq(entity_id))
  )

  subquery_user_relation_to_entity_by_group_membership = memb_table.project(memb_table[:user_id]).where(
    memb_table[:user_id].in(
      perm_table.project(perm_table[:actor_id]).where(
        perm_table[:actor_type].eq(group_class.base_class).
        and(perm_table[:role].in(roles)).
        and(perm_table[:entity_type].eq(entity.class.base_class)).
        and(perm_table[:entity_id].eq(entity_id))
      )
    )
  )
  user_class.where(
    user_table[:id].in(subquery_user_relation_to_entity_by_group_membership).
    or(user_table[:id].in(subqyery_user_relation_entity))
  )
end