Module: Rolypoly::ControllerRoleDSL

Defined in:
lib/rolypoly/controller_role_dsl.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(sub) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rolypoly/controller_role_dsl.rb', line 5

def self.included(sub)
  sub.before_filter(:rolypoly_check_role_access!) if sub.respond_to? :before_filter
  if sub.respond_to? :rescue_from
    sub.rescue_from(FailedRoleCheckError) do
      respond_to do |f|
        f.html { render text: "Not Authorized", status: 401 }
        f.json { render json: { error: "Not Authorized" }, status: 401 }
        f.xml { render xml: { error: "Not Authorized" }, status: 401 }
      end
    end
  end

  unless sub.method_defined? :current_roles
    define_method(:current_roles) { [] }
  end
  sub.send :extend, ClassMethods
  sub.class_eval do # Sometimes get Stack Too Deep errors if in ClassMethods
    define_singleton_method :inherited do |sub|
      super sub
      sub.instance_variable_set(:@rolypoly_gatekeepers, rolypoly_gatekeepers.map(&:clone))
    end
  end
end

Instance Method Details

#failed_role_check!Object



33
34
35
# File 'lib/rolypoly/controller_role_dsl.rb', line 33

def failed_role_check!
  raise Rolypoly::FailedRoleCheckError
end

#rolypoly_check_role_access!Object



29
30
31
# File 'lib/rolypoly/controller_role_dsl.rb', line 29

def rolypoly_check_role_access!
  failed_role_check! unless rolypoly_role_access?
end