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