forked from DavidMagda/CaSMM_fork_2023
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpermissions.js
135 lines (116 loc) · 3.25 KB
/
permissions.js
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
const _ = require('lodash');
module.exports = async (ctx, next) => {
let role;
if (ctx.state.user) {
// request is already authenticated in a different way
return next();
}
if (ctx.request && ctx.request.header && ctx.request.header.authorization) {
try {
const {
id,
ids,
isAdmin = false,
isStudent = false,
session = null,
} = await strapi.plugins['users-permissions'].services.jwt.getToken(ctx);
if (id === undefined && ids === undefined) {
throw new Error('Invalid token: Token did not contain required fields');
}
if (isAdmin) {
ctx.state.admin = await strapi
.query('administrator', 'admin')
.findOne({ id }, []);
} else if (isStudent) {
//
// students do not use the traditional local authentication
// their tokens are issued in session.join
// we spoof their user object so we can use roles
//
const role = await strapi
.query('role', 'users-permissions')
.findOne({ name: 'Student' }, []);
ctx.state.user = role ? { ids, session, isStudent, role } : undefined;
} else {
ctx.state.user = await strapi
.query('user', 'users-permissions')
.findOne({ id }, ['role']);
}
} catch (err) {
return handleErrors(ctx, err, 'unauthorized');
}
if (ctx.state.admin) {
if (ctx.state.admin.blocked === true) {
return handleErrors(
ctx,
'Your account has been blocked by the administrator.',
'unauthorized'
);
}
ctx.state.user = ctx.state.admin;
return await next();
}
if (!ctx.state.user) {
return handleErrors(ctx, 'User Not Found', 'unauthorized');
}
role = ctx.state.user.role;
if (role.type === 'root') {
return await next();
}
const store = await strapi.store({
environment: '',
type: 'plugin',
name: 'users-permissions',
});
if (
_.get(await store.get({ key: 'advanced' }), 'email_confirmation') &&
!ctx.state.user.confirmed
) {
return handleErrors(
ctx,
'Your account email is not confirmed.',
'unauthorized'
);
}
if (ctx.state.user.blocked) {
return handleErrors(
ctx,
'Your account has been blocked by the administrator.',
'unauthorized'
);
}
}
// Retrieve `public` role.
if (!role) {
role = await strapi
.query('role', 'users-permissions')
.findOne({ type: 'public' }, []);
}
const route = ctx.request.route;
const permission = await strapi
.query('permission', 'users-permissions')
.findOne(
{
role: role.id,
type: route.plugin || 'application',
controller: route.controller,
action: route.action,
enabled: true,
},
[]
);
if (!permission) {
return handleErrors(ctx, undefined, 'forbidden');
}
// Execute the policies.
if (permission.policy) {
return await strapi.plugins['users-permissions'].config.policies[
permission.policy
](ctx, next);
}
// Execute the action.
await next();
};
const handleErrors = (ctx, err = undefined, type) => {
throw strapi.errors[type](err);
};