1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/flarum_ynh.git synced 2024-09-03 18:36:24 +02:00

Merge pull request #58 from YunoHost-Apps/patch-email

Email admin with their credentials
This commit is contained in:
Titus PiJean 2018-02-14 10:54:14 +01:00 committed by GitHub
commit 8c5b136fa5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 137 additions and 82 deletions

View file

@ -50,9 +50,9 @@
"name": "admin",
"type": "user",
"ask": {
"en": "Choose the admin user",
"fr": "Choisissez ladministrateur",
"de": "Wählen einen Administrator"
"en": "Choose the admin user (required for automatic post-installation)",
"fr": "Choisissez ladministrateur (nécessaire pour la post-installation automatique)",
"de": "Wählen einen Administrator (erforderlich für die automatische Nachinstallation)"
},
"example": "johndoe",
"optional": true
@ -60,9 +60,9 @@
{
"name": "title",
"ask": {
"en": "Choose a title for your forum",
"fr": "Choisissez un titre pour votre forum",
"de": "Wählen einen Titel für eure Internetforum"
"en": "Choose a title for your forum (required for automatic post-installation)",
"fr": "Choisissez un titre pour votre forum (nécessaire pour la post-installation automatique)",
"de": "Wählen einen Titel für eure Internetforum (erforderlich für die automatische Nachinstallation)"
},
"example": "My forum",
"default": "",

View file

@ -10,3 +10,52 @@ exec_as() {
sudo -u "$USER" "$@"
fi
}
# Send an email to inform the administrator
#
# usage: ynh_send_readme_to_admin app_message [recipients]
# | arg: app_message - The message to send to the administrator.
# | arg: recipients - The recipients of this email. Use spaces to separate multiples recipients. - default: root
# example: "root admin@domain"
# If you give the name of a YunoHost user, ynh_send_readme_to_admin will find its email adress for you
# example: "root admin@domain user1 user2"
ynh_send_readme_to_admin() {
local app_message="${1:-...No specific information...}"
local recipients="${2:-root}"
# Retrieve the email of users
find_mails () {
local list_mails="$1"
local mail
local recipients=" "
# Read each mail in argument
for mail in $list_mails
do
# Keep root or a real email address as it is
if [ "$mail" = "root" ] || echo "$mail" | grep --quiet "@"
then
recipients="$recipients $mail"
else
# But replace an user name without a domain after by its email
if mail=$(ynh_user_get_info "$mail" "mail" 2> /dev/null)
then
recipients="$recipients $mail"
fi
fi
done
echo "$recipients"
}
recipients=$(find_mails "$recipients")
local mail_subject="☁️🆈🅽🅷☁️: \`$app\` was just installed!"
local mail_message="This is an automated message from your beloved YunoHost server.
Specific information for the application $app.
$app_message
---
Automatic diagnosis data from YunoHost
$(yunohost tools diagnosis | grep -B 100 "services:" | sed '/services:/d')"
# Send the email to the recipients
echo "$mail_message" | mail -a "Content-Type: text/plain; charset=UTF-8" -s "$mail_subject" "$recipients"
}

View file

@ -83,28 +83,28 @@ composer_path=/opt/${app}_composer
# Test if composer is installed
if ! type "${composer_path}/composer" > /dev/null; then
# Prepare composer directories
sudo mkdir -p $composer_path
sudo mkdir -p $composer_path/cache
sudo chown -R $app:www-data $composer_path
sudo chmod -R 0775 $composer_path
# Prepare composer directories
sudo mkdir -p $composer_path
sudo mkdir -p $composer_path/cache
sudo chown -R $app:www-data $composer_path
sudo chmod -R 0775 $composer_path
# Install composer (https://getcomposer.org)
EXPECTED_SIGNATURE=$(wget https://composer.github.io/installer.sig -O - -q)
php -r "copy('https://getcomposer.org/installer', '$composer_path/composer-setup.php');"
ACTUAL_SIGNATURE=$(php -r "echo hash_file('SHA384', '$composer_path/composer-setup.php');")
if [ "$EXPECTED_SIGNATURE" = "$ACTUAL_SIGNATURE" ]
then
# Install composer (https://getcomposer.org)
EXPECTED_SIGNATURE=$(wget https://composer.github.io/installer.sig -O - -q)
php -r "copy('https://getcomposer.org/installer', '$composer_path/composer-setup.php');"
ACTUAL_SIGNATURE=$(php -r "echo hash_file('SHA384', '$composer_path/composer-setup.php');")
if [ "$EXPECTED_SIGNATURE" = "$ACTUAL_SIGNATURE" ]
then
sudo su - $app -s /bin/bash -c "php $composer_path/composer-setup.php --install-dir=$composer_path --filename=composer --quiet"
RESULT=$?
else
else
>&2 echo 'ERROR: Invalid Composer installer signature'
RESULT=1
fi
if [ $RESULT != 0 ]
then
ynh_die 'Composer could not be installed'
fi
fi
if [ $RESULT != 0 ]
then
ynh_die 'Composer could not be installed'
fi
fi
@ -143,11 +143,11 @@ ynh_app_setting_set "$app" db_pwd "$db_pwd"
# Bug in Nginx with locations and aliases (see http://stackoverflow.com/a/35102259 )
if [ $path_url = "/" ]; then
sed -i "s@YNH_WWW_ROOTPATH@@g" ../conf/nginx.conf
sed -i "s@YNH_WWW_ROOTAPP@/@g" ../conf/nginx.conf
sed -i "s@YNH_WWW_ROOTPATH@@g" ../conf/nginx.conf
sed -i "s@YNH_WWW_ROOTAPP@/@g" ../conf/nginx.conf
else
sed -i "s@YNH_WWW_ROOTPATH@$path_url@g" ../conf/nginx.conf
sed -i "s@YNH_WWW_ROOTAPP@$path_url$path_url@g" ../conf/nginx.conf
sed -i "s@YNH_WWW_ROOTPATH@$path_url@g" ../conf/nginx.conf
sed -i "s@YNH_WWW_ROOTAPP@$path_url$path_url@g" ../conf/nginx.conf
fi
# Create a dedicated nginx config
ynh_add_nginx_config
@ -172,13 +172,13 @@ ynh_use_logrotate
if [ $is_public -eq 0 ]
then # Remove the public access
ynh_app_setting_delete $app skipped_uris
ynh_app_setting_delete $app skipped_uris
fi
# Make app public if necessary
if [ $is_public -eq 1 ]
then
# unprotected_uris allows SSO credentials to be passed anyway.
ynh_app_setting_set $app unprotected_uris "/"
# unprotected_uris allows SSO credentials to be passed anyway.
ynh_app_setting_set $app unprotected_uris "/"
fi
#=================================================
@ -193,63 +193,69 @@ systemctl reload nginx
# Only if admin user or title were specified
if [[ -n $admin && -n $title ]]; then
finalflarumconf="$final_path/configuration.yml"
cp ../sources/configuration.yml $finalflarumconf
admin_pwd=$(ynh_string_random 8)
sed -i "s@YNH_APP_DOMAIN@$domain@g" $finalflarumconf
sed -i "s@/YNH_WWW_PATH@$path_url@g" $finalflarumconf
sed -i "s@YNH_WWW_APP@$app@g" $finalflarumconf
sed -i "s@YNH_DB_PASS@$db_pwd@g" $finalflarumconf
sed -i "s@YNH_ADMIN_USER@$admin@g" $finalflarumconf
sed -i "s@YNH_ADMIN_PASS@$admin_pwd@g" $finalflarumconf
admin_mail=$(ynh_user_get_info $admin mail)
sed -i "s%YNH_ADMIN_EMAIL%$admin_mail%g" $finalflarumconf
sed -i "s@YNH_FORUM_TITLE@$title@g" $finalflarumconf
cd "$final_path"
exec_as www-data \
php -d memory_limit=-1 flarum install -f configuration.yml
finalflarumconf="$final_path/configuration.yml"
cp ../sources/configuration.yml $finalflarumconf
admin_pwd=$(ynh_string_random 8)
sed -i "s@YNH_APP_DOMAIN@$domain@g" $finalflarumconf
sed -i "s@/YNH_WWW_PATH@$path_url@g" $finalflarumconf
sed -i "s@YNH_WWW_APP@$app@g" $finalflarumconf
sed -i "s@YNH_DB_PASS@$db_pwd@g" $finalflarumconf
sed -i "s@YNH_ADMIN_USER@$admin@g" $finalflarumconf
sed -i "s@YNH_ADMIN_PASS@$admin_pwd@g" $finalflarumconf
admin_mail=$(ynh_user_get_info $admin mail)
sed -i "s%YNH_ADMIN_EMAIL%$admin_mail%g" $finalflarumconf
sed -i "s@YNH_FORUM_TITLE@$title@g" $finalflarumconf
cd "$final_path"
exec_as www-data \
php -d memory_limit=-1 flarum install -f configuration.yml
# Generate and add root token for user creation and deletion
roottoken=$(ynh_string_random 40)
apitablesql="CREATE TABLE IF NOT EXISTS api_keys (api_key TEXT(40) NOT NULL)"
rootsql="INSERT INTO api_keys VALUES ('"$roottoken"')"
ynh_mysql_execute_as_root "$apitablesql" $db_name
ynh_mysql_execute_as_root "$rootsql" $db_name
ynh_app_setting_set "$app" root_token "$roottoken"
# Generate and add root token for user creation and deletion
roottoken=$(ynh_string_random 40)
apitablesql="CREATE TABLE IF NOT EXISTS api_keys (api_key TEXT(40) NOT NULL)"
rootsql="INSERT INTO api_keys VALUES ('"$roottoken"')"
ynh_mysql_execute_as_root "$apitablesql" $db_name
ynh_mysql_execute_as_root "$rootsql" $db_name
ynh_app_setting_set "$app" root_token "$roottoken"
# Install the SSOwat auth extension
#sudo su - www-data -s /bin/bash -c "cd $final_path && /opt/flarum_composer/composer require 'tituspijean/flarum-ext-auth-ssowat:*@dev'"
# Install the SSOwat auth extension
#sudo su - www-data -s /bin/bash -c "cd $final_path && /opt/flarum_composer/composer require 'tituspijean/flarum-ext-auth-ssowat:*@dev'"
# Configure SSOwat auth extension
#ssowatdomain=$(</etc/yunohost/current_host)
#data='{"flarum-ext-auth-ssowat.address": "'$ssowatdomain'","flarum-ext-auth-ssowat.onlyUse": true}'
#rep=$(curl -s -o /dev/null -w "%{http_code}" -k -i \
# -H "Content-Type: application/json" \
# -H "Authorization: Token $roottoken; userId=1" \
# -X POST -d "$data" \
# -L https://${domain}${path}/api/settings )
#
#if [[ $rep != 204 ]]; then
# ynh_die "ERROR: Could not configure SSOwat extension"
#fi
# Configure SSOwat auth extension
#ssowatdomain=$(</etc/yunohost/current_host)
#data='{"flarum-ext-auth-ssowat.address": "'$ssowatdomain'","flarum-ext-auth-ssowat.onlyUse": true}'
#rep=$(curl -s -o /dev/null -w "%{http_code}" -k -i \
# -H "Content-Type: application/json" \
# -H "Authorization: Token $roottoken; userId=1" \
# -X POST -d "$data" \
# -L https://${domain}${path}/api/settings )
#
#if [[ $rep != 204 ]]; then
# ynh_die "ERROR: Could not configure SSOwat extension"
#fi
# Create missing users
for username in $(ynh_user_list); do
# Create missing users
for username in $(ynh_user_list); do
if [ "$username" == "$admin" ]; then continue; else
userpass=$(ynh_string_random 16)
usermail=$(ynh_user_get_info $username 'mail')
data='{"data":{"attributes":{"username":"'$username'","email":"'$usermail'","password":"'$userpass'"}}}'
rep=$(curl -s -o /dev/null -w "%{http_code}" -k -i \
-H "Content-Type: application/json" \
-H "Authentication: Token $roottoken" \
-X POST -d "$data" \
-L https://${domain}${path_url}/api/users )
if [[ $rep != 201 ]]; then
ynh_die "ERROR: Flarum account creation failed for $username"
fi
userpass=$(ynh_string_random 16)
usermail=$(ynh_user_get_info $username 'mail')
data='{"data":{"attributes":{"username":"'$username'","email":"'$usermail'","password":"'$userpass'"}}}'
rep=$(curl -s -o /dev/null -w "%{http_code}" -k -i \
-H "Content-Type: application/json" \
-H "Authentication: Token $roottoken" \
-X POST -d "$data" \
-L https://${domain}${path_url}/api/users )
if [[ $rep != 201 ]]; then
ynh_die "ERROR: Flarum account creation failed for $username"
fi
fi
done
>&2 echo "Installation successfull. Admin : $admin, password : $admin_pwd. Change it!"
done
app_message="User : $admin, password : $admin_pwd
Change your password!
Your forum is accessible at https://$domain/$path_url"
>&2 echo $app_message
ynh_send_readme_to_admin "$app_message" "$admin"
else
>&2 echo "Installation successfull. Post-installation required, visit your Flarum instance!"
app_message="Post-installation required, visit your Flarum instance."
>&2 echo $app_message
ynh_send_readme_to_admin "$app_message" "$admin"
fi