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
# File 'lib/can_i/authorization.rb', line 31

def initialize(role=:authorization)

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

  @auth_role = klass.new
  auth_role.class.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



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

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

#can?(action) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#cannot(action) ⇒ Object



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

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