forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck-permission.js
More file actions
67 lines (62 loc) · 2.34 KB
/
Copy pathcheck-permission.js
File metadata and controls
67 lines (62 loc) · 2.34 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
import { t } from '@lingui/macro'
export const checkPermission =
({ i18n, userKey, query }) =>
async ({ orgId }) => {
let cursor
const userKeyString = `users/${userKey}`
// Check for super admin
try {
cursor = await query`
WITH affiliations, organizations, users
FOR v, e IN 1 INBOUND ${userKeyString} affiliations
FILTER e.permission == "super_admin"
RETURN e.permission
`
} catch (err) {
console.error(`Database error when checking to see if user: ${userKeyString} has super admin permission: ${err}`)
throw new Error(i18n._(t`Authentication error. Please sign in.`))
}
let permission
try {
permission = await cursor.next()
} catch (err) {
console.error(`Cursor error when checking to see if user ${userKeyString} has super admin permission: ${err}`)
throw new Error(i18n._(t`Unable to check permission. Please try again.`))
}
if (permission === 'super_admin') {
return permission
}
// Check for other permission level
try {
cursor = await query`
WITH affiliations, organizations, users
LET userAffiliations = (
FOR v, e IN 1..1 ANY ${userKeyString} affiliations
FILTER e.permission != "pending"
RETURN v
)
LET hasVerifiedOrgAffiliation = POSITION(userAffiliations[*].verified, true)
LET org = DOCUMENT(${orgId})
LET orgIsVerified = org.verified
LET userOrgAffiliation = FIRST(
FOR v, e IN 1..1 ANY ${userKeyString} affiliations
FILTER e._from == ${orgId}
RETURN e
)
RETURN userOrgAffiliation.permission IN ["user", "admin", "owner", "super_admin"] ? userOrgAffiliation.permission
: (orgIsVerified && hasVerifiedOrgAffiliation) ? "user"
: userOrgAffiliation.permission == "pending" ? userOrgAffiliation
: null
`
} catch (err) {
console.error(`Database error occurred when checking ${userKeyString}'s permission: ${err}`)
throw new Error(i18n._(t`Authentication error. Please sign in.`))
}
try {
permission = await cursor.next()
} catch (err) {
console.error(`Cursor error when checking ${userKeyString}'s permission: ${err}`)
throw new Error(i18n._(t`Unable to check permission. Please try again.`))
}
return permission
}