From 9792cfed228ca45cfd09a696be4d03a0cb2aee6a Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Tue, 8 Sep 2020 19:49:56 +0200 Subject: [PATCH] Attempt to clarify/simplify some complex pieces of code --- src/yunohost/app.py | 53 +++++++++++++++++++++----------------- src/yunohost/permission.py | 33 ++++++++++++++---------- 2 files changed, 48 insertions(+), 38 deletions(-) diff --git a/src/yunohost/app.py b/src/yunohost/app.py index 7877bef91..f66fd4299 100644 --- a/src/yunohost/app.py +++ b/src/yunohost/app.py @@ -221,13 +221,27 @@ def app_map(app=None, raw=False, user=None): "other.tld/": "bar", "sub.other.tld/pwet": "pwet", } + + When using "raw", the structure changes to : + + { + "domain.tld": { + "/foo": {"label": "App foo", "id": "foo__2"}, + "/mail": {"label": "Rainloop", "id: "rainloop"}, + }, + "other.tld": { + "/": {"label": "Bar", "id": "bar"}, + }, + "sub.other.tld": { + "/pwet": {"label": "Pwet", "id": "pwet"} + } + } """ from yunohost.permission import user_permission_list apps = [] result = {} - permissions = user_permission_list(full=True, full_path=True)["permissions"] if app is not None: if not _is_installed(app): @@ -236,6 +250,7 @@ def app_map(app=None, raw=False, user=None): else: apps = os.listdir(APPS_SETTING_PATH) + permissions = user_permission_list(full=True, full_path=True)["permissions"] for app_id in apps: app_settings = _get_app_settings(app_id) if not app_settings: @@ -258,7 +273,7 @@ def app_map(app=None, raw=False, user=None): if user not in main_perm["corresponding_users"]: continue - this_app_perms = {p: i for p, i in permissions.items() if p.startswith(app_id + ".") and (i["url"] or i['additional_urls'] != [None])} + this_app_perms = {p: i for p, i in permissions.items() if p.startswith(app_id + ".") and (i["url"] or i['additional_urls'])} for perm_name, perm_info in this_app_perms.items(): # If we're building the map for a specific user, check the user @@ -266,29 +281,22 @@ def app_map(app=None, raw=False, user=None): if user and user not in perm_info["corresponding_users"]: continue - # The challenge with this is (beside actually implementing it) - # to migrate all the legacy stuff like - # protected/unprotected/skipped uris and regexes - perm_label = perm_info['label'] - perm_all_urls = [perm_info["url"]] + perm_info['additional_urls'] + perm_all_urls = [] + (perm_info["url"] if perm_info["url"] else []) + perm_info['additional_urls'] for url in perm_all_urls: - if url is None: - # Happens when 'additional_urls' is empty !! - continue - perm_domain, perm_path = url.split("/", 1) - perm_path = '/' + perm_path - if raw: + if not raw: + result[url] = perm_label + else: + perm_domain, perm_path = url.split("/", 1) + perm_path = '/' + perm_path if perm_domain not in result: result[perm_domain] = {} result[perm_domain][perm_path] = { 'label': perm_label, 'id': app_id } - else: - result[perm_domain + perm_path] = perm_label return result @@ -1430,20 +1438,17 @@ def app_ssowatconf(): # New permission system for perm_name, perm_info in all_permissions.items(): - # Ignore permissions for which there's no url defined - if perm_info["url"] is None and perm_info['additional_urls'] == [None]: - continue - uris = [] - if perm_info['url'] is not None: - uris += [perm_info['url'].rstrip('/')] - if perm_info['additional_urls'] != [None]: - uris += [uri.rstrip('/') for uri in perm_info['additional_urls']] + uris = [] + ([perm_info['url']] if perm_info['url'] else []) + perm_info['additional_urls'] + + # Ignore permissions for which there's no url defined + if not uris: + continue permissions[perm_name] = { "users": perm_info['corresponding_users'], "label": perm_info['label'], - "show_tile": perm_info['show_tile'] if perm_info['url'] and not perm_info["url"].startswith('re:') else False, + "show_tile": perm_info['show_tile'] and perm_info['url'] and (not perm_info["url"].startswith('re:')), "auth_header": perm_info['auth_header'], "public": "visitors" in perm_info["allowed"], "uris": uris diff --git a/src/yunohost/permission.py b/src/yunohost/permission.py index baab5942d..225a5c315 100644 --- a/src/yunohost/permission.py +++ b/src/yunohost/permission.py @@ -60,7 +60,7 @@ def user_permission_list(short=False, full=False, ignore_system_perms=False, ful # Parse / organize information to be outputed apps = [app["id"] for app in app_list()["apps"]] - apps_main_path = {app: app_setting(app, 'domain') + app_setting(app, 'path') + apps_base_path = {app: app_setting(app, 'domain') + app_setting(app, 'path') for app in apps if app_setting(app, 'domain') and app_setting(app, 'path')} @@ -73,21 +73,24 @@ def user_permission_list(short=False, full=False, ignore_system_perms=False, ful app = name.split('.')[0] - permissions[name] = {} - permissions[name]["allowed"] = [_ldap_path_extract(p, "cn") for p in infos.get('groupPermission', [])] + perm = {} + perm["allowed"] = [_ldap_path_extract(p, "cn") for p in infos.get('groupPermission', [])] if full: - permissions[name]["corresponding_users"] = [_ldap_path_extract(p, "uid") for p in infos.get('inheritPermission', [])] - permissions[name]["auth_header"] = infos.get("authHeader", [False])[0] == "TRUE" - permissions[name]["label"] = infos.get("label", [None])[0] - permissions[name]["show_tile"] = infos.get("showTile", [False])[0] == "TRUE" - permissions[name]["protected"] = infos.get("isProtected", [False])[0] == "TRUE" - if full_path and app in apps_main_path: - permissions[name]["url"] = _get_absolute_url(infos["URL"][0], apps_main_path[app]) if "URL" in infos else None - permissions[name]["additional_urls"] = [_get_absolute_url(url, apps_main_path[app]) for url in infos.get("additionalUrls", [None]) if url] - else: - permissions[name]["url"] = infos.get("URL", [None])[0] - permissions[name]["additional_urls"] = infos.get("additionalUrls", []) + perm["corresponding_users"] = [_ldap_path_extract(p, "uid") for p in infos.get('inheritPermission', [])] + perm["auth_header"] = infos.get("authHeader", [False])[0] == "TRUE" + perm["label"] = infos.get("label", [None])[0] + perm["show_tile"] = infos.get("showTile", [False])[0] == "TRUE" + perm["protected"] = infos.get("isProtected", [False])[0] == "TRUE" + perm["url"] = infos.get("URL", [None])[0] + perm["additional_urls"] = infos.get("additionalUrls", []) + + if full_path: + app_base_path = apps_base_path[app] + perm["url"] = _get_absolute_url(perm["url"], app_base_path) + perm["additional_urls"] = [_get_absolute_url(url, apps_base_path) for url in perm["additional_urls"]] + + permissions[name] = perm if short: permissions = permissions.keys() @@ -632,6 +635,8 @@ def _get_absolute_url(url, base_path): # (re:/foo.*, domain.tld/app) into re:domain\.tld/app/foo.* # (domain.tld/bar, domain.tld/app) into domain.tld/bar # + if url is None: + return None if url.startswith('/'): return base_path + url.rstrip("/") if url.startswith('re:/'):