++ fix: update .env.example and .gitignore, add CreateInitialAdmin command, add UsersIndex Livewire component, update entrypoint.sh, update app layout, add users-index blade view, update web routes
This commit is contained in:
@@ -2,8 +2,11 @@
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use Database\Seeders\RolesAndPermissionsSeeder;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
|
||||
@@ -19,10 +22,8 @@ class CreateInitialAdmin extends Command
|
||||
|
||||
public function handle(): int
|
||||
{
|
||||
if (User::count() > 0) {
|
||||
$this->info('Users already exist. Skipping initial admin creation.');
|
||||
return self::SUCCESS;
|
||||
}
|
||||
// Always ensure roles/permissions are present before assigning roles.
|
||||
Artisan::call('db:seed', ['--class' => RolesAndPermissionsSeeder::class, '--force' => true]);
|
||||
|
||||
$name = (string) ($this->option('name') ?? '');
|
||||
$email = (string) ($this->option('email') ?? '');
|
||||
@@ -45,6 +46,30 @@ class CreateInitialAdmin extends Command
|
||||
$password = $password !== '' ? $password : (string) $this->secret('Password amministratore (min 8 caratteri)');
|
||||
}
|
||||
|
||||
if (User::count() > 0) {
|
||||
$existingAdmin = User::role('amministratore')->first();
|
||||
if ($existingAdmin) {
|
||||
$this->info('An administrator already exists. Skipping initial admin creation.');
|
||||
return self::SUCCESS;
|
||||
}
|
||||
|
||||
if ($email !== '') {
|
||||
$existingUser = User::where('email', $email)->first();
|
||||
if ($existingUser) {
|
||||
$existingUser->assignRole('amministratore');
|
||||
$this->info("Granted admin role to existing user: {$existingUser->email}");
|
||||
return self::SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
$firstUser = User::query()->oldest('id')->first();
|
||||
if ($firstUser) {
|
||||
$firstUser->assignRole('amministratore');
|
||||
$this->warn("No admin role found. Granted admin role to first existing user: {$firstUser->email}");
|
||||
return self::SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
$validator = Validator::make([
|
||||
'name' => $name,
|
||||
'email' => $email,
|
||||
@@ -63,13 +88,17 @@ class CreateInitialAdmin extends Command
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
$admin = User::create([
|
||||
'name' => $name,
|
||||
'email' => $email,
|
||||
'password' => Hash::make($password),
|
||||
]);
|
||||
$admin = DB::transaction(function () use ($name, $email, $password) {
|
||||
$user = User::create([
|
||||
'name' => $name,
|
||||
'email' => $email,
|
||||
'password' => Hash::make($password),
|
||||
]);
|
||||
|
||||
$admin->assignRole('amministratore');
|
||||
$user->assignRole('amministratore');
|
||||
|
||||
return $user;
|
||||
});
|
||||
|
||||
$this->info("Initial admin created: {$admin->email}");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user