Sha256: 1c47708fc18c3f3970001e8ccbd65ed3e0b757f48d94f8bdc2e4c9468d1dbb64

Contents?: true

Size: 1.04 KB

Versions: 2

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

module Pragma
  module Operation
    module Macro
      def self.Policy(name: :default)
        step = ->(input, options) { Policy.for(input, name, options) }
        [step, name: "policy.#{name}"]
      end

      module Policy
        class << self
          def for(input, name, options)
            policy = options["policy.#{name}.class"].new(options['current_user'], options['model'])

            options["result.policy.#{name}"] = Trailblazer::Operation::Result.new(
              policy.send("#{input.class.operation_name}?"),
              'policy' => policy
            )

            unless options["result.policy.#{name}"].success?
              handle_unauthorized!(options)
              return false
            end

            true
          end

          private

          def handle_unauthorized!(options)
            options['result.response'] = Pragma::Operation::Response::Forbidden.new.decorate_with(
              Pragma::Decorator::Error
            )
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pragma-2.1.1 lib/pragma/operation/macro/policy.rb
pragma-2.1.0 lib/pragma/operation/macro/policy.rb