Class: CanI::Authorization

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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_roleObject (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.authorization = CanI::Authorization.new :admin
    true
  end
end

class MyController < UIViewController
  def viewDidLoad
    if App.delegate.authorization.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

Returns:

  • (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