Skip to content

Commit 249e206

Browse files
committed
#342 fixed not showing scripts in admin panel, if they are not allowed
1 parent 3453103 commit 249e206

4 files changed

Lines changed: 36 additions & 8 deletions

File tree

src/config/config_service.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,11 @@ def _save_config(self, config, path):
9292
config_json = json.dumps(sorted_config, indent=2)
9393
file_utils.write_file(path, config_json)
9494

95-
def list_configs(self, user):
95+
def list_configs(self, user, mode=None):
96+
edit_mode = mode == 'edit'
97+
if edit_mode:
98+
self._check_admin_access(user)
99+
96100
conf_service = self
97101

98102
def load_script(path, content):
@@ -103,7 +107,7 @@ def load_script(path, content):
103107
if short_config is None:
104108
return None
105109

106-
if not conf_service._can_access_script(user, short_config):
110+
if (not edit_mode) and (not conf_service._can_access_script(user, short_config)):
107111
return None
108112

109113
return short_config
@@ -194,7 +198,7 @@ def _can_access_script(self, user, short_config):
194198

195199
def _check_admin_access(self, user):
196200
if not self._authorizer.is_admin(user.user_id):
197-
raise AdminAccessRequiredException('Access to script is prohibited for ' + str(user))
201+
raise AdminAccessRequiredException('Admin access to scripts is prohibited for ' + str(user))
198202

199203

200204
class ConfigNotAllowedException(Exception):

src/tests/config_service_test.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,27 @@ def test_list_configs_when_none_allowed(self):
126126

127127
self.assert_list_config_names(self.user1, [])
128128

129+
def test_list_configs_when_edit_mode_and_admin(self):
130+
_create_script_config_file('a1', allowed_users=['adm_user'])
131+
_create_script_config_file('c2', allowed_users=['adm_user'])
132+
133+
self.assert_list_config_names(self.admin_user, ['a1', 'c2'], mode='edit')
134+
135+
def test_list_configs_when_edit_mode_and_admin_without_allowance(self):
136+
_create_script_config_file('a1', allowed_users=['user1'])
137+
_create_script_config_file('c2', allowed_users=['adm_user'])
138+
139+
self.assert_list_config_names(self.admin_user, ['a1', 'c2'], mode='edit')
140+
141+
def test_list_configs_when_edit_mode_and_non_admin(self):
142+
_create_script_config_file('a1', allowed_users=['user1'])
143+
_create_script_config_file('c2', allowed_users=['user1'])
144+
145+
self.assertRaises(AdminAccessRequiredException,
146+
self.config_service.list_configs,
147+
self.user1,
148+
'edit')
149+
129150
def test_load_config_when_user_allowed(self):
130151
_create_script_config_file('my_script', allowed_users=['ABC', 'user1', 'qwerty'])
131152

@@ -138,8 +159,8 @@ def test_load_config_when_user_not_allowed(self):
138159

139160
self.assertRaises(ConfigNotAllowedException, self.config_service.load_config_model, 'my_script', self.user1)
140161

141-
def assert_list_config_names(self, user, expected_names):
142-
configs = self.config_service.list_configs(user)
162+
def assert_list_config_names(self, user, expected_names, mode=None):
163+
configs = self.config_service.list_configs(user, mode)
143164
conf_names = [config.name for config in configs]
144165
self.assertCountEqual(expected_names, conf_names)
145166

@@ -151,8 +172,9 @@ def setUp(self):
151172
super().setUp()
152173
test_utils.setup()
153174

154-
authorizer = Authorizer([], [], [], EmptyGroupProvider())
175+
authorizer = Authorizer([], ['adm_user'], [], EmptyGroupProvider())
155176
self.user1 = User('user1', {})
177+
self.admin_user = User('adm_user', {})
156178
self.config_service = ConfigService(authorizer, test_utils.temp_folder)
157179

158180

src/web/server.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,9 @@ class GetScripts(BaseRequestHandler):
209209
@check_authorization
210210
@inject_user
211211
def get(self, user):
212-
configs = self.application.config_service.list_configs(user)
212+
mode = self.get_query_argument('mode', default=None)
213+
214+
configs = self.application.config_service.list_configs(user, mode)
213215

214216
scripts = [{'name': conf.name, 'group': conf.group} for conf in configs]
215217

web-src/src/admin/store/scripts-module.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export default {
1313
init({commit}) {
1414
commit('SET_LOADING', true);
1515

16-
axiosInstance.get('scripts').then(({data}) => {
16+
axiosInstance.get('scripts', {params: {mode: 'edit'}}).then(({data}) => {
1717
const {scripts} = data;
1818
let scriptNames = scripts.map(s => s.name);
1919
scriptNames.sort(function (name1, name2) {

0 commit comments

Comments
 (0)