forked from zammad/zammad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_context.rb
35 lines (28 loc) · 935 Bytes
/
user_context.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
# We need a special UserContext when authorizing in controller context
# because of Token authentication which has it's own permissions
# See: https://github.com/varvet/pundit#additional-context
# We use a Delegator here to have transparent / DuckType access
# to the underlying User instance in the Policy
class UserContext < Delegator
def initialize(user, token = nil) # rubocop:disable Lint/MissingSuper
@user = user
@token = token
end
def __getobj__
@user
end
def permissions?(permissions)
permissions!(permissions)
true
rescue Exceptions::Forbidden
false
end
def permissions!(permissions)
raise Exceptions::Forbidden, __('Authentication required') if !@user
if @token
return @token.with_context(user: @user) { permissions!(permissions) }
end
@user.permissions!(permissions)
end
end