From 8e98e49d330132bde1a16fbb990de9e46bf1d3d0 Mon Sep 17 00:00:00 2001 From: Aeris One Date: Tue, 31 Mar 2020 15:39:07 +0200 Subject: [PATCH] Migration using experimental helper (#14) * Create _common.sh * Use experimental helper in upgrade --- scripts/_common.sh | 52 ++++++++++++++++++++++++++++++++++++++++++++++ scripts/upgrade | 13 ++++++++---- 2 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 scripts/_common.sh diff --git a/scripts/_common.sh b/scripts/_common.sh new file mode 100644 index 0000000..a652bbf --- /dev/null +++ b/scripts/_common.sh @@ -0,0 +1,52 @@ +#!/bin/bash + +#================================================= +# COMMON VARIABLES +#================================================= + +#================================================= +# PERSONAL HELPERS +#================================================= + +#================================================= +# EXPERIMENTAL HELPERS +#================================================= + +#================================================= +# FUTURE OFFICIAL HELPERS +#================================================= + +# Check if a permission exists +# +# While waiting for this new helper https://github.com/YunoHost/yunohost/pull/905 +# We have to use another one because the new helper use a new YunoHost command, not available for now. +# +# usage: ynh_permission_has_user --permission=permission --user=user +# | arg: -p, --permission - the permission to check +# | arg: -u, --user - the user seek in the permission +# +# example: ynh_permission_has_user --permission=main --user=visitors +# +# Requires YunoHost version 3.7.1 or higher. +ynh_permission_has_user() { + local legacy_args=pu + # Declare an array to define the options of this helper. + declare -Ar args_array=( [p]=permission= [u]=user= ) + local permission + local user + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + if ! ynh_permission_exists --permission=$permission + then + return 1 + fi + + # List all permissions + # Filter only the required permission with a multiline sed (Here a cut from the permission to the next one), remove the url and his value + perm="$(yunohost user permission list --full --output-as plain | sed --quiet "/^#$app.$permission/,/^#[[:alnum:]]/p" | sed "/^##url/,+1d")" + # Remove all lines starting by # (got from the plain output before) + allowed_users="$(echo "$perm" | grep --invert-match '^#')" + # Grep the list of users an return the result if the user is indeed into the list + echo "$allowed_users" | grep --quiet --word "$user" +} diff --git a/scripts/upgrade b/scripts/upgrade index 2dcb016..23b240d 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -6,6 +6,7 @@ # IMPORT GENERIC HELPERS #================================================= +source _common.sh source /usr/share/yunohost/helpers #================================================= @@ -37,14 +38,18 @@ if [ -z "$final_path" ]; then fi # Migrate from legacy permission system - is_public=$(ynh_app_setting_get --app=$app --key=is_public) if [ -n "$is_public" ]; then - ynh_app_setting_delete --app=$app --key=skipped_uris - if [ $is_public -eq 1 ]; then + # Removing skipped/unprotected_uris under certain conditions, remove the visitors group added during the migration process of 3.7 + # Remove skipped_uris. If the app was public, add visitors again to the main permission + if ynh_permission_has_user --permission=main --user=visitors + then + ynh_app_setting_delete --app=$app --key=skipped_uris ynh_permission_update --permission "main" --add "visitors" - fi + else + ynh_app_setting_delete --app=$app --key=skipped_uris + fi ynh_app_setting_delete --app=$app --key=is_public fi