]> git.proxmox.com Git - pmg-gui.git/blame - js/UserView.js
add Unlock TFA button
[pmg-gui.git] / js / UserView.js
CommitLineData
ec1dd829
DM
1Ext.define('pmg-users', {
2 extend: 'Ext.data.Model',
3 fields: [
c87d46fb 4 'userid', 'firstname', 'lastname', 'email', 'comment',
4195a809 5 'role', 'keys', 'realm', 'totp-lock',
ec1dd829 6 { type: 'boolean', name: 'enable' },
c87d46fb 7 { type: 'date', dateFormat: 'timestamp', name: 'expire' },
ec1dd829
DM
8 ],
9 proxy: {
10 type: 'proxmox',
c87d46fb 11 url: "/api2/json/access/users",
ec1dd829 12 },
c87d46fb 13 idProperty: 'userid',
ec1dd829
DM
14});
15
16Ext.define('PMG.UserView', {
17 extend: 'Ext.grid.GridPanel',
18 alias: 'widget.pmgUserView',
19
8f5de6bf
DM
20 store: {
21 autoLoad: true,
22 model: 'pmg-users',
23 sorters: [
24 {
25 property: 'realm',
c87d46fb 26 direction: 'ASC',
8f5de6bf
DM
27 },
28 {
29 property: 'userid',
c87d46fb
TL
30 direction: 'ASC',
31 },
32 ],
8f5de6bf
DM
33 },
34
4468e69c 35 controller: {
ec1dd829 36
4468e69c 37 xclass: 'Ext.app.ViewController',
ec1dd829 38
e93402ee
DM
39 init: function(view) {
40 Proxmox.Utils.monStoreErrors(view, view.store);
41 },
42
4468e69c 43 renderUsername: function(userid) {
fb511cb2 44 return Ext.htmlEncode(userid.match(/^(.+)(@[^@]+)$/)[1]);
4468e69c 45 },
ec1dd829 46
4468e69c
DM
47 renderFullName: function(firstname, metaData, record) {
48 var first = firstname || '';
49 var last = record.data.lastname || '';
fb511cb2 50 return Ext.htmlEncode(first + " " + last);
4468e69c 51 },
ec1dd829 52
4468e69c
DM
53 onAdd: function() {
54 var view = this.getView();
55
56 var win = Ext.create('PMG.UserEdit', {});
131ba4f6 57 win.on('destroy', function() { view.reload(); });
4468e69c
DM
58 win.show();
59 },
60
61 onEdit: function() {
62 var view = this.getView();
ec1dd829 63
4468e69c 64 var rec = view.selModel.getSelection()[0];
ec1dd829
DM
65
66 var win = Ext.create('PMG.UserEdit', {
c87d46fb 67 userid: rec.data.userid,
ec1dd829 68 });
131ba4f6 69 win.on('destroy', function() { view.reload(); });
ec1dd829 70 win.show();
4468e69c
DM
71 },
72
73 onPassword: function(btn, event, rec) {
74 var view = this.getView();
ec1dd829 75
c87d46fb
TL
76 var win = Ext.create('Proxmox.window.PasswordEdit', {
77 userid: rec.data.userid,
4468e69c 78 });
131ba4f6 79 win.on('destroy', function() { view.reload(); });
4468e69c
DM
80 win.show();
81 },
82
83 onAfterRemove: function(btn, res) {
84 var view = this.getView();
85 view.reload();
c87d46fb 86 },
1533ccff
WB
87
88 onUnlockTfa: function(btn, event, rec) {
89 let me = this;
90 let view = me.getView();
91 Ext.Msg.confirm(
92 Ext.String.format(gettext('Unlock TFA authentication for {0}'), rec.data.userid),
93 gettext("Locked 2nd factors can happen if the user's password was leaked. Are you sure you want to unlock the user?"),
94 function(btn_response) {
95 if (btn_response === 'yes') {
96 Proxmox.Utils.API2Request({
97 url: `/access/users/${rec.data.userid}/unlock-tfa`,
98 waitMsgTarget: view,
99 method: 'PUT',
100 failure: function(response, options) {
101 Ext.Msg.alert(gettext('Error'), response.htmlStatus);
102 },
103 success: function(response, options) {
104 view.reload();
105 },
106 });
107 }
108 },
109 );
110 },
4468e69c
DM
111 },
112
113 listeners: {
114 scope: 'controller',
c87d46fb 115 itemdblclick: 'onEdit',
4468e69c
DM
116 },
117
118 tbar: [
119 {
120 text: gettext('Add'),
121 reference: 'addBtn',
c87d46fb 122 handler: 'onAdd',
4468e69c
DM
123 },
124 {
125 xtype: 'proxmoxButton',
ec1dd829
DM
126 text: gettext('Edit'),
127 disabled: true,
c87d46fb 128 handler: 'onEdit',
4468e69c
DM
129 },
130 {
131 xtype: 'proxmoxStdRemoveButton',
132 baseurl: '/access/users',
133 reference: 'removeBtn',
134 callback: 'onAfterRemove',
c87d46fb 135 waitMsgTarget: true,
4468e69c
DM
136 },
137 {
138 xtype: 'proxmoxButton',
ec1dd829
DM
139 text: gettext('Password'),
140 disabled: true,
c87d46fb
TL
141 handler: 'onPassword',
142 },
1533ccff
WB
143 '-',
144 {
145 xtype: 'proxmoxButton',
146 text: gettext('Unlock TFA'),
147 handler: 'onUnlockTfa',
148 enableFn: ({ data }) =>
149 data['totp-locked'] || (data['tfa-locked-until'] > (new Date().getTime() / 1000)),
150 },
4468e69c 151 ],
ec1dd829 152
4468e69c
DM
153 columns: [
154 {
155 header: gettext('User name'),
156 width: 200,
157 sortable: true,
158 renderer: 'renderUsername',
c87d46fb 159 dataIndex: 'userid',
4468e69c
DM
160 },
161 {
162 header: gettext('Realm'),
163 width: 100,
164 sortable: true,
c87d46fb 165 dataIndex: 'realm',
4468e69c 166 },
7818f0a3
DM
167 {
168 header: gettext('Role'),
169 width: 150,
170 sortable: true,
171 renderer: PMG.Utils.format_user_role,
c87d46fb 172 dataIndex: 'role',
7818f0a3 173 },
4468e69c
DM
174 {
175 header: gettext('Enabled'),
176 width: 80,
177 sortable: true,
178 renderer: Proxmox.Utils.format_boolean,
c87d46fb 179 dataIndex: 'enable',
4468e69c
DM
180 },
181 {
182 header: gettext('Expire'),
183 width: 80,
184 sortable: true,
185 renderer: Proxmox.Utils.format_expire,
c87d46fb 186 dataIndex: 'expire',
4468e69c
DM
187 },
188 {
189 header: gettext('Name'),
190 width: 150,
191 sortable: true,
192 renderer: 'renderFullName',
c87d46fb 193 dataIndex: 'firstname',
4468e69c 194 },
4195a809
WB
195 {
196 header: gettext('TFA Lock'),
197 width: 120,
198 sortable: true,
199 dataIndex: 'totp-locked',
200 renderer: function(v, metaData, record) {
201 let locked_until = record.data['tfa-locked-until'];
202 if (locked_until !== undefined) {
203 let now = new Date().getTime() / 1000;
204 if (locked_until > now) {
205 return gettext('Locked');
206 }
207 }
208
209 if (record.data['totp-locked']) {
210 return gettext('TOTP Locked');
211 }
212
213 return Proxmox.Utils.noText;
214 },
215 },
4468e69c
DM
216 {
217 header: gettext('Comment'),
218 sortable: false,
219 renderer: Ext.String.htmlEncode,
220 dataIndex: 'comment',
c87d46fb
TL
221 flex: 1,
222 },
4468e69c 223 ],
ec1dd829 224
4468e69c
DM
225 reload: function() {
226 var me = this;
ec1dd829 227
4468e69c 228 me.store.load();
c87d46fb 229 },
ec1dd829 230});