adejs/controllers/settings/views.js

72 lines
2.2 KiB
JavaScript

const getUrl = require('create-url').getUrl;
const User = require('auth/models');
const model = require('model');
const redirectIfNotLogged = require('auth/views').redirectIfNotLogged;
let SETTINGS_ERROR = {
INCORRECT_PASSWORD: 'Your password is incorrect.',
};
module.exports.accountSettings = redirectIfNotLogged('accountSettings', function(req, res, render) {
if (req.session.settingsFailed !== undefined) {
res.locals.settingsFailed = req.session.settingsFailed;
req.session.settingsFailed = undefined;
req.session.save();
}
render('settings.pug');
});
module.exports.calendarSettings = redirectIfNotLogged('calendarSettings', function(req, res, render) {
if (req.session.settingsFailed !== undefined) {
res.locals.settingsFailed = req.session.settingsFailed;
req.session.settingsFailed = undefined;
req.session.save();
}
res.locals.calendar = true;
render('settings.pug');
});
module.exports.accountSettingsTarget = function(req, res, render) {
User.getById(req.session.user.id, (err, user) => {
if (!model.PasswordField.testSync(req.body.oldpassword, user.password)) {
req.session.settingsFailed = SETTINGS_ERROR.INCORRECT_PASSWORD;
req.session.save();
return res.redirect(getUrl('settings'));
}
if (req.body.newpassword) {
req.session.settingsFailed = false;
req.session.save();
user.password = req.body.newpassword;
user.save(() => {
return res.redirect(getUrl('accountSettings'));
});
}
});
}
module.exports.calendarSettingsTarget = function(req, res, render) {
let projectId = parseInt(req.body.projectId, 10);
let resources = parseInt(req.body.resources, 10);
if (isNaN(projectId) || isNaN(resources)) {
return res.redirect(getUrl("settings"));
}
User.getById(req.session.user.id, (err, user) => {
user.projectId = projectId;
user.resources = resources;
user.save(() => {
req.session.user = user;
req.session.settingsFailed = false;
req.session.save();
res.redirect(getUrl('calendarSettings'));
});
});
}