perf: add optional 'apps' argument to user_permission_list to speed up user_info / user_list

This commit is contained in:
Alexandre Aubin 2021-04-09 21:07:35 +02:00
parent e3b90e6bdc
commit e6312db3c0
5 changed files with 19 additions and 13 deletions

View file

@ -295,6 +295,9 @@ user:
action_help: List permissions and corresponding accesses action_help: List permissions and corresponding accesses
api: GET /users/permissions api: GET /users/permissions
arguments: arguments:
apps:
help: Apps to list permission for (all by default)
nargs: "*"
-s: -s:
full: --short full: --short
help: Only list permission names help: Only list permission names

View file

@ -182,7 +182,7 @@ ynh_permission_exists() {
local permission local permission
ynh_handle_getopts_args "$@" ynh_handle_getopts_args "$@"
yunohost user permission list --output-as json --quiet \ yunohost user permission list "$app" --output-as json --quiet \
| jq -e --arg perm "$app.$permission" '.permissions[$perm]' >/dev/null | jq -e --arg perm "$app.$permission" '.permissions[$perm]' >/dev/null
} }

View file

@ -194,7 +194,7 @@ def app_info(app, full=False):
) )
local_manifest = _get_manifest_of_app(os.path.join(APPS_SETTING_PATH, app)) local_manifest = _get_manifest_of_app(os.path.join(APPS_SETTING_PATH, app))
permissions = user_permission_list(full=True, absolute_urls=True)["permissions"] permissions = user_permission_list(full=True, absolute_urls=True, apps=[app])["permissions"]
settings = _get_app_settings(app) settings = _get_app_settings(app)
@ -229,9 +229,7 @@ def app_info(app, full=False):
local_manifest.get("multi_instance", False) local_manifest.get("multi_instance", False)
) )
ret["permissions"] = { ret["permissions"] = permissions
p: i for p, i in permissions.items() if p.startswith(app + ".")
}
ret["label"] = permissions.get(app + ".main", {}).get("label") ret["label"] = permissions.get(app + ".main", {}).get("label")
if not ret["label"]: if not ret["label"]:
@ -1435,7 +1433,7 @@ def app_setting(app, key, value=None, delete=False):
permission_url, permission_url,
) )
permissions = user_permission_list(full=True)["permissions"] permissions = user_permission_list(full=True, apps=[app])["permissions"]
permission_name = "%s.legacy_%s_uris" % (app, key.split("_")[0]) permission_name = "%s.legacy_%s_uris" % (app, key.split("_")[0])
permission = permissions.get(permission_name) permission = permissions.get(permission_name)

View file

@ -46,7 +46,7 @@ SYSTEM_PERMS = ["mail", "xmpp", "sftp", "ssh"]
def user_permission_list( def user_permission_list(
short=False, full=False, ignore_system_perms=False, absolute_urls=False short=False, full=False, ignore_system_perms=False, absolute_urls=False, apps=[]
): ):
""" """
List permissions and corresponding accesses List permissions and corresponding accesses
@ -74,7 +74,9 @@ def user_permission_list(
) )
# Parse / organize information to be outputed # Parse / organize information to be outputed
apps = sorted(_installed_apps()) if apps:
ignore_system_perms = True
apps = apps if apps else sorted(_installed_apps())
apps_base_path = { apps_base_path = {
app: app_setting(app, "domain") + app_setting(app, "path") app: app_setting(app, "domain") + app_setting(app, "path")
for app in apps for app in apps
@ -85,11 +87,14 @@ def user_permission_list(
for infos in permissions_infos: for infos in permissions_infos:
name = infos["cn"][0] name = infos["cn"][0]
if ignore_system_perms and name.split(".")[0] in SYSTEM_PERMS:
continue
app = name.split(".")[0] app = name.split(".")[0]
if app in SYSTEM_PERMS:
if ignore_system_perms:
continue
elif app not in apps:
continue
perm = {} perm = {}
perm["allowed"] = [ perm["allowed"] = [
_ldap_path_extract(p, "cn") for p in infos.get("groupPermission", []) _ldap_path_extract(p, "cn") for p in infos.get("groupPermission", [])

View file

@ -862,10 +862,10 @@ def user_group_info(groupname):
# #
def user_permission_list(short=False, full=False): def user_permission_list(short=False, full=False, apps=[]):
import yunohost.permission import yunohost.permission
return yunohost.permission.user_permission_list(short, full, absolute_urls=True) return yunohost.permission.user_permission_list(short, full, absolute_urls=True, apps=apps)
def user_permission_update( def user_permission_update(