-
Notifications
You must be signed in to change notification settings - Fork 0
feat(admin): add first-admin bootstrap command #37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,84 @@ | ||||||
| <?php | ||||||
|
|
||||||
| namespace App\Console\Commands; | ||||||
|
|
||||||
| use App\Models\User; | ||||||
| use Illuminate\Console\Attributes\Description; | ||||||
| use Illuminate\Console\Attributes\Signature; | ||||||
| use Illuminate\Console\Command; | ||||||
| use Silber\Bouncer\BouncerFacade as Bouncer; | ||||||
|
|
||||||
| #[Signature('app:bootstrap-first-admin {email? : Existing user email to promote}')] | ||||||
| #[Description('Create or promote the first admin user')] | ||||||
| class BootstrapFirstAdmin extends Command | ||||||
| { | ||||||
| public function handle(): int | ||||||
| { | ||||||
| $email = $this->argument('email'); | ||||||
|
|
||||||
| if (is_string($email) && $email !== '') { | ||||||
| return $this->promoteExistingUser($email); | ||||||
| } | ||||||
|
|
||||||
| return $this->createFirstAdmin(); | ||||||
| } | ||||||
|
|
||||||
| protected function createFirstAdmin(): int | ||||||
| { | ||||||
| $user = User::query()->first(); | ||||||
|
||||||
| $user = User::query()->first(); | |
| $user = User::query()->orderBy('id')->first(); |
Copilot
AI
Apr 10, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
createFirstAdmin() will promote the first user even if an admin already exists (e.g., the second user is already an admin). That can unexpectedly grant additional admin access on subsequent runs and undermines the “first admin bootstrap” safety goal. Consider first checking whether any user already has the admin role and, if so, exiting without changing roles (or requiring the email argument).
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| <?php | ||
|
|
||
| use App\Models\User; | ||
| use Illuminate\Foundation\Testing\RefreshDatabase; | ||
|
|
||
| uses(RefreshDatabase::class); | ||
|
|
||
| it('promotes the first user when no admin exists', function (): void { | ||
| $user = User::factory()->create(); | ||
|
|
||
| $this->artisan('app:bootstrap-first-admin') | ||
| ->assertExitCode(0); | ||
|
|
||
| expect($user->fresh()->isAn('admin'))->toBeTrue(); | ||
| }); | ||
|
Comment on lines
+8
to
+15
|
||
|
|
||
| it('promotes an existing user by email', function (): void { | ||
| $user = User::factory()->create(); | ||
|
|
||
| $this->artisan('app:bootstrap-first-admin', [ | ||
| 'email' => $user->email, | ||
| ])->assertExitCode(0); | ||
|
|
||
| expect($user->fresh()->isAn('admin'))->toBeTrue(); | ||
| }); | ||
|
|
||
| it('fails when the user does not exist', function (): void { | ||
| $this->artisan('app:bootstrap-first-admin', [ | ||
| 'email' => 'missing@example.com', | ||
| ])->assertExitCode(1); | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The command description and method name suggest a “create” flow, but the implementation only promotes an existing user (either the first user or a user by email). To avoid confusing operators, consider updating the description text and/or renaming
createFirstAdmin()to reflect the actual behavior (or implement the missing create path if intended).