Module: Aegis::Permissions::ClassMethods

Defined in:
lib/aegis/permissions.rb

Instance Method Summary collapse

Instance Method Details

#default_role(role_name, options = {}) ⇒ Object



15
16
17
# File 'lib/aegis/permissions.rb', line 15

def default_role(role_name, options = {})
  @default_role = role(role_name, options)
end

#denied?(*args) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/aegis/permissions.rb', line 61

def denied?(*args)
  !allowed?(*args)
end

#evaluate_permission_blocks(role, blocks, *args) ⇒ Object



56
57
58
59
# File 'lib/aegis/permissions.rb', line 56

def evaluate_permission_blocks(role, blocks, *args)
    evaluator = Aegis::PermissionEvaluator.new(role)
    evaluator.evaluate(blocks, args)
end

#find_all_role_namesObject



25
26
27
# File 'lib/aegis/permissions.rb', line 25

def find_all_role_names
  @roles_by_name.keys
end

#find_all_rolesObject



29
30
31
# File 'lib/aegis/permissions.rb', line 29

def find_all_roles
  @roles_by_name.values.sort
end

#find_role_by_name(name) ⇒ Object



33
34
35
36
37
# File 'lib/aegis/permissions.rb', line 33

def find_role_by_name(name)
  return nil if name == ""
  # If name is nil, try to return the default role.
  name.nil? ? @default_role : @roles_by_name[name.to_sym]
end

#find_role_by_name!(name) ⇒ Object



39
40
41
# File 'lib/aegis/permissions.rb', line 39

def find_role_by_name!(name)
  find_role_by_name(name) or raise "Undefined role: #{name}"
end

#may?(role_or_role_name, permission, *args) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
54
# File 'lib/aegis/permissions.rb', line 50

def may?(role_or_role_name, permission, *args)
  role = role_or_role_name.is_a?(Aegis::Role) ? role_or_role_name : find_role_by_name(role_or_role_name)
  blocks = @permission_blocks[permission.to_sym]
  evaluate_permission_blocks(role, blocks, *args)
end

#permission(*permission_name_or_names, &block) ⇒ Object



43
44
45
46
47
48
# File 'lib/aegis/permissions.rb', line 43

def permission(*permission_name_or_names, &block)
  permission_names = Array(permission_name_or_names).map(&:to_s)
  permission_names.each do |permission_name|
    add_split_crud_permission(permission_name, &block)
  end
end

#role(role_name, options = {}) ⇒ Object



19
20
21
22
23
# File 'lib/aegis/permissions.rb', line 19

def role(role_name, options = {})
  role_name = role_name.to_sym
  role_name != Aegis::Constants::EVERYONE_ROLE_NAME or raise "Cannot define a role named: #{Aegis::Constants::EVERYONE_ROLE_NAME}"
  @roles_by_name[role_name] = Aegis::Role.new(role_name, self, options)
end