Skip to content

Commit

Permalink
Added magento 2 auto admin login feature
Browse files Browse the repository at this point in the history
  • Loading branch information
rbouma2 committed Apr 9, 2024
1 parent f859a08 commit d27a0f4
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 3 deletions.
14 changes: 13 additions & 1 deletion commands/env.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -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:-}
Expand Down
34 changes: 34 additions & 0 deletions commands/magento2/setup-autologin.cmd
Original file line number Diff line number Diff line change
@@ -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"
10 changes: 10 additions & 0 deletions commands/magento2/setup-autologin.help
Original file line number Diff line number Diff line change
@@ -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 <<EOF
\033[33mUsage:\033[0m
setup-autologin - setup autologin to magento admin when visiting admin url
\033[33mOptions:\033[0m
-h, --help Display this help menu
EOF
)
1 change: 1 addition & 0 deletions commands/magento2/usage.help
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ ENV_TYPE_USAGE=$(cat <<EOF
\n\033[33mMagento 2 commands:\033[0m
magento \033[32m%command%\033[0m run bin/magento inside php-fpm container with params
setup-autologin setup autologin to magento admin when visiting admin url
magerun \033[32m%command%\033[0m run magerun inside php-fpm container with params
cache clean magento 2 cache (simulair to \033[31m'bin/magento cache:clean'\033[0m)
fixowns Fix ownerships inside container
Expand Down
4 changes: 3 additions & 1 deletion docs/changelog.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# Change Log
# Change Log

See the full change log in GitHub: [https://github.com/dockergiant/rolldev/releases](https://github.com/dockergiant/rolldev/releases)
56 changes: 56 additions & 0 deletions docs/configuration/magento2/setup-admin-autologin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Setup Admin Auto Login in Magento 2



Enabling admin auto-login in Magento 2 can be a great time-saver for developers. By setting a specific environment variable and using a command-line tool, you can bypass the manual login process. This guide will walk you through enabling this feature safely in a development environment.

#### Prerequisites

Check warning on line 7 in docs/configuration/magento2/setup-admin-autologin.md

View workflow job for this annotation

GitHub Actions / deploy

Non-consecutive header level increase; H1 to H4 [myst.header]

- Access to the command line or terminal.
- Administrative access to your Magento 2 project files.
- The `roll` command-line tool installed in your development environment.

#### Step 1: Configure Your Environment File

Check warning on line 13 in docs/configuration/magento2/setup-admin-autologin.md

View workflow job for this annotation

GitHub Actions / deploy

Non-consecutive header level increase; H1 to H4 [myst.header]

1. **Locate Your `.env.roll` File**: This file should be in the root of your Magento 2 project.

2. **Set the ROLL_ADMIN_AUTOLOGIN Variable**:
- Open the `.env.roll` file in your text editor.
- Add or modify the following line:
```
ROLL_ADMIN_AUTOLOGIN=1
```
- Save the changes. This tells the auto-login mechanism to activate within your development environment.
3. restart containers by executing this in your project root
```
roll env up
```
#### Step 2: Generate a Local User for Auto-Login

Check warning on line 29 in docs/configuration/magento2/setup-admin-autologin.md

View workflow job for this annotation

GitHub Actions / deploy

Non-consecutive header level increase; H1 to H4 [myst.header]
1. **Open Your Terminal**: Navigate to the root directory of your Magento 2 project.
2. **Run the Setup Command**: Execute the following command:
```
roll setup-autologin
```
This command generates a local user in your Magento 2 database, configured for auto-login. Follow any prompts to complete the setup, which may involve specifying a username or password, though typically it will create a default user for you.
#### Step 3: Verify Auto-Login Functionality

Check warning on line 39 in docs/configuration/magento2/setup-admin-autologin.md

View workflow job for this annotation

GitHub Actions / deploy

Non-consecutive header level increase; H1 to H4 [myst.header]
- After completing the setup, navigate to your Magento 2 admin URL in your web browser. If configured correctly, you should be logged into the admin dashboard automatically without the need to enter login credentials.
#### Step 4: Disable Auto-Login (Important for Production)

Check warning on line 43 in docs/configuration/magento2/setup-admin-autologin.md

View workflow job for this annotation

GitHub Actions / deploy

Non-consecutive header level increase; H1 to H4 [myst.header]
- **For Security Reasons**: Remember to disable this feature before moving your project to a staging or production environment. You can disable it by setting:
```
ROLL_ADMIN_AUTOLOGIN=0
```
in your `.env.roll` file, then running any necessary deployment commands for your environment.
#### Security Considerations

Check warning on line 51 in docs/configuration/magento2/setup-admin-autologin.md

View workflow job for this annotation

GitHub Actions / deploy

Non-consecutive header level increase; H1 to H4 [myst.header]
- **Development Only**: This auto-login method should only be used in a local or secure development environment. Never use or enable auto-login in production, as it poses a significant security risk.
- **Access Control**: Ensure that your development environment is not accessible from the internet or by unauthorized users.
This tutorial assumes you're familiar with basic Magento 2 and shell/command line operations. Adjustments may be necessary based on your specific development setup or Magento version. Always consult your development team or Magento documentation for best practices tailored to your project.
6 changes: 5 additions & 1 deletion environments/magento2/init.env
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@ ROLL_TEST_DB=0
ROLL_MAGEPACK=0

# Set to 1 for enable static content browser caching
ROLL_MAGENTO_STATIC_CACHING=1
ROLL_MAGENTO_STATIC_CACHING=1

# Auto login prefilling fields when accessing admin url on /shopmanager or /backend
# Only works when you did run the auto login setup script > roll setup-autologin (only available for Magento 2 projects)
ROLL_ADMIN_AUTOLOGIN=0

0 comments on commit d27a0f4

Please sign in to comment.