|
5 | 5 | require 'workos'
|
6 | 6 | require 'date'
|
7 | 7 | require_relative 'audit_log_events.rb'
|
| 8 | +require 'pry' |
8 | 9 |
|
9 | 10 | # Pull API key and Client ID from ENV variable
|
10 | 11 | WorkOS.key = ENV['WORKOS_API_KEY']
|
|
22 | 23 | )
|
23 | 24 |
|
24 | 25 | get '/' do
|
| 26 | + before = params[:before] |
| 27 | + after = params[:after] |
| 28 | + if !before |
| 29 | + @organizations = WorkOS::Organizations.list_organizations( |
| 30 | + limit: 5 |
| 31 | + ) |
| 32 | + else |
| 33 | + @organizations = WorkOS::Organizations.list_organizations( |
| 34 | + limit: 5, |
| 35 | + before: before, |
| 36 | + after: after |
| 37 | + ) |
| 38 | + end |
| 39 | + @before = @organizations.list_metadata["before"] |
| 40 | + @after = @organizations.list_metadata["after"] |
25 | 41 | erb :login, :layout => :layout
|
26 | 42 | end
|
27 | 43 |
|
28 |
| -post '/set_org' do |
29 |
| - @organization_id = params[:org] |
30 |
| - |
31 |
| - session[:organization_id] = @organization_id |
32 |
| - |
33 |
| - organization = WorkOS::Organizations.get_organization( |
34 |
| - id: @organization_id |
35 |
| - ) |
36 |
| - |
37 |
| - @org_name = organization.name |
38 |
| - session[:organization_name] = @org_name |
39 |
| - erb :send_events, :layout => :layout |
40 |
| -end |
41 | 44 |
|
42 | 45 | get '/set_org' do
|
43 |
| - @organization_id = session[:organization_id] |
44 |
| - @org_name = session[:organization_name] |
| 46 | + @organization = WorkOS::Organizations.get_organization( |
| 47 | + id: params[:id] |
| 48 | + ) |
| 49 | + @today_iso = Time.now.utc.iso8601 |
| 50 | + @last_month_iso = (Time.now - (30 * 86400)).utc.iso8601 |
45 | 51 | erb :send_events, :layout => :layout
|
46 | 52 | end
|
47 | 53 |
|
48 |
| -post '/send_event' do |
49 |
| - event_type = params[:event] |
50 |
| - @organization_id = session[:organization_id] |
51 |
| - @org_name = session[:organization_name] |
52 |
| - |
53 |
| - events = [ |
54 |
| - $user_signed_in, |
55 |
| - $user_logged_out, |
56 |
| - $user_organization_deleted, |
57 |
| - $user_connection_deleted, |
58 |
| - ] |
59 |
| - |
60 |
| - event = events[event_type.to_i] |
| 54 | +get '/events' do |
| 55 | + link = WorkOS::Portal.generate_link( |
| 56 | + organization: params[:organization_id], |
| 57 | + intent: params[:intent], |
| 58 | + ) |
| 59 | + redirect link |
| 60 | +end |
61 | 61 |
|
| 62 | +post '/send_events' do |
| 63 | + |
| 64 | + organization_id = params["organization_id"] |
| 65 | + |
| 66 | + event = { |
| 67 | + "action": "user.organization_deleted", |
| 68 | + "version": params[:event_version].to_i, |
| 69 | + "occurred_at": Time.now.utc.iso8601, |
| 70 | + "actor": { |
| 71 | + "type": params[:actor_type], |
| 72 | + "name": params[:actor_name], |
| 73 | + "id": "user_01GBNJC3MX9ZZJW1FSTF4C5938", |
| 74 | + }, |
| 75 | + "targets": [ |
| 76 | + { |
| 77 | + "type": params[:target_type], |
| 78 | + "name": params[:target_name], |
| 79 | + "id": "team_01GBNJD4MKHVKJGEWK42JNMBGS", |
| 80 | + }, |
| 81 | + ], |
| 82 | + "context": { |
| 83 | + "location": "123.123.123.123", |
| 84 | + "user_agent": "Chrome/104.0.0.0", |
| 85 | + }, |
| 86 | + } |
62 | 87 | WorkOS::AuditLogs.create_event(
|
63 |
| - organization: @organization_id, |
64 |
| - event: event |
| 88 | + organization: organization_id, |
| 89 | + event: event, |
65 | 90 | )
|
| 91 | + |
| 92 | + redirect to("/set_org?id=#{organization_id}") |
66 | 93 |
|
67 |
| - erb :send_events, :layout => :layout |
68 |
| -end |
69 | 94 |
|
70 |
| -get '/export_events' do |
71 |
| - @organization_id = session[:organization_id] |
72 |
| - @org_name = session[:organization_name] |
73 |
| - erb :export_events, :layout => :layout |
74 | 95 | end
|
75 | 96 |
|
| 97 | + |
76 | 98 | post '/get_events' do
|
77 |
| - organization_id = session[:organization_id] |
| 99 | + |
| 100 | + organization_id = params[:organization_id] |
| 101 | + |
78 | 102 | event_type = params[:event]
|
79 | 103 | today = DateTime.now.to_s
|
80 | 104 | last_month = DateTime.now.prev_month.to_s
|
81 |
| - |
| 105 | + if params[:filter_actions] != "" |
| 106 | + actions = params[:filter_actions] |
| 107 | + else |
| 108 | + actions = nil |
| 109 | + end |
| 110 | + |
| 111 | + if params[:filter_actors] != "" |
| 112 | + actors = params[:filter_actors] |
| 113 | + else |
| 114 | + actors = nil |
| 115 | + end |
| 116 | + |
| 117 | + if params[:filter_targets] != "" |
| 118 | + targets = params[:filter_targets] |
| 119 | + else |
| 120 | + targets = nil |
| 121 | + end |
| 122 | + |
82 | 123 | if event_type == 'generate_csv'
|
83 | 124 | audit_log_export = WorkOS::AuditLogs.create_export(
|
84 | 125 | organization: organization_id,
|
85 |
| - range_start: last_month, |
86 |
| - range_end: today |
| 126 | + range_start: params[:range_start], |
| 127 | + range_end: params[:range_end], |
| 128 | + actions: actions, |
| 129 | + actors: actors, |
| 130 | + targets: targets |
87 | 131 | )
|
88 | 132 | session[:export_id] = audit_log_export.id
|
89 |
| - puts audit_log_export.id |
90 |
| - redirect '/export_events' |
| 133 | + redirect to("/set_org?id=#{organization_id}") |
91 | 134 | end
|
92 | 135 |
|
93 | 136 | if event_type == 'access_csv'
|
94 | 137 | export_id = session[:export_id].to_s
|
95 |
| - puts export_id |
96 | 138 | audit_log_export = WorkOS::AuditLogs.get_export(
|
97 | 139 | id: export_id
|
98 | 140 | )
|
|
102 | 144 | end
|
103 | 145 |
|
104 | 146 | end
|
105 |
| - |
106 |
| - |
107 |
| -# Logout a user |
108 |
| -get '/logout' do |
109 |
| - session[:organization_id] = nil |
110 |
| - session[:organization_name] = nil |
111 |
| - session[:export_id] = nil |
112 |
| - redirect '/' |
113 |
| -end |
|
0 commit comments