Skip to content

Instantly share code, notes, and snippets.

@morganestes
Last active March 6, 2023 16:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save morganestes/fb46715579ad4180ac10b2dd3f3071f4 to your computer and use it in GitHub Desktop.
Save morganestes/fb46715579ad4180ac10b2dd3f3071f4 to your computer and use it in GitHub Desktop.
Bash script to update a local user's password for local development with Lando.
---
# Separate from .env.local and .env.pantheon used by Roots, these are added to the lando containers.
# See https://docs.lando.dev/core/v3/env.html#environment-files
env_file:
- .env.lando
tooling:
update-user:
description: Adds/updates a local WP user account used for local administration.
service: appserver
level: app
dir: /app
cmd: lando-scripts/update-user.sh
options:
user:
default: $TERMINUS_USER # This is set by lando as an environment variable in the container.
describe: Email address of the user to change or create.
alias: email
password:
default: supersecretsquirrel
describe: The password to set for the user.
events:
post-pull:
appserver:
- wp plugin deactivate wp-native-php-sessions wp-saml-auth # these aren't needed in local development.
- lando-scripts/update-user.sh
- wp cache flush
- wp rewrite flush
#!/usr/bin/env bash
##
# Updates an existing user or adds a new one to use during local development.
#
# If used in an automated script, WP_USER_EMAIL and WP_USER_PASSWORD should be set as environment variables;
# either in a `.env.lando` file, or in a `.lando.local.yml` file in `services.appserver.overrides.environment`.
# If using the `lando update-user` command, use the --email and --password args.
##
EMAIL="${WP_USER_EMAIL:-$TERMINUS_USER}"
PASSWORD="${WP_USER_PASSWORD:-supersecretsquirrel}"
# PARSE THE ARGZZ
while (( "$#" )); do
case "$1" in
--email|--email=*)
if [ "${1##--email=}" != "$1" ]; then
EMAIL="${1##--email=}"
shift
else
EMAIL=$2
shift 2
fi
;;
--user|--user=*)
if [ "${1##--user=}" != "$1" ]; then
EMAIL="${1##--user=}"
shift
else
EMAIL=$2
shift 2
fi
;;
--password|--password=*)
if [ "${1##--password=}" != "$1" ]; then
PASSWORD="${1##--password=}"
shift
else
PASSWORD=$2
shift 2
fi
;;
--)
shift
break
;;
-*|--*=)
shift
;;
*)
shift
;;
esac
done
if ! expr "${EMAIL}" : '.*@' >/dev/null; then
printf "%s isn't a valid email address!\n" "${EMAIL}" >&2
exit 1
fi
declare -i USER_ID # This will be 0 instead of the error message if there's a problem getting the user from WP.
USER_ID="$(wp user get "${EMAIL}" --field=ID --quiet --no-color)"
if [[ "${USER_ID}" -gt 0 ]]
then
printf "Updating user %d (%s) with password %s.\n" "${USER_ID}" "${EMAIL}" "${PASSWORD}"
wp user update "${EMAIL}" --user_pass="${PASSWORD}" --quiet --no-color --skip-email
else
printf "Creating user for %s with password %s.\n" "${EMAIL}" "${PASSWORD}"
wp user create "${EMAIL}" "${EMAIL}" --role=administrator --user_pass="${PASSWORD}" --porcelain --quiet --no-color
fi
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment