-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
permissions.ts
116 lines (104 loc) · 2.73 KB
/
permissions.ts
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
import { TRPCError } from '@trpc/server'
import invariant from 'tiny-invariant'
import { type Context } from '../context'
import { type Meta, t } from '../initTRPC'
/** Send unauthorized rejection via middleware */
const reject = () => {
throw new TRPCError({ code: 'UNAUTHORIZED' })
}
export const checkPermissions = (meta: Meta | undefined, ctx: Context) => {
try {
/** No permissions submitted, throw error */
if (typeof meta?.hasPerm === 'undefined') {
throw new TRPCError({
code: 'INTERNAL_SERVER_ERROR',
message: 'Invalid procedure configuration, missing permission requirements.',
})
}
/** Check for session object, error if missing */
invariant(ctx.session?.user)
/** Check if user is `root`. If so, allow. */
if (ctx.session.user.permissions.includes('root')) {
return true
}
/** Check multiple permissions */
if (Array.isArray(meta.hasPerm)) {
if (meta.hasPerm.every((perm) => ctx.session?.user?.permissions?.includes(perm))) {
return true
}
return false
}
/** Check single permission */
return ctx.session.user.permissions.includes(meta.hasPerm)
} catch (err) {
/** If no session object, call reject */
if (err instanceof Error && err.message === 'Invariant failed') {
return reject()
}
/** Return false for any other errors */
return false
}
}
export const checkRole = (allowedRoles: string[], userRoles: string[]) => {
for (const userRole of userRoles) {
if (allowedRoles.includes(userRole)) {
return true
}
}
return false
}
export const isAdmin = t.middleware(({ ctx, meta, next }) => {
if (ctx.session === null) {
return reject()
}
if (
!(checkRole(['dataAdmin', 'sysadmin', 'root'], ctx.session?.user?.roles) && checkPermissions(meta, ctx))
) {
return reject()
}
return next({
ctx: {
...ctx,
session: { ...ctx.session, user: ctx.session.user },
actorId: ctx.session.user.id,
skipCache: true,
},
})
})
export const isStaff = t.middleware(({ ctx, meta, next }) => {
if (ctx.session === null) {
return reject()
}
if (
!(
checkRole(['dataManager', 'dataAdmin', 'sysadmin', 'system', 'root'], ctx.session?.user?.roles) &&
checkPermissions(meta, ctx)
)
) {
return reject()
}
return next({
ctx: {
...ctx,
session: { ...ctx.session, user: ctx.session.user },
actorId: ctx.session.user.id,
skipCache: true,
},
})
})
export const hasPermissions = t.middleware(({ ctx, meta, next }) => {
if (ctx.session === null) {
return reject()
}
if (checkPermissions(meta, ctx)) {
return next({
ctx: {
...ctx,
session: { ...ctx.session, user: ctx.session.user },
actorId: ctx.session.user.id,
skipCache: true,
},
})
}
throw new TRPCError({ code: 'UNAUTHORIZED', message: 'Insufficient permissions' })
})