2016-04-04 12:36:30 +00:00
|
|
|
'use strict';
|
|
|
|
|
2017-06-21 00:14:14 +00:00
|
|
|
const passport = require('../lib/passport');
|
|
|
|
const router = require('../lib/router-async').create();
|
|
|
|
const _ = require('../lib/translate')._;
|
|
|
|
const users = require('../models/users');
|
|
|
|
const interoperableErrors = require('../shared/interoperable-errors');
|
|
|
|
const tools = require('../lib/tools-async');
|
|
|
|
|
|
|
|
|
|
|
|
router.all('/rest/*', (req, res, next) => {
|
|
|
|
req.needsJSONResponse = true;
|
|
|
|
|
|
|
|
if (!req.user) {
|
|
|
|
throw new interoperableErrors.NotLoggedInError();
|
|
|
|
}
|
|
|
|
|
|
|
|
next();
|
2016-04-04 12:36:30 +00:00
|
|
|
});
|
|
|
|
|
2017-06-21 00:14:14 +00:00
|
|
|
router.getAsync('/rest/users/:userId', async (req, res) => {
|
|
|
|
const user = await users.getById(req.params.userId);
|
|
|
|
return res.json(user);
|
2016-04-04 12:36:30 +00:00
|
|
|
});
|
|
|
|
|
2017-06-21 00:14:14 +00:00
|
|
|
router.postAsync('/rest/users', passport.csrfProtection, async (req, res) => {
|
|
|
|
await users.create(req.body);
|
|
|
|
return res.json();
|
2016-04-04 12:36:30 +00:00
|
|
|
});
|
|
|
|
|
2017-06-21 00:14:14 +00:00
|
|
|
router.putAsync('/rest/users/:userId', passport.csrfProtection, async (req, res) => {
|
|
|
|
const user = req.body;
|
|
|
|
user.id = parseInt(req.params.userId);
|
2016-04-04 12:36:30 +00:00
|
|
|
|
2017-06-21 00:14:14 +00:00
|
|
|
await users.updateWithConsistencyCheck(user);
|
|
|
|
return res.json();
|
|
|
|
});
|
2016-04-04 12:36:30 +00:00
|
|
|
|
2017-06-21 00:14:14 +00:00
|
|
|
router.deleteAsync('/rest/users/:userId', passport.csrfProtection, async (req, res) => {
|
|
|
|
await users.remove(req.params.userId);
|
|
|
|
return res.json();
|
2016-04-04 12:36:30 +00:00
|
|
|
});
|
|
|
|
|
2017-06-21 00:14:14 +00:00
|
|
|
router.postAsync('/rest/validate', async (req, res) => {
|
|
|
|
const data = {};
|
2016-04-04 12:36:30 +00:00
|
|
|
|
2017-06-21 00:14:14 +00:00
|
|
|
if (req.body.username) {
|
|
|
|
data.username = {};
|
2016-04-04 12:36:30 +00:00
|
|
|
|
2017-06-21 00:14:14 +00:00
|
|
|
try {
|
|
|
|
await users.getByUsername(req.body.username);
|
|
|
|
data.username.exists = true;
|
|
|
|
} catch (error) {
|
|
|
|
if (error instanceof interoperableErrors.NotFoundError) {
|
|
|
|
data.username.exists = false;
|
|
|
|
} else {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
2016-05-07 11:28:24 +00:00
|
|
|
}
|
|
|
|
|
2017-06-21 00:14:14 +00:00
|
|
|
if (req.body.email) {
|
|
|
|
data.email = {};
|
|
|
|
|
|
|
|
try {
|
|
|
|
await tools.validateEmail(req.body.email);
|
|
|
|
data.email.invalid = false;
|
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
|
|
|
data.email.invalid = true;
|
2016-05-07 11:28:24 +00:00
|
|
|
}
|
2017-06-21 00:14:14 +00:00
|
|
|
}
|
2016-05-07 11:28:24 +00:00
|
|
|
|
2017-06-21 00:14:14 +00:00
|
|
|
return res.json(data);
|
2016-05-07 11:28:24 +00:00
|
|
|
});
|
|
|
|
|
2017-06-21 00:14:14 +00:00
|
|
|
|
|
|
|
router.postAsync('/rest/usersTable', async (req, res) => {
|
|
|
|
return res.json(await users.listDTAjax(req.body));
|
2016-05-07 11:28:24 +00:00
|
|
|
});
|
|
|
|
|
2017-06-21 00:14:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
router.all('/*', (req, res, next) => {
|
2016-04-04 12:36:30 +00:00
|
|
|
if (!req.user) {
|
2017-03-07 14:30:56 +00:00
|
|
|
req.flash('danger', _('Need to be logged in to access restricted content'));
|
2016-04-04 12:36:30 +00:00
|
|
|
return res.redirect('/users/login?next=' + encodeURIComponent(req.originalUrl));
|
|
|
|
}
|
2017-06-21 00:14:14 +00:00
|
|
|
// res.setSelectedMenu('users'); FIXME
|
2016-04-04 12:36:30 +00:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
2017-06-21 00:14:14 +00:00
|
|
|
router.getAsync('/*', passport.csrfProtection, async (req, res) => {
|
|
|
|
res.render('react-root', {
|
|
|
|
title: _('Users'),
|
|
|
|
reactEntryPoint: 'users',
|
|
|
|
reactCsrfToken: req.csrfToken()
|
2016-04-04 12:36:30 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-06-21 00:14:14 +00:00
|
|
|
|
2016-04-04 12:36:30 +00:00
|
|
|
module.exports = router;
|