diff --git a/app/Http/Controllers/Api/LoginController.php b/app/Http/Controllers/Api/LoginController.php index e0ccfd0..fb8ffee 100644 --- a/app/Http/Controllers/Api/LoginController.php +++ b/app/Http/Controllers/Api/LoginController.php @@ -38,7 +38,7 @@ class LoginController extends Controller return response()->json(['error' => 'Credenciales de google invalidas.'], 422); } - $user = $user = User::where("email", "=", $userProvider->email)->first(); + $user = $user = User::where("email", "=", $userProvider->email)->where("estado",true)->first(); if (isset($user)) { $userCreated = User::updateOrCreate( @@ -74,7 +74,7 @@ class LoginController extends Controller } else { return response()->json([ "status" => 0, - "message" => "Usuario no registrado en el sistema", + "message" => "Usuario no registrado o deshabilitado", ], 404); } } diff --git a/app/Http/Controllers/Api/UserController.php b/app/Http/Controllers/Api/UserController.php index c5cbee8..74be162 100644 --- a/app/Http/Controllers/Api/UserController.php +++ b/app/Http/Controllers/Api/UserController.php @@ -47,7 +47,7 @@ class UserController extends Controller "password" => "required" ]); - $user = User::where("email", "=", $request->email)->first(); + $user = User::where("email", "=", $request->email)->where("estado",true)->first(); if (isset($user->id)) { if (Hash::check($request->password, $user->password)) { @@ -66,7 +66,7 @@ class UserController extends Controller } else { return response()->json([ "status" => 0, - "message" => "Usuario no registrado", + "message" => "Usuario no registrado o deshabilitado", ], 404); } } @@ -92,6 +92,18 @@ class UserController extends Controller ]); } + public function listUserHabilitados(){ + $users = User::whereNotNull("name")->where("estado",true)->get(); + foreach ($users as $user) { + $user->rol=User::find($user->id)->roles[0]->name; + } + return response([ + "status" => 1, + "msg" => "!Lista de usuarios no nulos y habilitados", + "data" => $users, + ]); + } + public function logout() { auth()->user()->tokens()->delete(); @@ -99,4 +111,27 @@ class UserController extends Controller "message" => "Sesion cerrada" ]); } + public function updateRoleEstado(Request $request){ + $request->validate([ + "id"=>"exists:users", + "role" => "present|nullable|numeric|min:1|max:2", + "estado" => "present|nullable|boolean" + ]); + if(auth()->user()->isAdmin()){ + $user = User::find($request->id); + $user->update(['estado' =>$request->estado]); + $user->roles()->sync([$request->role]); + return response([ + "status" => 1, + "msg" => "!Update user", + "data" => $user, + ]); + } + else{ + return response()->json([ + "status" => 0, + "message" => "No eres administrador", + ], 404); + } + } } diff --git a/app/Models/User.php b/app/Models/User.php index b9e4c58..ad74932 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -20,6 +20,7 @@ class User extends Authenticatable 'lastname', 'email', 'password', + 'estado', ]; public $timestamps = false; diff --git a/routes/api.php b/routes/api.php index 2de1499..f9c50e3 100644 --- a/routes/api.php +++ b/routes/api.php @@ -38,14 +38,15 @@ Route::get('estandares', [EstandarController::class, 'listEstandarValores']); //CAMBIAR ESTO -Route::get('user', [UserController::class, 'listUser']); +//Route::get('user', [UserController::class, 'listUser']); +Route::get('user', [UserController::class, 'listUserHabilitados']); Route::get('plan/export/{id}', [PlanController::class, 'exportPlan'])->where('id', '[0-9]+'); Route::middleware("auth:sanctum")->group(function () { //rutas auth Route::get('user-profile', [UserController::class, 'userProfile']); - + Route::get('update', [UserController::class, 'updateRoleEstado']); Route::get('logout', [UserController::class, 'logout']); Route::post('register', [UserController::class, 'register']);