Total Complexity | 9 |
Complexity/F | 9 |
Lines of Code | 44 |
Function Count | 1 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | import { |
||
2 | config, |
||
3 | User |
||
4 | } from '../../cli' |
||
5 | |||
6 | var middleware = function(req, res, next) { |
||
7 | if (!config.users.enable) { |
||
8 | if (req.url.indexOf('/abe/users/login') > -1) { |
||
9 | res.redirect('/abe') |
||
10 | return |
||
11 | }else { |
||
12 | next() |
||
13 | return |
||
14 | } |
||
15 | } |
||
16 | |||
17 | if( req.url.indexOf('/abe/users/forgot') > -1 || req.url.indexOf('/abe/users/login') > -1 || !/^\/abe/.test(req.url)) { |
||
18 | next() |
||
19 | return |
||
20 | } |
||
21 | |||
22 | var isHtml = /text\/html/.test(req.get('accept')) ? true : false |
||
23 | |||
24 | var decoded = User.utils.decodeUser(req, res) |
||
25 | var user = User.utils.findSync(decoded.iss) |
||
26 | |||
27 | if (User.utils.isUserAllowedOnRoute(user, req.url)) { |
||
28 | res.user = user |
||
29 | next() |
||
30 | }else { |
||
31 | if(isHtml) { |
||
32 | res.redirect('/abe/users/login') |
||
33 | }else { |
||
34 | var notAuthorized = { |
||
35 | success: 0, |
||
36 | message: 'Not authorized !' |
||
37 | } |
||
38 | res.set('Content-Type', 'application/json') |
||
39 | res.send(JSON.stringify(notAuthorized)) |
||
40 | } |
||
41 | } |
||
42 | } |
||
43 | |||
44 | export default middleware |