Sha256: 0c7984b3d8c959134d48f55cf8a7ea6e9d19a6958b990248607d591fe2b95322

Contents?: true

Size: 1.49 KB

Versions: 1

Compression:

Stored size: 1.49 KB

Contents

require "access_policy/version"
require 'scoped_storage'

require 'access_policy/policy_check'
require 'access_policy/policy_enforcer'

module AccessPolicy

  class NestedStandardError < StandardError
    attr_reader :original
    def initialize(msg, original=$!)
      super(msg)
      @original = original
    end
  end

  class NotDefinedError < NestedStandardError
  end

  class NotAuthorizedError < NestedStandardError
  end

  class AuthorizeNotCalledError < NestedStandardError
  end

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

  def _default_error_policy
    ->(*){raise}
  end

  def _scope_storage
    ScopedStorage::ThreadLocalStorage
  end

  def _guard
    @_guard ||= PolicyCheck.new(default_error_policy: _default_error_policy,
                                scope_storage: _scope_storage
                               )
  end

  def _authorize(query)
    _guard.authorize self, query.to_sym
  end

  def with_user_or_role(user_or_role, error_policy =  _default_error_policy ,&block)
    _guard.with_user_or_role(user_or_role, error_policy, &block)
  end

  module ClassMethods

    def policy_guarded_method(action_name, query="#{action_name}?" ,&block)
      unsafe_action_name = unsafe_action_name(action_name)

      define_method action_name do |*args|
        _authorize query
        self.send(unsafe_action_name, *args)
      end

      define_method unsafe_action_name, block
    end

    def unsafe_action_name(action_name)
      :"#{action_name}_unsafe"
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
access_policy-0.0.7 lib/access_policy.rb