Module: CanHasPermission

Included in:
Anonymous
Defined in:
lib/can-has-permission.rb,
lib/can-has-permission/role.rb,
lib/can-has-permission/role_map.rb,
lib/can-has-permission/anonymous.rb,
lib/can-has-permission/permission.rb,
lib/can-has-permission/permission_map.rb

Defined Under Namespace

Classes: Anonymous, Permission, PermissionMap, Role, RoleMap

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
6
7
8
9
10
11
12
# File 'lib/can-has-permission.rb', line 3

def self.included(base)
  base.class_eval do
    has_many :role_maps, :as => 'permissible', :class_name => 'CanHasPermission::RoleMap'
    has_many :permission_maps, :as => 'permissible', :class_name => 'CanHasPermission::PermissionMap'
    has_many :permissions, :through => :permission_maps
    has_many :roles, :through => :role_maps
    accepts_nested_attributes_for :role_maps
    accepts_nested_attributes_for :permission_maps
  end
end

Instance Method Details

#can?(permission) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
17
18
19
20
# File 'lib/can-has-permission.rb', line 14

def can?(permission)
  return true if (!self.permissions.select{|p| p.name == permission.to_s}.empty?)
  self.roles.each do |role|
    return true if role.can?(permission)
  end
  false
end

#has_role?(role) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/can-has-permission.rb', line 22

def has_role?(role)
  !self.roles.select{|r| r.name == role.to_s}.empty?
end