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;
|
2022-10-20 03:15:33 +00:00
|
|
|
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";
|
2022-10-22 15:30:42 +00:00
|
|
|
$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([
|
2022-10-20 03:15:33 +00:00
|
|
|
"status" => 0,
|
2022-10-22 15:13:00 +00:00
|
|
|
"message" => "No eres administrador: Correo no registrado",
|
2022-10-20 03:15:33 +00:00
|
|
|
], 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",
|
2022-10-21 06:33:03 +00:00
|
|
|
"data" => auth()->user(),
|
2022-07-09 03:09:04 +00:00
|
|
|
]);
|
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-11-05 16:53:41 +00:00
|
|
|
|
2022-10-30 03:00:34 +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
|
|
|
}
|