Module: Authority

Defined in:
lib/authority.rb,
lib/authority/railtie.rb,
lib/authority/version.rb,
lib/authority/abilities.rb,
lib/authority/controller.rb,
lib/authority/authorizer.rb,
lib/authority/configuration.rb,
lib/authority/user_abilities.rb,
lib/authority/security_violation.rb,
lib/generators/authority/install_generator.rb

Defined Under Namespace

Modules: Abilities, Controller, Generators, UserAbilities Classes: Authorizer, Configuration, MissingUser, NoAuthorizerError, Railtie, SecurityViolation

Constant Summary

VERSION =
"3.0.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject

Returns the value of attribute configuration



49
50
51
# File 'lib/authority.rb', line 49

def configuration
  @configuration
end

Class Method Details

.abilitiesHash

NOTE: once this method is called, the library has started meta programming and abilities should no longer be modified

Returns:

  • (Hash)

    list of abilities, mapping verbs and adjectives, like :create => 'creatable'



16
17
18
# File 'lib/authority.rb', line 16

def self.abilities
  configuration.abilities.freeze
end

.action_authorized?(action, resource, user, options = {}) ⇒ Boolean

Returns:

  • (Boolean)

Raises:



42
43
44
45
46
# File 'lib/authority.rb', line 42

def self.action_authorized?(action, resource, user, options = {})
  raise MissingUser if user.nil?
  resource_and_maybe_options = [resource, options].tap {|args| args.pop if args.last == {}}
  user.send("can_#{action}?", *resource_and_maybe_options)
end

.adjectivesArray

Returns values from adjectives method

Returns:

  • (Array)

    values from adjectives method



26
27
28
# File 'lib/authority.rb', line 26

def self.adjectives
  abilities.values
end

.configure {|configuration| ... } ⇒ Object

Yields:



52
53
54
55
56
57
58
# File 'lib/authority.rb', line 52

def self.configure
  self.configuration ||= Configuration.new
  yield(configuration) if block_given?
  require_authority_internals!

  configuration
end

.enforce(action, resource, user, options = {}) ⇒ Model

Returns resource instance

Parameters:

  • action (Symbol)
  • resource (Model)

    instance

  • user (User)

    instance

  • options, (Hash)

    arbitrary options hash to delegate to the authorizer

Returns:

  • (Model)

    resource instance

Raises:



36
37
38
39
40
# File 'lib/authority.rb', line 36

def self.enforce(action, resource, user, options = {})
  unless action_authorized?(action, resource, user, options)
    raise SecurityViolation.new(user, action, resource)
  end
end

.loggerObject



60
61
62
# File 'lib/authority.rb', line 60

def self.logger
  @logger ||= configuration.logger
end

.verbsArray

Returns keys from adjectives method

Returns:

  • (Array)

    keys from adjectives method



21
22
23
# File 'lib/authority.rb', line 21

def self.verbs
  abilities.keys
end