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 @@
+ + {{-- si el usuario es admin se le muestra lo siguiente --}} + @if (auth()->user()->is_admin) +
+
+ +
+ @csrf + @method('PATCH') + +

+ {{ __('Switch de Admin') }} +

+ +

+ {{ __('Poporciona un email para alternar su cargo') }} +

+ +
+ + + {{-- --}} +
+ +
+ + {{ __('Switch') }} + +
+
+
+
+ @endif +
@include('profile.partials.update-profile-information-form') diff --git a/routes/web.php b/routes/web.php index d92a130..235713b 100644 --- a/routes/web.php +++ b/routes/web.php @@ -2,6 +2,7 @@ use App\Http\Controllers\Auth\ProviderController; use App\Http\Controllers\ProfileController; +use App\Http\Controllers\UserController; use Illuminate\Support\Facades\Route; use App\Http\Controllers\VideosController; @@ -70,12 +71,12 @@ Route::get('/cursos', 'index'); Route::get('/tienda', 'tienda'); - Route::get('/cursos/gestion', 'create'); - Route::post('/cursos/gestion', 'store'); + Route::get('/cursos/gestion', 'create')->middleware('admin'); + Route::post('/cursos/gestion', 'store')->middleware('admin'); Route::get('/cursos/{idCurso}', 'show'); - Route::get('/cursos/{idCurso}/editar', 'edit'); - Route::patch('/cursos/{idCurso}', 'update'); // patch es para actualizar, en el html usar el metodo POST y agregar @method('PATCH') en el formulario - Route::delete('/cursos/eliminar', 'destroy'); + Route::get('/cursos/{idCurso}/editar', 'edit')->middleware('admin'); + Route::patch('/cursos/{idCurso}', 'update')->middleware('admin'); // patch es para actualizar, en el html usar el metodo POST y agregar @method('PATCH') en el formulario + Route::delete('/cursos/eliminar', 'destroy')->middleware('admin'); }); @@ -91,3 +92,8 @@ Route::get('/carrito', function () { return view('carrito'); }); + +// Profile - añadir o quitar admin +Route::controller(UserController::class)->group(function(){ + Route::patch('/admin/switch', 'switchAdmin')->middleware('admin'); +});