1 | 'use strict'
|
2 |
|
3 | const AuthRequest = require('./auth-request')
|
4 | const debug = require('./../debug').accounts
|
5 |
|
6 |
|
7 | class DeleteAccountRequest extends AuthRequest {
|
8 | constructor (options) {
|
9 | super(options)
|
10 |
|
11 | this.username = options.username
|
12 | }
|
13 |
|
14 | |
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | error (error) {
|
21 | error.statusCode = error.statusCode || 400
|
22 |
|
23 | this.renderForm(error)
|
24 | }
|
25 |
|
26 | |
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | async loadUser () {
|
34 | let username = this.username
|
35 |
|
36 | return this.accountManager.accountExists(username)
|
37 | .then(exists => {
|
38 | if (!exists) {
|
39 | throw new Error('Account not found for that username')
|
40 | }
|
41 |
|
42 | let userData = { username }
|
43 |
|
44 | return this.accountManager.userAccountFrom(userData)
|
45 | })
|
46 | }
|
47 |
|
48 | |
49 |
|
50 |
|
51 | renderForm (error) {
|
52 | this.response.render('account/delete', {
|
53 | error,
|
54 | multiuser: this.accountManager.multiuser
|
55 | })
|
56 | }
|
57 |
|
58 | |
59 |
|
60 |
|
61 | renderSuccess () {
|
62 | this.response.render('account/delete-link-sent')
|
63 | }
|
64 |
|
65 | |
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | async sendDeleteLink (userAccount) {
|
74 | let accountManager = this.accountManager
|
75 |
|
76 | const recoveryEmail = await accountManager.loadAccountRecoveryEmail(userAccount)
|
77 | userAccount.email = recoveryEmail
|
78 |
|
79 | debug('Preparing delete account email to:', recoveryEmail)
|
80 |
|
81 | return accountManager.sendDeleteAccountEmail(userAccount)
|
82 | }
|
83 |
|
84 | |
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 | validate () {
|
91 | if (this.accountManager.multiuser && !this.username) {
|
92 | throw new Error('Username required')
|
93 | }
|
94 | }
|
95 |
|
96 | static async post (req, res) {
|
97 | const request = DeleteAccountRequest.fromParams(req, res)
|
98 |
|
99 | debug(`User '${request.username}' requested to be sent a delete account email`)
|
100 |
|
101 | return DeleteAccountRequest.handlePost(request)
|
102 | }
|
103 |
|
104 | |
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 | static async handlePost (request) {
|
113 | try {
|
114 | request.validate()
|
115 | const userAccount = await request.loadUser()
|
116 | await request.sendDeleteLink(userAccount)
|
117 | return request.renderSuccess()
|
118 | } catch (error) {
|
119 | return request.error(error)
|
120 | }
|
121 | }
|
122 |
|
123 | static get (req, res) {
|
124 | const request = DeleteAccountRequest.fromParams(req, res)
|
125 |
|
126 | request.renderForm()
|
127 | }
|
128 |
|
129 | static fromParams (req, res) {
|
130 | let locals = req.app.locals
|
131 | let accountManager = locals.accountManager
|
132 | let username = this.parseParameter(req, 'username')
|
133 |
|
134 | let options = {
|
135 | accountManager,
|
136 | response: res,
|
137 | username
|
138 | }
|
139 |
|
140 | return new DeleteAccountRequest(options)
|
141 | }
|
142 | }
|
143 |
|
144 | module.exports = DeleteAccountRequest
|