2016-06-14 10:31:48 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const router = require('../router.js');
|
|
|
|
const api = require('../api.js');
|
2017-01-20 21:51:04 +01:00
|
|
|
const uri = require('../util/uri.js');
|
2016-06-19 19:16:40 +02:00
|
|
|
const User = require('../models/user.js');
|
2016-06-14 10:31:48 +02:00
|
|
|
const topNavigation = require('../models/top_navigation.js');
|
|
|
|
const RegistrationView = require('../views/registration_view.js');
|
2016-08-23 21:18:03 +02:00
|
|
|
const EmptyView = require('../views/empty_view.js');
|
2016-06-14 10:31:48 +02:00
|
|
|
|
|
|
|
class UserRegistrationController {
|
|
|
|
constructor() {
|
2018-02-24 05:05:58 +01:00
|
|
|
if (!api.hasPrivilege('users:create:self')) {
|
2016-08-23 21:18:03 +02:00
|
|
|
this._view = new EmptyView();
|
|
|
|
this._view.showError('Registration is closed.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-14 10:31:48 +02:00
|
|
|
topNavigation.activate('register');
|
2016-07-13 21:50:07 +02:00
|
|
|
topNavigation.setTitle('Registration');
|
2016-06-14 10:31:48 +02:00
|
|
|
this._view = new RegistrationView();
|
|
|
|
this._view.addEventListener('submit', e => this._evtRegister(e));
|
|
|
|
}
|
|
|
|
|
|
|
|
_evtRegister(e) {
|
|
|
|
this._view.clearMessages();
|
|
|
|
this._view.disableForm();
|
2016-06-19 19:16:40 +02:00
|
|
|
const user = new User();
|
|
|
|
user.name = e.detail.name;
|
|
|
|
user.email = e.detail.email;
|
|
|
|
user.password = e.detail.password;
|
2018-02-24 05:05:58 +01:00
|
|
|
const isLoggedIn = api.isLoggedIn();
|
2016-06-19 19:16:40 +02:00
|
|
|
user.save().then(() => {
|
2018-02-24 05:05:58 +01:00
|
|
|
if (isLoggedIn) {
|
|
|
|
return Promise.resolve();
|
|
|
|
} else {
|
|
|
|
api.forget();
|
|
|
|
return api.login(e.detail.name, e.detail.password, false);
|
|
|
|
}
|
2016-06-14 10:31:48 +02:00
|
|
|
}).then(() => {
|
2018-02-24 05:05:58 +01:00
|
|
|
if (isLoggedIn) {
|
|
|
|
const ctx = router.show(uri.formatClientLink('users'));
|
|
|
|
ctx.controller.showSuccess('User added!');
|
|
|
|
} else {
|
|
|
|
const ctx = router.show(uri.formatClientLink());
|
|
|
|
ctx.controller.showSuccess('Welcome aboard!');
|
|
|
|
}
|
2017-01-08 02:12:38 +01:00
|
|
|
}, error => {
|
|
|
|
this._view.showError(error.message);
|
2016-06-14 10:31:48 +02:00
|
|
|
this._view.enableForm();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = router => {
|
2017-01-20 21:51:04 +01:00
|
|
|
router.enter(['register'], (ctx, next) => {
|
2016-06-14 10:31:48 +02:00
|
|
|
new UserRegistrationController();
|
|
|
|
});
|
|
|
|
};
|