2012-11-04 10:20:45 +01:00
|
|
|
require "pundit/version"
|
2012-11-19 10:57:17 +01:00
|
|
|
require "pundit/policy_finder"
|
2012-11-19 13:02:42 +01:00
|
|
|
require "active_support/concern"
|
|
|
|
require "active_support/core_ext/string/inflections"
|
|
|
|
require "active_support/core_ext/object/blank"
|
2012-11-04 10:20:45 +01:00
|
|
|
|
|
|
|
module Pundit
|
2012-11-19 10:57:17 +01:00
|
|
|
class NotAuthorizedError < StandardError; end
|
|
|
|
class NotDefinedError < StandardError; end
|
|
|
|
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
class << self
|
|
|
|
def policy_scope(user, scope)
|
2013-11-05 18:26:38 +01:00
|
|
|
policy_scope = PolicyFinder.new(scope).scope
|
|
|
|
policy_scope.new(user, scope).resolve if policy_scope
|
2012-11-19 10:57:17 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def policy_scope!(user, scope)
|
|
|
|
PolicyFinder.new(scope).scope!.new(user, scope).resolve
|
|
|
|
end
|
|
|
|
|
|
|
|
def policy(user, record)
|
2013-11-05 18:26:38 +01:00
|
|
|
policy = PolicyFinder.new(record).policy
|
|
|
|
policy.new(user, record) if policy
|
2012-11-19 10:57:17 +01:00
|
|
|
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
|
2013-07-14 00:50:39 +02:00
|
|
|
helper_method :pundit_user
|
2012-11-19 10:57:17 +01:00
|
|
|
end
|
2013-03-28 17:26:24 +01:00
|
|
|
if respond_to?(:hide_action)
|
|
|
|
hide_action :authorize
|
|
|
|
hide_action :verify_authorized
|
2013-04-17 22:05:24 -07:00
|
|
|
hide_action :verify_policy_scoped
|
2013-07-14 00:50:39 +02:00
|
|
|
hide_action :pundit_user
|
2013-03-28 17:26:24 +01:00
|
|
|
end
|
2012-11-19 10:57:17 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def verify_authorized
|
|
|
|
raise NotAuthorizedError unless @_policy_authorized
|
|
|
|
end
|
|
|
|
|
2013-04-17 22:05:24 -07:00
|
|
|
def verify_policy_scoped
|
|
|
|
raise NotAuthorizedError unless @_policy_scoped
|
|
|
|
end
|
|
|
|
|
2012-11-19 10:57:17 +01:00
|
|
|
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 13:02:42 +01:00
|
|
|
true
|
2012-11-19 10:57:17 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def policy_scope(scope)
|
2013-04-17 22:05:24 -07:00
|
|
|
@_policy_scoped = true
|
2013-08-12 10:38:50 +10:00
|
|
|
@policy_scope or Pundit.policy_scope!(pundit_user, scope)
|
2012-11-19 10:57:17 +01:00
|
|
|
end
|
2013-08-12 10:38:50 +10:00
|
|
|
attr_writer :policy_scope
|
2012-11-19 10:57:17 +01:00
|
|
|
|
|
|
|
def policy(record)
|
2013-08-12 10:38:50 +10:00
|
|
|
@policy or Pundit.policy!(pundit_user, record)
|
2013-07-13 05:42:34 +02:00
|
|
|
end
|
2013-08-12 10:38:50 +10:00
|
|
|
attr_writer :policy
|
2013-07-13 05:42:34 +02:00
|
|
|
|
|
|
|
def pundit_user
|
2013-07-13 16:24:13 +02:00
|
|
|
current_user
|
2012-11-19 10:57:17 +01:00
|
|
|
end
|
2012-11-04 10:20:45 +01:00
|
|
|
end
|