-
Notifications
You must be signed in to change notification settings - Fork 0
/
jullunch_admin.rb
274 lines (217 loc) · 9.4 KB
/
jullunch_admin.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# encoding: utf-8
require 'google/api_client'
require 'google/api_client/client_secrets'
require 'google/api_client/auth/file_storage'
require_relative 'lib/database'
require_relative 'lib/notification'
###############################################################################
# Web Application
###############################################################################
class JullunchAdmin < Sinatra::Base
use Rack::MethodOverride
use Rack::Session::Cookie, key: 'athega_jullunch',
secret: 'Knowledge is power and true Sith do not share power.'
def api_client; settings.api_client; end
def oauth2; settings.oauth2; end
def user_credentials
# Build a per-request oauth credential based on token stored in session
# which allows us to use a shared API client.
@authorization ||= (
auth = api_client.authorization.dup
auth.redirect_uri = to('/auth/admin/callback')
auth.update_token!(session)
auth
)
end
#############################################################################
# Configuration
#############################################################################
configure do
set :root, File.dirname(__FILE__)
set :credential_store_file, "./tmp/jullunch_admin-oauth2.json"
enable :sessions
end
configure :development do
set :forced_authentication, true
end
configure :production do
set :static_cache_control, [:public, :max_age => 300]
client = Google::APIClient.new(:application_name => 'Athega Jullunch',
:application_version => '1.0.0')
file_storage = Google::APIClient::FileStorage.new(settings.credential_store_file)
if file_storage.authorization.nil?
client_secrets = ENV['CLIENT_SECRETS'] ?
Google::APIClient::ClientSecrets.new(JSON.parse(ENV['CLIENT_SECRETS'])) :
Google::APIClient::ClientSecrets.load
client.authorization = client_secrets.to_authorization
client.authorization.scope = 'https://www.googleapis.com/auth/userinfo.email'
else
client.authorization = file_storage.authorization
end
set :api_client, client
set :oauth2, client.discovered_api('oauth2', 'v2')
end
helpers do
def logged_in?
return true if settings.respond_to?(:forced_authentication)
return (user_credentials.access_token && session[:user_email].end_with?("@athega.se")) ? true : false
end
include Rack::Utils
alias_method :h, :escape_html
end
#############################################################################
# Authentication routes
#############################################################################
before '/admin/*' do
redirect to('/auth/authorize') unless logged_in?
end
after do
unless settings.respond_to?(:forced_authentication)
# Serialize the access/refresh token to the session and credential store.
session[:access_token] = user_credentials.access_token
session[:refresh_token] = user_credentials.refresh_token
session[:expires_in] = user_credentials.expires_in
session[:issued_at] = user_credentials.issued_at
file_storage = Google::APIClient::FileStorage.new(settings.credential_store_file)
file_storage.write_credentials(user_credentials)
end
end
get '/auth/authorize' do
# Request authorization
redirect user_credentials.authorization_uri.to_s, 303
end
get '/auth/admin/callback' do
# Exchange token
user_credentials.code = params[:code] if params[:code]
user_credentials.fetch_access_token!
# Store email
result = api_client.execute!(:api_method => settings.oauth2.userinfo.get, :authorization => user_credentials)
session[:user_email] = result.data.email
redirect to('/admin')
end
get '/logout/?' do
session.clear
redirect '/'
end
#############################################################################
# Application routes
#############################################################################
get '/admin/?' do
redirect to('/admin/guests')
end
get '/admin/guests' do
statistics = [
OpenStruct.new(text: 'TOTALT:', count: Guest.count),
OpenStruct.new(text: 'Inbjudningar:', count: Guest.invited.count),
OpenStruct.new(text: 'Manuella:', count: Guest.invited_manually.count),
OpenStruct.new(text: 'Tackat ja:', count: Guest.said_yes.count),
OpenStruct.new(text: 'Tackat nej:', count: Guest.all_by_sitting_key(0).count),
OpenStruct.new(text: 'Ej valt:', count: Guest.all_by_sitting_key(nil).count),
]
haml :'admin/guests/index', locals: {
page_title: 'Gäster - Athega Jullunch',
guests: Guest.sort([:company, 1], [:name, 1]).all,
statistics: statistics
}
end
post '/admin/guests/import_from_spreadsheet' do
import_count = ImportFromSpreadsheet.guests!
redirect to("/admin/guests?number_of_imported_guests=#{import_count}")
end
post '/admin/guests' do
guest = Guest.new name: params[:name],
company: params[:company],
email: params[:email],
invited_by: params[:invited_by],
invited_manually: (params[:invited_manually] == 'yes')
guest.save if guest.valid?
redirect '/admin/guests'
end
delete '/admin/guests/:token' do
guest = Guest.by_token(params[:token])
guest.delete unless guest.nil?
redirect to('/admin/guests')
end
get '/admin/sittings' do
haml :'admin/sittings', locals: {
page_title: 'Sittningar - Athega Jullunch',
sittings: Sitting.sort([:starts_at, -1]).all
}
end
post '/admin/sittings' do
sitting = Sitting.by_key(params[:key].to_i)
if sitting.nil?
sitting = Sitting.new
sitting.key = params[:key].to_i
sitting.title = params[:title]
sitting.starts_at = Time.parse(params[:starts_at]).utc
sitting.number_of_guests_allowed = params[:number_of_guests_allowed].to_i
sitting.number_of_reserved_seats = params[:number_of_reserved_seats].to_i
sitting.save
end
redirect to('/admin/sittings')
end
post '/admin/sittings/:key' do
sitting = Sitting.by_key(params[:key].to_i)
unless sitting.nil?
if params[:title].empty? || params[:starts_at].empty?
sitting.delete
else
sitting.title = params[:title]
sitting.starts_at = Time.parse(params[:starts_at]).utc
sitting.number_of_guests_allowed = params[:number_of_guests_allowed].to_i
sitting.number_of_reserved_seats = params[:number_of_reserved_seats].to_i
sitting.save
end
end
redirect to('/admin/sittings')
end
get '/admin/notifications' do
haml :'admin/notifications', locals: {
page_title: 'Notifikationer - Athega Jullunch'
}
end
post '/admin/notifications/send_all_pending_invitations' do
sent_count = Notification.send_all_pending_invitations!
redirect to("/admin/notifications?number_of_invitations_sent=#{sent_count}")
end
post '/admin/notifications/send_all_pending_reminders' do
sent_count = Notification.send_all_pending_reminders!
redirect to("/admin/notifications?number_of_invitations_sent=#{sent_count}")
end
post '/admin/notifications/send_all_pending_welcomes' do
sent_count = Notification.send_all_pending_welcomes!
redirect to("/admin/notifications?number_of_invitations_sent=#{sent_count}")
end
post '/admin/notifications/send_all_pending_thank_you_notes' do
sent_count = Notification.send_all_pending_thank_you_notes!
redirect to("/admin/notifications?number_of_thank_you_notes_sent=#{sent_count}")
end
get '/admin/prepare_db_qwerty1234' do
Sitting.delete_all
number_of_guests_allowed = params[:number_of_guests_allowed].to_i
number_of_reserved_seats = params[:number_of_reserved_seats].to_i
Sitting.new(key: 1130, title: '11:30', starts_at: Time.parse('2016-12-16 11:30:00 CET').utc,
number_of_guests_allowed: number_of_guests_allowed, number_of_reserved_seats: number_of_reserved_seats).save
Sitting.new(key: 1200, title: '12:00', starts_at: Time.parse('2016-12-16 12:00:00 CET').utc,
number_of_guests_allowed: number_of_guests_allowed, number_of_reserved_seats: number_of_reserved_seats).save
Sitting.new(key: 1230, title: '12:30', starts_at: Time.parse('2016-12-16 12:30:00 CET').utc,
number_of_guests_allowed: number_of_guests_allowed, number_of_reserved_seats: number_of_reserved_seats).save
Sitting.new(key: 1300, title: '13:00', starts_at: Time.parse('2016-12-16 13:00:00 CET').utc,
number_of_guests_allowed: number_of_guests_allowed, number_of_reserved_seats: number_of_reserved_seats).save
Sitting.new(key: 1330, title: '13:30', starts_at: Time.parse('2016-12-16 13:30:00 CET').utc,
number_of_guests_allowed: number_of_guests_allowed, number_of_reserved_seats: number_of_reserved_seats).save
Sitting.new(key: 0000, title: 'Jag måste tyvärr tacka nej').save
redirect '/admin/guests'
end
get '/admin/guest/untagged' do
haml :'register/tag', locals: {
page_title: 'Taggning - Athega Jullunch',
remaining: Guest.said_yes.untagged.count
}
end
get '/admin/load_test_users_qwerty1234' do
import_count = ImportFromSpreadsheet.test!
redirect "/admin/guests?imported_test_users=#{import_count}"
end
end