Sha256: ccc38a26cfca6643e4f510ce9e9fb6b85abe592b16e580954cc7ec2e247917f6

Contents?: true

Size: 1.22 KB

Versions: 2

Compression:

Stored size: 1.22 KB

Contents

# frozen_string_literal: true

require "active_support/concern"
require "miau/version"
require "miau/error"
require "miau/storage"
require "miau/application_policy"

module Miau
  extend ActiveSupport::Concern

  included do
    if respond_to?(:helper_method)
      helper_method :authorized?
      helper_method :miau_user
    end
  end

  def authorize!(resource = nil, hsh = {})
    @_miau_authorization_performed = true
    return true if authorized?(resource, hsh)

    klass, action = klass_action(hsh)
    msg = "class <#{klass} action <#{action}>"
    raise Miau::NotAuthorizedError, msg
  end

  def authorized?(resource = nil, hsh = {})
    klass, action = klass_action(hsh)
    Miau::PolicyStorage.instance.run(klass, action, miau_user, resource)
  end

  def miau_user
    current_user
  end

  def skip_authorization
    @_miau_authorization_performed = true
  end

  def verify_authorized
    raise AuthorizationNotPerformedError unless miau_authorization_performed?
  end

  def miau_authorization_performed?
    !!@_miau_authorization_performed
  end

  private

  def klass_action(hsh)
    klass = hsh[:class]
    klass ||= params[:controller]
    action = hsh[:action]
    action ||= params[:action]
    [klass, action]
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
miau-1.0.1 lib/miau.rb
miau-1.0.0 lib/miau.rb