[pve-devel] [PATCH v2 manager 9/9] www: add TokenView with fixed userid

Fabian Grünbichler f.gruenbichler at proxmox.com
Thu Nov 21 15:43:52 CET 2019


Signed-off-by: Fabian Grünbichler <f.gruenbichler at proxmox.com>
---
 www/manager6/Workspace.js    |  10 +++
 www/manager6/dc/TokenEdit.js |   2 +-
 www/manager6/dc/TokenView.js | 122 ++++++++++++++++++++++++++++-------
 3 files changed, 108 insertions(+), 26 deletions(-)

diff --git a/www/manager6/Workspace.js b/www/manager6/Workspace.js
index 335e7e61..b2bf142d 100644
--- a/www/manager6/Workspace.js
+++ b/www/manager6/Workspace.js
@@ -358,6 +358,16 @@ Ext.define('PVE.StdWorkspace', {
 					win.show();
 				    }
 				},
+				{
+				    text: gettext('API Tokens'),
+				    iconCls: 'fa fa-fw fa-user-o',
+				    handler: function(btn, event, rec) {
+					var win = Ext.create('PVE.window.TokenView',{
+					    userid: Proxmox.UserName
+					});
+					win.show();
+				    }
+				},
 				'-',
 				{
 				    iconCls: 'fa fa-fw fa-sign-out',
diff --git a/www/manager6/dc/TokenEdit.js b/www/manager6/dc/TokenEdit.js
index c047c175..1af299da 100644
--- a/www/manager6/dc/TokenEdit.js
+++ b/www/manager6/dc/TokenEdit.js
@@ -23,7 +23,7 @@ Ext.define('PVE.dc.TokenEdit', {
 
 	var column1 = [
 	    {
-		xtype: me.isCreate ? 'pveUserSelector' : 'displayfield',
+		xtype: me.isCreate && !me.fixedUser ? 'pveUserSelector' : 'displayfield',
 		name: 'userid',
 		fieldLabel: gettext('User'),
 		value: me.userid,
diff --git a/www/manager6/dc/TokenView.js b/www/manager6/dc/TokenView.js
index 201aac50..3a6eaad0 100644
--- a/www/manager6/dc/TokenView.js
+++ b/www/manager6/dc/TokenView.js
@@ -10,11 +10,13 @@ Ext.define('PVE.dc.TokenView', {
     stateId: 'grid-tokens',
 
     // use fixed user
-    userid: undefined,
+    fixedUser: undefined,
 
     initComponent : function() {
 	var me = this;
 
+	fixedUser = me.userid;
+
 	var caps = Ext.state.Manager.get('GuiCap');
 
 	var store = new Ext.data.Store({
@@ -33,34 +35,64 @@ Ext.define('PVE.dc.TokenView', {
 	});
 
 	var reload = function() {
-	    Proxmox.Utils.API2Request({
-		url: '/access/users/?full=1',
-		method: 'GET',
-		failure: function(response, opts) {
-		    Proxmox.Utils.setErrorMask(me, response.htmlStatus);
-		    me.load_task.delay(me.load_delay);
-		},
-		success: function(response, opts) {
-		    Proxmox.Utils.setErrorMask(me, false);
-		    var result = Ext.decode(response.responseText);
-		    var data = result.data || [];
-		    var records = [];
-		    Ext.Array.each(data, function(user) {
-			tokens = user.tokens || [];
-			Ext.Array.each(tokens, function(token) {
+	    var me = this;
+
+	    if (me.fixedUser) {
+		Proxmox.Utils.API2Request({
+		    url: '/access/users/' + encodeURIComponent(me.fixedUser) + '/token',
+		    method: 'GET',
+		    failure: function(response, opts) {
+			Proxmox.Utils.setErrorMask(me, response.htmlStatus);
+			me.load_task.delay(me.load_delay);
+		    },
+		    success: function(response, opts) {
+			Proxmox.Utils.setErrorMask(me, false);
+			var result = Ext.decode(response.responseText);
+			var data = result.data || [];
+			var records = [];
+			Ext.Array.each(data, function(token) {
 			    var r = {};
-			    r.id = user.userid + '!' + token.tokenid;
-			    r.userid = user.userid;
+			    r.id = me.fixedUser + '!' + token.tokenid;
+			    r.userid = me.fixedUser;
 			    r.tokenid = token.tokenid;
 			    r.comment = token.comment;
 			    r.expire = token.expire;
 			    r.privsep = token.privsep === 1 ? true : false;
 			    records.push(r);
 			});
-		    });
-		    store.loadData(records);
-		},
-	    });
+			store.loadData(records);
+		    },
+		});
+	    } else {
+		Proxmox.Utils.API2Request({
+		    url: '/access/users/?full=1',
+		    method: 'GET',
+		    failure: function(response, opts) {
+			Proxmox.Utils.setErrorMask(me, response.htmlStatus);
+			me.load_task.delay(me.load_delay);
+		    },
+		    success: function(response, opts) {
+			Proxmox.Utils.setErrorMask(me, false);
+			var result = Ext.decode(response.responseText);
+			var data = result.data || [];
+			var records = [];
+			Ext.Array.each(data, function(user) {
+			    tokens = user.tokens || [];
+			    Ext.Array.each(tokens, function(token) {
+				var r = {};
+				r.id = user.userid + '!' + token.tokenid;
+				r.userid = user.userid;
+				r.tokenid = token.tokenid;
+				r.comment = token.comment;
+				r.expire = token.expire;
+				r.privsep = token.privsep === 1 ? true : false;
+				records.push(r);
+			    });
+			});
+			store.loadData(records);
+		    },
+		});
+	    }
 	};
 
 	var sm = Ext.create('Ext.selection.RowModel', {});
@@ -122,7 +154,10 @@ Ext.define('PVE.dc.TokenView', {
 		handler: function() {
 		    var rec = sm.getSelection()[0];
 		    var data = {};
-		    if (rec && rec.data) {
+		    if (me.fixedUser) {
+			data.userid = me.fixedUser;
+			data.fixedUser = true;
+		    } else if (rec && rec.data) {
 			data.userid = rec.data.userid;
 		    }
 		    var win = Ext.create('PVE.dc.TokenEdit', data);
@@ -155,14 +190,16 @@ Ext.define('PVE.dc.TokenView', {
 		    width: 200,
 		    sortable: true,
 		    renderer: render_username,
-		    dataIndex: 'userid'
+		    dataIndex: 'userid',
+		    hidden: !!me.fixedUser
 		},
 		{
 		    header: gettext('Realm'),
 		    width: 100,
 		    sortable: true,
 		    renderer: render_realm,
-		    dataIndex: 'userid'
+		    dataIndex: 'userid',
+		    hidden: !!me.fixedUser
 		},
 		{
 		    header: gettext('Token name'),
@@ -198,6 +235,41 @@ Ext.define('PVE.dc.TokenView', {
 	    }
 	});
 
+	if (me.fixedUser) {
+	    reload();
+	}
+
+	me.callParent();
+    }
+});
+
+Ext.define('PVE.window.TokenView', {
+    extend: 'Ext.window.Window',
+
+    modal: true,
+    title: gettext('API Tokens'),
+    subject: gettext('API Tokens'),
+    scrollable: true,
+    layout: 'fit',
+    width: 800,
+    height: 400,
+
+    initComponent: function() {
+	var me = this;
+
+	if (!me.userid) {
+	    throw "no userid given";
+	}
+
+	Ext.apply(me, {
+	    subject: gettext('API Tokens') + ' - ' + me.userid,
+	    items: [
+		Ext.create('PVE.dc.TokenView', {
+		    fixedUser: me.userid
+		})
+	    ]
+	});
+
 	me.callParent();
     }
 });
-- 
2.20.1





More information about the pve-devel mailing list