Class: Yes::Auth::Principals::User
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Yes::Auth::Principals::User
- Defined in:
- lib/yes/auth/principals/user.rb
Overview
Represents an authorization principal user with roles and resource accesses.
Constant Summary collapse
- NO_AUTHORIZATION_ROLES_YET =
['no-roles-yet'].freeze
Instance Method Summary collapse
-
#read_resource_access_authorization_roles ⇒ Array<String>
NOTE: Runs 2 queries (resource access roles + direct roles).
-
#super_admin? ⇒ Boolean
Whether the user has the super admin role.
-
#write_resource_access_authorization_roles ⇒ Array<String>
NOTE: Runs 2 queries (resource access roles + direct roles).
Instance Method Details
#read_resource_access_authorization_roles ⇒ Array<String>
NOTE: Runs 2 queries (resource access roles + direct roles). The direct roles query is shared with write_resource_access_authorization_roles but cannot be easily combined since they query different join tables. Use .includes(:roles) when loading the User to avoid N+1 on the direct roles association.
31 32 33 34 35 36 37 |
# File 'lib/yes/auth/principals/user.rb', line 31 def read_role_names = Set.new( Role.joins(:read_resource_accesses).where(read_resource_accesses: { principal_id: id }).complete.pluck(:name) ) (read_role_names + complete_role_names).to_a end |
#super_admin? ⇒ Boolean
Returns whether the user has the super admin role.
53 54 55 56 57 58 |
# File 'lib/yes/auth/principals/user.rb', line 53 def super_admin? super_admin_role_id = Role.super_admin_role&.id return false unless super_admin_role_id roles.ids.include?(super_admin_role_id) end |
#write_resource_access_authorization_roles ⇒ Array<String>
NOTE: Runs 2 queries (resource access roles + direct roles). The direct roles query is shared with read_resource_access_authorization_roles but cannot be easily combined since they query different join tables. Use .includes(:roles) when loading the User to avoid N+1 on the direct roles association.
44 45 46 47 48 49 50 |
# File 'lib/yes/auth/principals/user.rb', line 44 def write_role_names = Set.new( Role.joins(:write_resource_accesses).where(write_resource_accesses: { principal_id: id }).complete.pluck(:name) ) (write_role_names + complete_role_names).to_a end |