UsersProfilesController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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_ESTA as ESTATUS',
  53. 'PERF_PERM as PERMISOS'
  54. )->get();
  55. }catch(PDOException $e){
  56. return $this->responseController->makeResponse(true, "No se pudo realizar la consulta a la base.", [], 500);
  57. }
  58. return $this->responseController->makeresponse(false, "EXITO", $profiles);
  59. }
  60. public function getProfile($id){
  61. try{
  62. $profile = DB::table('samperf')->select(
  63. 'PERF_IDPE as IDPERFIL',
  64. 'PERF_NOPE as NOMBREPERFIL',
  65. 'PERF_ESTA as ESTATUS',
  66. 'PERF_PERM as PERMISOS'
  67. )->where('PERF_IDPE', '=', $id)->get()->first();
  68. }catch(PDOException $e){
  69. return $this->responseController->makeResponse(true, "No se pudo realizar la consulta a la base.", [], 500);
  70. }
  71. if(is_null($profile)){
  72. return $this->responseController->makeResponse(true, "El perfil consultado no existe.", [], 404);
  73. }
  74. return $this->responseController->makeresponse(false, "EXITO", $profile);
  75. }
  76. public function updateUser(Request $request){
  77. $validator = Validator::make($request->all(), [
  78. 'id' => 'required|string',
  79. 'name' => 'required|string|max:50',
  80. 'fApe' => 'required|string|max:50',
  81. 'email' => 'required|string|email',
  82. 'perf' => 'required|integer',
  83. ]);
  84. if($validator->fails()){
  85. return $this->responseController->makeResponse(
  86. true,
  87. "Se encontraron uno o más errores.",
  88. $this->responseController->makeErrors(
  89. $validator->errors()->messages()
  90. ),
  91. 401
  92. );
  93. }
  94. $user = $request->all();
  95. try{
  96. DB::table('samusua')->where('USUA_IDUS', '=', $user['id'])->update([
  97. 'USUA_NOMB' => $user['name'],
  98. 'USUA_APPA' => $user['fApe'],
  99. 'USUA_APMA' => array_key_exists('sApe', $user) ? $user['sApe'] : null,
  100. 'USUA_EMAI' => $user['email'],
  101. 'USUA_PERF' => $user['perf']
  102. ]);
  103. }catch(PDOException $e){
  104. return $this->responseController->makeResponse(true, "No se pudo realizar la actualización del usuario.", [], 500);
  105. }
  106. return $this->responseController->makeResponse(false, "EXITO: Actualización correcta.");
  107. }
  108. public function createUser(Request $request){
  109. $validator = Validator::make($request->all(), [
  110. 'name' => 'required|string|max:50',
  111. 'fApe' => 'required|string|max:50',
  112. 'email' => 'required|string|email',
  113. 'perf' => 'required|integer',
  114. 'password' => 'required|string|min:8|confirmed',
  115. ]);
  116. if($validator->fails()){
  117. return $this->responseController->makeResponse(
  118. true,
  119. "Se encontraron uno o más errores.",
  120. $this->responseController->makeErrors(
  121. $validator->errors()->messages()
  122. ),
  123. 401
  124. );
  125. }
  126. $user = $request->all();
  127. try{
  128. $userVer = DB::table('samusua')->where('USUA_EMAI', '=', $user['email'])->get()->first();
  129. }catch(PDOException $e){
  130. return $this->responseController->makeResponse(true, "No se pudo realizar la consulta a la base.", [], 500);
  131. }
  132. if($userVer){
  133. return $this->responseController->makeResponse(true, "El correo electrónico ya se encuentra registrado en la base.", [], 401);
  134. }
  135. try{
  136. $lastID = DB::table('samusua')->orderByDesc('USUA_IDUS')->limit(1)->get()->first();
  137. }catch(PDOException $e){
  138. return $this->responseController->makeResponse(true, "No se pudo realizar la consulta a la base.", [], 500);
  139. }
  140. $idNum = intval(substr($lastID->USUA_IDUS, 3));
  141. $idNum++;
  142. $idUsr = "SAM";
  143. if($idNum < 10) $idUsr .= "00$idNum";
  144. else if($idNum < 100) $idUsr .= "0$idNum";
  145. else $idUsr .= "$idNum";
  146. $pass = Hash::make($user['password']);
  147. try{
  148. DB::table('samusua')->insert([
  149. 'USUA_IDUS' => $idUsr,
  150. 'USUA_NOMB' => $user['name'],
  151. 'USUA_APPA' => $user['fApe'],
  152. 'USUA_APMA' => array_key_exists('sApe', $user) ? $user['sApe'] : null,
  153. 'USUA_PERF' => $user['perf'],
  154. 'USUA_EMAI' => $user['email'],
  155. 'USUA_CONT' => $pass,
  156. ]);
  157. }catch(PDOException $e){
  158. return $this->responseController->makeResponse(true, "No se pudo realizar la inserción del usuario a la base.", [], 500);
  159. }
  160. return $this->responseController->makeResponse(false, "EXITO: Registro correcto.");
  161. }
  162. public function blockUser(Request $request){
  163. $validator = Validator::make($request->all(), [
  164. 'id' => 'required|string',
  165. 'estatus' => 'required|in:Activo,Inactivo'
  166. ]);
  167. if($validator->fails()){
  168. return $this->responseController->makeResponse(
  169. true,
  170. "Se encontraron uno o más errores.",
  171. $this->responseController->makeErrors(
  172. $validator->errors()->messages()
  173. ),
  174. 401
  175. );
  176. }
  177. $user = $request->all();
  178. try{
  179. DB::table('samusua')->where('USUA_IDUS', '=', $user['id'])->update([
  180. 'USUA_ESTA' => $user['estatus']
  181. ]);
  182. }catch(PDOException $e){
  183. return $this->responseController->makeResponse(true, "No se pudo realizar la actualización del usuario.", [], 500);
  184. }
  185. return $this->responseController->makeResponse(false, "EXITO: Actualización correcta.");
  186. }
  187. public function updatePass(Request $request){
  188. $validator = Validator::make($request->all(), [
  189. 'id' => 'required|string',
  190. 'password' => 'required|string|min:8|confirmed',
  191. ]);
  192. if($validator->fails()){
  193. return $this->responseController->makeResponse(
  194. true,
  195. "Se encontraron uno o más errores.",
  196. $this->responseController->makeErrors(
  197. $validator->errors()->messages()
  198. ),
  199. 401
  200. );
  201. }
  202. $user = $request->all();
  203. $newPass = Hash::make($user['password']);
  204. try{
  205. $usr = DB::table('samusua')->select('USUA_CONT')->where('USUA_IDUS', '=', $user['id'])->get()->first();
  206. }catch(PDOException $e){
  207. return $this->responseController->makeResponse(true, "No se pudo realizar la consulta a la base.", [], 500);
  208. }
  209. if(is_null($usr)){
  210. return $this->responseController->makeResponse(true, "El usuario consultado no existe.", [], 404);
  211. }
  212. if(Hash::check($user['password'], $usr->USUA_CONT)){
  213. return $this->responseController->makeResponse(true, "La contraseña nueva es igual a la anterior.", [], 401);
  214. }
  215. try{
  216. DB::table('samusua')->where('USUA_IDUS', '=', $user['id'])->update([
  217. 'USUA_CONT' => $newPass
  218. ]);
  219. }catch(PDOException $e){
  220. return $this->responseController->makeResponse(true, "No se pudo realizar la actualización del usuario.", [], 500);
  221. }
  222. return $this->responseController->makeResponse(false, "EXITO: Actualización correcta.");
  223. }
  224. public function deleteUser(Request $request){
  225. $validator = Validator::make($request->all(), [
  226. 'id' => 'required|string',
  227. ]);
  228. if($validator->fails()){
  229. return $this->responseController->makeResponse(
  230. true,
  231. "Se encontraron uno o más errores.",
  232. $this->responseController->makeErrors(
  233. $validator->errors()->messages()
  234. ),
  235. 401
  236. );
  237. }
  238. $user = $request->all();
  239. try{
  240. DB::table('samusua')->where('USUA_IDUS', '=', $user['id'])->update([
  241. 'USUA_ESTA' => 'Eliminado'
  242. ]);
  243. }catch(PDOException $e){
  244. return $this->responseController->makeResponse(true, "No se pudo realizar la actualización del usuario.", [], 500);
  245. }
  246. return $this->responseController->makeResponse(false, "EXITO: Actualización correcta.");
  247. }
  248. public function getModules(){
  249. try{
  250. $modules = DB::table('sammodu')->select(
  251. 'MODU_IDMO as IDMODULO',
  252. 'MODU_NOMO as NOMBREMODULO'
  253. )->get();
  254. }catch(PDOException $e){
  255. return $this->responseController->makeResponse(true, "No se pudo realizar la consulta a la base.", [], 500);
  256. }
  257. return $this->responseController->makeresponse(false, "EXITO", $modules);
  258. }
  259. }