Class: CanI::Authorization
- Inherits:
-
Object
- Object
- CanI::Authorization
- Defined in:
- lib/can_i/authorization.rb
Instance Attribute Summary collapse
-
#auth_role ⇒ Object
readonly
This is the main class to interact with to determine if an action is permitted, or to define permissions.
Instance Method Summary collapse
- #can(action) ⇒ Object
- #can?(action) ⇒ Boolean
- #can_do_anything! ⇒ Object
- #cannot(action) ⇒ Object
-
#initialize(role = :authorization) ⇒ Authorization
constructor
A new instance of Authorization.
- #reset_with_role!(role) ⇒ Object
Constructor Details
#initialize(role = :authorization) ⇒ Authorization
Returns a new instance of Authorization.
31 32 33 |
# File 'lib/can_i/authorization.rb', line 31 def initialize(role=:authorization) reset_with_role! role end |
Instance Attribute Details
#auth_role ⇒ Object (readonly)
This is the main class to interact with to determine if an action is permitted, or to define permissions
Usage:
class AppDelegate
attr_accessor :authorization
def application(application, didFinishLaunchingWithOptions:launchOptions)
self. = CanI::Authorization.new :admin
true
end
end
class MyController < UIViewController
def viewDidLoad
if App.delegate..can? :get_drunk
App.alert "Let's get drunk!"
else
App.alert "I'm married"
end
end
end
29 30 31 |
# File 'lib/can_i/authorization.rb', line 29 def auth_role @auth_role end |
Instance Method Details
#can(action) ⇒ Object
35 36 37 |
# File 'lib/can_i/authorization.rb', line 35 def can(action) approved_actions << action.to_sym unless approved_actions.include?(action.to_sym) end |
#can?(action) ⇒ Boolean
39 40 41 |
# File 'lib/can_i/authorization.rb', line 39 def can?(action) can_do_anything? || approved_actions.include?(action.to_sym) end |
#can_do_anything! ⇒ Object
47 48 49 |
# File 'lib/can_i/authorization.rb', line 47 def can_do_anything! @can_do_anything = true end |
#cannot(action) ⇒ Object
43 44 45 |
# File 'lib/can_i/authorization.rb', line 43 def cannot(action) approved_actions.delete action.to_sym end |
#reset_with_role!(role) ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'lib/can_i/authorization.rb', line 51 def reset_with_role!(role) @can_do_anything = false @approved_actions = [] klass = role_for_symbol(role) @auth_role = klass.new klass.registration_blocks.each { |b| instance_eval &b } end |