-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/user-role' into dev
- Loading branch information
Showing
8 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export default { | ||
// end user | ||
USER: 'USER', | ||
// system administrator | ||
ADMIN: 'ADMIN', | ||
// root | ||
ROOT: 'ROOT', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// ref: | ||
// - <https://github.com/ReactTraining/react-router/issues/3103> | ||
// - <https://github.com/baronswindle/react-router-compose-hooks/blob/master/index.js> | ||
|
||
export default { | ||
parallel(...hooks) { | ||
let callbacksRequired = hooks.reduce((totalCallbacks, hook) => { | ||
if (hook.length >= 3) { | ||
totalCallbacks++; | ||
} | ||
return totalCallbacks; | ||
}, 0); | ||
|
||
return function onEnter(nextState, replace, executeTransition) { | ||
let callbacksInvoked = 0; | ||
hooks.forEach((hook) => { | ||
hook.call(this, nextState, replace, () => { | ||
if (++callbacksInvoked === callbacksRequired) { | ||
executeTransition(); | ||
} | ||
}); | ||
}); | ||
if (!callbacksRequired) { | ||
executeTransition(); | ||
} | ||
}; | ||
}, | ||
|
||
series(...hooks) { | ||
return function onEnter(nextState, replace, executeTransition) { | ||
(function executeHooksSynchronously(remainingHooks) { | ||
if (!remainingHooks.length) { | ||
return executeTransition(); | ||
} | ||
let nextHook = remainingHooks[0]; | ||
if (nextHook.length >= 3) { | ||
nextHook.call(this, nextState, replace, () => { | ||
executeHooksSynchronously(remainingHooks.slice(1)); | ||
}); | ||
} else { | ||
nextHook.call(this, nextState, replace); | ||
executeHooksSynchronously(remainingHooks.slice(1)); | ||
} | ||
})(hooks); | ||
}; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export default (store) => (requiredRoles) => (nextState, replace) => { | ||
let { user } = store.getState().cookies; | ||
user = (user && JSON.parse(user)) || {}; | ||
|
||
if (!(( | ||
requiredRoles instanceof Array && | ||
requiredRoles.indexOf(user.role) >= 0 | ||
) || ( | ||
user.role === requiredRoles | ||
))) { | ||
replace({ | ||
pathname: '/', | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import Errors from '../../common/constants/Errors'; | ||
|
||
const roleRequired = (requiredRoles) => (req, res, next) => { | ||
if (( | ||
requiredRoles instanceof Array && | ||
requiredRoles.indexOf(req.user.role) >= 0 | ||
) || ( | ||
req.user.role === requiredRoles | ||
)) { | ||
next(); | ||
} else { | ||
return res.errors([Errors.PERMISSION_DENIED]); | ||
} | ||
}; | ||
|
||
export default roleRequired; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters