unsa-pis-be/app/Http/Controllers/Api/UserController.php

138 lines
3.8 KiB
PHP
Raw Normal View History

2022-06-14 01:21:39 +00:00
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\User;
use App\Models\Estandar;
2022-06-14 01:21:39 +00:00
use Illuminate\Support\Facades\Hash;
2022-10-20 22:27:03 +00:00
use Illuminate\Support\Facades\Auth;
2022-06-14 01:21:39 +00:00
class UserController extends Controller
{
2022-10-20 22:27:03 +00:00
public function register(Request $request)
{
$request->validate([
2022-10-22 15:13:00 +00:00
'email' => 'required|email|unique:users',
2022-10-24 04:01:17 +00:00
'rol'=> 'required|numeric|min:1|max:2'
2022-06-14 01:21:39 +00:00
]);
2022-10-20 22:27:03 +00:00
$userAuth = auth()->user()->roles[0]->name;
2022-10-22 15:13:00 +00:00
if ($userAuth == "Admin") {
2022-10-20 22:27:03 +00:00
$user = new User();
$user->name = "null";
$user->lastname = "null";
$user->email = $request->email;
$user->password = "null";
$user->estado = true;
2022-10-20 22:27:03 +00:00
$user->save();
2022-10-22 15:13:00 +00:00
$user->roles()->attach($request->rol);
2022-10-20 22:27:03 +00:00
return response()->json([
'message' => 'Correo registrado exitosamente',
'userAuth' => $user,
]);
} else {
return response()->json([
"status" => 0,
2022-10-22 15:13:00 +00:00
"message" => "No eres administrador: Correo no registrado",
], 404);
2022-10-20 22:27:03 +00:00
}
2022-06-14 01:21:39 +00:00
}
2022-10-20 22:27:03 +00:00
public function login(Request $request)
{
2022-06-14 01:21:39 +00:00
$request->validate([
"email" => "required|email",
"password" => "required"
]);
$user = User::where("email", "=", $request->email)->where("estado",true)->first();
2022-06-14 01:21:39 +00:00
2022-10-20 22:27:03 +00:00
if (isset($user->id)) {
if (Hash::check($request->password, $user->password)) {
2022-06-14 01:21:39 +00:00
$token = $user->createToken("auth_token")->plainTextToken;
return response()->json([
2022-06-29 22:38:43 +00:00
"message" => "Usuario logueado",
"access_token" => $token,
"nombre" => $user->name,
"apellido" => $user->lastname,
]);
2022-10-20 22:27:03 +00:00
} else {
2022-06-14 01:21:39 +00:00
return response()->json([
2022-06-29 22:38:43 +00:00
"message" => "La password es incorrecta",
], 404);
2022-06-14 01:21:39 +00:00
}
2022-10-20 22:27:03 +00:00
} else {
2022-06-14 01:21:39 +00:00
return response()->json([
"status" => 0,
"message" => "Usuario no registrado o deshabilitado",
], 404);
2022-06-14 01:21:39 +00:00
}
}
2022-10-20 22:27:03 +00:00
public function userProfile()
{
2022-06-14 01:21:39 +00:00
return response()->json([
"status" => 0,
2022-06-29 22:38:43 +00:00
"message" => "Perfil de usuario",
"data" => auth()->user(),
]);
2022-06-14 01:21:39 +00:00
}
2022-10-22 15:55:51 +00:00
public function listUser(){
$users = User::all();
2022-10-27 22:04:17 +00:00
foreach ($users as $user) {
$user->rol=User::find($user->id)->roles[0]->name;
}
2022-10-22 15:55:51 +00:00
return response([
"status" => 1,
"msg" => "!Lista de usuarios",
"data" => $users,
]);
}
2022-11-03 03:01:58 +00:00
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,
]);
}
2022-10-20 22:27:03 +00:00
public function logout()
{
2022-06-14 01:21:39 +00:00
auth()->user()->tokens()->delete();
return response()->json([
2022-10-20 22:27:03 +00:00
"message" => "Sesion cerrada"
]);
2022-06-14 01:21:39 +00:00
}
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);
}
}
2022-06-14 01:21:39 +00:00
}