feat: make admin email configurable and interactive password seeding

This commit is contained in:
idevakk
2025-11-30 08:06:12 -08:00
parent 9f45e37693
commit a84a4a0c15
5 changed files with 56 additions and 7 deletions

View File

@@ -67,7 +67,7 @@ class User extends Authenticatable implements FilamentUser, MustVerifyEmail
public function canAccessPanel(Panel $panel): bool
{
return str_ends_with($this->email, '@zemail.me') && $this->level === UserLevel::SUPERADMIN && $this->hasVerifiedEmail();
return $this->email === config('app.admin_email') && $this->level === UserLevel::SUPERADMIN && $this->hasVerifiedEmail();
}
/**

View File

@@ -65,6 +65,19 @@ return [
'url' => env('APP_URL', 'http://localhost'),
/*
|--------------------------------------------------------------------------
| Admin Email
|--------------------------------------------------------------------------
|
| This is the email address that has access to the admin panel. Only this
| exact email address with SUPERADMIN level and verified email can access
| the admin panel.
|
*/
'admin_email' => env('ADMIN_EMAIL'),
/*
|--------------------------------------------------------------------------
| Application Timezone

View File

@@ -2,6 +2,7 @@
namespace Database\Seeders;
use App\enum\UserLevel;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;
@@ -13,12 +14,48 @@ class AdminSeeder extends Seeder
*/
public function run(): void
{
$adminEmail = config('app.admin_email');
if (! $adminEmail) {
$this->command->error('ADMIN_EMAIL not configured in config/app.php or .env file');
return;
}
// Check if admin user already exists
$existingAdmin = User::where('email', $adminEmail)->first();
if ($existingAdmin) {
$this->command->info("Admin user with email {$adminEmail} already exists");
return;
}
// Ask for admin password
$password = $this->command->secret('Enter admin password (input will be hidden):');
if (empty($password)) {
$this->command->error('Password cannot be empty');
return;
}
// Confirm password
$passwordConfirmation = $this->command->secret('Confirm admin password:');
if ($password !== $passwordConfirmation) {
$this->command->error('Passwords do not match');
return;
}
User::query()->create([
'name' => 'admin',
'email' => 'admin@zemail.me',
'password' => Hash::make('password'),
'level' => 9,
'email' => $adminEmail,
'password' => Hash::make($password),
'level' => UserLevel::SUPERADMIN,
'email_verified_at' => now(),
]);
$this->command->info("Admin user created successfully with email: {$adminEmail}");
}
}

View File

@@ -12,10 +12,9 @@ class DatabaseSeeder extends Seeder
*/
public function run(): void
{
// User::factory(10)->create();
$this->call([
MetaSeeder::class,
AdminSeeder::class,
MetaSeeder::class,
SettingsSeeder::class,
]);
}

View File

@@ -15,7 +15,7 @@ class UserSeeder extends Seeder
// Create super admin user
User::factory()->superAdmin()->create([
'name' => 'Super Admin',
'email' => 'admin@zemail.me',
'email' => 'super@admin.test',
]);
// Create normal users