1 | const Redis = require('./libraries/redis') |
||
2 | const Constant = require('./libraries/constant') |
||
3 | const ApiError = require('./util/api_error') |
||
4 | const _ = require('underscore') |
||
5 | const ServiceAudit = require('./services/catering/audit') |
||
6 | const ServiceStore = require('./services/catering/store') |
||
7 | |||
8 | module.exports = function (permission) { |
||
9 | |||
10 | return async function (ctx, next) { |
||
11 | |||
12 | async function checkToken() { |
||
13 | let token = (typeof (ctx.request.headers.token) == 'undefined' || !ctx.request.headers.token) ? |
||
14 | ctx.cookies.get('token') : ctx.request.headers.token |
||
15 | let uid = (typeof (ctx.request.headers.uid) == 'undefined' || !ctx.request.headers.uid) ? |
||
16 | ctx.cookies.get('uid') : ctx.request.headers.uid |
||
17 | |||
18 | if (!token || !uid) { |
||
19 | console.log('token: ' + token) |
||
0 ignored issues
–
show
Debugging Code
introduced
by
![]() |
|||
20 | console.log('uid: ' + uid) |
||
21 | throw new ApiError('auth.error', 'token missing') |
||
22 | } |
||
23 | |||
24 | sessionKey = Constant.CATERING_SESSION + token |
||
0 ignored issues
–
show
|
|||
25 | session = await Redis.get(sessionKey) |
||
0 ignored issues
–
show
|
|||
26 | session = JSON.parse(session) |
||
27 | if (!session) { |
||
28 | throw new ApiError('auth.error', 'token error') |
||
29 | } |
||
30 | |||
31 | if (session.uid == uid) { |
||
32 | ctx.uid = uid |
||
33 | return true |
||
34 | } else { |
||
0 ignored issues
–
show
|
|||
35 | throw new ApiError('auth.error', 'no permission') |
||
36 | } |
||
37 | |||
38 | } |
||
39 | |||
40 | async function checkUser() { |
||
41 | await checkToken() |
||
42 | await next() |
||
43 | } |
||
44 | |||
45 | async function checkAudit() { |
||
46 | await checkToken() |
||
47 | await isAudit() |
||
48 | await next() |
||
49 | } |
||
50 | |||
51 | async function checkStore() { |
||
52 | await checkToken() |
||
53 | await ownerStore() |
||
54 | await next() |
||
55 | } |
||
56 | |||
57 | async function isAudit() { |
||
58 | let check = await ServiceAudit.getAudit(ctx.uid) |
||
59 | if (_.isEmpty(check)) throw new ApiError('auth.error', 'no permission audit') |
||
0 ignored issues
–
show
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later. Consider: if (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed. ![]() |
|||
60 | return true |
||
61 | } |
||
62 | |||
63 | async function ownerStore() { |
||
64 | let storeId = ctx.params.storeId |
||
65 | let check = await ServiceStore.getStore(storeId) |
||
66 | if (_.isEmpty(check)) throw new ApiError('auth.error', 'no permission store') |
||
0 ignored issues
–
show
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later. Consider: if (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed. ![]() |
|||
67 | if (check.seller_id != ctx.uid) throw new ApiError('auth.notPermission') |
||
0 ignored issues
–
show
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later. Consider: if (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed. ![]() |
|||
68 | return true |
||
69 | } |
||
70 | |||
71 | // 检查header |
||
72 | if (!_.has(ctx.request.headers, 'store-id')) { |
||
73 | throw new ApiError('validate.error', 'store-id') |
||
74 | } |
||
75 | if (!_.has(ctx.request.headers, 'mina-source')) { |
||
76 | throw new ApiError('validate.error', 'mina-source') |
||
77 | } |
||
78 | |||
79 | // guest |
||
80 | if (permission === 'guest') { |
||
81 | await next() |
||
82 | } else if (permission === 'user') { |
||
0 ignored issues
–
show
|
|||
83 | return await checkUser() |
||
84 | } else if (permission === 'audit') { |
||
85 | return await checkAudit() |
||
86 | } else if (permission === 'store') { |
||
87 | return await checkStore() |
||
88 | } else { |
||
89 | throw new ApiError('role.notExist') |
||
90 | } |
||
91 | |||
92 | } |
||
93 | |||
94 | } |
||
95 |