Class: Machina::Authorized
- Inherits:
-
Object
- Object
- Machina::Authorized
- Defined in:
- lib/machina/authorized.rb
Overview
Immutable value object representing the currently authenticated user, their organization/workspace context, and granted permissions.
Instance Attribute Summary collapse
-
#avatar_url ⇒ Object
readonly
Returns the value of attribute avatar_url.
-
#expires_at ⇒ Object
readonly
Returns the value of attribute expires_at.
-
#integration_name ⇒ Object
readonly
Returns the value of attribute integration_name.
-
#org_name ⇒ Object
readonly
Returns the value of attribute org_name.
-
#org_personal ⇒ Object
readonly
Returns the value of attribute org_personal.
-
#org_role ⇒ Object
readonly
Returns the value of attribute org_role.
-
#org_slug ⇒ Object
readonly
Returns the value of attribute org_slug.
-
#organization_id ⇒ Object
readonly
Returns the value of attribute organization_id.
-
#session_id ⇒ Object
readonly
Returns the value of attribute session_id.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#user_email ⇒ Object
readonly
Returns the value of attribute user_email.
-
#user_id ⇒ Object
readonly
Returns the value of attribute user_id.
-
#user_name ⇒ Object
readonly
Returns the value of attribute user_name.
-
#workspace_id ⇒ Object
(also: #tenant_ref)
readonly
Returns the value of attribute workspace_id.
-
#workspace_name ⇒ Object
readonly
Returns the value of attribute workspace_name.
-
#workspace_slug ⇒ Object
readonly
Returns the value of attribute workspace_slug.
Instance Method Summary collapse
- #authorize!(key) ⇒ Object
- #authorize_all!(*keys) ⇒ Object
- #authorize_any!(*keys) ⇒ Object
- #can?(key) ⇒ Boolean
- #can_all?(*keys) ⇒ Boolean
- #can_any?(*keys) ⇒ Boolean
- #cannot?(key) ⇒ Boolean
-
#initialize(data = {}) ⇒ Authorized
constructor
A new instance of Authorized.
- #permissions ⇒ Object
Constructor Details
#initialize(data = {}) ⇒ Authorized
Returns a new instance of Authorized.
12 13 14 15 16 17 18 19 |
# File 'lib/machina/authorized.rb', line 12 def initialize(data = {}) assign_user_attrs(data) assign_org_attrs(data) assign_workspace_attrs(data) assign_session_attrs(data) @permissions = Set.new(data['permissions'] || []) freeze end |
Instance Attribute Details
#avatar_url ⇒ Object (readonly)
Returns the value of attribute avatar_url.
7 8 9 |
# File 'lib/machina/authorized.rb', line 7 def avatar_url @avatar_url end |
#expires_at ⇒ Object (readonly)
Returns the value of attribute expires_at.
7 8 9 |
# File 'lib/machina/authorized.rb', line 7 def expires_at @expires_at end |
#integration_name ⇒ Object (readonly)
Returns the value of attribute integration_name.
7 8 9 |
# File 'lib/machina/authorized.rb', line 7 def integration_name @integration_name end |
#org_name ⇒ Object (readonly)
Returns the value of attribute org_name.
7 8 9 |
# File 'lib/machina/authorized.rb', line 7 def org_name @org_name end |
#org_personal ⇒ Object (readonly)
Returns the value of attribute org_personal.
7 8 9 |
# File 'lib/machina/authorized.rb', line 7 def org_personal @org_personal end |
#org_role ⇒ Object (readonly)
Returns the value of attribute org_role.
7 8 9 |
# File 'lib/machina/authorized.rb', line 7 def org_role @org_role end |
#org_slug ⇒ Object (readonly)
Returns the value of attribute org_slug.
7 8 9 |
# File 'lib/machina/authorized.rb', line 7 def org_slug @org_slug end |
#organization_id ⇒ Object (readonly)
Returns the value of attribute organization_id.
7 8 9 |
# File 'lib/machina/authorized.rb', line 7 def organization_id @organization_id end |
#session_id ⇒ Object (readonly)
Returns the value of attribute session_id.
7 8 9 |
# File 'lib/machina/authorized.rb', line 7 def session_id @session_id end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
7 8 9 |
# File 'lib/machina/authorized.rb', line 7 def type @type end |
#user_email ⇒ Object (readonly)
Returns the value of attribute user_email.
7 8 9 |
# File 'lib/machina/authorized.rb', line 7 def user_email @user_email end |
#user_id ⇒ Object (readonly)
Returns the value of attribute user_id.
7 8 9 |
# File 'lib/machina/authorized.rb', line 7 def user_id @user_id end |
#user_name ⇒ Object (readonly)
Returns the value of attribute user_name.
7 8 9 |
# File 'lib/machina/authorized.rb', line 7 def user_name @user_name end |
#workspace_id ⇒ Object (readonly) Also known as: tenant_ref
Returns the value of attribute workspace_id.
7 8 9 |
# File 'lib/machina/authorized.rb', line 7 def workspace_id @workspace_id end |
#workspace_name ⇒ Object (readonly)
Returns the value of attribute workspace_name.
7 8 9 |
# File 'lib/machina/authorized.rb', line 7 def workspace_name @workspace_name end |
#workspace_slug ⇒ Object (readonly)
Returns the value of attribute workspace_slug.
7 8 9 |
# File 'lib/machina/authorized.rb', line 7 def workspace_slug @workspace_slug end |
Instance Method Details
#authorize!(key) ⇒ Object
37 38 39 |
# File 'lib/machina/authorized.rb', line 37 def (key) raise Machina::Unauthorized, key unless can?(key) end |
#authorize_all!(*keys) ⇒ Object
45 46 47 |
# File 'lib/machina/authorized.rb', line 45 def (*keys) keys.each { |key| (key) } end |
#authorize_any!(*keys) ⇒ Object
41 42 43 |
# File 'lib/machina/authorized.rb', line 41 def (*keys) raise Machina::Unauthorized, keys.join(', ') unless can_any?(*keys) end |
#can?(key) ⇒ Boolean
21 22 23 |
# File 'lib/machina/authorized.rb', line 21 def can?(key) @permissions.include?(key) end |
#can_all?(*keys) ⇒ Boolean
29 30 31 |
# File 'lib/machina/authorized.rb', line 29 def can_all?(*keys) keys.all? { |key| @permissions.include?(key) } end |
#can_any?(*keys) ⇒ Boolean
25 26 27 |
# File 'lib/machina/authorized.rb', line 25 def can_any?(*keys) keys.any? { |key| @permissions.include?(key) } end |
#cannot?(key) ⇒ Boolean
33 34 35 |
# File 'lib/machina/authorized.rb', line 33 def cannot?(key) !can?(key) end |
#permissions ⇒ Object
52 53 54 |
# File 'lib/machina/authorized.rb', line 52 def @permissions.to_a.freeze end |