2012-11-04 04:20:45 -05:00
|
|
|
require "pundit/version"
|
2012-11-19 04:57:17 -05:00
|
|
|
require "pundit/policy_finder"
|
2012-11-19 07:02:42 -05:00
|
|
|
require "active_support/concern"
|
|
|
|
require "active_support/core_ext/string/inflections"
|
|
|
|
require "active_support/core_ext/object/blank"
|
2012-11-04 04:20:45 -05:00
|
|
|
|
|
|
|
module Pundit
|
2012-11-19 04:57:17 -05:00
|
|
|
class NotAuthorizedError < StandardError; end
|
|
|
|
class NotDefinedError < StandardError; end
|
|
|
|
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
class << self
|
|
|
|
def policy_scope(user, scope)
|
|
|
|
policy = PolicyFinder.new(scope).scope
|
|
|
|
policy.new(user, scope).resolve if policy
|
|
|
|
end
|
|
|
|
|
|
|
|
def policy_scope!(user, scope)
|
|
|
|
PolicyFinder.new(scope).scope!.new(user, scope).resolve
|
|
|
|
end
|
|
|
|
|
|
|
|
def policy(user, record)
|
|
|
|
scope = PolicyFinder.new(record).policy
|
|
|
|
scope.new(user, record) if scope
|
|
|
|
end
|
|
|
|
|
|
|
|
def policy!(user, record)
|
|
|
|
PolicyFinder.new(record).policy!.new(user, record)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
included do
|
|
|
|
if respond_to?(:helper_method)
|
|
|
|
helper_method :policy_scope
|
|
|
|
helper_method :policy
|
|
|
|
end
|
2013-03-28 12:26:24 -04:00
|
|
|
if respond_to?(:hide_action)
|
|
|
|
hide_action :authorize
|
|
|
|
hide_action :verify_authorized
|
|
|
|
end
|
2012-11-19 04:57:17 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def verify_authorized
|
|
|
|
raise NotAuthorizedError unless @_policy_authorized
|
|
|
|
end
|
|
|
|
|
|
|
|
def authorize(record, query=nil)
|
|
|
|
query ||= params[:action].to_s + "?"
|
|
|
|
@_policy_authorized = true
|
|
|
|
unless policy(record).public_send(query)
|
|
|
|
raise NotAuthorizedError, "not allowed to #{query} this #{record}"
|
|
|
|
end
|
2012-11-19 07:02:42 -05:00
|
|
|
true
|
2012-11-19 04:57:17 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def policy_scope(scope)
|
|
|
|
Pundit.policy_scope!(current_user, scope)
|
|
|
|
end
|
|
|
|
|
|
|
|
def policy(record)
|
|
|
|
Pundit.policy!(current_user, record)
|
|
|
|
end
|
2012-11-04 04:20:45 -05:00
|
|
|
end
|