2022-11-24 19:48:16 +01:00
#!/bin/bash
#=================================================
# GENERIC STARTING
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh
source /usr/share/yunohost/helpers
ynh_abort_if_errors
#=================================================
# RETRIEVE ARGUMENTS
#=================================================
2022-12-14 22:30:47 +01:00
function get__encryption {
encryption=$(ynh_app_setting_get --app $app --key encryption)
echo "'${encryption}'"
}
2022-11-24 19:48:16 +01:00
#=================================================
# SPECIFIC GETTERS FOR TOML SHORT KEY
#=================================================
# SPECIFIC VALIDATORS FOR TOML SHORT KEYS
#=================================================
#=================================================
# SPECIFIC SETTERS FOR TOML SHORT KEYS
#=================================================
#=================================================
# GENERIC FINALIZATION
#=================================================
2023-01-06 01:26:44 +01:00
function get__listuser {
2023-01-08 21:55:20 +01:00
botusers=$(grep -- "\".*: user" "$final_path/config.yaml" | sed -r 's/: user//' | tr -d '[:blank:]' | sed '/^#/d' | tr -d '\"' | tr '\n' ',')
2023-01-06 01:26:44 +01:00
cat <<EOF
2023-01-08 21:55:20 +01:00
value: '$botusers'
2023-01-06 01:26:44 +01:00
EOF
}
function get__listrelay {
2023-01-08 21:55:20 +01:00
relayusers=$(grep -- "\".*: relay" "$final_path/config.yaml" | sed -r 's/: relay//' | tr -d '[:blank:]' | sed '/^#/d' | tr -d '\"' | tr '\n' ',')
2023-01-06 01:26:44 +01:00
cat <<EOF
2023-01-08 21:55:20 +01:00
value: '$relayusers'
2023-01-06 01:26:44 +01:00
EOF
}
function get__listadmin {
2023-01-08 21:55:20 +01:00
botadmin=$(grep -- "\".*: admin" "$final_path/config.yaml" | sed -r 's/: admin//' | tr -d '[:blank:]' | sed '/^#/d' | tr -d '\"' | tr '\n' ',')
2023-01-06 01:26:44 +01:00
cat <<EOF
2023-01-08 21:55:20 +01:00
value: '$botadmin'
2023-01-06 01:26:44 +01:00
EOF
}
function get__role {
cat <<EOF
choices: ["admin", "user", "relay"]
default: "user"
EOF
}
function set__role {
set -o noglob # Disable globbing to avoid expansions when passing * as value.
declare values="list$role"
newValues="${!values}" # Here we expand the dynamic variable we created in the previous line. ! Does the trick
usersArray=(${newValues//,/ }) # Split the values using comma (,) as separator.
if [ -n "$newValues" ]
then
ynh_systemd_action --service_name="$app" --action=stop
# Get all entries between "permissions:" and "relay:" keys, remove the role part, remove commented parts, format it with newlines and clean whitespaces and double quotes.
allDefinedEntries=$(awk '/permissions:/{flag=1; next} /relay:/{flag=0} flag' "$final_path/config.yaml" | sed "/: $role/d" | sed -r 's/: (admin|user|relay)//' | tr -d '[:blank:]' | sed '/^#/d' | tr -d '\"' | tr ',' '\n' )
# Delete everything from the corresponding role to insert the new defined values. This way we also handle deletion of users.
sed -i "/permissions:/,/relay:/{/: $role/d;}" "$final_path/config.yaml"
for user in "${usersArray[@]}"
do
if grep -q -x "${user}" <<< "$allDefinedEntries"
then
ynh_print_info "User $user already defined in another role."
else
sed -i "/permissions:/a \ \\\"$user\": $role" "$final_path/config.yaml" # Whitespaces are needed so that the file can be correctly parsed
fi
done
fi
set +o noglob
ynh_print_info "Users with role $role added in $final_path/config.yaml"
}
2022-11-30 22:02:09 +01:00
ynh_app_config_run $1