| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- <?php
- namespace App\Http\Controllers;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Validator;
- use Illuminate\Support\Facades\Hash;
- class UsersProfilesController extends Controller{
- private $responseController;
- public function __construct($responseController = new ResponseController()){
- $this->responseController = $responseController;
- }
-
- public function getUsers(){
- try{
- $users = DB::table('samusua')->join('samperf', 'USUA_PERF', '=', 'PERF_IDPE')->select(
- 'USUA_IDUS as IDUSUARIO',
- 'USUA_NOMB as NOMBRE',
- 'USUA_APPA as APEPAT',
- 'USUA_APMA as APEMAT',
- 'USUA_EMAI as EMAIL',
- 'PERF_NOPE as PERFIL',
- 'USUA_ESTA as ESTATUS',
- )->get();
- }catch(PDOException $e){
- return $this->responseController->makeResponse(true, "No se pudo realizar la consulta a la base.", [], 500);
- }
-
- return $this->responseController->makeresponse(false, "EXITO", $users);
- }
- public function getUser($id){
- try{
- $user = DB::table('samusua')->select(
- 'USUA_IDUS as IDUSUARIO',
- 'USUA_NOMB as NOMBRE',
- 'USUA_APPA as APEPAT',
- 'USUA_APMA as APEMAT',
- 'USUA_EMAI as EMAIL',
- 'USUA_PERF as PERFIL',
- 'USUA_ESTA as ESTATUS'
- )->where('USUA_IDUS', '=', $id)->get()->first();
- }catch(PDOException $e){
- return $this->responseController->makeResponse(true, "No se pudo realizar la consulta a la base.", [], 500);
- }
- if(is_null($user)){
- return $this->responseController->makeResponse(true, "El usuario consultado no existe.", [], 404);
- }
-
- return $this->responseController->makeresponse(false, "EXITO", $user);
- }
- public function getProfiles(){
- try{
- $profiles = DB::table('samperf')->select(
- 'PERF_IDPE as IDPERFIL',
- 'PERF_NOPE as NOMBREPERFIL',
- 'PERF_PERM as PERMISOS'
- )->get();
- }catch(PDOException $e){
- return $this->responseController->makeResponse(true, "No se pudo realizar la consulta a la base.", [], 500);
- }
-
- return $this->responseController->makeresponse(false, "EXITO", $profiles);
- }
- public function updateUser(Request $request){
- $validator = Validator::make($request->all(), [
- 'id' => 'required|string',
- 'name' => 'required|string|max:50',
- 'fApe' => 'required|string|max:50',
- 'email' => 'required|string|email',
- 'perf' => 'required|integer',
- ]);
- if($validator->fails()){
- return $this->responseController->makeResponse(
- true,
- "Se encontraron uno o más errores.",
- $this->responseController->makeErrors(
- $validator->errors()->messages()
- ),
- 401
- );
- }
- $user = $request->all();
- try{
- DB::table('samusua')->where('USUA_IDUS', '=', $user['id'])->update([
- 'USUA_NOMB' => $user['name'],
- 'USUA_APPA' => $user['fApe'],
- 'USUA_APMA' => array_key_exists('sApe', $user) ? $user['sApe'] : null,
- 'USUA_EMAI' => $user['email'],
- 'USUA_PERF' => $user['perf']
- ]);
- }catch(PDOException $e){
- return $this->responseController->makeResponse(true, "No se pudo realizar la actualización del usuario.", [], 500);
- }
- return $this->responseController->makeResponse(false, "EXITO: Actualización correcta.");
- }
- public function createUser(Request $request){
- $validator = Validator::make($request->all(), [
- 'name' => 'required|string|max:50',
- 'fApe' => 'required|string|max:50',
- 'email' => 'required|string|email',
- 'perf' => 'required|integer',
- 'password' => 'required|string|min:8|confirmed',
- ]);
- if($validator->fails()){
- return $this->responseController->makeResponse(
- true,
- "Se encontraron uno o más errores.",
- $this->responseController->makeErrors(
- $validator->errors()->messages()
- ),
- 401
- );
- }
- $user = $request->all();
- try{
- $userVer = DB::table('samusua')->where('USUA_EMAI', '=', $user['email'])->get()->first();
- }catch(PDOException $e){
- return $this->responseController->makeResponse(true, "No se pudo realizar la consulta a la base.", [], 500);
- }
- if($userVer){
- return $this->responseController->makeResponse(true, "El correo electrónico ya se encuentra registrado en la base.", [], 401);
- }
- try{
- $lastID = DB::table('samusua')->orderByDesc('USUA_IDUS')->limit(1)->get()->first();
- }catch(PDOException $e){
- return $this->responseController->makeResponse(true, "No se pudo realizar la consulta a la base.", [], 500);
- }
- $idNum = intval(substr($lastID->USUA_IDUS, 3));
- $idNum++;
- $idUsr = "SAM";
- if($idNum < 10) $idUsr .= "00$idNum";
- else if($idNum < 100) $idUsr .= "0$idNum";
- else $idUsr .= "$idNum";
- $pass = Hash::make($user['password']);
-
- try{
- DB::table('samusua')->insert([
- 'USUA_IDUS' => $idUsr,
- 'USUA_NOMB' => $user['name'],
- 'USUA_APPA' => $user['fApe'],
- 'USUA_APMA' => array_key_exists('sApe', $user) ? $user['sApe'] : null,
- 'USUA_PERF' => $user['perf'],
- 'USUA_EMAI' => $user['email'],
- 'USUA_CONT' => $pass,
- ]);
- }catch(PDOException $e){
- return $this->responseController->makeResponse(true, "No se pudo realizar la inserción del usuario a la base.", [], 500);
- }
- return $this->responseController->makeResponse(false, "EXITO: Registro correcto.");
- }
- public function blockUser(Request $request){
- $validator = Validator::make($request->all(), [
- 'id' => 'required|string',
- 'estatus' => 'required|in:Activo,Inactivo'
- ]);
- if($validator->fails()){
- return $this->responseController->makeResponse(
- true,
- "Se encontraron uno o más errores.",
- $this->responseController->makeErrors(
- $validator->errors()->messages()
- ),
- 401
- );
- }
- $user = $request->all();
- try{
- DB::table('samusua')->where('USUA_IDUS', '=', $user['id'])->update([
- 'USUA_ESTA' => $user['estatus']
- ]);
- }catch(PDOException $e){
- return $this->responseController->makeResponse(true, "No se pudo realizar la actualización del usuario.", [], 500);
- }
- return $this->responseController->makeResponse(false, "EXITO: Actualización correcta.");
- }
- public function updatePass(Request $request){
- $validator = Validator::make($request->all(), [
- 'id' => 'required|string',
- 'password' => 'required|string|min:8|confirmed',
- ]);
- if($validator->fails()){
- return $this->responseController->makeResponse(
- true,
- "Se encontraron uno o más errores.",
- $this->responseController->makeErrors(
- $validator->errors()->messages()
- ),
- 401
- );
- }
- $user = $request->all();
- $newPass = Hash::make($user['password']);
- try{
- $usr = DB::table('samusua')->select('USUA_CONT')->where('USUA_IDUS', '=', $user['id'])->get()->first();
- }catch(PDOException $e){
- return $this->responseController->makeResponse(true, "No se pudo realizar la consulta a la base.", [], 500);
- }
- if(is_null($usr)){
- return $this->responseController->makeResponse(true, "El usuario consultado no existe.", [], 404);
- }
- if(Hash::check($user['password'], $usr->USUA_CONT)){
- return $this->responseController->makeResponse(true, "La contraseña nueva es igual a la anterior.", [], 401);
- }
- try{
- DB::table('samusua')->where('USUA_IDUS', '=', $user['id'])->update([
- 'USUA_CONT' => $newPass
- ]);
- }catch(PDOException $e){
- return $this->responseController->makeResponse(true, "No se pudo realizar la actualización del usuario.", [], 500);
- }
- return $this->responseController->makeResponse(false, "EXITO: Actualización correcta.");
- }
- public function deleteUser(Request $request){
- $validator = Validator::make($request->all(), [
- 'id' => 'required|string',
- ]);
- if($validator->fails()){
- return $this->responseController->makeResponse(
- true,
- "Se encontraron uno o más errores.",
- $this->responseController->makeErrors(
- $validator->errors()->messages()
- ),
- 401
- );
- }
- $user = $request->all();
- try{
- DB::table('samusua')->where('USUA_IDUS', '=', $user['id'])->update([
- 'USUA_ESTA' => 'Eliminado'
- ]);
- }catch(PDOException $e){
- return $this->responseController->makeResponse(true, "No se pudo realizar la actualización del usuario.", [], 500);
- }
- return $this->responseController->makeResponse(false, "EXITO: Actualización correcta.");
- }
- }
|