Files
computer-playbook/roles/web-app-discourse/files/ruby/ensure_admin_user.rb
kevinveenbirkenbachandClaude Opus 5 ea2e373e1c fix(discourse): only set the admin password when it is not already the one set
Job 104153193860 died at "Ensure the administrator account carries the current
password" with:

    Validation failed: Password is the same as your current password.

Discourse's UserPasswordValidator adds :same_as_current when password_hash_
changed? and confirm_password?(value) are both true. Assigning the current
password again satisfies both, because the reassignment salts a fresh hash. The
script assigned unconditionally, so any run that did not follow a rotation was a
hard failure rather than a no-op, and the task was never idempotent.

The assignment is now guarded by confirm_password?, which makes the rule
unreachable: it requires confirm_password? to be true, and that is exactly when
nothing is assigned. A new user is covered without a new_record? check, since
User#confirm_password? returns false when no user_password row exists.

web-app-openproject/files/ruby/ensure_admin_user.rb already guards the same way
with check_password?; this brings Discourse in line with it.

The two saves collapse into one while we are here. The second existed only to
set `active`, and one save writes username, password and active together.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-15 00:56:01 +02:00

18 lines
613 B
Ruby

# nocheck: mirrored-unit-test - runs in the Discourse console against User and its
# password validators; the models only exist once the application has booted
email = ENV.fetch("DISCOURSE_ADMIN_EMAIL")
username = ENV.fetch("DISCOURSE_ADMIN_USERNAME")
password = ENV.fetch("DISCOURSE_ADMIN_PASSWORD")
user = User.find_by_email(email) || User.new(email: email)
user.username = username
user.password = password unless user.confirm_password?(password)
user.active = true
user.save!
user.grant_admin!
user.change_trust_level!(1) if user.trust_level < 1
user.email_tokens.update_all(confirmed: true)
user.activate