2017-02-09 01:52:37 +00:00
|
|
|
'use strict'
|
2016-08-17 00:56:55 +00:00
|
|
|
|
2017-10-30 01:36:05 +00:00
|
|
|
/* global rights */
|
2017-04-02 23:56:47 +00:00
|
|
|
|
2016-08-17 00:56:55 +00:00
|
|
|
/**
|
|
|
|
* Authentication middleware
|
|
|
|
*
|
|
|
|
* @param {Express Request} req Express Request object
|
|
|
|
* @param {Express Response} res Express Response object
|
|
|
|
* @param {Function} next Next callback function
|
|
|
|
* @return {any} void
|
|
|
|
*/
|
|
|
|
module.exports = (req, res, next) => {
|
2017-02-09 01:52:37 +00:00
|
|
|
// Is user authenticated ?
|
2016-08-17 00:56:55 +00:00
|
|
|
|
2017-02-09 01:52:37 +00:00
|
|
|
if (!req.isAuthenticated()) {
|
2017-05-13 14:41:33 +00:00
|
|
|
if (req.app.locals.appconfig.public !== true) {
|
2017-04-02 23:56:47 +00:00
|
|
|
return res.redirect('/login')
|
|
|
|
} else {
|
|
|
|
req.user = rights.guest
|
|
|
|
res.locals.isGuest = true
|
|
|
|
}
|
2017-05-13 14:41:33 +00:00
|
|
|
} else {
|
2017-04-02 23:56:47 +00:00
|
|
|
res.locals.isGuest = false
|
2017-02-09 01:52:37 +00:00
|
|
|
}
|
2016-08-17 00:56:55 +00:00
|
|
|
|
2017-02-09 01:52:37 +00:00
|
|
|
// Check permissions
|
2016-08-17 00:56:55 +00:00
|
|
|
|
2017-05-13 14:41:33 +00:00
|
|
|
res.locals.rights = rights.check(req)
|
2017-04-02 23:56:47 +00:00
|
|
|
|
2017-05-13 14:41:33 +00:00
|
|
|
if (!res.locals.rights.read) {
|
|
|
|
return res.render('error-forbidden')
|
2017-02-09 01:52:37 +00:00
|
|
|
}
|
2016-11-01 02:44:00 +00:00
|
|
|
|
2017-02-09 01:52:37 +00:00
|
|
|
// Expose user data
|
2016-08-17 00:56:55 +00:00
|
|
|
|
2017-02-09 01:52:37 +00:00
|
|
|
res.locals.user = req.user
|
2016-08-17 00:56:55 +00:00
|
|
|
|
2017-02-09 01:52:37 +00:00
|
|
|
return next()
|
|
|
|
}
|