forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck-domain-ownership.js
More file actions
84 lines (77 loc) · 2.85 KB
/
check-domain-ownership.js
File metadata and controls
84 lines (77 loc) · 2.85 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
import { t } from '@lingui/macro'
export const checkDomainOwnership =
({ i18n, query, userKey }) =>
async ({ domainId }) => {
let userAffiliatedOwnership, ownership
const userKeyString = `users/${userKey}`
// Check to see if the user is a super admin
let superAdminAffiliationCursor
try {
superAdminAffiliationCursor = await query`
WITH affiliations, organizations, users
LET domainOwnerships = (FOR v, e IN 1..1 ANY ${domainId} ownership RETURN e._from)
LET superAdmin = (
FOR v, e IN 1..1 ANY ${userKeyString} affiliations
FILTER e.permission == "super_admin"
RETURN e.from
)
RETURN {
domainOwnership: (LENGTH(domainOwnerships) > 0 ? true : false),
superAdmin: (LENGTH(superAdmin) > 0 ? true : false)
}
`
} catch (err) {
console.error(
`Database error when retrieving super admin affiliated organization ownership for user: ${userKey} and domain: ${domainId}: ${err}`,
)
throw new Error(
i18n._(t`Ownership check error. Unable to request domain information.`),
)
}
let superAdminAffiliation
try {
superAdminAffiliation = await superAdminAffiliationCursor.next()
} catch (err) {
console.error(
`Cursor error when retrieving super admin affiliated organization ownership for user: ${userKey} and domain: ${domainId}: ${err}`,
)
throw new Error(
i18n._(t`Ownership check error. Unable to request domain information.`),
)
}
if (superAdminAffiliation.superAdmin) {
if (superAdminAffiliation.domainOwnership) {
return true
} else {
return false
}
}
// Get user affiliations and affiliated orgs owning provided domain
try {
userAffiliatedOwnership = await query`
WITH affiliations, domains, organizations, ownership, users
LET userAffiliations = (FOR v, e IN 1..1 ANY ${userKeyString} affiliations RETURN e._from)
LET domainOwnerships = (FOR v, e IN 1..1 ANY ${domainId} ownership RETURN e._from)
LET affiliatedOwnership = INTERSECTION(userAffiliations, domainOwnerships)
RETURN affiliatedOwnership
`
} catch (err) {
console.error(
`Database error when retrieving affiliated organization ownership for user: ${userKey} and domain: ${domainId}: ${err}`,
)
throw new Error(
i18n._(t`Ownership check error. Unable to request domain information.`),
)
}
try {
ownership = await userAffiliatedOwnership.next()
} catch (err) {
console.error(
`Cursor error when retrieving affiliated organization ownership for user: ${userKey} and domain: ${domainId}: ${err}`,
)
throw new Error(
i18n._(t`Ownership check error. Unable to request domain information.`),
)
}
return ownership[0] !== undefined
}