/
user.moon
502 lines (382 loc) · 13.9 KB
/
user.moon
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
-- User management actions not related to modules
lapis = require "lapis"
db = require "lapis.db"
import
respond_to
assert_error
yield_error
capture_errors_json
from require "lapis.application"
import assert_valid, with_params from require "lapis.validate"
shapes = require "helpers.shapes"
types = require "lapis.validate.types"
password_shape = types.valid_text * types.string\length 2, 150
import
ApiKeys
Users
Manifests
ManifestModules
Followings
from require "models"
import
assert_csrf
require_login
ensure_https
capture_errors_404
assert_editable
verify_return_to
capture_errors
from require "helpers.app"
import
transfer_endorsements
from require "helpers.toolbox"
import load_module, load_manifest from require "helpers.loaders"
import paginated_modules from require "helpers.modules"
import preload from require "lapis.db.model"
assert_table = (val) ->
assert_error type(val) == "table", "malformed input, expecting table"
val
validate_reset_token = =>
if @params.token
assert_valid @params, {
{ "id", is_integer: true }
}
@user = assert_error Users\find(@params.id), "invalid token"
@user\get_data!
assert_error @user.data.password_reset_token == @params.token, "invalid token"
@token = @params.token
true
class MoonRocksUser extends lapis.Application
[user_profile: "/modules/:user"]: capture_errors_404 =>
@user = assert_error Users\find(slug: @params.user), "invalid user"
@title = "#{@user\name_for_display!}'s Modules"
@user_following = @current_user and @current_user\find_follow @user
paginated_modules @, @user, (mods) ->
for mod in *mods
mod.user = @user
mods
render: true
[user_login: "/login"]: ensure_https respond_to {
before: =>
@canonical_url = @build_url @url_for "user_login"
@title = "Login"
GET: =>
render: true
POST: capture_errors with_params {
{ "username", types.limited_text 25}
{ "password", types.valid_text * types.string\length 1, 150}
}, (params) =>
assert_csrf @
user = assert_error Users\login params.username, params.password
user\write_session @, type: "login_password"
redirect_to: verify_return_to(@params.return_to) or @url_for "index"
}
[user_register: "/register"]: ensure_https respond_to {
before: =>
@canonical_url = @build_url @url_for "user_register"
@title = "Register Account"
GET: =>
render: true
POST: capture_errors with_params {
{ "username", types.limited_text 25 }
{ "password", password_shape }
{ "password_repeat", password_shape }
{ "email", shapes.email }
}, (params) =>
assert_csrf @
assert_error params.password == params.password_repeat, "Password repeat does not match"
{:username, :password, :email } = params
user = assert_error Users\create username, password, email
user\write_session @, type: "register"
redirect_to: verify_return_to(@params.return_to) or @url_for"index"
}
-- TODO: make this post
[user_logout: "/logout"]: =>
@session.user = false
if @current_user_session
@current_user_session\revoke!
redirect_to: "/"
[user_forgot_password: "/user/forgot_password"]: ensure_https respond_to {
GET: capture_errors =>
validate_reset_token @
render: true
POST: capture_errors =>
assert_csrf @
if validate_reset_token @
params = assert_valid @params, types.params_shape {
{"password", password_shape}
{"password_repeat", password_shape}
}
assert_error params.password == params.password_repeat, "Password repeat does not match"
@user\update_password params.password, @
@user.data\update { password_reset_token: db.NULL }
redirect_to: @url_for"index"
else
params = assert_valid @params, types.params_shape {
{"email", shapes.email / string.lower}
}
user = assert_error Users\find([db.raw "lower(email)"]: params.email),
"don't know anyone with that email"
token = user\generate_password_reset!
reset_url = @build_url @url_for"user_forgot_password",
query: "token=#{token}&id=#{user.id}"
UserPasswordResetEmail = require "emails.user_password_reset"
UserPasswordResetEmail\send @, user.email, { :user, :reset_url }
redirect_to: @url_for"user_forgot_password" .. "?sent=true"
}
["user_settings.link_github": "/settings/link-github"]: ensure_https require_login respond_to {
GET: =>
@user = @current_user
@title = "Link GitHub - User Settings"
@github_accounts = @user\get_github_accounts!
render: true
}
["user_settings.import_toolbox": "/settings/import-toolbox"]: ensure_https require_login respond_to {
before: =>
@user = @current_user
import ToolboxImport from require "helpers.toolbox"
import Modules from require "models"
@to_import = ToolboxImport!\modules_endorsed_by_user @user
if @to_import
Modules\preload_relation @to_import, "user"
Modules\preload_follows @to_import, @user
@already_following = [m for m in *@to_import when m.current_user_following]
@to_import = [m for m in *@to_import when not m.current_user_following]
GET: =>
@title = "Import Lua Toolbox - User Settings"
render: true
POST: =>
assert_csrf @
assert_error @to_import and next(@to_import), "missing modules to follow"
for m in *@to_import
@flow("followings")\follow_object m, "subscription"
redirect_to: @url_for("user_settings.import_toolbox")
}
["user_settings.reset_password": "/settings/reset-password"]: ensure_https require_login respond_to {
before: =>
@user = @current_user
@title = "Reset Password - User Settings"
GET: =>
render: true
POST: capture_errors with_params {
{"password", types.params_shape {
{"current_password", password_shape}
{"new_password", password_shape}
{"new_password_repeat", password_shape}
}}
}, (params) =>
import UserActivityLogs from require "models"
assert_csrf @
passwords = params.password
assert_error passwords.new_password == passwords.new_password_repeat,
"Password repeat does not match"
unless @user\check_password passwords.current_password
UserActivityLogs\create_from_request @, {
user_id: @user.id
source: "web"
action: "account.update_password_attempt"
data: { reason: "incorrect old password" }
}
yield_error "Incorrect old password"
error "Incorrect old password (not captured)"
old_pword = @user.encrypted_password
@user\update_password passwords.new_password, @
UserActivityLogs\create_from_request @, {
user_id: @user.id
source: "web"
action: "account.update_password"
data: {
encrypted_password: {
before: old_pword
after: @user.encrypted_password
}
}
}
redirect_to: @url_for "user_settings.reset_password", nil, reset_password: "true"
}
["user_settings.api_keys": "/settings/api-keys"]: ensure_https require_login respond_to {
before: =>
@user = @current_user
@title = "Api Keys - User Settings"
@api_keys = if @params.revoked
@show_revoked = true
@user\get_revoked_api_keys!
else
@user\get_api_keys!
GET: =>
render: true
POST: capture_errors with_params {
{"api_key", types.limited_text 512 }
{"comment", types.empty / db.NULL + types.limited_text 255 }
}, (params)=>
assert_csrf @
key = ApiKeys\find @current_user.id, assert params.api_key
assert_error key and key.user_id == @current_user.id, "invalid key"
assert_error not key.revoked, "invalid key"
key\update {
comment: params.comment
}
redirect_to: @url_for "user_settings.api_keys"
}
["user_settings.profile": "/settings/profile"]: ensure_https require_login respond_to {
before: =>
@user = @current_user
@title = "Profile - User Settings"
GET: =>
render: true
POST: capture_errors with_params {
{"profile", types.params_shape {
{"website", shapes.url + types.empty / db.NULL}
{"twitter", shapes.twitter_username + types.empty / db.NULL}
{"github", types.limited_text(120) + types.empty / db.NULL}
{"profile", types.limited_text(1024*4) + types.empty / db.NULL}
}}
}, (params) =>
assert_csrf @
profile_update = params.profile
difference = shapes.difference profile_update, @user\get_data!
if next difference
@user\get_data!\update profile_update
import UserActivityLogs from require "models"
UserActivityLogs\create_from_request @, {
user_id: @user.id
source: "web"
action: "account.update_profile"
data: difference
}
redirect_to: @url_for "user_settings.profile"
}
["user_settings.security_audit": "/settings/security-audit"]: ensure_https require_login respond_to {
before: =>
@user = @current_user
@title = "Security Audit"
GET: =>
import UserServerLogs from require "models"
@server_logs = UserServerLogs\select "where user_id = ? order by log_date asc", @current_user.id
if @params.download
ngx.header["Content-Type"] = "text/plain"
for log in *@server_logs
ngx.say log.log
return skip_render: true
render: true
}
["user_settings.sessions": "/settings/sessions"]: ensure_https require_login respond_to {
POST: capture_errors_json =>
assert_csrf @
assert_valid @params, {
{"action", one_of: {"disable_session"}}
}
switch @params.action
when "disable_session"
assert_valid @params, {
{"session_id", exists: true, is_integer: true}
}
import UserSessions from require "models"
session = UserSessions\find {
user_id: assert @current_user.id
id: assert @params.session_id
}
if session
session\revoke!
return redirect_to: @url_for "user_settings.sessions"
GET: =>
import UserSessions from require "models"
pager = UserSessions\paginated "
where user_id = ?
order by coalesce(last_active_at, created_at) desc
", @current_user.id, {
per_page: 20
}
@sessions = pager\get_page!
render: true
}
["user_settings.activity": "/settings/activity"]: ensure_https require_login respond_to {
GET: =>
import UserActivityLogs from require "models"
pager = UserActivityLogs\paginated "
where user_id = ?
order by created_at desc
", @current_user.id, {
per_page: 40
prepare_results: (logs) ->
preload logs, "object", "user"
logs
}
@user_activity_logs = pager\get_page!
render: true
}
-- old settings url goes to api keys page since that's where tool points to
"/settings": ensure_https require_login =>
redirect_to: @url_for "user_settings.api_keys"
[add_to_manifest: "/add-to-manifest/:user/:module"]: capture_errors_404 require_login respond_to {
before: =>
load_module @
assert_editable @, @module
@title = "Add Module To Manifest"
already_in = { m.id, true for m in *@module\get_manifests! }
@manifests = for m in *Manifests\select!
continue if already_in[m.id]
m
GET: =>
render: true
POST: capture_errors =>
assert_csrf @
assert_valid @params, {
{ "manifest_id", is_integer: true }
}
manifest = assert_error Manifests\find(id: @params.manifest_id), "Invalid manifest id"
unless manifest\allowed_to_add @current_user
yield_error "Don't have permission to add to manifest"
assert_error ManifestModules\create manifest, @module
redirect_to: @url_for("module", @)
}
[remove_from_manifest: "/remove-from-manifest/:user/:module/:manifest"]: capture_errors_404 require_login respond_to {
before: =>
load_module @
load_manifest @
assert_editable @, @module
GET: =>
@title = "Remove Module From Manifest"
assert_error ManifestModules\find({
manifest_id: @manifest.id
module_id: @module.id
}), "Module is not in manifest"
render: true
POST: =>
assert_csrf @
ManifestModules\remove @manifest, @module
redirect_to: @url_for("module", @)
}
[notifications: "/notifications"]: require_login =>
import Notifications from require "models"
@unseen_notifications = Notifications\select "
where user_id = ? and not seen
order by id desc
", @current_user.id
@seen_notifications = Notifications\select "
where user_id = ? and seen
order by id desc
limit 20
", @current_user.id
if next(@unseen_notifications) and not @params.keep_notifications
db.update Notifications\table_name!, {
seen: true
}, id: db.list [n.id for n in *@unseen_notifications]
all = [n for n in *@unseen_notifications]
for n in *@seen_notifications
table.insert all, n
Notifications\preload_for_display all
@title = "Notifications"
render: true
[follow_user: "/users/:slug/follow"]: require_login capture_errors_404 =>
followed_user = assert_error Users\find(slug: @params.slug),
"Invalid User"
assert_error @current_user.id != followed_user.id,
"You can't follow yourself"
@flow("followings")\follow_object followed_user, "subscription"
redirect_to: @url_for followed_user
[unfollow_user: "/users/:slug/unfollow"]: require_login capture_errors_404 =>
unfollowed_user = assert_error Users\find(slug: @params.slug),
"Invalid User"
@flow("followings")\unfollow_object unfollowed_user, "subscription"
redirect_to: @url_for unfollowed_user