ui: window/AddTfaRecovery: rewrite to a Proxmox.window.Edit

we can reuse the edit window from widget toolkit for the most part
this solves some spacing and layout issues and is less code

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
This commit is contained in:
Dominik Csapak 2021-01-13 12:06:51 +01:00 committed by Thomas Lamprecht
parent 1cb89f302f
commit 884fec7735

View File

@ -1,23 +1,34 @@
Ext.define('PBS.window.AddTfaRecovery', { Ext.define('PBS.window.AddTfaRecovery', {
extend: 'Ext.window.Window', extend: 'Proxmox.window.Edit',
alias: 'widget.pbsAddTfaRecovery', alias: 'widget.pbsAddTfaRecovery',
mixins: ['Proxmox.Mixin.CBind'], mixins: ['Proxmox.Mixin.CBind'],
onlineHelp: 'user_mgmt', onlineHelp: 'user_mgmt',
isCreate: true,
modal: true, isAdd: true,
resizable: false, subject: gettext('TFA recovery keys'),
title: gettext('Add TFA recovery keys'),
width: 512, width: 512,
method: 'POST',
fixedUser: false, fixedUser: false,
baseurl: '/api2/extjs/access/tfa', url: '/api2/extjs/access/tfa',
submitUrl: function(url, values) {
let userid = values.userid;
delete values.userid;
return `${url}/${userid}`;
},
initComponent: function() { apiCallDone: function(success, response) {
let me = this; if (!success) {
me.callParent(); return;
Ext.GlobalEvents.fireEvent('proxmoxShowHelp', me.onlineHelp); }
let values = response.result.data.recovery.join("\n");
Ext.create('PBS.window.TfaRecoveryShow', {
autoShow: true,
values,
});
}, },
viewModel: { viewModel: {
@ -28,27 +39,13 @@ Ext.define('PBS.window.AddTfaRecovery', {
controller: { controller: {
xclass: 'Ext.app.ViewController', xclass: 'Ext.app.ViewController',
control: {
'#': {
show: function() {
let me = this;
let view = me.getView();
if (Proxmox.UserName === 'root@pam') {
view.lookup('password').setVisible(false);
view.lookup('password').setDisabled(true);
}
},
},
},
hasEntry: async function(userid) { hasEntry: async function(userid) {
let me = this; let me = this;
let view = me.getView(); let view = me.getView();
try { try {
await PBS.Async.api2({ await PBS.Async.api2({
url: `${view.baseurl}/${userid}/recovery`, url: `${view.url}/${userid}/recovery`,
method: 'GET', method: 'GET',
}); });
return true; return true;
@ -57,11 +54,11 @@ Ext.define('PBS.window.AddTfaRecovery', {
} }
}, },
init: function() { init: function(view) {
this.onUseridChange(null, Proxmox.UserName); this.onUseridChange(null, Proxmox.UserName);
}, },
onUseridChange: async function(_field, userid) { onUseridChange: async function(field, userid) {
let me = this; let me = this;
me.userid = userid; me.userid = userid;
@ -69,43 +66,6 @@ Ext.define('PBS.window.AddTfaRecovery', {
let has_entry = await me.hasEntry(userid); let has_entry = await me.hasEntry(userid);
me.getViewModel().set('has_entry', has_entry); me.getViewModel().set('has_entry', has_entry);
}, },
onAdd: async function() {
let me = this;
let view = me.getView();
view.mask(gettext('Please wait...'), 'x-mask-loading');
let baseurl = view.baseurl;
let userid = me.userid;
if (userid === undefined) {
throw "no userid set";
}
let params = { type: 'recovery' };
if (Proxmox.UserName !== 'root@pam') {
params.password = me.lookup('password').getValue();
}
try {
let response = await PBS.Async.api2({
url: `${baseurl}/${userid}`,
method: 'POST',
params,
});
let values = response.result.data.recovery.join("\n");
Ext.create('PBS.window.TfaRecoveryShow', {
autoShow: true,
values,
});
} catch (ex) {
Ext.Msg.alert(gettext('Error'), ex);
} finally {
view.close();
}
},
}, },
items: [ items: [
@ -119,6 +79,9 @@ Ext.define('PBS.window.AddTfaRecovery', {
editConfig: { editConfig: {
xtype: 'pbsUserSelector', xtype: 'pbsUserSelector',
allowBlank: false, allowBlank: false,
validator: function(_value) {
return !this.up('window').getViewModel().get('has_entry');
},
}, },
renderer: Ext.String.htmlEncode, renderer: Ext.String.htmlEncode,
value: Proxmox.UserName, value: Proxmox.UserName,
@ -126,11 +89,18 @@ Ext.define('PBS.window.AddTfaRecovery', {
change: 'onUseridChange', change: 'onUseridChange',
}, },
}, },
{
xtype: 'hiddenfield',
name: 'type',
value: 'recovery',
},
{ {
xtype: 'displayfield', xtype: 'displayfield',
bind: { bind: {
hidden: '{!has_entry}', hidden: '{!has_entry}',
}, },
hidden: true,
userCls: 'pmx-hint',
value: gettext('User already has recovery keys.'), value: gettext('User already has recovery keys.'),
}, },
{ {
@ -142,25 +112,11 @@ Ext.define('PBS.window.AddTfaRecovery', {
name: 'password', name: 'password',
allowBlank: false, allowBlank: false,
validateBlank: true, validateBlank: true,
padding: '0 0 5 5', hidden: Proxmox.UserName === 'root@pam',
disabled: Proxmox.UserName === 'root@pam',
emptyText: gettext('verify current password'), emptyText: gettext('verify current password'),
}, },
], ],
buttons: [
{
xtype: 'proxmoxHelpButton',
},
'->',
{
xtype: 'button',
text: gettext('Add'),
handler: 'onAdd',
bind: {
disabled: '{has_entry}',
},
},
],
}); });
Ext.define('PBS.window.TfaRecoveryShow', { Ext.define('PBS.window.TfaRecoveryShow', {