Sha256: 2c43ecc05db49d4d4f1df45e8d410e783ea7f7f39d892c09840ff74ec34ec5ce

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 KB

Contents

module Authority
  class Authorizer
    extend Forwardable

    # The base Authorizer class, from which all the authorizers in an app will
    # descend. Provides the authorizer with both class and instance methods
    # like `updatable_by?(user)`.
    # Exactly which methods get defined is determined from `config.abilities`;
    # the class is evaluated after any user-supplied config block is run
    # in order to make that possible.

    attr_reader :resource

    def initialize(resource)
      @resource = resource
    end

    # Whitelisting approach: anything not specified will be forbidden
    def self.default(adjective, user, options = {})
      false
    end

    # Each instance method simply calls the corresponding class method
    Authority.adjectives.each do |adjective|
      def_delegator :"self.class", :"#{adjective}_by?"
    end

    # Each class method simply calls the `default` method
    Authority.adjectives.each do |adjective|
      class_eval <<-RUBY, __FILE__, __LINE__ + 1
        def self.#{adjective}_by?(user, options = {})
          if options.empty?
            default(:#{adjective}, user)
          else
            default(:#{adjective}, user, options)
          end
        end
      RUBY
    end

  end

  class NoAuthorizerError < StandardError ; end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
authority-2.3.2 lib/authority/authorizer.rb