Class: Hubbado::Sequence::Macros::Policy::Check

Inherits:
Object
  • Object
show all
Defined in:
lib/hubbado/sequence/macros/policy/check.rb

Defined Under Namespace

Modules: Substitute

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.buildObject



9
10
11
# File 'lib/hubbado/sequence/macros/policy/check.rb', line 9

def self.build
  new
end

Instance Method Details

#call(ctx, policy, record_key, action) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/hubbado/sequence/macros/policy/check.rb', line 13

def call(ctx, policy, record_key, action)
  current_user = ctx[:current_user]
  record = ctx[record_key]

  policy_instance = policy.build(current_user, record)
  policy_result = policy_instance.public_send(action)

  if policy_result.permitted?
    Result.success(ctx)
  else
    Result.failure(
      ctx,
      error: {
        code: :forbidden,
        data: { policy: policy_instance, policy_result: policy_result }
      }
    )
  end
end