login-required.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. const loggerFactory = require('@alias/logger');
  2. const logger = loggerFactory('growi:middleware:login-required');
  3. /**
  4. * require login handler
  5. *
  6. * @param {boolean} isGuestAllowed whethere guest user is allowed (default false)
  7. */
  8. module.exports = (crowi, isGuestAllowed = false) => {
  9. return function(req, res, next) {
  10. // check the route config and ACL
  11. if (isGuestAllowed && crowi.aclService.isGuestAllowedToRead()) {
  12. logger.debug('Allowed to read: ', req.path);
  13. return next();
  14. }
  15. const User = crowi.model('User');
  16. // check the user logged in
  17. if (req.user != null && (req.user instanceof Object) && '_id' in req.user) {
  18. if (req.user.status === User.STATUS_ACTIVE) {
  19. // Active の人だけ先に進める
  20. return next();
  21. }
  22. if (req.user.status === User.STATUS_REGISTERED) {
  23. return res.redirect('/login/error/registered');
  24. }
  25. if (req.user.status === User.STATUS_SUSPENDED) {
  26. return res.redirect('/login/error/suspended');
  27. }
  28. if (req.user.status === User.STATUS_INVITED) {
  29. return res.redirect('/login/invited');
  30. }
  31. }
  32. // is api path
  33. const path = req.path || '';
  34. if (path.match(/^\/_api\/.+$/)) {
  35. return res.sendStatus(403);
  36. }
  37. req.session.redirectTo = req.originalUrl;
  38. return res.redirect('/login');
  39. };
  40. };