Module: Platanus::Canned2::Controller::ClassMethods

Defined in:
lib/platanus/canned2.rb

Instance Method Summary collapse

Instance Method Details

#before_auth(_callback = nil, &pblock) ⇒ Object

Specifies a block or method to be called before tests are ran.

IMPORTANT Resources loaded here are avaliable to tests.



85
86
87
# File 'lib/platanus/canned2.rb', line 85

def before_auth(_callback=nil, &pblock)
  self.brk_before = (_callback || pblock)
end

#canned_setup(_def, _provider = nil, &_block) ⇒ Object

Setups the controller user profile definitions and profile provider block (or proc)

The passed method or block must return a list of profiles to be validated by the definition.

Parameters:

  • _def (Definition)

    Profile definitions

  • _provider (Symbol) (defaults to: nil)

    Profile provider method name

  • _block (Block)

    Profile provider block



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/platanus/canned2.rb', line 38

def canned_setup(_def, _provider=nil, &_block)
  self.before_filter do

    # no auth if action is excluded
    next if self.class.brk_excluded == :all
    next if !self.class.brk_excluded.nil? and self.class.brk_excluded.include? params[:action].to_sym

    # call initializer block
    profiles = if _provider.nil? then self.instance_eval(&_block) else self.send(_provider) end
    raise AuthError if profiles.nil?
    profiles = [profiles] unless profiles.is_a? Array

    # call resource loader
    brk_before = self.class.brk_before
    unless brk_before.nil?
      if brk_before.is_a? Symbol; self.send(brk_before)
      else self.instance_eval &(brk_before) end
    end

    # execute authentication
    # TODO: Add forbidden begin - rescue
    result = profiles.collect do |profile|
      _def.can?(self, profile, params[:controller]) or
        _def.can?(self, profile, params[:controller] + '#' + params[:action])
    end
    raise AuthError unless result.any?
  end
end

#uncan_allObject

Removes protection for all controller actions.



68
69
70
# File 'lib/platanus/canned2.rb', line 68

def uncan_all()
  self.brk_excluded = :all
end

#uncanned(*_excluded) ⇒ Object

Removes protection for the especified controller actions.

Parameters:

  • _excluded (splat)

    List of actions to be excluded.



76
77
78
79
# File 'lib/platanus/canned2.rb', line 76

def uncanned(*_excluded)
  self.brk_excluded ||= []
  self.brk_excluded.push(*_excluded)
end