-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmiddleware.js
42 lines (39 loc) · 1.14 KB
/
middleware.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
const get = require('mout/object/get');
const errors = require('../errors');
const constants = require('../constants');
const {isMongoDB} = require('../helper');
const helper = require('./helper');
/**
* Middleware : Check Admin Role
*
* @returns {function(*, *, *)}
*/
module.exports = options => {
const AdminRoles = options.admin_roles;
const AdminUsers = options.admin_users;
const superRole = options.super_role || constants.VIRON_SUPER_ROLE;
return async (req, res, next) => {
if (!get(req, 'swagger.operation.security')) {
// 認証不要なリクエスト
return next();
}
const adminUser = isMongoDB(AdminUsers) ?
await AdminUsers.findOne({
email: get(req, 'auth.sub')
}) :
await AdminUsers.findOne({
where: {
email: get(req, 'auth.sub')
}
});
if (!adminUser) {
return next(errors.frontend.Forbidden());
}
const roles = await helper.getRoles(AdminRoles, adminUser.role_id, superRole);
if (!helper.canAccess(req.path, req.method, roles)) {
return next(errors.frontend.Forbidden());
}
req.auth.roles = roles;
next();
};
};