feat(auth): bootstrap the first user as admin, default the rest to editor - #124
Merged
Conversation
…itor New CMS users were promoted to admin whenever CMS_AUTO_PROMOTE_ALL_ADMINS was set, and existing users were re-promoted on every login. Drop the flag and give new accounts the editor role instead. The first user to log in against an empty cms_users table is still bootstrapped as an admin so a fresh install has someone who can manage roles. The count and the insert share a transaction with a locking read so two simultaneous first logins can't both come out as admin. Existing admins keep their role; this only governs account creation. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
New CMS users are no longer created as admins. The first user to log in against an empty
cms_userstable is bootstrapped as an admin so a fresh install has someone who can manage roles; everyone after that is created as an editor and has to be promoted from the users screen.CMS_AUTO_PROMOTE_ALL_ADMINSis removed entirely — it also re-promoted existing users to admin on every login, which made a demotion impossible to keep while the flag was on.How
FindOrCreateUserno longer reads the env var, and the promote-on-login branch for existing users is gone.insertUserhelper doesSELECT COUNT(*) FROM cms_users FOR UPDATEand theINSERTinside one transaction. Count 0 → admin, otherwise editor. The transaction is what keeps two simultaneous first logins from both landing as admin..env.example,deploy/cms.env.example,docker-compose.yml,deploy/compose.cms.yml, and thedeploy/README.mdvariable list. README now documents the bootstrap behavior instead.The
cms_users.rolecolumn default was already'editor', so there's no schema change.Deploy note
Existing admins keep their role — this only governs account creation. Any accounts that were auto-promoted while the flag was on will need to be demoted by hand.
Remove
CMS_AUTO_PROMOTE_ALL_ADMINSfrom the production env file when this ships; it's now ignored either way.Testing
go build ./...andgo test ./...pass. The bootstrap path itself is not covered by a test — it needs a live DB for the transaction and locking read, which the current suite doesn't stand up.🤖 Generated with Claude Code