Skip to content

Commit 2f41d9e

Browse files
authored
Merge pull request #29 from LibreCodeCoop/feat/auto-reset-local-user-passwords
feat(wordpress): auto-reset local users password on startup
2 parents ab38f43 + 5720542 commit 2f41d9e

3 files changed

Lines changed: 68 additions & 1 deletion

File tree

.docker/wordpress/entrypoint.sh

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@ trim_value() {
3737
echo "$1" | sed -E 's/^[[:space:]]+//; s/[[:space:]]+$//'
3838
}
3939

40+
is_truthy() {
41+
case "$(echo "${1:-}" | tr '[:upper:]' '[:lower:]')" in
42+
1|true|yes|on)
43+
return 0
44+
;;
45+
*)
46+
return 1
47+
;;
48+
esac
49+
}
50+
4051
plugins_config_exists() {
4152
[ -n "$(trim_value "${WORDPRESS_SETUP_CONFIG_YAML:-}")" ]
4253
}
@@ -127,6 +138,46 @@ sync_site_urls() {
127138
replace_url_occurrences "${prod_host}" "${local_host}"
128139
}
129140

141+
reset_local_user_passwords() {
142+
local reset_all_users="${WORDPRESS_LOCAL_RESET_ALL_USERS_PASSWORDS:-0}"
143+
local target_user
144+
local shared_password="${WORDPRESS_LOCAL_USERS_PASSWORD:-}"
145+
local user_id
146+
147+
target_user="$(trim_value "${WORDPRESS_LOCAL_RESET_PASSWORD_FOR_USER:-}")"
148+
149+
if [ -z "$shared_password" ]; then
150+
echo "WORDPRESS_LOCAL_USERS_PASSWORD not set; skipping local user password reset."
151+
return
152+
fi
153+
154+
if is_truthy "$reset_all_users"; then
155+
echo "Resetting password for all local WordPress users..."
156+
while IFS= read -r user_id; do
157+
user_id="$(trim_value "$user_id")"
158+
if [ -z "$user_id" ]; then
159+
continue
160+
fi
161+
runuser -u www-data -- wp user update "$user_id" --user_pass="$shared_password" >/dev/null
162+
done < <(runuser -u www-data -- wp user list --field=ID)
163+
echo " ✓ Password reset completed for all users"
164+
return
165+
fi
166+
167+
if [ -n "$target_user" ]; then
168+
echo "Resetting password for local WordPress user '$target_user'..."
169+
if runuser -u www-data -- wp user get "$target_user" --field=ID >/dev/null 2>&1; then
170+
runuser -u www-data -- wp user update "$target_user" --user_pass="$shared_password" >/dev/null
171+
echo " ✓ Password reset completed for user '$target_user'"
172+
else
173+
echo " ⚠ User '$target_user' not found; skipping password reset"
174+
fi
175+
return
176+
fi
177+
178+
echo "No local password reset target configured; set WORDPRESS_LOCAL_RESET_ALL_USERS_PASSWORDS=1 or WORDPRESS_LOCAL_RESET_PASSWORD_FOR_USER."
179+
}
180+
130181
install_plugin() {
131182
local plugin_slug="$1"
132183

@@ -312,6 +363,7 @@ echo "Installing plugins and themes..."
312363

313364
if wordpress_is_installed; then
314365
sync_site_urls
366+
reset_local_user_passwords
315367

316368
if plugins_config_exists; then
317369
install_org_plugins_from_config

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ services:
3838
# Local URL that replaces PROD_SITE_URL during startup synchronization.
3939
# Keep as localhost for local development, or change if you use another host.
4040
LOCAL_SITE_URL: ${LOCAL_SITE_URL:-http://localhost}
41+
# Local-only password override for imported users (do not use in production).
42+
# Set one shared password and choose one strategy:
43+
# - reset all local users, or
44+
# - reset only one specific user login/email/ID.
45+
WORDPRESS_LOCAL_USERS_PASSWORD: ${WORDPRESS_LOCAL_USERS_PASSWORD:-localdev123}
46+
WORDPRESS_LOCAL_RESET_ALL_USERS_PASSWORDS: ${WORDPRESS_LOCAL_RESET_ALL_USERS_PASSWORDS:-1}
47+
WORDPRESS_LOCAL_RESET_PASSWORD_FOR_USER: ${WORDPRESS_LOCAL_RESET_PASSWORD_FOR_USER:-}
4148
# Inline YAML config for automatic plugin/theme installation.
4249
# Use wordpress_org_plugins, wordpress_archive_plugins, wordpress_custom_plugins, and wordpress_custom_themes.
4350
WORDPRESS_SETUP_CONFIG_YAML: |
@@ -89,9 +96,14 @@ After that, start the stack with the standard Compose command:
8996
9097
```bash
9198
docker compose up -d --build
92-
docker compose exec --user www-data wordpress wp user reset-password <username> --show-password --skip-email
9399
```
94100

101+
On each startup (when WordPress is already installed), the entrypoint can automatically reset local user passwords:
102+
- `WORDPRESS_LOCAL_RESET_ALL_USERS_PASSWORDS=1`: reset all users to `WORDPRESS_LOCAL_USERS_PASSWORD`.
103+
- `WORDPRESS_LOCAL_RESET_PASSWORD_FOR_USER=<login|email|id>`: reset only one user.
104+
105+
Use only one strategy at a time. For local environments importing production data, resetting all users is usually the simplest approach.
106+
95107
### Database dump
96108

97109
If you need to bootstrap the environment with existing data, place your SQL dump in the folder below:

common-services.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ services:
1616
- WORDPRESS_DB_PASSWORD=${WORDPRESS_DB_PASSWORD:-root}
1717
- WORDPRESS_TABLE_PREFIX=${WORDPRESS_TABLE_PREFIX:-wp_}
1818
- WORDPRESS_SETUP_CONFIG_YAML=${WORDPRESS_SETUP_CONFIG_YAML:-}
19+
- WORDPRESS_LOCAL_USERS_PASSWORD=${WORDPRESS_LOCAL_USERS_PASSWORD:-}
20+
- WORDPRESS_LOCAL_RESET_ALL_USERS_PASSWORDS=${WORDPRESS_LOCAL_RESET_ALL_USERS_PASSWORDS:-0}
21+
- WORDPRESS_LOCAL_RESET_PASSWORD_FOR_USER=${WORDPRESS_LOCAL_RESET_PASSWORD_FOR_USER:-}
1922
- XDEBUG_CONFIG=${XDEBUG_CONFIG:-client_host=host.docker.internal start_with_request=yes}
2023
- XDEBUG_MODE=${XDEBUG_MODE:-off}
2124
- HOST_UID=${HOST_UID:-1000}

0 commit comments

Comments
 (0)