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"
|
2014-05-21 22:09:17 -04:00
|
|
|
require "active_support/core_ext/module/introspection"
|
2014-04-22 22:02:03 -06:00
|
|
|
require "active_support/dependencies/autoload"
|
2012-11-04 10:20:45 +01:00
|
|
|
|
|
|
|
module Pundit
|
2015-04-01 11:17:30 +02:00
|
|
|
SUFFIX = "Policy"
|
|
|
|
|
2015-04-07 14:46:50 -04:00
|
|
|
class Error < StandardError; end
|
|
|
|
class NotAuthorizedError < Error
|
2015-02-25 20:30:08 +01:00
|
|
|
attr_reader :query, :record, :policy
|
|
|
|
|
|
|
|
def initialize(options = {})
|
2015-05-05 08:07:50 +02:00
|
|
|
if options.is_a? String
|
|
|
|
message = options
|
|
|
|
else
|
|
|
|
@query = options[:query]
|
|
|
|
@record = options[:record]
|
|
|
|
@policy = options[:policy]
|
2015-02-25 20:30:08 +01:00
|
|
|
|
2015-05-05 08:07:50 +02:00
|
|
|
message = options.fetch(:message) { "not allowed to #{query} this #{record.inspect}" }
|
|
|
|
end
|
2015-02-25 20:30:08 +01:00
|
|
|
|
|
|
|
super(message)
|
|
|
|
end
|
2014-02-25 18:54:22 -08:00
|
|
|
end
|
2015-04-07 14:46:50 -04:00
|
|
|
class AuthorizationNotPerformedError < Error; end
|
2014-07-07 20:50:22 -05:00
|
|
|
class PolicyScopingNotPerformedError < AuthorizationNotPerformedError; end
|
2015-04-07 14:46:50 -04:00
|
|
|
class NotDefinedError < Error; end
|
2012-11-19 10:57:17 +01:00
|
|
|
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
class << self
|
2015-03-26 10:32:20 +01:00
|
|
|
def authorize(user, record, query)
|
|
|
|
policy = policy!(user, record)
|
|
|
|
|
|
|
|
unless policy.public_send(query)
|
|
|
|
raise NotAuthorizedError.new(query: query, record: record, policy: policy)
|
|
|
|
end
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2014-08-22 11:19:36 +02:00
|
|
|
def policy_scope(user, scope)
|
|
|
|
policy_scope = PolicyFinder.new(scope).scope
|
2013-11-05 18:26:38 +01:00
|
|
|
policy_scope.new(user, scope).resolve if policy_scope
|
2012-11-19 10:57:17 +01:00
|
|
|
end
|
|
|
|
|
2014-08-22 11:19:36 +02:00
|
|
|
def policy_scope!(user, scope)
|
|
|
|
PolicyFinder.new(scope).scope!.new(user, scope).resolve
|
2012-11-19 10:57:17 +01:00
|
|
|
end
|
|
|
|
|
2014-08-22 11:19:36 +02:00
|
|
|
def policy(user, record)
|
|
|
|
policy = PolicyFinder.new(record).policy
|
2013-11-05 18:26:38 +01:00
|
|
|
policy.new(user, record) if policy
|
2012-11-19 10:57:17 +01:00
|
|
|
end
|
|
|
|
|
2014-08-22 11:19:36 +02:00
|
|
|
def policy!(user, record)
|
|
|
|
PolicyFinder.new(record).policy!.new(user, record)
|
2012-11-19 10:57:17 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-03-27 13:51:02 +01:00
|
|
|
module Helper
|
|
|
|
def policy_scope(scope)
|
|
|
|
pundit_policy_scope(scope)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-11-19 10:57:17 +01:00
|
|
|
included do
|
2015-03-27 13:51:02 +01:00
|
|
|
helper Helper if respond_to?(:helper)
|
2012-11-19 10:57:17 +01:00
|
|
|
if respond_to?(:helper_method)
|
|
|
|
helper_method :policy
|
2015-03-27 13:51:02 +01:00
|
|
|
helper_method :pundit_policy_scope
|
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)
|
2014-03-09 23:01:53 +04:00
|
|
|
hide_action :policy
|
2014-08-22 13:25:50 +02:00
|
|
|
hide_action :policy_scope
|
|
|
|
hide_action :policies
|
|
|
|
hide_action :policy_scopes
|
2013-03-28 17:26:24 +01:00
|
|
|
hide_action :authorize
|
|
|
|
hide_action :verify_authorized
|
2013-04-17 22:05:24 -07:00
|
|
|
hide_action :verify_policy_scoped
|
2015-03-27 10:14:09 +01:00
|
|
|
hide_action :permitted_attributes
|
2013-07-14 00:50:39 +02:00
|
|
|
hide_action :pundit_user
|
2015-04-19 13:11:35 +02:00
|
|
|
hide_action :skip_authorization
|
2015-04-19 13:14:58 +02:00
|
|
|
hide_action :skip_policy_scope
|
2015-07-10 13:14:05 +09:00
|
|
|
hide_action :pundit_policy_authorized?
|
|
|
|
hide_action :pundit_policy_scoped?
|
2013-03-28 17:26:24 +01:00
|
|
|
end
|
2012-11-19 10:57:17 +01:00
|
|
|
end
|
|
|
|
|
2015-03-18 15:50:06 +01:00
|
|
|
def pundit_policy_authorized?
|
|
|
|
!!@_pundit_policy_authorized
|
|
|
|
end
|
|
|
|
|
|
|
|
def pundit_policy_scoped?
|
|
|
|
!!@_pundit_policy_scoped
|
|
|
|
end
|
|
|
|
|
2012-11-19 10:57:17 +01:00
|
|
|
def verify_authorized
|
2015-03-18 15:50:06 +01:00
|
|
|
raise AuthorizationNotPerformedError unless pundit_policy_authorized?
|
2012-11-19 10:57:17 +01:00
|
|
|
end
|
|
|
|
|
2013-04-17 22:05:24 -07:00
|
|
|
def verify_policy_scoped
|
2015-03-18 15:50:06 +01:00
|
|
|
raise PolicyScopingNotPerformedError unless pundit_policy_scoped?
|
2013-04-17 22:05:24 -07:00
|
|
|
end
|
|
|
|
|
2012-11-19 10:57:17 +01:00
|
|
|
def authorize(record, query=nil)
|
|
|
|
query ||= params[:action].to_s + "?"
|
2015-02-25 20:30:08 +01:00
|
|
|
|
2014-08-26 08:57:27 -07:00
|
|
|
@_pundit_policy_authorized = true
|
2014-02-25 18:54:22 -08:00
|
|
|
|
|
|
|
policy = policy(record)
|
|
|
|
unless policy.public_send(query)
|
2015-02-25 20:30:08 +01:00
|
|
|
raise NotAuthorizedError.new(query: query, record: record, policy: policy)
|
2012-11-19 10:57:17 +01:00
|
|
|
end
|
2014-02-25 18:54:22 -08:00
|
|
|
|
2012-11-19 13:02:42 +01:00
|
|
|
true
|
2012-11-19 10:57:17 +01:00
|
|
|
end
|
|
|
|
|
2015-02-20 14:38:08 -05:00
|
|
|
def skip_authorization
|
|
|
|
@_pundit_policy_authorized = true
|
|
|
|
end
|
|
|
|
|
2015-04-08 09:17:32 +08:00
|
|
|
def skip_policy_scope
|
|
|
|
@_pundit_policy_scoped = true
|
|
|
|
end
|
|
|
|
|
2012-11-19 10:57:17 +01:00
|
|
|
def policy_scope(scope)
|
2014-08-26 08:57:27 -07:00
|
|
|
@_pundit_policy_scoped = true
|
2015-03-27 13:51:02 +01:00
|
|
|
pundit_policy_scope(scope)
|
2012-11-19 10:57:17 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def policy(record)
|
2014-08-22 13:20:32 +02:00
|
|
|
policies[record] ||= Pundit.policy!(pundit_user, record)
|
2014-05-19 15:05:09 +10:00
|
|
|
end
|
|
|
|
|
2015-03-27 10:14:09 +01:00
|
|
|
def permitted_attributes(record)
|
|
|
|
name = record.class.to_s.demodulize.underscore
|
2015-07-02 19:35:39 +05:00
|
|
|
params.require(name).permit(*policy(record).permitted_attributes)
|
2015-03-27 10:14:09 +01:00
|
|
|
end
|
|
|
|
|
2014-08-22 13:20:32 +02:00
|
|
|
def policies
|
2014-08-26 08:57:27 -07:00
|
|
|
@_pundit_policies ||= {}
|
2014-08-22 13:20:32 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def policy_scopes
|
2014-08-26 08:57:27 -07:00
|
|
|
@_pundit_policy_scopes ||= {}
|
2013-07-13 05:42:34 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def pundit_user
|
2013-07-13 16:24:13 +02:00
|
|
|
current_user
|
2012-11-19 10:57:17 +01:00
|
|
|
end
|
2015-03-27 13:51:02 +01:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def pundit_policy_scope(scope)
|
|
|
|
policy_scopes[scope] ||= Pundit.policy_scope!(pundit_user, scope)
|
|
|
|
end
|
2012-11-04 10:20:45 +01:00
|
|
|
end
|