From dc57e4bc6c83d6c869c318256057e3636bfc5d66 Mon Sep 17 00:00:00 2001
From: Jo-Philipp Wich <jo@mein.io>
Date: Thu, 16 Apr 2020 15:30:44 +0200
Subject: [PATCH] luci-app-opkg: disable action buttons on insufficient ACLs

Also flush menu cache after opkg actions.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
---
 .../htdocs/luci-static/resources/view/opkg.js | 29 ++++++++++++-------
 1 file changed, 19 insertions(+), 10 deletions(-)

diff --git a/applications/luci-app-opkg/htdocs/luci-static/resources/view/opkg.js b/applications/luci-app-opkg/htdocs/luci-static/resources/view/opkg.js
index 952f83676..a5abfc168 100644
--- a/applications/luci-app-opkg/htdocs/luci-static/resources/view/opkg.js
+++ b/applications/luci-app-opkg/htdocs/luci-static/resources/view/opkg.js
@@ -85,6 +85,8 @@ var css = '								\
 	}									\
 ';
 
+var isReadonlyView = !L.hasViewPermission() || null;
+
 var callMountPoints = rpc.declare({
 	object: 'luci',
 	method: 'getMountPoints',
@@ -685,7 +687,7 @@ function handleInstall(ev)
 		errs || inst || '',
 		E('div', { 'class': 'right' }, [
 			E('label', { 'class': 'cbi-checkbox', 'style': 'float:left' }, [
-				E('input', { 'id': 'overwrite-cb', 'type': 'checkbox', 'name': 'overwrite' }), ' ',
+				E('input', { 'id': 'overwrite-cb', 'type': 'checkbox', 'name': 'overwrite', 'disabled': isReadonlyView }), ' ',
 				E('label', { 'for': 'overwrite-cb' }), ' ',
 				_('Overwrite files from other package(s)')
 			]),
@@ -698,7 +700,8 @@ function handleInstall(ev)
 				'data-command': 'install',
 				'data-package': name,
 				'class': 'btn cbi-button-action',
-				'click': handleOpkg
+				'click': handleOpkg,
+				'disabled': isReadonlyView
 			}, _('Install'))
 		])
 	]);
@@ -804,7 +807,8 @@ function handleConfig(ev)
 							ui.addNotification(null, E('p', {}, [ _('Unable to save %s: %s').format(file, err) ]));
 						});
 					})).then(ui.hideModal);
-				}
+				},
+				'disabled': isReadonlyView
 			}, _('Save')),
 		]));
 
@@ -840,8 +844,9 @@ function handleRemove(ev)
 		]),
 		desc || '',
 		E('div', { 'style': 'display:flex; justify-content:space-between; flex-wrap:wrap' }, [
-			E('label', {}, [
-				E('input', { type: 'checkbox', checked: 'checked', name: 'autoremove' }),
+			E('label', { 'class': 'cbi-checkbox', 'style': 'float:left' }, [
+				E('input', { 'id': 'autoremove-cb', 'type': 'checkbox', 'checked': 'checked', 'name': 'autoremove', 'disabled': isReadonlyView }), ' ',
+				E('label', { 'for': 'autoremove-cb' }), ' ',
 				_('Automatically remove unused dependencies')
 			]),
 			E('div', { 'style': 'flex-grow:1', 'class': 'right' }, [
@@ -854,7 +859,8 @@ function handleRemove(ev)
 					'data-command': 'remove',
 					'data-package': name,
 					'class': 'btn cbi-button-negative',
-					'click': handleOpkg
+					'click': handleOpkg,
+					'disabled': isReadonlyView
 				}, _('Remove'))
 			])
 		])
@@ -903,6 +909,9 @@ function handleOpkg(ev)
 				E('div', {
 					'class': 'btn',
 					'click': L.bind(function(res) {
+						if (ui.menu && ui.menu.flushCache)
+							ui.menu.flushCache();
+
 						ui.hideModal();
 						updateLists();
 
@@ -1026,16 +1035,16 @@ return view.extend({
 				E('div', {}, [
 					E('label', {}, _('Download and install package') + ':'),
 					E('span', { 'class': 'control-group' }, [
-						E('input', { 'type': 'text', 'name': 'install', 'placeholder': _('Package name or URL…'), 'keydown': function(ev) { if (ev.keyCode === 13) handleManualInstall(ev) } }),
-						E('button', { 'class': 'btn cbi-button cbi-button-action', 'click': handleManualInstall }, [ _('OK') ])
+						E('input', { 'type': 'text', 'name': 'install', 'placeholder': _('Package name or URL…'), 'keydown': function(ev) { if (ev.keyCode === 13) handleManualInstall(ev) }, 'disabled': isReadonlyView }),
+						E('button', { 'class': 'btn cbi-button cbi-button-action', 'click': handleManualInstall, 'disabled': isReadonlyView }, [ _('OK') ])
 					])
 				]),
 
 				E('div', {}, [
 					E('label', {}, _('Actions') + ':'), ' ',
 					E('span', { 'class': 'control-group' }, [
-						E('button', { 'class': 'btn cbi-button-positive', 'data-command': 'update', 'click': handleOpkg }, [ _('Update lists…') ]), ' ',
-						E('button', { 'class': 'btn cbi-button-action', 'click': handleUpload }, [ _('Upload Package…') ]), ' ',
+						E('button', { 'class': 'btn cbi-button-positive', 'data-command': 'update', 'click': handleOpkg, 'disabled': isReadonlyView }, [ _('Update lists…') ]), ' ',
+						E('button', { 'class': 'btn cbi-button-action', 'click': handleUpload, 'disabled': isReadonlyView }, [ _('Upload Package…') ]), ' ',
 						E('button', { 'class': 'btn cbi-button-neutral', 'click': handleConfig }, [ _('Configure opkg…') ])
 					])
 				])
-- 
2.25.1