From 3c2b4bc2a7e9456d986cc00710784eccf8a70e04 Mon Sep 17 00:00:00 2001 From: Tagadda <36127788+Tagadda@users.noreply.github.com> Date: Mon, 31 Jan 2022 12:48:53 +0000 Subject: [PATCH] configpanel update with_sftp --- config_panel.toml | 5 +++-- scripts/_common.sh | 35 +++++++++++++++++++++++++++++++++++ scripts/config | 30 +++++++++++++++++++++++++++++- 3 files changed, 67 insertions(+), 3 deletions(-) diff --git a/config_panel.toml b/config_panel.toml index 8bd0fc3..56d9cb1 100644 --- a/config_panel.toml +++ b/config_panel.toml @@ -7,7 +7,7 @@ name = "My webapp configuration" [main.sftp] name = "SFTP access" - [main.sftp.sftp] + [main.sftp.with_sftp] ask = "Do you need a SFTP access?" type = "boolean" default = true @@ -16,7 +16,8 @@ name = "My webapp configuration" ask = "Set a password for the SFTP access" type = "password" optional = true - help = "If a password already exist, it will not be replaced." + visible = "stfp" + help = "If a password already exist, leave blank and it will not be replaced." [main.php_fpm_config] name = "PHP-FPM configuration" diff --git a/scripts/_common.sh b/scripts/_common.sh index ed3594e..31498b2 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -365,3 +365,38 @@ ynh_app_changelog () { echo "No significative changes from the changelog..." > "${final_changelog}_lite" fi } + +ynh_system_user_add_group() { + # Declare an array to define the options of this helper. + local legacy_args=uhs + local -A args_array=([u]=username= [g]=groups=) + local username + local groups + + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + groups="${groups:-}" + + local group + for group in $groups; do + usermod -a -G "$group" "$username" + done +} + + +ynh_system_user_del_group() { + # Declare an array to define the options of this helper. + local legacy_args=uhs + local -A args_array=([u]=username= [g]=groups=) + local username + local groups + + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + groups="${groups:-}" + + local group + for group in $groups; do + gpasswd -d "$username" "$group" + done +} diff --git a/scripts/config b/scripts/config index 5ccd0d7..d6b5e66 100644 --- a/scripts/config +++ b/scripts/config @@ -20,6 +20,13 @@ final_path=$(ynh_app_setting_get $app final_path) phpversion=$(ynh_app_setting_get --app=$app --key=phpversion) current_fpm_footprint=$(ynh_app_setting_get --app=$app --key=fpm_footprint) +is_password_exist=0 +current_password=$(ynh_app_setting_get --app=$app --key=password) +if [ -n "$current_password" ] +then + is_password_exist=1 +fi + #================================================= # SPECIFIC GETTERS FOR TOML SHORT KEY #================================================= @@ -47,7 +54,6 @@ get__free_footprint() { fi } - #================================================= # SPECIFIC VALIDATORS FOR TOML SHORT KEYS #================================================= @@ -70,6 +76,13 @@ set__free_footprint() { fi } +set__password() { + if [ ! $is_password_exist -eq 1 ] + then + ynh_app_setting_set --app=$app --key=password --value="$password" + fi +} + #================================================= # GENERIC FINALIZATION #================================================= @@ -97,6 +110,21 @@ ynh_app_config_apply() { _ynh_app_config_apply ynh_add_fpm_config --phpversion=$phpversion --usage=$fpm_usage --footprint=$fpm_footprint + + if [ $with_sftp -eq 1 ] + then + ynh_system_user_add_group --username=$app --groups="sftp.app" + + if [ $is_password_exist -eq 1 ] + then + ynh_print_warn --message="A password already exist, it will not be replaced." + else + # Add the password to the user + chpasswd <<< "${app}:${password}" + fi + else + ynh_system_user_del_group --username=$app --groups="sftp.app" + fi } ynh_app_config_run $1