forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjwt_workflow.rb
More file actions
52 lines (46 loc) · 1.32 KB
/
Copy pathjwt_workflow.rb
File metadata and controls
52 lines (46 loc) · 1.32 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
module Canvas
class JWTWorkflow
def initialize(&token_state)
@token_state = token_state
end
def state_for(context, user)
@token_state ? @token_state.call(context, user) : {}
end
def self.state_for(workflows, context, user)
workflows.inject({}) do |memo, label|
workflow = get(label)
workflow ? memo.merge(workflow.state_for(context, user)) : memo
end
end
def self.get(label)
@workflows ? @workflows[label.to_sym] : nil
end
def self.register(label, &token_state)
@workflows ||= {}
@workflows[label.to_sym] = JWTWorkflow.new(&token_state)
end
# Register jwt token workflows with specific state requirments.
#
# - Try to keep workflow state in tokens to a minium. Remember this will be
# passed around with every request in the service workflow.
#
register(:rich_content) do |context, user|
{
usage_rights_required: (
context &&
context.feature_enabled?(:usage_rights_required)
) || false,
can_upload_files: (
user &&
context &&
context.grants_any_right?(user, :manage_files)
) || false
}
end
register(:ui) do |_, user|
{
use_high_contrast: user.try(:prefers_high_contrast?)
}
end
end
end