UsersProfilesController.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Support\Facades\Validator;
  6. use Illuminate\Support\Facades\Hash;
  7. class UsersProfilesController extends Controller{
  8. private $responseController;
  9. public function __construct($responseController = new ResponseController()){
  10. $this->responseController = $responseController;
  11. }
  12. public function getUsers(){
  13. try{
  14. $users = DB::table('samusua')->join('samperf', 'USUA_PERF', '=', 'PERF_IDPE')->select(
  15. 'USUA_IDUS as IDUSUARIO',
  16. 'USUA_NOMB as NOMBRE',
  17. 'USUA_APPA as APEPAT',
  18. 'USUA_APMA as APEMAT',
  19. 'USUA_EMAI as EMAIL',
  20. 'PERF_NOPE as PERFIL',
  21. 'USUA_ESTA as ESTATUS',
  22. )->get();
  23. }catch(PDOException $e){
  24. return $this->responseController->makeResponse(true, "No se pudo realizar la consulta a la base.", [], 500);
  25. }
  26. return $this->responseController->makeresponse(false, "EXITO", $users);
  27. }
  28. public function getUser($id){
  29. try{
  30. $user = DB::table('samusua')->select(
  31. 'USUA_IDUS as IDUSUARIO',
  32. 'USUA_NOMB as NOMBRE',
  33. 'USUA_APPA as APEPAT',
  34. 'USUA_APMA as APEMAT',
  35. 'USUA_EMAI as EMAIL',
  36. 'USUA_PERF as PERFIL',
  37. 'USUA_ESTA as ESTATUS'
  38. )->where('USUA_IDUS', '=', $id)->get()->first();
  39. }catch(PDOException $e){
  40. return $this->responseController->makeResponse(true, "No se pudo realizar la consulta a la base.", [], 500);
  41. }
  42. if(is_null($user)){
  43. return $this->responseController->makeResponse(true, "El usuario consultado no existe.", [], 404);
  44. }
  45. return $this->responseController->makeresponse(false, "EXITO", $user);
  46. }
  47. public function getProfiles(){
  48. try{
  49. $profiles = DB::table('samperf')->select(
  50. 'PERF_IDPE as IDPERFIL',
  51. 'PERF_NOPE as NOMBREPERFIL',
  52. 'PERF_PERM as PERMISOS'
  53. )->get();
  54. }catch(PDOException $e){
  55. return $this->responseController->makeResponse(true, "No se pudo realizar la consulta a la base.", [], 500);
  56. }
  57. return $this->responseController->makeresponse(false, "EXITO", $profiles);
  58. }
  59. public function updateUser(Request $request){
  60. $validator = Validator::make($request->all(), [
  61. 'id' => 'required|string',
  62. 'name' => 'required|string|max:50',
  63. 'fApe' => 'required|string|max:50',
  64. 'email' => 'required|string|email',
  65. 'perf' => 'required|integer',
  66. ]);
  67. if($validator->fails()){
  68. return $this->responseController->makeResponse(
  69. true,
  70. "Se encontraron uno o más errores.",
  71. $this->responseController->makeErrors(
  72. $validator->errors()->messages()
  73. ),
  74. 401
  75. );
  76. }
  77. $user = $request->all();
  78. try{
  79. DB::table('samusua')->where('USUA_IDUS', '=', $user['id'])->update([
  80. 'USUA_NOMB' => $user['name'],
  81. 'USUA_APPA' => $user['fApe'],
  82. 'USUA_APMA' => array_key_exists('sApe', $user) ? $user['sApe'] : null,
  83. 'USUA_EMAI' => $user['email'],
  84. 'USUA_PERF' => $user['perf']
  85. ]);
  86. }catch(PDOException $e){
  87. return $this->responseController->makeResponse(true, "No se pudo realizar la actualización del usuario.", [], 500);
  88. }
  89. return $this->responseController->makeResponse(false, "EXITO: Actualización correcta.");
  90. }
  91. public function createUser(Request $request){
  92. $validator = Validator::make($request->all(), [
  93. 'name' => 'required|string|max:50',
  94. 'fApe' => 'required|string|max:50',
  95. 'email' => 'required|string|email',
  96. 'perf' => 'required|integer',
  97. 'password' => 'required|string|min:8|confirmed',
  98. ]);
  99. if($validator->fails()){
  100. return $this->responseController->makeResponse(
  101. true,
  102. "Se encontraron uno o más errores.",
  103. $this->responseController->makeErrors(
  104. $validator->errors()->messages()
  105. ),
  106. 401
  107. );
  108. }
  109. $user = $request->all();
  110. try{
  111. $userVer = DB::table('samusua')->where('USUA_EMAI', '=', $user['email'])->get()->first();
  112. }catch(PDOException $e){
  113. return $this->responseController->makeResponse(true, "No se pudo realizar la consulta a la base.", [], 500);
  114. }
  115. if($userVer){
  116. return $this->responseController->makeResponse(true, "El correo electrónico ya se encuentra registrado en la base.", [], 401);
  117. }
  118. try{
  119. $lastID = DB::table('samusua')->orderByDesc('USUA_IDUS')->limit(1)->get()->first();
  120. }catch(PDOException $e){
  121. return $this->responseController->makeResponse(true, "No se pudo realizar la consulta a la base.", [], 500);
  122. }
  123. $idNum = intval(substr($lastID->USUA_IDUS, 3));
  124. $idNum++;
  125. $idUsr = "SAM";
  126. if($idNum < 10) $idUsr .= "00$idNum";
  127. else if($idNum < 100) $idUsr .= "0$idNum";
  128. else $idUsr .= "$idNum";
  129. $pass = Hash::make($user['password']);
  130. try{
  131. DB::table('samusua')->insert([
  132. 'USUA_IDUS' => $idUsr,
  133. 'USUA_NOMB' => $user['name'],
  134. 'USUA_APPA' => $user['fApe'],
  135. 'USUA_APMA' => array_key_exists('sApe', $user) ? $user['sApe'] : null,
  136. 'USUA_PERF' => $user['perf'],
  137. 'USUA_EMAI' => $user['email'],
  138. 'USUA_CONT' => $pass,
  139. ]);
  140. }catch(PDOException $e){
  141. return $this->responseController->makeResponse(true, "No se pudo realizar la inserción del usuario a la base.", [], 500);
  142. }
  143. return $this->responseController->makeResponse(false, "EXITO: Registro correcto.");
  144. }
  145. public function blockUser(Request $request){
  146. $validator = Validator::make($request->all(), [
  147. 'id' => 'required|string',
  148. 'estatus' => 'required|in:Activo,Inactivo'
  149. ]);
  150. if($validator->fails()){
  151. return $this->responseController->makeResponse(
  152. true,
  153. "Se encontraron uno o más errores.",
  154. $this->responseController->makeErrors(
  155. $validator->errors()->messages()
  156. ),
  157. 401
  158. );
  159. }
  160. $user = $request->all();
  161. try{
  162. DB::table('samusua')->where('USUA_IDUS', '=', $user['id'])->update([
  163. 'USUA_ESTA' => $user['estatus']
  164. ]);
  165. }catch(PDOException $e){
  166. return $this->responseController->makeResponse(true, "No se pudo realizar la actualización del usuario.", [], 500);
  167. }
  168. return $this->responseController->makeResponse(false, "EXITO: Actualización correcta.");
  169. }
  170. public function updatePass(Request $request){
  171. $validator = Validator::make($request->all(), [
  172. 'id' => 'required|string',
  173. 'password' => 'required|string|min:8|confirmed',
  174. ]);
  175. if($validator->fails()){
  176. return $this->responseController->makeResponse(
  177. true,
  178. "Se encontraron uno o más errores.",
  179. $this->responseController->makeErrors(
  180. $validator->errors()->messages()
  181. ),
  182. 401
  183. );
  184. }
  185. $user = $request->all();
  186. $newPass = Hash::make($user['password']);
  187. try{
  188. $usr = DB::table('samusua')->select('USUA_CONT')->where('USUA_IDUS', '=', $user['id'])->get()->first();
  189. }catch(PDOException $e){
  190. return $this->responseController->makeResponse(true, "No se pudo realizar la consulta a la base.", [], 500);
  191. }
  192. if(is_null($usr)){
  193. return $this->responseController->makeResponse(true, "El usuario consultado no existe.", [], 404);
  194. }
  195. if(Hash::check($user['password'], $usr->USUA_CONT)){
  196. return $this->responseController->makeResponse(true, "La contraseña nueva es igual a la anterior.", [], 401);
  197. }
  198. try{
  199. DB::table('samusua')->where('USUA_IDUS', '=', $user['id'])->update([
  200. 'USUA_CONT' => $newPass
  201. ]);
  202. }catch(PDOException $e){
  203. return $this->responseController->makeResponse(true, "No se pudo realizar la actualización del usuario.", [], 500);
  204. }
  205. return $this->responseController->makeResponse(false, "EXITO: Actualización correcta.");
  206. }
  207. public function deleteUser(Request $request){
  208. $validator = Validator::make($request->all(), [
  209. 'id' => 'required|string',
  210. ]);
  211. if($validator->fails()){
  212. return $this->responseController->makeResponse(
  213. true,
  214. "Se encontraron uno o más errores.",
  215. $this->responseController->makeErrors(
  216. $validator->errors()->messages()
  217. ),
  218. 401
  219. );
  220. }
  221. $user = $request->all();
  222. try{
  223. DB::table('samusua')->where('USUA_IDUS', '=', $user['id'])->update([
  224. 'USUA_ESTA' => 'Eliminado'
  225. ]);
  226. }catch(PDOException $e){
  227. return $this->responseController->makeResponse(true, "No se pudo realizar la actualización del usuario.", [], 500);
  228. }
  229. return $this->responseController->makeResponse(false, "EXITO: Actualización correcta.");
  230. }
  231. }