From 7934b9a5e9201575845f1edf04d4cb2810c16ca3 Mon Sep 17 00:00:00 2001 From: tituspijean Date: Tue, 18 Apr 2023 17:40:47 +0200 Subject: [PATCH] Use permissions hooks to create/enable/disable users --- hooks/post_app_addaccess | 37 +++++++++++++++++++++++++++++++++++++ hooks/post_app_removeaccess | 28 ++++++++++++++++++++++++++++ hooks/post_user_create | 25 ------------------------- hooks/post_user_delete | 17 ----------------- scripts/install | 21 +++------------------ scripts/upgrade | 29 +++++++---------------------- 6 files changed, 75 insertions(+), 82 deletions(-) create mode 100644 hooks/post_app_addaccess create mode 100644 hooks/post_app_removeaccess delete mode 100644 hooks/post_user_create delete mode 100644 hooks/post_user_delete diff --git a/hooks/post_app_addaccess b/hooks/post_app_addaccess new file mode 100644 index 0000000..521614d --- /dev/null +++ b/hooks/post_app_addaccess @@ -0,0 +1,37 @@ +#!/bin/bash +app=$1 + +# Source YunoHost helpers +source /usr/share/yunohost/helpers +source /etc/yunohost/apps/$app/scripts/_common.sh + +# Retrieve arguments +usernames=$2 +permission=$3 +groups=$4 +final_path=$(ynh_app_setting_get "$app" final_path) +phpversion=$(ynh_app_setting_get "$app" phpversion) + +IFS=',' read -r -a user_list <<< "$usernames" +IFS=',' read -r -a group_list <<< "$groups" + +for group in "${group_list[@]}" +do + group_array=$(yunohost user group list --output-as json --quiet | jq -r --arg group "$group" ".groups.$group.members | .[]") + user_list+=("${group_array[@]}") +done + +pushd $final_path + for user in "${user_list[@]}" + do + mail=$(ynh_user_get_info --username="$user" --key=mail) + user_exists=$(ynh_exec_as $app php$phpversion bin/console aeneria:user:exist "$mail") + if [ $user_exists -eq 0 ] + then + user_pass=$(ynh_string_random) + ynh_exec_as $app php$phpversion bin/console aeneria:user:add "$mail" "$user_pass" -n + else + ynh_exec_as $app php$phpversion bin/console aeneria:user:activate "$mail" + fi + done +popd diff --git a/hooks/post_app_removeaccess b/hooks/post_app_removeaccess new file mode 100644 index 0000000..1cd9c1c --- /dev/null +++ b/hooks/post_app_removeaccess @@ -0,0 +1,28 @@ +#!/bin/bash +app=$1 + +# Source YunoHost helpers +source /usr/share/yunohost/helpers +source /etc/yunohost/apps/$app/scripts/_common.sh + +# Retrieve arguments +usernames=$2 +permission=$3 +groups=$4 +final_path=$(ynh_app_setting_get "$app" final_path) +phpversion=$(ynh_app_setting_get "$app" phpversion) + +IFS=',' read -r -a user_list <<< "$usernames" +IFS=',' read -r -a group_list <<< "$groups" + +for group in "${group_list[@]}" +do + group_array=$(yunohost user group list --output-as json --quiet | jq -r --arg group "$group" ".groups.$group.members | .[]") + user_list+=("${group_array[@]}") +done + +for user in "${user_list[@]}" +do + mail=$(ynh_user_get_info --username="$user" --key=mail) + cd "$final_path" && ynh_exec_as $app php$phpversion bin/console aeneria:user:deactivate "$mail" -n +done diff --git a/hooks/post_user_create b/hooks/post_user_create deleted file mode 100644 index ab5efb9..0000000 --- a/hooks/post_user_create +++ /dev/null @@ -1,25 +0,0 @@ -#!/bin/bash -app=APPNAMETOCHANGE - -# Source YunoHost helpers -source /usr/share/yunohost/helpers -source /etc/yunohost/apps/$app/scripts/_common.sh - -# Retrieve arguments -username=$1 -mail=$2 -password=$3 # Clear password -firstname=$4 -lastname=$5 -final_path=$(ynh_app_setting_get "$app" final_path) - -pushd $final_path - user_exists=$(ynh_exec_as $app php7.3 bin/console aeneria:user:exist "$mail") - if [ $user_exists -eq 0 ] - then - user_pass=$(ynh_string_random) - ynh_exec_as $app php7.3 bin/console aeneria:user:add "$mail" "$user_pass" -n - else - ynh_exec_as $app php7.3 bin/console aeneria:user:activate "$mail" - fi -popd diff --git a/hooks/post_user_delete b/hooks/post_user_delete deleted file mode 100644 index f5a6eae..0000000 --- a/hooks/post_user_delete +++ /dev/null @@ -1,17 +0,0 @@ -#!/bin/bash -app=APPNAMETOCHANGE - -# Source YunoHost helpers -source /usr/share/yunohost/helpers -source /etc/yunohost/apps/$app/scripts/_common.sh - -# Retrieve arguments -username=$1 -mail=$2 -password=$3 # Clear password -firstname=$4 -lastname=$5 -final_path=$(ynh_app_setting_get "$app" final_path) - -# Deactivate the user in aeneria -cd "$final_path" && ynh_exec_as $app php7.3 bin/console aeneria:user:deactivate "$mail" -n \ No newline at end of file diff --git a/scripts/install b/scripts/install index 09b226d..23ca50b 100644 --- a/scripts/install +++ b/scripts/install @@ -131,16 +131,9 @@ ynh_script_progression --message="Installing aeneria..." --weight=1 pushd $final_path ynh_exec_as $app php$phpversion bin/console aeneria:install "$app" -n - # Create aeneria's user - for username in $(ynh_user_list) - do - mail=$(ynh_user_get_info --username=$username --key='mail') - user_pass=$(ynh_string_random) - ynh_exec_as $app php$phpversion bin/console aeneria:user:add "$mail" "$user_pass" - done - - # Set admin user - mail=$(ynh_user_get_info --username=$admin --key='mail') + # Create admin user + mail=$(ynh_user_get_info --username="$admin" --key='mail') + ynh_exec_as $app php$phpversion bin/console aeneria:user:add "$mail" "$(ynh_string_random)" -n ynh_exec_as $app php$phpversion bin/console aeneria:user:grant "$mail" popd @@ -156,14 +149,6 @@ ynh_add_config --template="../conf/aeneria.cron" --destination="/etc/cron.d/$app chown root: "/etc/cron.d/$app" chmod 644 "/etc/cron.d/$app" -#================================================= -# ADAPT HOOK FOR AENERIA INSTANCE -#================================================= -ynh_script_progression --message="Adapting hooks..." --weight=1 - -ynh_replace_string --match_string="APPNAMETOCHANGE" --replace_string="$app" --target_file="../hooks/post_user_create" -ynh_replace_string --match_string="APPNAMETOCHANGE" --replace_string="$app" --target_file="../hooks/post_user_delete" - #================================================= # SETUP SSOWAT #================================================= diff --git a/scripts/upgrade b/scripts/upgrade index c857551..f33b523 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -166,21 +166,14 @@ pushd $final_path ynh_exec_as $app php$phpversion bin/console cache:clear -n ynh_exec_as $app php$phpversion bin/console doctrine:migrations:migrate -n - # Create aeneria's user - for username in $(ynh_user_list) - do - mail=$(ynh_user_get_info --username=$username --key='mail') - user_exists=$(ynh_exec_as $app php$phpversion bin/console aeneria:user:exist "$mail") - if [ $user_exists -eq 0 ] - then - user_pass=$(ynh_string_random) - ynh_exec_as $app php$phpversion bin/console aeneria:user:add "$mail" "$user_pass" -n - fi - done - # Set admin user - mail=$(ynh_user_get_info --username=$admin --key='mail') - ynh_exec_as $app php$phpversion bin/console aeneria:user:grant "$mail" -n + mail=$(ynh_user_get_info --username="$admin" --key='mail') + user_exists=$(ynh_exec_as $app php$phpversion bin/console aeneria:user:exist "$mail") + if [ $user_exists -eq 0 ] + then + user_pass=$(ynh_string_random) + ynh_exec_as $app php$phpversion bin/console aeneria:user:add "$mail" "$user_pass" -n + fi # Regenerate RSA keys ynh_exec_as $app php$phpversion bin/console aeneria:generate-key --force -n @@ -195,14 +188,6 @@ ynh_add_config --template="../conf/aeneria.cron" --destination="/etc/cron.d/$app chown root: "/etc/cron.d/$app" chmod 644 "/etc/cron.d/$app" -#================================================= -# ADAPT HOOK FOR AENERIA INSTANCE -#================================================= -ynh_script_progression --message="Adapting hooks..." --weight=1 - -ynh_replace_string --match_string="APPNAMETOCHANGE" --replace_string=$app --target_file="../hooks/post_user_create" -ynh_replace_string --match_string="APPNAMETOCHANGE" --replace_string=$app --target_file="../hooks/post_user_delete" - #================================================= # RELOAD NGINX #=================================================