fix(install): install command creates admin user if not exists, resets existing admin user#2256
Conversation
WalkthroughRefactors admin user creation in InstallCommand to use direct email lookup and a create-or-update flow (skip if enabled, re-enable and reset password if disabled, or create new verified user). Adds unit tests for those scenarios, tweaks a functional test setup/teardown, and adds Changes
Sequence Diagram(s)sequenceDiagram
participant Cmd as InstallCommand
participant Repo as UserRepository
participant OM as ObjectManager
participant Hasher as PasswordHasher
participant DB as Database
Cmd->>Repo: findOneBy(['email' => $email])
Repo->>DB: SELECT user WHERE email
DB-->>Repo: user || null
Repo-->>Cmd: user || null
alt user exists
alt enabled
Cmd->>Cmd: output "skipping creation"
else disabled
Cmd->>Cmd: set enabled = true
Cmd->>Hasher: hash(newPassword)
Hasher-->>Cmd: hashedPassword
Cmd->>Cmd: set password = hashedPassword
Cmd->>OM: flush()
OM->>DB: UPDATE user
end
else user not found
Cmd->>Cmd: create User, set email, enabled, verified
Cmd->>Hasher: hash(password)
Hasher-->>Cmd: hashedPassword
Cmd->>Cmd: set password = hashedPassword
Cmd->>OM: persist(user)
Cmd->>OM: flush()
OM->>DB: INSERT user
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Suggested labels
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches🧪 Generate unit tests (beta)
No actionable comments were generated in the recent review. 🎉 Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@src/InstallBundle/Command/InstallCommand.php`:
- Around line 181-199: The catch-all for UserNotFoundException around
userRepository->loadUserByIdentifier($email) can hide other failure modes (like
NonUniqueResultException) and cause duplicate user creation; change the handler
to distinguish cases: call a repository method that finds by email ignoring
"enabled" (or use a try/catch that catches NonUniqueResultException separately),
if a disabled User is returned re-enable and persist it instead of creating a
new User, and if a non-unique result is detected throw a clear RuntimeException
requiring manual resolution; keep references to loadUserByIdentifier, User
(entity), $this->registry->getManagerForClass(User::class), and
$this->userPasswordHasher to locate where to update behavior.
🧹 Nitpick comments (1)
src/InstallBundle/Tests/Command/InstallCommandTest.php (1)
38-96: Prefer Foundry factories for entity fixtures in tests.These tests manually instantiate
User. The project’s testing guidelines call for Foundry-based fixtures; consider using aUserFactory(or similar) to build the entity and keep fixture behavior consistent. Based on learnings, ...
The NonUniqueResultException case is primarily a safeguard for data integrity issues, as the database schema enforces a unique constraint on the email column.
@pierredup, let me know if you want me to do a pass to add missing docstrings. |
|
Tip For best results, initiate chat on the files or code changes.
In the meantime, the PR looks well-structured with comprehensive test coverage for the new admin user creation logic! 👍 |
Bundle ReportChanges will increase total bundle size by 3.85MB (100.0%) ⬆️
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## 2.4.x #2256 +/- ##
============================================
+ Coverage 48.18% 48.55% +0.36%
- Complexity 2830 2831 +1
============================================
Files 519 519
Lines 9561 9567 +6
============================================
+ Hits 4607 4645 +38
+ Misses 4954 4922 -32
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Thanks for this!
That's not necessary, thanks |
pierredup
left a comment
There was a problem hiding this comment.
Changes looks good, just a few small comments
- simplify create user with findOneBy - use setVerified - revert off-pattern config dir logic added trying to get CI tests to pass
|
Thank you @fluxium |
While packaging SolidInvoice for NixOS I ran in to this bug when automating provisioning. This fix was implemented in line with what seemed to be the original intent of the try/catch.