forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclose-account.js
More file actions
511 lines (487 loc) · 17.9 KB
/
Copy pathclose-account.js
File metadata and controls
511 lines (487 loc) · 17.9 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
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
503
504
505
506
507
508
509
510
511
import { t } from '@lingui/macro'
import { GraphQLID } from 'graphql'
import { fromGlobalId, mutationWithClientMutationId } from 'graphql-relay'
import { closeAccountUnion } from '../unions'
export const closeAccount = new mutationWithClientMutationId({
name: 'CloseAccount',
description: `This mutation allows a user to close their account, or a super admin to close another user's account.`,
inputFields: () => ({
userId: {
type: GraphQLID,
description: 'The user id of a user you want to close the account of.',
},
}),
outputFields: () => ({
result: {
type: closeAccountUnion,
description:
'`CloseAccountUnion` returning either a `CloseAccountResult`, or `CloseAccountError` object.',
resolve: (payload) => payload,
},
}),
mutateAndGetPayload: async (
args,
{
i18n,
query,
collections,
transaction,
auth: { checkSuperAdmin, userRequired },
loaders: { loadUserByKey },
validators: { cleanseInput },
},
) => {
let submittedUserId
if (args?.userId) {
submittedUserId = fromGlobalId(cleanseInput(args.userId)).id
}
const user = await userRequired()
let userId = ''
if (submittedUserId) {
const permission = await checkSuperAdmin()
if (!permission) {
console.warn(
`User: ${user._key} attempted to close user: ${submittedUserId} account, but requesting user is not a super admin.`,
)
return {
_type: 'error',
code: 400,
description: i18n._(
t`Permission error: Unable to close other user's account.`,
),
}
}
const checkUser = await loadUserByKey.load(submittedUserId)
if (typeof checkUser === 'undefined') {
console.warn(
`User: ${user._key} attempted to close user: ${submittedUserId} account, but requested user is undefined.`,
)
return {
_type: 'error',
code: 400,
description: i18n._(t`Unable to close account of an undefined user.`),
}
}
userId = checkUser._id
} else {
userId = user._id
}
// check to see if user owns any orgs
let orgOwnerAffiliationCursor
try {
orgOwnerAffiliationCursor = await query`
WITH users, affiliations, organizations
FOR v, e IN 1..1 INBOUND ${userId} affiliations
FILTER e.owner == true
RETURN e
`
} catch (err) {
console.error(
`Database error occurred when getting affiliations when user: ${user._key} attempted to close account: ${userId}: ${err}`,
)
throw new Error(i18n._(t`Unable to close account. Please try again.`))
}
let orgOwnerAffiliationCheck
try {
orgOwnerAffiliationCheck = await orgOwnerAffiliationCursor.all()
} catch (err) {
console.error(
`Cursor error occurred when getting affiliations when user: ${user._key} attempted to close account: ${userId}: ${err}`,
)
throw new Error(i18n._(t`Unable to close account. Please try again.`))
}
// Generate list of collections names
const collectionStrings = []
for (const property in collections) {
collectionStrings.push(property.toString())
}
// Setup Trans action
const trx = await transaction(collectionStrings)
// loop through each found org
for (const affiliation of orgOwnerAffiliationCheck) {
let dmarcSummaryCheckCursor
try {
dmarcSummaryCheckCursor = await query`
WITH domains, ownership, organizations
FOR v, e IN 1..1 OUTBOUND ${affiliation._from} ownership
RETURN e
`
} catch (err) {
console.error(
`Database error occurred when getting ownership info when user: ${user._key} attempted to close account: ${userId}: ${err}`,
)
throw new Error(i18n._(t`Unable to close account. Please try again.`))
}
let dmarcSummaryCheckList
try {
dmarcSummaryCheckList = await dmarcSummaryCheckCursor.all()
} catch (err) {
console.error(
`Cursor error occurred when getting ownership info when user: ${user._key} attempted to close account: ${userId}: ${err}`,
)
throw new Error(i18n._(t`Unable to close account. Please try again.`))
}
// remove dmarc summary related things
for (const ownership of dmarcSummaryCheckList) {
try {
await trx.step(
() => query`
WITH ownership, organizations, domains, dmarcSummaries, domainsToDmarcSummaries
LET dmarcSummaryEdges = (
FOR v, e IN 1..1 OUTBOUND ${ownership._to} domainsToDmarcSummaries
RETURN { edgeKey: e._key, dmarcSummaryId: e._to }
)
LET removeDmarcSummaryEdges = (
FOR dmarcSummaryEdge IN dmarcSummaryEdges
REMOVE dmarcSummaryEdge.edgeKey IN domainsToDmarcSummaries
OPTIONS { waitForSync: true }
)
LET removeDmarcSummary = (
FOR dmarcSummaryEdge IN dmarcSummaryEdges
LET key = PARSE_IDENTIFIER(dmarcSummaryEdge.dmarcSummaryId).key
REMOVE key IN dmarcSummaries
OPTIONS { waitForSync: true }
)
RETURN true
`,
)
} catch (err) {
console.error(
`Trx step error occurred when removing dmarc summaries when user: ${user._key} attempted to close account: ${userId}: ${err}`,
)
throw new Error(i18n._(t`Unable to close account. Please try again.`))
}
try {
await trx.step(
() => query`
WITH ownership, organizations, domains
REMOVE ${ownership._key} IN ownership
OPTIONS { waitForSync: true }
`,
)
} catch (err) {
console.error(
`Trx step error occurred when removing ownerships when user: ${user._key} attempted to close account: ${userId}: ${err}`,
)
throw new Error(i18n._(t`Unable to close account. Please try again.`))
}
}
let domainCountCursor
try {
domainCountCursor = await query`
WITH claims, domains, organizations
LET domainIds = (
FOR v, e IN 1..1 OUTBOUND ${affiliation._from} claims
RETURN e._to
)
FOR domain IN domains
FILTER domain._id IN domainIds
LET count = LENGTH(
FOR v, e IN 1..1 INBOUND domain._id claims
RETURN 1
)
RETURN {
_id: domain._id,
_key: domain._key,
domain: domain.domain,
count
}
`
} catch (err) {
console.error(
`Database error occurred when getting claim info when user: ${user._key} attempted to close account: ${userId}: ${err}`,
)
throw new Error(i18n._(t`Unable to close account. Please try again.`))
}
let domainCountList
try {
domainCountList = await domainCountCursor.all()
} catch (err) {
console.error(
`Cursor error occurred when getting claim info when user: ${user._key} attempted to close account: ${userId}: ${err}`,
)
throw new Error(i18n._(t`Unable to close account. Please try again.`))
}
for (const domainObj of domainCountList) {
if (domainObj.count === 1) {
try {
await trx.step(
() => query`
WITH dkim, domains, domainsDKIM, dkimToDkimResults, dkimResults
LET dkimEdges = (
FOR v, e IN 1..1 OUTBOUND ${domainObj._id} domainsDKIM
RETURN { edgeKey: e._key, dkimId: e._to }
)
FOR dkimEdge IN dkimEdges
LET dkimResultEdges = (
FOR v, e IN 1..1 OUTBOUND dkimEdge.dkimId dkimToDkimResults
RETURN { edgeKey: e._key, dkimResultId: e._to }
)
LET removeDkimResultEdges = (
FOR dkimResultEdge IN dkimResultEdges
REMOVE dkimResultEdge.edgeKey IN dkimToDkimResults
OPTIONS { waitForSync: true }
)
LET removeDkimResult = (
FOR dkimResultEdge IN dkimResultEdges
REMOVE PARSE_IDENTIFIER(dkimResultEdge.dkimResultId).key
IN dkimResults OPTIONS { waitForSync: true }
)
RETURN true
`,
)
} catch (err) {
console.error(
`Trx step error occurred when removing dkimResults when user: ${user._key} attempted to close account: ${userId}: ${err}`,
)
throw new Error(
i18n._(t`Unable to close account. Please try again.`),
)
}
try {
await Promise.all([
trx.step(
() => query`
WITH dkim, domains, domainsDKIM
LET dkimEdges = (
FOR v, e IN 1..1 OUTBOUND ${domainObj._id} domainsDKIM
RETURN { edgeKey: e._key, dkimId: e._to }
)
LET removeDkimEdges = (
FOR dkimEdge IN dkimEdges
REMOVE dkimEdge.edgeKey IN domainsDKIM
OPTIONS { waitForSync: true }
)
LET removeDkim = (
FOR dkimEdge IN dkimEdges
REMOVE PARSE_IDENTIFIER(dkimEdge.dkimId).key
IN dkim OPTIONS { waitForSync: true }
)
RETURN true
`,
),
trx.step(
() => query`
WITH dmarc, domains, domainsDMARC
LET dmarcEdges = (
FOR v, e IN 1..1 OUTBOUND ${domainObj._id} domainsDMARC
RETURN { edgeKey: e._key, dmarcId: e._to }
)
LET removeDmarcEdges = (
FOR dmarcEdge IN dmarcEdges
REMOVE dmarcEdge.edgeKey IN domainsDMARC
OPTIONS { waitForSync: true }
)
LET removeDmarc = (
FOR dmarcEdge IN dmarcEdges
REMOVE PARSE_IDENTIFIER(dmarcEdge.dmarcId).key
IN dmarc OPTIONS { waitForSync: true }
)
RETURN true
`,
),
trx.step(
() => query`
WITH spf, domains, domainsSPF
LET spfEdges = (
FOR v, e IN 1..1 OUTBOUND ${domainObj._id} domainsSPF
RETURN { edgeKey: e._key, spfId: e._to }
)
LET removeSpfEdges = (
FOR spfEdge IN spfEdges
REMOVE spfEdge.edgeKey IN domainsSPF
OPTIONS { waitForSync: true }
)
LET removeSpf = (
FOR spfEdge IN spfEdges
REMOVE PARSE_IDENTIFIER(spfEdge.spfId).key
IN spf OPTIONS { waitForSync: true }
)
RETURN true
`,
),
trx.step(
() => query`
WITH https, domains, domainsHTTPS
LET httpsEdges = (
FOR v, e IN 1..1 OUTBOUND ${domainObj._id} domainsHTTPS
RETURN { edgeKey: e._key, httpsId: e._to }
)
LET removeHttpsEdges = (
FOR httpsEdge IN httpsEdges
REMOVE httpsEdge.edgeKey IN domainsHTTPS
OPTIONS { waitForSync: true }
)
LET removeHttps = (
FOR httpsEdge IN httpsEdges
REMOVE PARSE_IDENTIFIER(httpsEdge.httpsId).key
IN https OPTIONS { waitForSync: true }
)
RETURN true
`,
),
trx.step(
() => query`
WITH ssl, domains, domainsSSL
LET sslEdges = (
FOR v, e IN 1..1 OUTBOUND ${domainObj._id} domainsSSL
RETURN { edgeKey: e._key, sslId: e._to }
)
LET removeSslEdges = (
FOR sslEdge IN sslEdges
REMOVE sslEdge.edgeKey IN domainsSSL
OPTIONS { waitForSync: true }
)
LET removeSsl = (
FOR sslEdge IN sslEdges
REMOVE PARSE_IDENTIFIER(sslEdge.sslId).key
IN ssl OPTIONS { waitForSync: true }
)
RETURN true
`,
),
])
} catch (err) {
console.error(
`Trx step error occurred when removing scan info when user: ${user._key} attempted to close account: ${userId}: ${err}`,
)
throw new Error(
i18n._(t`Unable to close account. Please try again.`),
)
}
try {
await trx.step(
() => query`
WITH claims, domains, organizations
LET domainEdges = (
FOR v, e IN 1..1 OUTBOUND ${affiliation._from} claims
FILTER e._to == ${domainObj._id}
RETURN { edgeKey: e._key, domainId: e._to }
)
LET removeDomainEdges = (
FOR domainEdge IN domainEdges
REMOVE domainEdge.edgeKey IN claims
OPTIONS { waitForSync: true }
)
LET removeDomain = (
FOR domainEdge IN domainEdges
REMOVE PARSE_IDENTIFIER(domainEdge.domainId).key
IN domains OPTIONS { waitForSync: true }
)
RETURN true
`,
)
} catch (err) {
console.error(
`Trx step error occurred when removing domains and claims when user: ${user._key} attempted to close account: ${userId}: ${err}`,
)
throw new Error(
i18n._(t`Unable to close account. Please try again.`),
)
}
} else {
try {
await trx.step(
() => query`
WITH claims, domains, organizations
LET domainEdges = (
FOR v, e IN 1..1 OUTBOUND ${affiliation._from} claims
RETURN { edgeKey: e._key, domainId: e._to }
)
LET removeDomainEdges = (
FOR domainEdge IN domainEdges
REMOVE domainEdge.edgeKey IN claims
OPTIONS { waitForSync: true }
)
RETURN true
`,
)
} catch (err) {
console.error(
`Trx step error occurred when removing domain claims when user: ${user._key} attempted to close account: ${userId}: ${err}`,
)
throw new Error(
i18n._(t`Unable to close account. Please try again.`),
)
}
}
}
// remove users affiliation
try {
await Promise.all([
trx.step(
() => query`
WITH affiliations, organizations, users
LET userEdges = (
FOR v, e IN 1..1 INBOUND ${affiliation._from} affiliations
RETURN { edgeKey: e._key, userKey: e._to }
)
LET removeUserEdges = (
FOR userEdge IN userEdges
REMOVE userEdge.userKey IN affiliations
OPTIONS { waitForSync: true }
)
RETURN true
`,
),
trx.step(
() => query`
WITH organizations
REMOVE PARSE_IDENTIFIER(${affiliation._from}).key
IN organizations OPTIONS { waitForSync: true }
`,
),
])
} catch (err) {
console.error(
`Trx step error occurred when removing ownership org and users affiliations when user: ${user._key} attempted to close account: ${userId}: ${err}`,
)
throw new Error(i18n._(t`Unable to close account. Please try again.`))
}
}
try {
await trx.step(
() => query`
WITH affiliations, organizations, users
FOR v, e IN 1..1 INBOUND ${userId} affiliations
REMOVE { _key: e._key } IN affiliations
OPTIONS { waitForSync: true }
`,
)
} catch (err) {
console.error(
`Trx step error occurred when removing users remaining affiliations when user: ${user._key} attempted to close account: ${userId}: ${err}`,
)
throw new Error(i18n._(t`Unable to close account. Please try again.`))
}
try {
await trx.step(
() => query`
WITH users
REMOVE PARSE_IDENTIFIER(${userId}).key
IN users OPTIONS { waitForSync: true }
`,
)
} catch (err) {
console.error(
`Trx step error occurred when removing user: ${user._key} attempted to close account: ${userId}: ${err}`,
)
throw new Error(i18n._(t`Unable to close account. Please try again.`))
}
try {
await trx.commit()
} catch (err) {
console.error(
`Trx commit error occurred when user: ${user._key} attempted to close account: ${userId}: ${err}`,
)
throw new Error(i18n._(t`Unable to close account. Please try again.`))
}
console.info(
`User: ${user._key} successfully closed user: ${userId} account.`,
)
return {
_type: 'regular',
status: i18n._(t`Successfully closed account.`),
}
},
})