Class: Micro::Authorization::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/micro/authorization/model.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, permissions:, policies: {}) ⇒ Model

Returns a new instance of Model.



18
19
20
21
22
23
24
25
# File 'lib/micro/authorization/model.rb', line 18

def initialize(context, permissions:, policies: {})
  @context = context
  @policies = {}
  @policies_cache = {}
  @permissions = Permissions[permissions]

  add_policies(policies)
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



6
7
8
# File 'lib/micro/authorization/model.rb', line 6

def context
  @context
end

#permissionsObject (readonly)

Returns the value of attribute permissions.



6
7
8
# File 'lib/micro/authorization/model.rb', line 6

def permissions
  @permissions
end

Class Method Details

.build(permissions:, context:, policies: {}) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/micro/authorization/model.rb', line 8

def self.build(permissions:, context:, policies: {})
  permissions_context =
    context.delete(:to_permit) || context.delete(:permissions)

  permissions_model =
    Permissions.new(permissions, context: permissions_context)

  self.new(context, permissions: permissions_model, policies: policies)
end

Instance Method Details

#add_policies(new_policies) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/micro/authorization/model.rb', line 51

def add_policies(new_policies)
  unless new_policies.is_a?(Hash)
    raise ArgumentError, "policies must be a Hash. e.g: `{policy_name: #{Policy.name}}`"
  end

  new_policies.each(&method(:add_policy))

  self
end

#add_policy(key, policy_klass) ⇒ Object

Raises:

  • (ArgumentError)


41
42
43
44
45
46
47
48
49
# File 'lib/micro/authorization/model.rb', line 41

def add_policy(key, policy_klass)
  raise ArgumentError, 'key must be a Symbol' unless key.is_a?(Symbol)

  default_ref = key == :default && policy_klass.is_a?(Symbol)

  @policies[key] ||= default_ref ? policy_klass : Policy.type(policy_klass)

  self
end

#map(context: nil, policies: nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/micro/authorization/model.rb', line 27

def map(context: nil, policies: nil)
  if context.nil? && policies.nil?
    raise ArgumentError, 'context or policies keywords args must be defined'
  end

  permissions_context = context || permissions.context

  new_permissions =
    Permissions.new(permissions.role, context: permissions_context)

  self.class.new(context, permissions: new_permissions,
                          policies: policies || @policies)
end

#policy(key = :default, subject: nil) ⇒ Object



73
74
75
# File 'lib/micro/authorization/model.rb', line 73

def policy(key = :default, subject: nil)
  to(key, subject: subject)
end

#to(policy_key, subject: nil) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/micro/authorization/model.rb', line 61

def to(policy_key, subject: nil)
  policy_klass = fetch_policy(policy_key)

  return policy_klass.new(context, subject, permissions: permissions) if subject

  return @policies_cache[policy_key] if @policies_cache[policy_key]

  policy_klass.new(context, permissions: permissions).tap do |instance|
    @policies_cache[policy_key] = instance if policy_klass != Policy
  end
end