Class: Micro::Authorization::Model
- Inherits:
-
Object
- Object
- Micro::Authorization::Model
- Defined in:
- lib/micro/authorization/model.rb
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#permissions ⇒ Object
readonly
Returns the value of attribute permissions.
Class Method Summary collapse
Instance Method Summary collapse
- #add_policies(new_policies) ⇒ Object
- #add_policy(key, policy_klass) ⇒ Object
-
#initialize(context, permissions:, policies: {}) ⇒ Model
constructor
A new instance of Model.
- #map(context: nil, policies: nil) ⇒ Object
- #policy(key = :default, subject: nil) ⇒ Object
- #to(policy_key, subject: nil) ⇒ Object
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[] add_policies(policies) end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
6 7 8 |
# File 'lib/micro/authorization/model.rb', line 6 def context @context end |
#permissions ⇒ Object (readonly)
Returns the value of attribute permissions.
6 7 8 |
# File 'lib/micro/authorization/model.rb', line 6 def @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: {}) = context.delete(:to_permit) || context.delete(:permissions) = Permissions.new(, context: ) self.new(context, permissions: , 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
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 = context || .context = Permissions.new(.role, context: ) self.class.new(context, 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: ) if subject return @policies_cache[policy_key] if @policies_cache[policy_key] policy_klass.new(context, permissions: ).tap do |instance| @policies_cache[policy_key] = instance if policy_klass != Policy end end |