From 6ebefb11200056b7dbc28566c3b9c516c8d74d56 Mon Sep 17 00:00:00 2001 From: Javier Feliz Date: Fri, 1 Aug 2025 22:34:22 -0400 Subject: [PATCH] ability to change user roles --- CLAUDE.md | 5 ++- app/Livewire/ManageUsers.php | 19 ++++++++++ .../views/livewire/manage-users.blade.php | 38 ++++++++++++++++--- 3 files changed, 55 insertions(+), 7 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index 0b4a078..7af6d30 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -78,4 +78,7 @@ AuthentiKate is a lightweight SSO/OIDC solution built with Laravel and Livewire, - OAuth keys stored in `storage/oauth/` - Uses Laravel's built-in authentication system - Email verification and password reset supported -- Uses the free version of FluxUI. A livewire component library. \ No newline at end of file +- 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 \ No newline at end of file diff --git a/app/Livewire/ManageUsers.php b/app/Livewire/ManageUsers.php index d4b1746..26a529b 100644 --- a/app/Livewire/ManageUsers.php +++ b/app/Livewire/ManageUsers.php @@ -50,6 +50,25 @@ class ManageUsers extends Component $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() { return view('livewire.manage-users'); diff --git a/resources/views/livewire/manage-users.blade.php b/resources/views/livewire/manage-users.blade.php index b579c76..e30e6b5 100644 --- a/resources/views/livewire/manage-users.blade.php +++ b/resources/views/livewire/manage-users.blade.php @@ -3,16 +3,42 @@
Users
+ + @if (session()->has('success')) +
+ {{ session('success') }} +
+ @endif + + @if (session()->has('error')) +
+ {{ session('error') }} +
+ @endif + @foreach ($users as $u) -
- {{$u->name}} - {{$u->email}} +
+
+ {{$u->name}} + {{$u->email}} +
+ + {{ $u->isAdmin() ? 'Admin' : 'User' }} + +
+
+ @can('update', $u) + + + + + @endcan + @can('delete', $u) + Delete + @endcan
- @can('delete', $u) - Delete - @endcan @endforeach