lib/critic/policy.rb in critic-0.2.0 vs lib/critic/policy.rb in critic-0.2.1
- old
+ new
@@ -1,6 +1,7 @@
# frozen_string_literal: true
+# Represents the authorization policy interface
module Critic::Policy
extend ActiveSupport::Concern
def self.policies
@_policies ||= Hash.new { |h, k| h[k.to_s] = nil }
@@ -37,35 +38,26 @@
def scope(action = nil)
action.nil? ? (@scope || :index) : (@scope = action)
end
end
- attr_reader :subject, :resource, :errors
- attr_accessor :authorization
-
- delegate :messages, :metadata, to: :authorization
-
def initialize(subject, resource)
@subject = subject
@resource = resource
@errors = []
end
- def failure_message(action)
- "#{subject} is not authorized to #{action} #{resource}"
- end
-
def authorize(action, *args)
self.authorization = Critic::Authorization.new(self, action)
result = catch(:halt) { process_authorization(action, args) }
authorization.result = result if authorization.result.nil?
case authorization.result
- when Critic::Authorization
- # user has accessed authorization directly
+ # when Critic::Authorization
+ # # user has accessed authorization directly
when String
authorization.granted = false
authorization.messages << result
when nil, false
authorization.granted = false
@@ -75,10 +67,20 @@
end
authorization
end
+ attr_accessor :authorization
+
protected
+
+ attr_reader :subject, :resource, :errors
+
+ delegate :messages, :metadata, to: :authorization
+
+ def failure_message(action)
+ "#{subject} is not authorized to #{action} #{resource}"
+ end
def halt(*response)
throw :halt, *response
end