From 6372a418ab19a3316daa81bd5c890b488f38eb21 Mon Sep 17 00:00:00 2001 From: Fabian Wilkens Date: Tue, 9 Aug 2022 09:59:02 +0200 Subject: [PATCH] Update: update config_panel, add actions --- actions.toml | 8 ++ conf/cron.env | 2 +- conf/env_auth-worker.env.sample | 2 +- conf/env_auth.env.sample | 2 +- config_panel.toml | 28 +++++-- scripts/actions/add_subscription.sh | 55 ++++++++++++++ scripts/config | 111 ++++++++++++++++++++++++++++ scripts/install | 5 ++ scripts/upgrade | 13 ++++ 9 files changed, 215 insertions(+), 11 deletions(-) create mode 100644 actions.toml create mode 100755 scripts/actions/add_subscription.sh create mode 100644 scripts/config diff --git a/actions.toml b/actions.toml new file mode 100644 index 0000000..abb7e4a --- /dev/null +++ b/actions.toml @@ -0,0 +1,8 @@ +[add_subscriptions] +name = "Add a subscription and file space to all users without a subscription." +command = "/bin/bash scripts/actions/add_subscription.sh" +# user = "root" # optional +# cwd = "/" # optional +# accepted_return_codes = [0, 1, 2, 3] # optional +accepted_return_codes = [0] +description = "Add subs" diff --git a/conf/cron.env b/conf/cron.env index eb03e00..bcd313f 100644 --- a/conf/cron.env +++ b/conf/cron.env @@ -5,7 +5,7 @@ # Default FILES Upload Linit in MB for new users. # 100 = 100MB, 1000 = 1000MB, 1024 = 1GB, 10240 = 10GB -FILES_SIZE=100 +FILES_SIZE=__FILES_SIZE__ DB_HOST=localhost DB_PORT=3306 diff --git a/conf/env_auth-worker.env.sample b/conf/env_auth-worker.env.sample index dfd11f1..282a3c3 100644 --- a/conf/env_auth-worker.env.sample +++ b/conf/env_auth-worker.env.sample @@ -26,7 +26,7 @@ DB_MIGRATIONS_PATH=dist/migrations/*.js REDIS_URL=redis://localhost:6379/__REDIS_DB__ -DISABLE_USER_REGISTRATION=false +DISABLE_USER_REGISTRATION=__DISABLE_USER_REGISTRATION__ ACCESS_TOKEN_AGE=5184000 REFRESH_TOKEN_AGE=31556926 diff --git a/conf/env_auth.env.sample b/conf/env_auth.env.sample index 9cf3633..614f3ad 100644 --- a/conf/env_auth.env.sample +++ b/conf/env_auth.env.sample @@ -26,7 +26,7 @@ DB_MIGRATIONS_PATH=dist/migrations/*.js REDIS_URL=redis://localhost:6379/__REDIS_DB__ -DISABLE_USER_REGISTRATION=false +DISABLE_USER_REGISTRATION=__DISABLE_USER_REGISTRATION__ ACCESS_TOKEN_AGE=5184000 REFRESH_TOKEN_AGE=31556926 diff --git a/config_panel.toml b/config_panel.toml index dc8fa9f..88e2fe5 100644 --- a/config_panel.toml +++ b/config_panel.toml @@ -2,14 +2,26 @@ version = "1.0" [main] name = "StandardNotes Server configuration" -services = ["__APP__"] - [main.config] - name = "Configuration Options" + [main.new_user] + name = "New User Options" - [main.config.files_limit] - ask = "Default user files limit" - type = "string" + [main.new_user.disable_user_registration] + ask = "Disable user registration?" + type = "boolean" + default = "false" + help = "false = New users can register\ntrue = No new user registrations allowed" + + [main.new_user.files_limit] + ask = "Default files limit" + type = "number" default = "100" - help = "Choose a default limit in MB for the user file upload space.\n 100 = 100 MB, 1024 = 1GB, 10240 = 10GB" - bind = "FILES_SIZE:__FINALPATH__/cron.env" + help = "Choose a default limit in MB for the user file upload space.\n 100 = 100 MB\n 1024 = 1GB\n 10240 = 10GB" + + [main.subscription] + name = "User Subscription" + + [main.subscription.info] + ask = "" + type = "alert" + style = "info" diff --git a/scripts/actions/add_subscription.sh b/scripts/actions/add_subscription.sh new file mode 100755 index 0000000..f1642ae --- /dev/null +++ b/scripts/actions/add_subscription.sh @@ -0,0 +1,55 @@ +#!/bin/bash + +source /usr/share/yunohost/helpers + +#================================================= +# RETRIEVE ARGUMENTS +#================================================= + +app=$YNH_APP_INSTANCE_NAME +final_path=$(ynh_app_setting_get $app final_path) + +#================================================= +# LOAD ENV FILE +#================================================= + +if [ ! -f .env ] +then + export $(cat $final_path/cron.env | xargs) +fi + +FILE_UPLOAD_BYTES_PER_MB=1048576 +FILE_UPLOAD_BYTES_LIMIT=$(($FILES_SIZE*$FILE_UPLOAD_BYTES_PER_MB)) + +#================================================= +# ADD SUBSCRIPTION AND FILES SPACE TO ALL USERS +#================================================= + +ynh_script_progression --message="Add a subscription and $FILES_SIZE MB of file space to all users without a subscription" + +# Searching for new users in the last 10 minutes without a Pro_Plan subscription. +mysql --password=$DB_PASSWORD --database=$DB_DATABASE <<< " \ + SELECT email FROM users WHERE NOT EXISTS( SELECT * FROM user_subscriptions WHERE user_subscriptions.plan_name = \"PRO_PLAN\" AND user_subscriptions.user_uuid = users.uuid ); \ +" 2>/dev/null | + +# Read through the piped result until it's empty. +while IFS='\n' read email; do + if [[ ${email} = "email" ]]; then + ynh_print_info --message="New users found:" + ynh_print_info --message="----------------------------------------" + else + # ADD new user with Email $EMAIL a PRO_PLAN subscription + ynh_print_info --message="[$(date -Iseconds)] User: $email added to the PRO_PLAN subscription." + mysql --password=$DB_PASSWORD --database=$DB_DATABASE -e \ + "INSERT INTO user_roles (role_uuid , user_uuid) VALUES ((SELECT uuid FROM roles WHERE name=\"PRO_USER\" ORDER BY version DESC limit 1) ,(SELECT uuid FROM users WHERE email=\"$email\")) ON DUPLICATE KEY UPDATE role_uuid = VALUES(role_uuid);" + mysql --password=$DB_PASSWORD --database=$DB_DATABASE -e \ + "INSERT INTO user_subscriptions SET uuid=UUID(), plan_name=\"PRO_PLAN\", ends_at=8640000000000000, created_at=0, updated_at=0, user_uuid=(SELECT uuid FROM users WHERE email=\"$email\"), subscription_id=1, subscription_type=\"regular\";" + + # Add new user Files space. Size is 1GB*$FILES_SIZE + ynh_print_info --message="[$(date -Iseconds)] User: $email added $FILES_SIZE MB of file spcae." + mysql --password=$DB_PASSWORD --database=$DB_DATABASE -e \ + "INSERT INTO subscription_settings(uuid, name, value, created_at, updated_at, user_subscription_uuid) VALUES (UUID(), \"FILE_UPLOAD_BYTES_LIMIT\", \"$FILE_UPLOAD_BYTES_LIMIT\", FLOOR(UNIX_TIMESTAMP(NOW(6))*1000000), FLOOR(UNIX_TIMESTAMP(NOW(6))*1000000), (SELECT us.uuid FROM user_subscriptions us INNER JOIN users u ON us.user_uuid=u.uuid WHERE u.email=\"$email\"));" + fi +done + +ynh_script_progression --message="Execution completed" --last diff --git a/scripts/config b/scripts/config new file mode 100644 index 0000000..237d207 --- /dev/null +++ b/scripts/config @@ -0,0 +1,111 @@ +#!/bin/bash + +#================================================= +# GENERIC STARTING +#================================================= +# IMPORT GENERIC HELPERS +#================================================= + +source /usr/share/yunohost/helpers + +ynh_abort_if_errors + +#================================================= +# RETRIEVE ARGUMENTS +#================================================= + +app=$(ynh_app_setting_get $app id) +final_path=$(ynh_app_setting_get $app final_path) + +#================================================= +# SPECIFIC GETTERS FOR TOML SHORT KEY +#================================================= + +get__disable_user_registration(){ + + disabled=$(ynh_read_var_in_file --file="$final_path/live/auth.env" --key="DISABLE_USER_REGISTRATION") + + echo $disabled +} + +get__files_limit(){ + limit=$(ynh_read_var_in_file --file="$final_path/cron.env" --key="FILES_SIZE") + + echo $limit +} + +get__info(){ + domain="$(cat /etc/yunohost/current_host)" + limit=$(ynh_read_var_in_file --file="$final_path/cron.env" --key="FILES_SIZE") + link="https://$domain/yunohost/admin/#/apps/$app/actions" + cat << EOF +ask: "Add subscriptions:\nAdd a subscription and $limit MB of file space to all users without a subscription.\n$link" +EOF + +} + +#================================================= +# SPECIFIC VALIDATORS FOR TOML SHORT KEYS +#================================================= + +#================================================= +# SPECIFIC SETTERS FOR TOML SHORT KEYS +#================================================= + +set__disable_user_registration(){ + #--------------------------------------------- + # IMPORTANT: setter are trigger only if a change is detected + #--------------------------------------------- + if [ $disable_user_registration = "1" ]; then + disabled="true" + fi + if [ $disable_user_registration = "0" ]; then + disabled="false" + fi + + config_auth="$final_path/live/auth.env" + config_auth_worker="$final_path/live/auth-worker.env" + + ynh_write_var_in_file --file="$config_auth" --key="DISABLE_USER_REGISTRATION" --value="$disabled" + ynh_write_var_in_file --file="$config_auth_worker" --key="DISABLE_USER_REGISTRATION" --value="$disabled" + + ynh_store_file_checksum --file="$config_auth" + ynh_store_file_checksum --file="$config_auth_worker" + + ynh_systemd_action \ + --service_name="$app-auth" \ + --action="restart" \ + --log_path="/var/log/$app/auth.log" \ + --line_match='^.*Server started on port.*$|^.*Starting worker.*$' + ynh_systemd_action \ + --service_name="$app-auth-worker" \ + --action="restart" \ + --log_path="/var/log/$app/auth-worker.log" \ + --line_match='^.*Server started on port.*$|^.*Starting worker.*$' + + #--------------------------------------------- + # IMPORTANT: to be able to upgrade properly, you have to saved the value in settings too + #--------------------------------------------- + ynh_app_setting_set --app="$app" --key="disable_user_registration" --value="$disabled" +} + +set__files_limit(){ + #--------------------------------------------- + # IMPORTANT: setter are trigger only if a change is detected + #--------------------------------------------- + config_cron="$final_path/cron.env" + + ynh_write_var_in_file --file="$config_cron" --key="FILES_SIZE" --value="$files_limit" + + ynh_store_file_checksum --file="$config_cron" + + #--------------------------------------------- + # IMPORTANT: to be able to upgrade properly, you have to saved the value in settings too + #--------------------------------------------- + ynh_app_setting_set --app="$app" --key="files_size" --value="$files_limit" +} + +#================================================= +# GENERIC FINALIZATION +#================================================= +ynh_app_config_run $1 diff --git a/scripts/install b/scripts/install index 917cf2f..0bddbcd 100755 --- a/scripts/install +++ b/scripts/install @@ -58,6 +58,11 @@ ynh_app_setting_set --app=$app --key=auth_version --value=$auth_version ynh_app_setting_set --app=$app --key=files_version --value=$files_version ynh_app_setting_set --app=$app --key=syncing_server_version --value=$syncing_server_version +disable_user_registration=false +files_size=100 +ynh_app_setting_set --app=$app --key=DISABLE_USER_REGISTRATION --value=$disable_user_registration +ynh_app_setting_set --app=$app --key=FILES_SIZE --value=$files_size + #================================================= # STANDARD MODIFICATIONS #================================================= diff --git a/scripts/upgrade b/scripts/upgrade index 0f5b487..74da0eb 100755 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -48,6 +48,9 @@ syncing_server_version_installed=$(ynh_app_setting_get --app=$app --key=syncing_ auth_version_installed=$(ynh_app_setting_get --app=$app --key=auth_version) api_gateway_version_installed=$(ynh_app_setting_get --app=$app --key=api_gateway_version) +disable_user_registration=$(ynh_app_setting_get --app=$app --key=DISABLE_USER_REGISTRATION) +files_size=$(ynh_app_setting_get --app=$app --key=FILES_SIZE) + config_api_gateway="$final_path/live/api-gateway.env" config_auth="$final_path/live/auth.env" config_auth_worker="$final_path/live/auth-worker.env" @@ -195,6 +198,16 @@ if [ -z "$valet_token_secret" ]; then valet_token_secret=$(ynh_string_random --length=48 | base64) ynh_app_setting_set --app=$app --key=valet_token_secret --value=$valet_token_secret fi +# If disable_user_registration doesn't exist, create it +if [ -z "$diable_user_registration" ]; then + disable_user_registration=false + ynh_app_setting_set --app=$app --key=DISABLE_USER_REGISTRATION --value=$disable_user_registration +fi +# If files_zise doesn't exist, create it +if [ -z "$files_size" ]; then + files_size=100 + ynh_app_setting_set --app=$app --key=FILES_SIZE --value=$files_size +fi # Remove old Settings, Services, Files, Dependencies