Passed
Push — master ( f07e23...db34f1 )
by lv
35s
created

module.exports   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
nc 3
dl 0
loc 1
c 0
b 0
f 0
cc 6
nop 2
rs 8.6666
1
const Redis = require('./libraries/redis')
0 ignored issues
show
Unused Code introduced by
The constant Redis seems to be never used. Consider removing it.
Loading history...
2
const Constant = require('./libraries/constant')
0 ignored issues
show
Unused Code introduced by
The constant Constant seems to be never used. Consider removing it.
Loading history...
3
const ApiError = require('./util/api_error')
0 ignored issues
show
Unused Code introduced by
The constant ApiError seems to be never used. Consider removing it.
Loading history...
4
const _ = require('underscore')
0 ignored issues
show
Unused Code introduced by
The constant _ seems to be never used. Consider removing it.
Loading history...
5
const ServiceAudit = require('./services/catering/audit')
0 ignored issues
show
Unused Code introduced by
The constant ServiceAudit seems to be never used. Consider removing it.
Loading history...
6
7
module.exports = function (permission) {
8
9
	return async function (ctx, next) {
0 ignored issues
show
Bug introduced by
The variable async seems to be never declared. If this is a global, consider adding a /** global: async */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
10
11
		async function checkToken() {
0 ignored issues
show
Bug introduced by
The variable async seems to be never declared. If this is a global, consider adding a /** global: async */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
12
			let token = (typeof (ctx.request.headers.token) == 'undefined' || !ctx.request.headers.token) ?
13
				ctx.cookies.get('token') : ctx.request.headers.token
14
			let uid = (typeof (ctx.request.headers.uid) == 'undefined' || !ctx.request.headers.uid) ?
15
				ctx.cookies.get('uid') : ctx.request.headers.uid
16
17
			if (!token || !uid) {
18
				console.log('token: ' + token)
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
19
				console.log('uid: ' + uid)
20
				throw new ApiError('auth.error', 'token missing')
0 ignored issues
show
Bug introduced by
The variable ApiError seems to be never declared. If this is a global, consider adding a /** global: ApiError */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
21
			}
22
23
			sessionKey = Constant.CATERING_SESSION + token
0 ignored issues
show
Bug introduced by
The variable sessionKey seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.sessionKey.
Loading history...
Bug introduced by
The variable Constant seems to be never declared. If this is a global, consider adding a /** global: Constant */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
24
			session = await Redis.get(sessionKey)
0 ignored issues
show
Bug introduced by
The variable await seems to be never declared. If this is a global, consider adding a /** global: await */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Bug introduced by
The variable session seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.session.
Loading history...
Bug introduced by
The variable Redis seems to be never declared. If this is a global, consider adding a /** global: Redis */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
25
			session = JSON.parse(session)
26
			if (!session) {
27
				throw new ApiError('auth.error', 'token error')
28
			}
29
30
			if (session.uid == uid) {
31
				ctx.uid = uid
32
				return true
33
			} else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
34
				throw new ApiError('auth.error', 'no permission')
35
			}
36
			
37
		}
38
39
		async function checkUser() {
0 ignored issues
show
introduced by
The function checkUser does not seem to be used and can be removed.
Loading history...
40
			await checkToken()
0 ignored issues
show
Bug introduced by
The variable await seems to be never declared. If this is a global, consider adding a /** global: await */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
41
			await next()
42
		}
43
44
		async function checkAudit() {
0 ignored issues
show
introduced by
The function checkAudit does not seem to be used and can be removed.
Loading history...
45
			await checkToken()
0 ignored issues
show
Bug introduced by
The variable await seems to be never declared. If this is a global, consider adding a /** global: await */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
46
			await isAudit()
47
			await next()
48
		}
49
50
		async function isAudit() {
51
			let check = await ServiceAudit.getAudit(ctx.uid)
0 ignored issues
show
Bug introduced by
The variable await seems to be never declared. If this is a global, consider adding a /** global: await */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Bug introduced by
The variable ServiceAudit seems to be never declared. If this is a global, consider adding a /** global: ServiceAudit */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
52
			if (_.isEmpty(check)) throw new ApiError('auth.error', 'no permission audit')
0 ignored issues
show
Bug introduced by
The variable _ seems to be never declared. If this is a global, consider adding a /** global: _ */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Bug introduced by
The variable ApiError seems to be never declared. If this is a global, consider adding a /** global: ApiError */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Coding Style Best Practice introduced by
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 b = 42 will always be executed, while the logging statement will be executed conditionally.

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.

Loading history...
53
			return true
54
		}
55
56
		// 检查header
57
		if (!_.has(ctx.request.headers, 'store-id')) {
0 ignored issues
show
Bug introduced by
The variable _ seems to be never declared. If this is a global, consider adding a /** global: _ */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
58
			throw new ApiError('validate.error', 'store-id')
0 ignored issues
show
Bug introduced by
The variable ApiError seems to be never declared. If this is a global, consider adding a /** global: ApiError */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
59
		}
60
		if (!_.has(ctx.request.headers, 'mina-source')) {
61
			throw new ApiError('validate.error', 'mina-source')
62
		}
63
64
		// guest
65
		if (permission === 'guest') {
66
			await next()
0 ignored issues
show
Bug introduced by
The variable await seems to be never declared. If this is a global, consider adding a /** global: await */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
67
		} else if (permission === 'user') {
68
			return await checkUser()
0 ignored issues
show
introduced by
This code is unreachable and can thus be removed without consequences.
Loading history...
69
		} else if (permission === 'audit') {
70
			return await checkAudit()
0 ignored issues
show
introduced by
This code is unreachable and can thus be removed without consequences.
Loading history...
71
		} else {
72
			throw new ApiError('role.notExist')
73
		}
74
75
	}
76
77
}
78