generated from thegrind/laravel-dockerized
ability to change user roles
This commit is contained in:
parent
f7ee4baa61
commit
6ebefb1120
@ -78,4 +78,7 @@ AuthentiKate is a lightweight SSO/OIDC solution built with Laravel and Livewire,
|
|||||||
- OAuth keys stored in `storage/oauth/`
|
- OAuth keys stored in `storage/oauth/`
|
||||||
- Uses Laravel's built-in authentication system
|
- Uses Laravel's built-in authentication system
|
||||||
- Email verification and password reset supported
|
- Email verification and password reset supported
|
||||||
- Uses the free version of FluxUI. A livewire component library.
|
- Uses the free version of FluxUI. A livewire component library.
|
||||||
|
|
||||||
|
## Code Guidance
|
||||||
|
- Stop initializing collections using collect(). This is not compatible with Database\Eloquent\Collection. It is also not necessary to do this since the components are only visible to the admin
|
@ -50,6 +50,25 @@ class ManageUsers extends Component
|
|||||||
$this->users = User::all();
|
$this->users = User::all();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function changeUserRole(User $user, string $role)
|
||||||
|
{
|
||||||
|
$this->authorize('update', $user);
|
||||||
|
|
||||||
|
// Prevent admins from demoting themselves
|
||||||
|
if ($user->id === auth()->id() && $role === 'user') {
|
||||||
|
session()->flash('error', 'You cannot demote yourself from admin.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$isAdmin = $role === 'admin';
|
||||||
|
$user->update(['is_admin' => $isAdmin]);
|
||||||
|
|
||||||
|
// Refresh the data
|
||||||
|
$this->users = User::all();
|
||||||
|
|
||||||
|
session()->flash('success', "User role updated to {$role}.");
|
||||||
|
}
|
||||||
|
|
||||||
public function render()
|
public function render()
|
||||||
{
|
{
|
||||||
return view('livewire.manage-users');
|
return view('livewire.manage-users');
|
||||||
|
@ -3,16 +3,42 @@
|
|||||||
<div class="flex justify-between items-center">
|
<div class="flex justify-between items-center">
|
||||||
<flux:heading size="xl">Users</flux:heading>
|
<flux:heading size="xl">Users</flux:heading>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
@if (session()->has('success'))
|
||||||
|
<div class="bg-green-100 border border-green-400 text-green-700 px-4 py-3 rounded my-4">
|
||||||
|
{{ session('success') }}
|
||||||
|
</div>
|
||||||
|
@endif
|
||||||
|
|
||||||
|
@if (session()->has('error'))
|
||||||
|
<div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded my-4">
|
||||||
|
{{ session('error') }}
|
||||||
|
</div>
|
||||||
|
@endif
|
||||||
|
|
||||||
<flux:separator class="my-8" />
|
<flux:separator class="my-8" />
|
||||||
@foreach ($users as $u)
|
@foreach ($users as $u)
|
||||||
<x-card class="flex items-center justify-between p-6">
|
<x-card class="flex items-center justify-between p-6">
|
||||||
<div class="flex gap-4">
|
<div class="flex gap-4 items-center">
|
||||||
<flux:heading>{{$u->name}}</flux:heading>
|
<div>
|
||||||
<flux:text>{{$u->email}}</flux:text>
|
<flux:heading>{{$u->name}}</flux:heading>
|
||||||
|
<flux:text>{{$u->email}}</flux:text>
|
||||||
|
</div>
|
||||||
|
<flux:badge color="{{ $u->isAdmin() ? 'yellow' : 'gray' }}">
|
||||||
|
{{ $u->isAdmin() ? 'Admin' : 'User' }}
|
||||||
|
</flux:badge>
|
||||||
|
</div>
|
||||||
|
<div class="flex gap-2 items-center">
|
||||||
|
@can('update', $u)
|
||||||
|
<flux:select wire:change="changeUserRole({{ $u->id }}, $event.target.value)" size="sm">
|
||||||
|
<option value="user" {{ !$u->isAdmin() ? 'selected' : '' }}>User</option>
|
||||||
|
<option value="admin" {{ $u->isAdmin() ? 'selected' : '' }}>Admin</option>
|
||||||
|
</flux:select>
|
||||||
|
@endcan
|
||||||
|
@can('delete', $u)
|
||||||
|
<flux:button variant="danger" size="sm" wire:click="deleteUser({{ $u->id }})">Delete</flux:button>
|
||||||
|
@endcan
|
||||||
</div>
|
</div>
|
||||||
@can('delete', $u)
|
|
||||||
<flux:button variant="danger" size="sm" wire:click="deleteUser({{ $u->id }})">Delete</flux:button>
|
|
||||||
@endcan
|
|
||||||
</x-card>
|
</x-card>
|
||||||
@endforeach
|
@endforeach
|
||||||
<div class="flex justify-between items-center mt-8">
|
<div class="flex justify-between items-center mt-8">
|
||||||
|
Loading…
x
Reference in New Issue
Block a user