mirror of https://github.com/requarks/wiki
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.1 KiB
51 lines
1.1 KiB
8 years ago
|
'use strict'
|
||
8 years ago
|
|
||
8 years ago
|
/* global appdata, rights */
|
||
|
|
||
8 years ago
|
const moment = require('moment-timezone')
|
||
8 years ago
|
|
||
|
/**
|
||
|
* 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) => {
|
||
8 years ago
|
// Is user authenticated ?
|
||
8 years ago
|
|
||
8 years ago
|
if (!req.isAuthenticated()) {
|
||
8 years ago
|
if (!appdata.capabilities.guest || req.app.locals.appconfig.public !== true) {
|
||
|
return res.redirect('/login')
|
||
|
} else {
|
||
|
req.user = rights.guest
|
||
|
res.locals.isGuest = true
|
||
|
}
|
||
|
} else if (appdata.capabilities.guest) {
|
||
|
res.locals.isGuest = false
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
// Check permissions
|
||
8 years ago
|
|
||
8 years ago
|
if (appdata.capabilities.rights) {
|
||
|
res.locals.rights = rights.check(req)
|
||
|
|
||
|
if (!res.locals.rights.read) {
|
||
|
return res.render('error-forbidden')
|
||
|
}
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
// Set i18n locale
|
||
8 years ago
|
|
||
8 years ago
|
req.i18n.changeLanguage(req.user.lang)
|
||
|
res.locals.userMoment = moment
|
||
|
res.locals.userMoment.locale(req.user.lang)
|
||
8 years ago
|
|
||
8 years ago
|
// Expose user data
|
||
8 years ago
|
|
||
8 years ago
|
res.locals.user = req.user
|
||
8 years ago
|
|
||
8 years ago
|
return next()
|
||
|
}
|