Module: Lawkeeper::Helpers
- Defined in:
- lib/lawkeeper.rb
Instance Method Summary collapse
- #authorize(model, action, policy_class = nil) ⇒ Object
- #can?(action, model, policy_class = nil) ⇒ Boolean
- #policy_scope(scope) ⇒ Object
- #set_lawkeeper_header(header) ⇒ Object
- #skip_authorization ⇒ Object
Instance Method Details
#authorize(model, action, policy_class = nil) ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/lawkeeper.rb', line 64 def (model, action, policy_class = nil) if can?(action, model, policy_class) set_lawkeeper_header(AUTHORIZED_HEADER) else raise NotAuthorized end end |
#can?(action, model, policy_class = nil) ⇒ Boolean
58 59 60 61 62 |
# File 'lib/lawkeeper.rb', line 58 def can?(action, model, policy_class = nil) policy_class ||= Lawkeeper::PolicyLookup[model] policy_method = "#{action}?" policy_class.new(current_user, model).public_send(policy_method) end |
#policy_scope(scope) ⇒ Object
72 73 74 75 76 |
# File 'lib/lawkeeper.rb', line 72 def policy_scope(scope) set_lawkeeper_header(AUTHORIZED_HEADER) klass = Lawkeeper::PolicyLookup.for_scope(scope)::Scope klass.new(current_user, scope).resolve end |
#set_lawkeeper_header(header) ⇒ Object
82 83 84 |
# File 'lib/lawkeeper.rb', line 82 def set_lawkeeper_header(header) headers[header] = 'true' unless Lawkeeper.skip_set_headers end |
#skip_authorization ⇒ Object
78 79 80 |
# File 'lib/lawkeeper.rb', line 78 def set_lawkeeper_header(SKIPPED_HEADER) end |