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
34
35
36
37
38
39
40
# File 'lib/can_i/authorization.rb', line 31

def initialize(role=:authorization)

  @can_do_anything = false
  klass = CanI.const_defined?("#{role}_role".camelize) ? CanI.const_get("#{role}_role".camelize) : Kernel.const_get("#{role}_role".camelize)

  @auth_role = klass.new
  klass.registration_blocks.each do |more_roles|
    instance_eval &more_roles
  end
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



42
43
44
# File 'lib/can_i/authorization.rb', line 42

def can(action)
  approved_actions << action.to_sym unless approved_actions.include?(action.to_sym)
end

#can?(action) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/can_i/authorization.rb', line 46

def can?(action)
  can_do_anything? || approved_actions.include?(action.to_sym)
end

#can_do_anything!Object



54
55
56
# File 'lib/can_i/authorization.rb', line 54

def can_do_anything!
  @can_do_anything = true
end

#cannot(action) ⇒ Object



50
51
52
# File 'lib/can_i/authorization.rb', line 50

def cannot(action)
  approved_actions.delete action.to_sym
end