Module: Allows

Defined in:
lib/allows.rb,
lib/exceptions.rb

Defined Under Namespace

Modules: ClassMethods Classes: NoDefinedPermissions, NoInstanceVariable, NoPermissionChecker, NoPermissionDefinedForAction, Unauthorized

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/allows.rb', line 6

def self.included(base)
  base.extend(ClassMethods)

  base.send :before_filter, :set_permissions
  base.send :before_filter, :check_permissions
  
  class << base
    attr_accessor :permissions
    cattr_accessor :permission_checker
  end

  # set default permission_checker
  base.permission_checker = :current_user
end

Instance Method Details

#check_permissionsObject

Raises:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/allows.rb', line 23

def check_permissions
  im_allowed = false

  klass = self.class
  if klass.permission_checker && respond_to?(klass.permission_checker)
    if klass.permissions
      permissions_for_action = klass.permissions[action_name.to_sym]
      if permissions_for_action
        permissions_for_action.each do |permission|
          if assert_permission(permission)
            im_allowed = true
          end
        end
      else
        # Can execute the action. No "allow" rule affects this action.
        im_allowed = true
      end
    else
      # Can execute any action. Controller does not declare any "allow" rule
      im_allowed = true
    end
  else
    raise NoPermissionChecker # Permission checker isnt defined, so no permissions can be validated
  end
  raise Unauthorized unless im_allowed
end