nginx-proxy-manager/src/frontend/js/app/user/form.js

107 lines
3.4 KiB
JavaScript
Raw Normal View History

'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');
require('jquery-serializejson');
module.exports = Mn.View.extend({
template: template,
className: 'modal-dialog',
ui: {
form: 'form',
buttons: '.modal-footer button',
cancel: 'button.cancel',
save: 'button.save',
error: '.secret-error'
},
events: {
2018-06-20 02:57:34 -04:00
'click @ui.save': function (e) {
e.preventDefault();
this.ui.error.hide();
let view = this;
let data = this.ui.form.serializeJSON();
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();
this.ui.buttons.prop('disabled', false).removeClass('btn-disabled');
return;
}
// 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)) {
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;
if (this.model.get('id')) {
// edit
2018-07-18 02:55:09 -04:00
method = App.Api.Users.update;
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-07-18 02:55:09 -04:00
if (view.model.get('id') !== App.Cache.User.get('id')) {
App.Controller.showUsers();
}
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);
} 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
}
});
})
.catch(err => {
this.ui.error.text(err.message).show();
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');
},
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();
}
}
});