Sha256: 3dacb47e1c3e84c684d5f049c42de543f7673911c011e80751c4a2d70a25cbcd

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 KB

Contents

require "access_policy/version"
require 'scoped_storage'

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

module AccessPolicy

  class NotDefinedError < StandardError
  end

  class NotAuthorizedError < StandardError
  end

  class AuthorizeNotCalledError < StandardError
  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, &block)
      unsafe_action_name = unsafe_action_name(action_name)

      define_method action_name do |*args|
        _authorize "#{action_name}?"
        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.5 lib/access_policy.rb