++ 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}");
|
||||
|
||||
|
||||
60
app/Livewire/Settings/UsersIndex.php
Normal file
60
app/Livewire/Settings/UsersIndex.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Settings;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Livewire\Component;
|
||||
use Spatie\Permission\Models\Permission;
|
||||
|
||||
class UsersIndex extends Component
|
||||
{
|
||||
public string $name = '';
|
||||
public string $email = '';
|
||||
public string $password = '';
|
||||
public string $password_confirmation = '';
|
||||
public array $selectedPermissions = [];
|
||||
public array $availablePermissions = [];
|
||||
|
||||
public function mount(): void
|
||||
{
|
||||
$this->availablePermissions = Permission::query()
|
||||
->orderBy('name')
|
||||
->pluck('name')
|
||||
->all();
|
||||
}
|
||||
|
||||
protected function rules(): array
|
||||
{
|
||||
return [
|
||||
'name' => ['required', 'string', 'max:255'],
|
||||
'email' => ['required', 'email', 'max:255', Rule::unique('users', 'email')],
|
||||
'password' => ['required', 'string', 'min:8', 'confirmed'],
|
||||
'selectedPermissions' => ['array'],
|
||||
'selectedPermissions.*' => ['string', Rule::in($this->availablePermissions)],
|
||||
];
|
||||
}
|
||||
|
||||
public function createUser(): void
|
||||
{
|
||||
$validated = $this->validate();
|
||||
|
||||
$user = User::create([
|
||||
'name' => $validated['name'],
|
||||
'email' => $validated['email'],
|
||||
'password' => $validated['password'],
|
||||
]);
|
||||
|
||||
$user->syncPermissions($validated['selectedPermissions'] ?? []);
|
||||
|
||||
$this->reset(['name', 'email', 'password', 'password_confirmation', 'selectedPermissions']);
|
||||
session()->flash('success', 'Utente creato con successo.');
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.settings.users-index', [
|
||||
'users' => User::query()->with('roles', 'permissions')->orderBy('name')->get(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user