-
Notifications
You must be signed in to change notification settings - Fork 13
/
_common.sh
69 lines (58 loc) · 2.7 KB
/
_common.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/bin/bash
#=================================================
# PERSONAL HELPERS
#=================================================
get_synapse_db_name() {
# Parameters: synapse instance identifier
# Returns: database name
ynh_app_setting_get --app="$1" --key=db_name
}
#=================================================
# CONFIG PANEL SETTERS
#=================================================
apply_permissions() {
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
newValues="${newValues//\"}"
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' "$install_dir/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;}" "$install_dir/config.yaml"
# Ensure that entries with value surrounded with quotes are deleted too. E.g. "users".
sed -i "/permissions:/,/relay:/{/: \"$role\"/d;}" "$install_dir/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" "$install_dir/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 $install_dir/config.yaml"
}
set__listuser() {
role="user"
ynh_app_setting_set --app=$app --key=listuser --value="$listuser"
apply_permissions
ynh_store_file_checksum --file="$install_dir/config.yaml"
}
set__listrelay() {
role="relay"
ynh_app_setting_set --app=$app --key=listrelay --value="$listrelay"
apply_permissions
ynh_store_file_checksum --file="$install_dir/config.yaml"
}
set__listadmin() {
role="admin"
ynh_app_setting_set --app=$app --key=listadmin --value="$listadmin"
apply_permissions
ynh_store_file_checksum --file="$install_dir/config.yaml"
}