Class: Machina::Authorized

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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_urlObject (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_atObject (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_nameObject (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_nameObject (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_personalObject (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_roleObject (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_slugObject (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_idObject (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_idObject (readonly)

Returns the value of attribute session_id.



7
8
9
# File 'lib/machina/authorized.rb', line 7

def session_id
  @session_id
end

#typeObject (readonly)

Returns the value of attribute type.



7
8
9
# File 'lib/machina/authorized.rb', line 7

def type
  @type
end

#user_emailObject (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_idObject (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_nameObject (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_idObject (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_nameObject (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_slugObject (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 authorize!(key)
  raise Machina::Unauthorized, key unless can?(key)
end

#authorize_all!(*keys) ⇒ Object



45
46
47
# File 'lib/machina/authorized.rb', line 45

def authorize_all!(*keys)
  keys.each { |key| authorize!(key) }
end

#authorize_any!(*keys) ⇒ Object



41
42
43
# File 'lib/machina/authorized.rb', line 41

def authorize_any!(*keys)
  raise Machina::Unauthorized, keys.join(', ') unless can_any?(*keys)
end

#can?(key) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/machina/authorized.rb', line 21

def can?(key)
  @permissions.include?(key)
end

#can_all?(*keys) ⇒ Boolean

Returns:

  • (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

Returns:

  • (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

Returns:

  • (Boolean)


33
34
35
# File 'lib/machina/authorized.rb', line 33

def cannot?(key)
  !can?(key)
end

#permissionsObject



52
53
54
# File 'lib/machina/authorized.rb', line 52

def permissions
  @permissions.to_a.freeze
end