++ 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:
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