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

102 lines
2.6 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 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(){
2022-11-09 02:17:18 +00:00
$users = User::whereNotIn("name",["null"])->where("estado",true)->get();
2022-11-03 03:01:58 +00:00
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 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
}