diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php new file mode 100644 index 0000000..b584d2a --- /dev/null +++ b/app/Http/Controllers/UserController.php @@ -0,0 +1,111 @@ +email)->first(); + $user->is_admin = false; + $user->save(); + return redirect('/profile'); + } + public function añadirAdmin(Request $request) + { + $user = User::where('email', $request->email)->first(); + $user->is_admin = true; + $user->save(); + return redirect('/profile'); + } + //switch admin + public function switchAdmin(Request $request) + { + // Obtiene el usuario autenticado + $authenticatedUser = Auth::user(); + + // Busca el usuario por su correo electrónico en la base de datos + $user = User::where('email', $request->email)->first(); + + if ($user) { + // Verifica que el usuario autenticado no sea el mismo que se está modificando + if ($user->id !== $authenticatedUser->id) { + // Realiza la conmutación del estado 'is_admin' + $user->is_admin = !$user->is_admin; + + // Guarda los cambios en la base de datos + $user->save(); + + return redirect('/profile')->with('success', 'Cambios guardados exitosamente'); + } else { + return redirect('/profile')->with('error', 'No puedes quitarte a ti mismo el rol de administrador'); + } + } else { + // El usuario no se encontró en la base de datos + return redirect('/profile')->with('error', 'Usuario no encontrado'); + } + } + +} diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php index 494c050..a55bcd5 100644 --- a/app/Http/Kernel.php +++ b/app/Http/Kernel.php @@ -54,6 +54,7 @@ class Kernel extends HttpKernel */ protected $middlewareAliases = [ 'auth' => \App\Http\Middleware\Authenticate::class, + 'admin' => \App\Http\Middleware\AdminMiddleware::class, 'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class, 'auth.session' => \Illuminate\Session\Middleware\AuthenticateSession::class, 'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class, diff --git a/app/Http/Middleware/AdminMiddleware.php b/app/Http/Middleware/AdminMiddleware.php new file mode 100644 index 0000000..98560a7 --- /dev/null +++ b/app/Http/Middleware/AdminMiddleware.php @@ -0,0 +1,24 @@ +check() && auth()->user()->is_admin){ + return $next($request); + } else{ + return redirect('/dashboard'); + } + } +} diff --git a/resources/views/profile/edit.blade.php b/resources/views/profile/edit.blade.php index aa6b05f..a6ee944 100644 --- a/resources/views/profile/edit.blade.php +++ b/resources/views/profile/edit.blade.php @@ -7,6 +7,46 @@