2018-03-08 12:56:54 +00:00
|
|
|
module DeclarativePolicy
|
|
|
|
# The return value of a rule { ... } declaration.
|
|
|
|
# Can call back to register rules with the containing
|
|
|
|
# Policy class (context_class here). See Base.rule
|
|
|
|
#
|
|
|
|
# Note that the #policy method just performs an #instance_eval,
|
|
|
|
# which is useful for multiple #enable or #prevent callse.
|
|
|
|
#
|
|
|
|
# Also provides a #method_missing proxy to the context
|
|
|
|
# class's class methods, so that helper methods can be
|
|
|
|
# defined and used in a #policy { ... } block.
|
|
|
|
class PolicyDsl
|
|
|
|
def initialize(context_class, rule)
|
|
|
|
@context_class = context_class
|
|
|
|
@rule = rule
|
|
|
|
end
|
|
|
|
|
2018-07-04 14:02:01 +00:00
|
|
|
def policy(&block)
|
|
|
|
instance_eval(&block)
|
2018-03-08 12:56:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def enable(*abilities)
|
|
|
|
@context_class.enable_when(abilities, @rule)
|
|
|
|
end
|
|
|
|
|
|
|
|
def prevent(*abilities)
|
|
|
|
@context_class.prevent_when(abilities, @rule)
|
|
|
|
end
|
|
|
|
|
|
|
|
def prevent_all
|
|
|
|
@context_class.prevent_all_when(@rule)
|
|
|
|
end
|
|
|
|
|
2018-07-04 14:02:01 +00:00
|
|
|
def method_missing(msg, *args, &block)
|
|
|
|
return super unless @context_class.respond_to?(msg)
|
2018-03-08 12:56:54 +00:00
|
|
|
|
2018-07-04 14:02:01 +00:00
|
|
|
@context_class.__send__(msg, *args, &block) # rubocop:disable GitlabSecurity/PublicSend
|
2018-03-08 12:56:54 +00:00
|
|
|
end
|
|
|
|
|
2018-07-04 14:02:01 +00:00
|
|
|
def respond_to_missing?(msg)
|
|
|
|
@context_class.respond_to?(msg) || super
|
2018-03-08 12:56:54 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|