Class: Authoraise::Policy
- Inherits:
-
Object
- Object
- Authoraise::Policy
- Defined in:
- lib/authoraise.rb
Instance Method Summary collapse
- #allow(&procedure) ⇒ Object
- #authorize(options = {}) ⇒ Object
-
#initialize {|_self| ... } ⇒ Policy
constructor
A new instance of Policy.
Constructor Details
#initialize {|_self| ... } ⇒ Policy
Returns a new instance of Policy.
37 38 39 40 |
# File 'lib/authoraise.rb', line 37 def initialize @checks = [] yield(self) if block_given? end |
Instance Method Details
#allow(&procedure) ⇒ Object
42 43 44 45 |
# File 'lib/authoraise.rb', line 42 def allow(&procedure) @checks << Check.new(procedure.parameters.map(&:last), procedure) end |
#authorize(options = {}) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/authoraise.rb', line 47 def ( = {}) raise Error, 'Policy is empty' if @checks.empty? given_keys = .keys.to_set assert_all_keys_match(given_keys) if Authoraise.strict_mode missing_keys = Set.new @checks.each do |check| if check.required_keys.subset?(given_keys) return true if check.() else missing_keys += check.missing_keys(given_keys) end end if missing_keys.empty? return false else raise Error, "Inconclusive authorization, missing keys: #{missing_keys.to_a}" end end |