2018-06-20 02:51:18 -04:00
|
|
|
'use strict';
|
|
|
|
|
2018-07-18 02:55:09 -04:00
|
|
|
const Mn = require('backbone.marionette');
|
|
|
|
const App = require('../main');
|
|
|
|
const UserModel = require('../../models/user');
|
|
|
|
const template = require('./form.ejs');
|
2018-06-20 02:51:18 -04:00
|
|
|
|
|
|
|
require('jquery-serializejson');
|
|
|
|
|
|
|
|
module.exports = Mn.View.extend({
|
|
|
|
template: template,
|
|
|
|
className: 'modal-dialog',
|
|
|
|
|
|
|
|
ui: {
|
|
|
|
form: 'form',
|
|
|
|
buttons: '.modal-footer button',
|
|
|
|
cancel: 'button.cancel',
|
2018-06-25 21:56:10 -04:00
|
|
|
save: 'button.save',
|
|
|
|
error: '.secret-error'
|
2018-06-20 02:51:18 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
events: {
|
|
|
|
|
2018-06-20 02:57:34 -04:00
|
|
|
'click @ui.save': function (e) {
|
2018-06-20 02:51:18 -04:00
|
|
|
e.preventDefault();
|
2018-06-25 21:56:10 -04:00
|
|
|
this.ui.error.hide();
|
2018-06-20 02:51:18 -04:00
|
|
|
let view = this;
|
|
|
|
let data = this.ui.form.serializeJSON();
|
|
|
|
|
2018-07-16 03:00:52 -04:00
|
|
|
let show_password = this.model.get('email') === 'admin@example.com';
|
|
|
|
|
|
|
|
// admin@example.com is not allowed
|
|
|
|
if (data.email === 'admin@example.com') {
|
2018-07-18 02:55:09 -04:00
|
|
|
this.ui.error.text(App.i18n('users', 'default_error')).show();
|
2018-07-16 03:00:52 -04:00
|
|
|
this.ui.buttons.prop('disabled', false).removeClass('btn-disabled');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-20 02:51:18 -04:00
|
|
|
// Manipulate
|
|
|
|
data.roles = [];
|
2018-07-18 02:55:09 -04:00
|
|
|
if ((this.model.get('id') === App.Cache.User.get('id') && this.model.isAdmin()) || (typeof data.is_admin !== 'undefined' && data.is_admin)) {
|
2018-06-20 02:51:18 -04:00
|
|
|
data.roles.push('admin');
|
|
|
|
delete data.is_admin;
|
|
|
|
}
|
|
|
|
|
|
|
|
data.is_disabled = typeof data.is_disabled !== 'undefined' ? !!data.is_disabled : false;
|
|
|
|
this.ui.buttons.prop('disabled', true).addClass('btn-disabled');
|
2018-07-18 02:55:09 -04:00
|
|
|
let method = App.Api.Users.create;
|
2018-06-20 02:51:18 -04:00
|
|
|
|
|
|
|
if (this.model.get('id')) {
|
|
|
|
// edit
|
2018-07-18 02:55:09 -04:00
|
|
|
method = App.Api.Users.update;
|
2018-06-20 02:51:18 -04:00
|
|
|
data.id = this.model.get('id');
|
|
|
|
}
|
|
|
|
|
|
|
|
method(data)
|
|
|
|
.then(result => {
|
2018-07-18 02:55:09 -04:00
|
|
|
if (result.id === App.Cache.User.get('id')) {
|
|
|
|
App.Cache.User.set(result);
|
2018-06-20 02:51:18 -04:00
|
|
|
}
|
|
|
|
|
2018-07-18 02:55:09 -04:00
|
|
|
if (view.model.get('id') !== App.Cache.User.get('id')) {
|
|
|
|
App.Controller.showUsers();
|
2018-06-20 02:51:18 -04:00
|
|
|
}
|
2018-06-20 02:57:34 -04:00
|
|
|
|
|
|
|
view.model.set(result);
|
2018-07-24 03:16:11 -04:00
|
|
|
App.UI.closeModal(function () {
|
2018-07-18 02:55:09 -04:00
|
|
|
if (method === App.Api.Users.create) {
|
2018-07-04 18:27:25 -04:00
|
|
|
// Show permissions dialog immediately
|
2018-07-18 02:55:09 -04:00
|
|
|
App.Controller.showUserPermissions(view.model);
|
2018-07-16 03:00:52 -04:00
|
|
|
} else if (show_password) {
|
2018-07-18 02:55:09 -04:00
|
|
|
App.Controller.showUserPasswordForm(view.model);
|
2018-07-04 18:27:25 -04:00
|
|
|
}
|
|
|
|
});
|
2018-06-20 02:51:18 -04:00
|
|
|
})
|
|
|
|
.catch(err => {
|
2018-06-25 21:56:10 -04:00
|
|
|
this.ui.error.text(err.message).show();
|
2018-06-20 02:51:18 -04:00
|
|
|
this.ui.buttons.prop('disabled', false).removeClass('btn-disabled');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
templateContext: function () {
|
|
|
|
let view = this;
|
|
|
|
|
|
|
|
return {
|
|
|
|
isSelf: function () {
|
2018-07-18 02:55:09 -04:00
|
|
|
return view.model.get('id') === App.Cache.User.get('id');
|
2018-06-20 02:51:18 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
isAdmin: function () {
|
|
|
|
return view.model.isAdmin();
|
|
|
|
},
|
|
|
|
|
|
|
|
isDisabled: function () {
|
|
|
|
return view.model.isDisabled();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
initialize: function (options) {
|
|
|
|
if (typeof options.model === 'undefined' || !options.model) {
|
|
|
|
this.model = new UserModel.Model();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|