From d27a0f458b8866bb1cb59da83d3cc09db20542d3 Mon Sep 17 00:00:00 2001 From: Rick Bouma Date: Tue, 9 Apr 2024 14:29:34 +0200 Subject: [PATCH] Added magento 2 auto admin login feature --- commands/env.cmd | 14 ++++- commands/magento2/setup-autologin.cmd | 34 +++++++++++ commands/magento2/setup-autologin.help | 10 ++++ commands/magento2/usage.help | 1 + docs/changelog.md | 4 +- .../magento2/setup-admin-autologin.md | 56 +++++++++++++++++++ environments/magento2/init.env | 6 +- 7 files changed, 122 insertions(+), 3 deletions(-) create mode 100644 commands/magento2/setup-autologin.cmd create mode 100644 commands/magento2/setup-autologin.help create mode 100644 docs/configuration/magento2/setup-admin-autologin.md diff --git a/commands/env.cmd b/commands/env.cmd index 827b9a6..cf05ab7 100644 --- a/commands/env.cmd +++ b/commands/env.cmd @@ -74,10 +74,22 @@ if [[ ${ROLL_ENV_TYPE} == "magento2" ]]; then ROLL_VARNISH=${ROLL_VARNISH:-1} ROLL_ELASTICSEARCH=${ROLL_ELASTICSEARCH:-1} ROLL_RABBITMQ=${ROLL_RABBITMQ:-1} + ROLL_ADMIN_AUTOLOGIN=${ROLL_ADMIN_AUTOLOGIN:-0} if [[ ${ROLL_MAGENTO_STATIC_CACHING} -eq 1 ]]; then - export NGINX_TEMPLATE=${NGINX_TEMPLATE:-magento2.conf} + if [[ ${ROLL_ADMIN_AUTOLOGIN} -eq 1 ]]; then + export NGINX_TEMPLATE=${NGINX_TEMPLATE:-magento2-autologin.conf} + else + export NGINX_TEMPLATE=${NGINX_TEMPLATE:-magento2.conf} + fi fi + + if [[ ${ROLL_ADMIN_AUTOLOGIN} -eq 1 ]]; then + export NGINX_TEMPLATE=${NGINX_TEMPLATE:-magento2-dev-autologin.conf} + else + export NGINX_TEMPLATE=${NGINX_TEMPLATE:-magento2-dev.conf} + fi + export NGINX_TEMPLATE=${NGINX_TEMPLATE:-magento2-dev.conf} fi export NGINX_TEMPLATE=${NGINX_TEMPLATE:-} diff --git a/commands/magento2/setup-autologin.cmd b/commands/magento2/setup-autologin.cmd new file mode 100644 index 0000000..1954bca --- /dev/null +++ b/commands/magento2/setup-autologin.cmd @@ -0,0 +1,34 @@ +#!/bin/bash +[[ ! ${ROLL_DIR} ]] && >&2 echo -e "\033[31mThis script is not intended to be run directly!\033[0m" && exit 1 + +ROLL_ENV_PATH="$(locateEnvPath)" || exit $? +loadEnvConfig "${ROLL_ENV_PATH}" || exit $? +assertDockerRunning + +## allow return codes from sub-process to bubble up normally +trap '' ERR + +if [[ "${ROLL_ENV_TYPE}" != "magento2" ]]; then + boxerror "This command is only working for Magento 2 projects" && exit 1 +fi + +echo "Confirming n98-magerun2 is installed..." +"${ROLL_DIR}/bin/roll" magerun > /dev/null 2>&1 + + +LOCALADMIN_EXISTS=$("${ROLL_DIR}/bin/roll" magerun admin:user:list | grep "localadmin") + + +if [[ -z "$LOCALADMIN_EXISTS" ]]; then + echo "Setup user account localadmin..." + "${ROLL_DIR}/bin/roll" magerun admin:user:create --admin-user "localadmin" --admin-password "admin123" --admin-email "localadmin@roll.test" --admin-firstname "Local" --admin-lastname "Admin" +fi + +echo "Force google as default TFA Provider" +"${ROLL_DIR}/bin/roll" magento config:set twofactorauth/general/force_providers google + +echo "Setting autologin 2fa code" +"${ROLL_DIR}/bin/roll" magento security:tfa:google:set-secret localadmin "LAJWZZTAI4KAHY7NBS6NOM3BDZK62IPDT3U5ARGXCJ4WVG7PJSG37FC4XODYWV2UNYMQG3LVMEUTIHO52FQGZU4Z462VNWYKPOM23M2YZNWAJW732RTNAVTQ2APUV64BPBJZKT7I4CAT62KLFEP5DZLWINPZ3JVOWJ6CPOAA77RSFK2PAG6YPS4VP55WYX5BPTX4Z7P6EZ4CY" + +echo -e "\n-------------------------------" +printf "\n\nDone!\n\nNow you need to set\n\n ROLL_ADMIN_AUTOLOGIN=1\n\nin your .env.roll configuration\n\n!!!!!\nIf you executed this command due to the admin popup modal\nthen just refresh the login page!\n!!!!!\n\n\n" \ No newline at end of file diff --git a/commands/magento2/setup-autologin.help b/commands/magento2/setup-autologin.help new file mode 100644 index 0000000..dc79540 --- /dev/null +++ b/commands/magento2/setup-autologin.help @@ -0,0 +1,10 @@ +#!/usr/bin/env bash +[[ ! ${ROLL_DIR} ]] && >&2 echo -e "\033[31mThis script is not intended to be run directly!\033[0m" && exit 1 + +ROLL_USAGE=$(cat < roll setup-autologin (only available for Magento 2 projects) +ROLL_ADMIN_AUTOLOGIN=0 \ No newline at end of file