UsersProfilesController.php 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644
  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. use Illuminate\Support\Carbon;
  8. class UsersProfilesController extends Controller{
  9. private $responseController;
  10. private $encryptionController;
  11. private $functionsController;
  12. private $moduleCode = 'S002V01M02USPE';
  13. public function __construct(){
  14. $this->responseController = new ResponseController();
  15. $this->encryptionController = new EncryptionController();
  16. $this->functionsController = new FunctionsController();
  17. }
  18. public function getUsers($idUser, $line){
  19. DB::enableQueryLog();
  20. $idUser = $this->encryptionController->decrypt($idUser);
  21. if(!$idUser){
  22. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  23. }
  24. $usr = DB::table('S002V01TUSUA')->where([
  25. ['USUA_IDUS', '=', $idUser],
  26. ['USUA_NULI', '=', $line],
  27. ])->first();
  28. if(is_null($usr)){
  29. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no existe', [], 400);
  30. }
  31. $users = DB::table('S002V01TUSUA')
  32. ->join('S002V01TPERF', 'USUA_PERF', '=', 'PERF_IDPE')
  33. ->leftJoin('S002V01TBIAC', 'USUA_ULCO', '=', 'BIAC_IDCO')
  34. ->leftJoin('S002V01TPACO', 'USUA_PCRE', '=', 'PACO_IDPC')
  35. ->select(
  36. 'USUA_IDUS as IDUSUARIO',
  37. 'USUA_NOMB as NOMBRE',
  38. 'USUA_APPA as APEPAT',
  39. 'USUA_APMA as APEMAT',
  40. 'USUA_COEL as EMAIL',
  41. DB::raw("CONCAT(PERF_NOPE, ' (', PERF_IDPE, ')') AS PERFIL"),
  42. DB::raw("IF(ISNULL(PACO_IDPC), FALSE, TRUE) AS TIENE_PANEL"),
  43. DB::raw("IF(ISNULL(PACO_IDPC), '-', CONCAT(PACO_NPCO, ' (', PACO_IDPC, ')')) AS PANEL_CONTROL"),
  44. 'USUA_ESTA as ESTATUS',
  45. 'BIAC_FECO as ULCON'
  46. )->where('USUA_NULI', '=', $line)->get()->all();
  47. foreach($users as $key=>$user){
  48. $user->IDUSUARIO = $this->encryptionController->encrypt($user->IDUSUARIO);
  49. $user->TIENE_PANEL = $user->TIENE_PANEL == 1;
  50. $users[$key] = $user;
  51. }
  52. $now = $this->functionsController->now();
  53. $nowStr = $now->toDateTimeString();
  54. $actions = DB::getQueryLog();
  55. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  56. $idac = $this->functionsController->registerActivity(
  57. $line,
  58. $this->moduleCode,
  59. 'S002V01F01ADUS',
  60. 'S002V01P01COUS',
  61. 'Consulta',
  62. "El usuario $name (" . $usr->USUA_IDUS . ") consultó los usuarios registrados.",
  63. $idUser,
  64. $nowStr,
  65. 'S002V01S01GEUS'
  66. );
  67. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  68. return $this->responseController->makeresponse(false, "EXITO", $users);
  69. }
  70. public function getUser($id, $idUser, $line){
  71. DB::enableQueryLog();
  72. $idUser = $this->encryptionController->decrypt($idUser);
  73. if(!$idUser){
  74. return $this->responseController->makeResponse(true, 'El ID del usuaio que realizó la solicitud no está encriptado correctamente', [], 400);
  75. }
  76. $id = $this->encryptionController->decrypt($id);
  77. if(!$id){
  78. return $this->responseController->makeResponse(true, "El id del usuario no está encriptado correctamente.", [], 400);
  79. }
  80. $usr = DB::table('S002V01TUSUA')->where([
  81. ['USUA_IDUS', '=', $idUser],
  82. ['USUA_NULI', '=', $line],
  83. ])->first();
  84. if(is_null($usr)){
  85. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no existe', [], 400);
  86. }
  87. $user = DB::table('S002V01TUSUA')->select(
  88. 'USUA_IDUS AS IDUSUARIO',
  89. 'USUA_NOMB AS NOMBRE',
  90. 'USUA_APPA AS APEPAT',
  91. 'USUA_APMA AS APEMAT',
  92. 'USUA_COEL AS EMAIL',
  93. 'USUA_PERF AS PERFIL',
  94. 'USUA_ESTA AS ESTATUS',
  95. )->where([
  96. ['USUA_IDUS', '=', $id],
  97. ['USUA_NULI', '=', $line],
  98. ])->get()->first();
  99. if(is_null($user)){
  100. return $this->responseController->makeResponse(true, "El usuario consultado no existe.", [], 404);
  101. }
  102. $prof = DB::table('S002V01TPERF')->where([
  103. ['PERF_NULI', '=', $line],
  104. ['PERF_IDPE', '=', $user->PERFIL],
  105. ])->first();
  106. $user->PERFNOMB = $prof->PERF_NOPE;
  107. $now = $this->functionsController->now();
  108. $nowStr = $now->toDateTimeString();
  109. $actions = DB::getQueryLog();
  110. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  111. $nameCons = $this->functionsController->joinName($user->NOMBRE, $user->APEPAT, $user->APEMAT);
  112. $idac = $this->functionsController->registerActivity(
  113. $line,
  114. $this->moduleCode,
  115. 'S002V01F01ADUS',
  116. 'S002V01P01COUS',
  117. 'Consulta',
  118. "El usuario $name (" . $usr->USUA_IDUS . ") consultó al usuario $nameCons (" . $user->IDUSUARIO . ").",
  119. $idUser,
  120. $nowStr,
  121. 'S002V01S01GEUS'
  122. );
  123. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  124. return $this->responseController->makeresponse(false, "EXITO", $user);
  125. }
  126. public function getProfiles($idUser, $line){
  127. DB::enableQueryLog();
  128. $idUser = $this->encryptionController->decrypt($idUser);
  129. if(!$idUser){
  130. return $this->responseController->makeResponse(true, 'El ID del usuaio que realizó la solicitud no está encriptado correctamente', [], 400);
  131. }
  132. $usr = DB::table('S002V01TUSUA')->where([
  133. ['USUA_IDUS', '=', $idUser],
  134. ['USUA_NULI', '=', $line]
  135. ])->first();
  136. if(is_null($usr)){
  137. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  138. }
  139. $profiles = DB::table('S002V01TPERF')->select(
  140. 'PERF_IDPE AS IDPERFIL',
  141. 'PERF_NOPE AS NOMBREPERFIL',
  142. 'PERF_PERM AS PERMISOS',
  143. 'PERF_ESTA AS ESTADO',
  144. 'PERF_USRE AS USRREG',
  145. 'PERF_FERE AS FECREG',
  146. 'PERF_USMO AS USRMOD',
  147. 'PERF_FEMO AS FECMOD',
  148. )->where('PERF_NULI', '=', $line)->orderBy('PERF_IDPE', 'desc')->get()->all();
  149. foreach($profiles as $key=>$profile){
  150. $relatedUsers = DB::table('S002V01TUSUA')->where([
  151. ['USUA_NULI', '=', $line],
  152. ['USUA_PERF', '=', $profile->IDPERFIL]
  153. ])->get()->all();
  154. $profile->IDPERFIL = $this->encryptionController->encrypt($profile->IDPERFIL);
  155. $profile->PERMISOS = $this->encryptionController->encrypt($profile->PERMISOS);
  156. $usrReg = DB::table('S002V01TUSUA')->where([
  157. ['USUA_NULI', '=', $line],
  158. ['USUA_IDUS', '=', $profile->USRREG]
  159. ])->first();
  160. $usrRegName = $this->functionsController->joinName($usrReg->USUA_NOMB, $usrReg->USUA_APPA, $usrReg->USUA_APMA);
  161. $profile->USRREG = $usrRegName . " (" . $profile->USRREG . ")";
  162. if(!is_null($profile->USRMOD)){
  163. $usrMod = DB::table('S002V01TUSUA')->where([
  164. ['USUA_NULI', '=', $line],
  165. ['USUA_IDUS', '=', $profile->USRMOD]
  166. ])->first();
  167. $usrModName = $this->functionsController->joinName($usrMod->USUA_NOMB, $usrMod->USUA_APPA, $usrMod->USUA_APMA);
  168. $profile->USRMOD = $usrModName . " (" . $profile->USRMOD . ")";
  169. }
  170. $profile->USUARIOS_RELACIONADOS = count($relatedUsers);
  171. $profiles[$key] = $profile;
  172. }
  173. $now = $this->functionsController->now();
  174. $nowStr = $now->toDateTimeString();
  175. $actions = DB::getQueryLog();
  176. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  177. $idac = $this->functionsController->registerActivity(
  178. $line,
  179. $this->moduleCode,
  180. 'S002V01F01ADPE',
  181. 'S002V01P01COPE',
  182. 'Consulta',
  183. "El usuario $name (" . $usr->USUA_IDUS . ") consultó los perfiles registrados.",
  184. $idUser,
  185. $nowStr,
  186. 'S002V01S02GEPE'
  187. );
  188. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  189. return $this->responseController->makeresponse(false, "EXITO", $profiles);
  190. }
  191. public function getProfile($idProfile, $idUser, $line){
  192. DB::enableQueryLog();
  193. $idUser = $this->encryptionController->decrypt($idUser);
  194. if(!$idUser){
  195. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  196. }
  197. $usr = DB::table('S002V01TUSUA')->where([
  198. ['USUA_IDUS', '=', $idUser],
  199. ['USUA_NULI', '=', $line],
  200. ])->first();
  201. if(is_null($usr)){
  202. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no existe', [], 400);
  203. }
  204. $idProfile = $this->encryptionController->decrypt($idProfile);
  205. if(!$idProfile){
  206. return $this->responseController->makeResponse(true, "El ID del perfil no está encriptado correctamente.", [], 401);
  207. }
  208. $profile = DB::table('S002V01TPERF')->select(
  209. 'PERF_IDPE AS IDPERFIL',
  210. 'PERF_NOPE AS NOMBREPERFIL',
  211. 'PERF_ESTA AS ESTATUS',
  212. 'PERF_PERM AS PERMISOS',
  213. )->where([
  214. ['PERF_IDPE', '=', $idProfile],
  215. ['PERF_NULI', '=', $line],
  216. ])->first();
  217. if(is_null($profile)){
  218. return $this->responseController->makeResponse(true, "El perfil consultado no existe.", [], 404);
  219. }
  220. $profile->IDPERFIL = $this->encryptionController->encrypt($profile->IDPERFIL);
  221. $profilePermissions = json_decode($profile->PERMISOS, true);
  222. $permissions = [];
  223. $modules = DB::table('S002V01TMODU')->where('MODU_NULI', '=', $line)->get()->all();
  224. $moduleIndex = 0;
  225. foreach($modules as $module){
  226. $submodules = DB::table('S002V01TSUBM')->where([
  227. ['SUBM_IDMO', '=', $module->MODU_IDMO],
  228. ['SUBM_NULI', '=', $line]
  229. ])->get()->all();
  230. $permissionsPerSubmodule = [];
  231. $submoduleIndex = 0;
  232. foreach($submodules as $submodule){
  233. $functions = DB::table('S002V01TFUNC')->where([
  234. ['FUNC_IDMO', '=', $module->MODU_IDMO],
  235. ['FUNC_IDSM', '=', $submodule->SUBM_IDSM],
  236. ['FUNC_NULI', '=', $line],
  237. ])->get()->all();
  238. $permissionsPerFunction = [];
  239. $functionIndex = 0;
  240. foreach($functions as $function){
  241. $screens = DB::table('S002V01TPANT')->where([
  242. ['PANT_IDFU', '=', $function->FUNC_IDFU],
  243. ['PANT_NULI', '=', $line],
  244. ])->get()->all();
  245. $permissionsPerScreen = [];
  246. $screenIndex = 0;
  247. foreach($screens as $screen){
  248. $screenAccess = 0;
  249. if(isset($profilePermissions['permissions'][$moduleIndex]['children'][$submoduleIndex]['children'][$functionIndex]['children'][$screenIndex]['access'])){
  250. $screenAccess = $profilePermissions['permissions'][$moduleIndex]['children'][$submoduleIndex]['children'][$functionIndex]['children'][$screenIndex]['access'];
  251. }
  252. $permissionsPerScreen[] = [
  253. 'id' => $this->encryptionController->encrypt($screen->PANT_IDPA),
  254. 'name' => $screen->PANT_NOMB,
  255. 'access' => $screenAccess
  256. ];
  257. $screenIndex++;
  258. }
  259. $functionAcces = 0;
  260. if(isset($profilePermissions['permissions'][$moduleIndex]['children'][$submoduleIndex]['children'][$functionIndex]['access'])){
  261. $functionAcces = $profilePermissions['permissions'][$moduleIndex]['children'][$submoduleIndex]['children'][$functionIndex]['access'];
  262. }
  263. $permissionsPerFunction[] = [
  264. 'id' => $this->encryptionController->encrypt($function->FUNC_IDFU),
  265. 'name' => $function->FUNC_NOMB,
  266. 'access' => $functionAcces,
  267. 'children' => $permissionsPerScreen,
  268. ];
  269. $functionIndex++;
  270. }
  271. $submoduleAccess = 0;
  272. if(isset($profilePermissions['permissions'][$moduleIndex]['children'][$submoduleIndex]['access'])){
  273. $submoduleAccess = $profilePermissions['permissions'][$moduleIndex]['children'][$submoduleIndex]['access'];
  274. }
  275. $permissionsPerSubmodule[] = [
  276. 'id' => $this->encryptionController->encrypt($submodule->SUBM_IDSM),
  277. 'name' => $submodule->SUBM_NOMB,
  278. 'access' => $submoduleAccess,
  279. 'children' => $permissionsPerFunction,
  280. ];
  281. $submoduleIndex++;
  282. }
  283. $functions = DB::table('S002V01TFUNC')->where([
  284. ['FUNC_IDMO', '=', $module->MODU_IDMO],
  285. ['FUNC_IDSM', '=', null],
  286. ['FUNC_NULI', '=', $line],
  287. ])->get()->all();
  288. $permissionsPerFunction = [];
  289. $functionIndex = 0;
  290. foreach($functions as $function){
  291. $screens = DB::table('S002V01TPANT')->where([
  292. ['PANT_IDFU', '=', $function->FUNC_IDFU],
  293. ['PANT_NULI', '=', $line],
  294. ])->get()->all();
  295. $permissionsPerScreen = [];
  296. $screenIndex = 0;
  297. foreach($screens as $screen){
  298. $screenAccess = 0;
  299. if(isset($profilePermissions['permissions'][$moduleIndex]['children'][$functionIndex]['children'][$screenIndex]['access'])){
  300. $screenAccess = $profilePermissions['permissions'][$moduleIndex]['children'][$functionIndex]['children'][$screenIndex]['access'];
  301. }
  302. $permissionsPerScreen[] = [
  303. 'id' => $this->encryptionController->encrypt($screen->PANT_IDPA),
  304. 'name' => $screen->PANT_NOMB,
  305. 'access' => $screenAccess
  306. ];
  307. $screenIndex++;
  308. }
  309. $functionAcces = 0;
  310. if(isset($profilePermissions['permissions'][$moduleIndex]['children'][$functionIndex]['access'])){
  311. $functionAcces = $profilePermissions['permissions'][$moduleIndex]['children'][$functionIndex]['access'];
  312. }
  313. $permissionsPerFunction[] = [
  314. 'id' => $this->encryptionController->encrypt($function->FUNC_IDFU),
  315. 'name' => $function->FUNC_NOMB,
  316. 'access' => $functionAcces,
  317. 'children' => $permissionsPerScreen,
  318. ];
  319. $functionIndex++;
  320. }
  321. $permissionsArr = [];
  322. foreach($permissionsPerSubmodule as $perm){ $permissionsArr[] = $perm; }
  323. foreach($permissionsPerFunction as $perm){ $permissionsArr[] = $perm; }
  324. $moduleAccess = $profilePermissions['permissions'][$moduleIndex]['access'];
  325. $permissions[] = [
  326. 'id' => $this->encryptionController->encrypt($module->MODU_IDMO),
  327. 'name' => $module->MODU_NOMO,
  328. 'access' => $moduleAccess,
  329. 'children' => $permissionsArr,
  330. ];
  331. $moduleIndex++;
  332. }
  333. $now = $this->functionsController->now();
  334. $nowStr = $now->toDateTimeString();
  335. $profile->PERMISOS = ['permissions' => $permissions];
  336. $actions = DB::getQueryLog();
  337. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  338. $idac = $this->functionsController->registerActivity(
  339. $line,
  340. $this->moduleCode,
  341. 'S002V01F01ADPE',
  342. 'S002V01P01COPE',
  343. 'Consulta',
  344. "El usuario $name (" . $usr->USUA_IDUS . ") consultó el perfil " . $profile->NOMBREPERFIL . " (" . $profile->IDPERFIL . ").",
  345. $idUser,
  346. $nowStr,
  347. 'S002V01S02GEPE'
  348. );
  349. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  350. return $this->responseController->makeresponse(false, "EXITO", $profile);
  351. }
  352. public function updateUser(Request $request){
  353. DB::enableQueryLog();
  354. $validator = Validator::make($request->all(), [
  355. 'id' => 'required|string',
  356. 'name' => 'required|string|max:50',
  357. 'fApe' => 'required|string|max:50',
  358. 'email' => 'required|string|email',
  359. 'perf' => 'required|integer',
  360. 'id_user' => 'required|string',
  361. 'linea' => 'required|integer'
  362. ]);
  363. if($validator->fails()){
  364. return $this->responseController->makeResponse(
  365. true,
  366. "Se encontraron uno o más errores.",
  367. $this->responseController->makeErrors(
  368. $validator->errors()->messages()
  369. ),
  370. 401
  371. );
  372. }
  373. $user = $request->all();
  374. $idUser = $this->encryptionController->decrypt($user['id']);
  375. if(!$idUser){
  376. return $this->responseController->makeResponse(true, "El id del usuario que desea modificar no está encriptado correctamente.", [], 400);
  377. }
  378. $usrMod = DB::table('S002V01TUSUA')->where([
  379. ['USUA_IDUS', '=', $idUser],
  380. ['USUA_NULI', '=', $user['linea']],
  381. ])->first();
  382. if(is_null($usrMod)){
  383. return $this->responseController->makeResponse(true, 'El usuario que desea modificar no exise.', [], 404);
  384. }
  385. $idUserMod = $this->encryptionController->decrypt($user['id_user']);
  386. if(!$idUserMod){
  387. return $this->responseController->makeResponse(true, "El id del usuario que modifica no está encriptado correctamente.", [], 400);
  388. }
  389. $usr = DB::table('S002V01TUSUA')->where([
  390. ['USUA_IDUS', '=', $idUserMod],
  391. ['USUA_NULI', '=', $user['linea']],
  392. ])->first();
  393. if(is_null($usr)){
  394. return $this->responseController->makeResponse(true, 'El usuario que realiza la solicitud no existe', [], 404);
  395. }
  396. if($idUser == $idUserMod){
  397. return $this->responseController->makeResponse(true, "El usuario no puede modificar su propio registro.", [], 401);
  398. }
  399. $now = $this->functionsController->now();
  400. $nowStr = $now->toDateTimeString();
  401. $correos = DB::table('S002V01TUSUA')->where([
  402. ['USUA_COEL', '=', $user['email']],
  403. ['USUA_IDUS', '!=', $idUser]
  404. ])->get()->all();
  405. if(count($correos) > 0){
  406. return $this->responseController->makeResponse(true, "El nuevo correo electrónico ya está registrado en la base.", [], 401);
  407. }
  408. $perfil = DB::table('S002V01TPERF')->where('PERF_IDPE', '=', $user['perf'])->get()->first();
  409. if(is_null($perfil)){
  410. return $this->responseController->makeResponse(true, "El perfil asignado no existe.", [], 404);
  411. }
  412. DB::table('S002V01TUSUA')->where([
  413. ['USUA_IDUS', '=', $idUser],
  414. ['USUA_NULI', '=', $user['linea']]
  415. ])->update([
  416. 'USUA_NOMB' => $user['name'],
  417. 'USUA_APPA' => $user['fApe'],
  418. 'USUA_APMA' => array_key_exists('sApe', $user) ? $user['sApe'] : null,
  419. 'USUA_COEL' => $user['email'],
  420. 'USUA_PERF' => $user['perf'],
  421. 'USUA_USMO' => $idUserMod,
  422. 'USUA_FEMO' => $nowStr
  423. ]);
  424. $now = $this->functionsController->now();
  425. $nowStr = $now->toDateTimeString();
  426. $actions = DB::getQueryLog();
  427. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  428. $nameMod = $this->functionsController->joinName($usrMod->USUA_NOMB, $usrMod->USUA_APPA, $usrMod->USUA_APMA);
  429. $idac = $this->functionsController->registerActivity(
  430. $user['linea'],
  431. $this->moduleCode,
  432. 'S002V01F01ADUS',
  433. 'S002V01P02RAUS',
  434. 'Actualización',
  435. "El usuario $name (" . $usr->USUA_IDUS . ") actualizó al usuario $nameMod (" . $usrMod->USUA_IDUS . ").",
  436. $idUser,
  437. $nowStr,
  438. 'S002V01S01GEUS'
  439. );
  440. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $user['linea']);
  441. return $this->responseController->makeResponse(false, "EXITO: Actualización correcta.");
  442. }
  443. public function createUser(Request $request){
  444. DB::enableQueryLog();
  445. $validator = Validator::make($request->all(), [
  446. 'id_user' => 'required|string',
  447. 'linea' => 'required|integer',
  448. 'name' => 'required|string|max:50',
  449. 'fApe' => 'required|string|max:50',
  450. 'perf' => 'required|integer',
  451. 'email' => 'required|string|email',
  452. 'password' => 'required|string|min:8|confirmed',
  453. ]);
  454. if($validator->fails()){
  455. return $this->responseController->makeResponse(
  456. true,
  457. "Se encontraron uno o más errores.",
  458. $this->responseController->makeErrors(
  459. $validator->errors()->messages()
  460. ),
  461. 401
  462. );
  463. }
  464. $user = $request->all();
  465. $idUsre = $this->encryptionController->decrypt($user['id_user']);
  466. if(!$idUsre){
  467. return $this->responseController->makeResponse(true, "El ID del usuario que registra no está encriptado correctamente.", [], 400);
  468. }
  469. $usr = DB::table('S002V01TUSUA')->where([
  470. ['USUA_IDUS', '=', $idUsre],
  471. ['USUA_NULI', '=', $user['linea']]
  472. ])->first();
  473. if(is_null($usr)){
  474. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  475. }else if($usr->USUA_ESTA == 'Eliminado'){
  476. return $this->responseController->makeResponse(true, "El usuario que realiza el registro está eliminado.", [], 401);
  477. }else if($usr->USUA_ESTA == 'Inactivo'){
  478. return $this->responseController->makeResponse(true, "El usuario que realiza el registro está boloqueado.", [], 401);
  479. }
  480. $email = DB::table('S002V01TUSUA')->where([
  481. ['USUA_COEL', '=', $user['email']],
  482. ['USUA_NULI', '=', $user['linea']]
  483. ])->get()->first();
  484. if(!is_null($email)){
  485. return $this->responseController->makeResponse(true, "El correo electrónico ya se encuentra registrado.", [], 401);
  486. }
  487. $perf = DB::table('S002V01TPERF')->where('PERF_IDPE', '=', $user['perf'])->get()->first();
  488. if(is_null($perf)){
  489. return $this->responseController->makeResponse(true, "El perfil asignado no existe.", [], 404);
  490. }
  491. $passEnc = Hash::make($user['password']);
  492. $apemat = isset($user['sApe']) ? $user['sApe'] : null;
  493. $now = $this->functionsController->now();
  494. $nowStr = $now->toDateTimeString();
  495. $idUser = DB::table('S002V01TUSUA')->insertGetId([
  496. 'USUA_NULI' => $user['linea'],
  497. 'USUA_NOMB' => $user['name'],
  498. 'USUA_APPA' => $user['fApe'],
  499. 'USUA_APMA' => $apemat,
  500. 'USUA_PERF' => $user['perf'],
  501. 'USUA_CONT' => $passEnc,
  502. 'USUA_COEL' => $user['email'],
  503. 'USUA_USRE' => $idUsre,
  504. 'USUA_FERE' => $nowStr,
  505. ]);
  506. $actions = DB::getQueryLog();
  507. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  508. $nameReg = $this->functionsController->joinName($user['name'], $user['fApe'], $apemat);
  509. $idac = $this->functionsController->registerActivity(
  510. $user['linea'],
  511. $this->moduleCode,
  512. 'S002V01F01ADUS',
  513. 'S002V01P02RAUS',
  514. 'Registro',
  515. "El usuario $name (" . $usr->USUA_IDUS . ") registró al usuario $nameReg ($idUser).",
  516. $idUsre,
  517. $nowStr,
  518. 'S002V01S01GEUS'
  519. );
  520. $this->functionsController->registerLog($actions, $idUsre, $nowStr, $idac, $user['linea']);
  521. return $this->responseController->makeResponse(false, "EXITO: Registro correcto.");
  522. }
  523. public function blockUser(Request $request){
  524. DB::enableQueryLog();
  525. $validator = Validator::make($request->all(), [
  526. 'id' => 'required|string',
  527. 'estatus' => 'required|in:Activo,Inactivo',
  528. 'id_user' => 'required|string',
  529. 'linea' => 'required|integer'
  530. ]);
  531. if($validator->fails()){
  532. return $this->responseController->makeResponse(
  533. true,
  534. "Se encontraron uno o más errores.",
  535. $this->responseController->makeErrors(
  536. $validator->errors()->messages()
  537. ),
  538. 401
  539. );
  540. }
  541. $user = $request->all();
  542. $idUser = $this->encryptionController->decrypt($user['id']);
  543. if(!$idUser){
  544. return $this->responseController->makeResponse(true, "El id del usuario que desea modificar no está encriptado correctamente.", [], 400);
  545. }
  546. $usr = DB::table('S002V01TUSUA')->where([
  547. ['USUA_IDUS', '=', $idUser],
  548. ['USUA_NULI', '=', $user['linea']],
  549. ])->first();
  550. if(is_null($usr)){
  551. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no existe', [], 400);
  552. }
  553. $idUserMod = $this->encryptionController->decrypt($user['id_user']);
  554. if(!$idUserMod){
  555. return $this->responseController->makeResponse(true, "El id del usuario que modifica no está encriptado correctamente.", [], 400);
  556. }
  557. if($idUser == $idUserMod){
  558. return $this->responseController->makeResponse(true, "El usuario no puede modificarse a sí mismo", [], 400);
  559. }
  560. $usrMod = DB::table('S002V01TUSUA')->where([
  561. ['USUA_IDUS', '=', $idUserMod],
  562. ['USUA_NULI', '=', $user['linea']],
  563. ])->first();
  564. if(is_null($usrMod)){
  565. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no existe', [], 400);
  566. }
  567. $now = $this->functionsController->now();
  568. $nowStr = $now->toDateTimeString();
  569. DB::table('S002V01TUSUA')->where('USUA_IDUS', '=', $idUserMod)->update([
  570. 'USUA_ESTA' => $user['estatus'],
  571. 'USUA_USMO' => $idUser,
  572. 'USUA_FEMO' => $nowStr
  573. ]);
  574. $actions = DB::getQueryLog();
  575. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  576. $nameMod = $this->functionsController->joinName($usrMod->USUA_NOMB, $usrMod->USUA_APPA, $usrMod->USUA_APMA);
  577. $tipoAccion = $user['estatus'] == 'Activo' ? 'desbloqueó' : 'bloqueó';
  578. $idac = $this->functionsController->registerActivity(
  579. $user['linea'],
  580. $this->moduleCode,
  581. 'S002V01F02BDAC',
  582. '-',
  583. 'Actualización',
  584. "El usuario $name (" . $usr->USUA_IDUS . ") $tipoAccion al usuario $nameMod (" . $usrMod->USUA_IDUS . ").",
  585. $idUser,
  586. $nowStr,
  587. 'S002V01S01GEUS'
  588. );
  589. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $user['linea']);
  590. return $this->responseController->makeResponse(false, "EXITO: Actualización correcta.");
  591. }
  592. public function updatePass(Request $request){
  593. DB::enableQueryLog();
  594. $validator = Validator::make($request->all(), [
  595. 'id' => 'required|string',
  596. 'password' => 'required|string|min:8|confirmed',
  597. 'id_user' => 'required|string',
  598. 'linea' => 'required|integer'
  599. ]);
  600. if($validator->fails()){
  601. return $this->responseController->makeResponse(
  602. true,
  603. "Se encontraron uno o más errores.",
  604. $this->responseController->makeErrors(
  605. $validator->errors()->messages()
  606. ),
  607. 401
  608. );
  609. }
  610. $user = $request->all();
  611. $idUser = $this->encryptionController->decrypt($user['id']);
  612. if(!$idUser){
  613. return $this->responseController->makeResponse(true, "El id del usuario que desea modificar no está encriptado correctamente.", [], 400);
  614. }
  615. $usrMod = DB::table('S002V01TUSUA')->where([
  616. ['USUA_IDUS', '=', $idUser],
  617. ['USUA_NULI', '=', $user['linea']],
  618. ])->first();
  619. if(is_null($usrMod)){
  620. return $this->responseController->makeResponse(true, 'El usuario que desea modificar no existe', [], 400);
  621. }
  622. $idUserMod = $this->encryptionController->decrypt($user['id_user']);
  623. if(!$idUserMod){
  624. return $this->responseController->makeResponse(true, "El id del usuario que modifica no está encriptado correctamente.", [], 400);
  625. }
  626. if($idUser == $idUserMod){
  627. return $this->responseController->makeResponse(true, "El usuario no puede modificarse a sí mismo.", [], 401);
  628. }
  629. $usr = DB::table('S002V01TUSUA')->where([
  630. ['USUA_IDUS', '=', $idUserMod],
  631. ['USUA_NULI', '=', $user['linea']],
  632. ])->first();
  633. if(is_null($usr)){
  634. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no existe', [], 400);
  635. }
  636. $lastPass = $usr->USUA_CONT;
  637. if(Hash::check($user['password'], $lastPass)){
  638. return $this->responseController->makeResponse(true, "La contraseña nueva es igual a la anterior.", [], 401);
  639. }
  640. $newPass = Hash::make($user['password']);
  641. $now = $this->functionsController->now();
  642. $nowStr = $now->toDateTimeString();
  643. DB::table('S002V01TUSUA')->where([
  644. ['USUA_IDUS', '=', $idUser],
  645. ['USUA_NULI', '=', $user['linea']],
  646. ])->update([
  647. 'USUA_CONT' => $newPass,
  648. 'USUA_USMO' => $idUserMod,
  649. 'USUA_FEMO' => $nowStr
  650. ]);
  651. $actions = DB::getQueryLog();
  652. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  653. $idac = $this->functionsController->registerActivity(
  654. $user['linea'],
  655. $this->moduleCode,
  656. 'S002V01F01ADUS',
  657. 'S002V01P01COUS',
  658. 'Consulta',
  659. "El usuario $name (" . $usr->USUA_IDUS . ") consultó los usuarios registrados.",
  660. $idUser,
  661. $nowStr,
  662. 'S002V01S01GEUS'
  663. );
  664. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $user['linea']);
  665. return $this->responseController->makeResponse(false, "EXITO: Actualización correcta.");
  666. }
  667. public function deleteUser(Request $request){
  668. DB::enableQueryLog();
  669. $validator = Validator::make($request->all(), [
  670. 'id' => 'required|string',
  671. 'id_user' => 'required|string',
  672. 'linea' => 'required|integer'
  673. ]);
  674. if($validator->fails()){
  675. return $this->responseController->makeResponse(
  676. true,
  677. "Se encontraron uno o más errores.",
  678. $this->responseController->makeErrors(
  679. $validator->errors()->messages()
  680. ),
  681. 401
  682. );
  683. }
  684. $user = $request->all();
  685. $idUser = $this->encryptionController->decrypt($user['id']);
  686. if(!$idUser){
  687. return $this->responseController->makeResponse(true, "El id del usuario que desea eliminar no está encriptado correctamente", [], 400);
  688. }
  689. $usr = DB::table('S002V01TUSUA')->where([
  690. ['USUA_IDUS', '=', $idUser],
  691. ['USUA_NULI', '=', $user['linea']]
  692. ])->first();
  693. if(is_null($usr)){
  694. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  695. }
  696. $idUserMod = $this->encryptionController->decrypt($user['id_user']);
  697. if(!$idUserMod){
  698. return $this->responseController->makeResponse(true, "El id del usuario que elimina no está encriptado correctamente", [], 400);
  699. }
  700. if($idUser == $idUserMod){
  701. return $this->responseController->makeResponse(true, "El usuario no puede eliminarse a sí mismo", [], 401);
  702. }
  703. $usrMod = DB::table('S002V01TUSUA')->where([
  704. ['USUA_IDUS', '=', $idUserMod],
  705. ['USUA_NULI', '=', $user['linea']]
  706. ])->first();
  707. if(is_null($usrMod)){
  708. return $this->responseController->makeResponse(true, 'El usuario que desea eliminar no está registrado', [], 404);
  709. }
  710. $now = $this->functionsController->now();
  711. $nowStr = $now->toDateTimeString();
  712. DB::table('S002V01TUSUA')->where('USUA_IDUS', '=', $idUser)->update([
  713. 'USUA_ESTA' => 'Eliminado',
  714. 'USUA_USMO' => $idUserMod,
  715. 'USUA_FEMO' => $nowStr
  716. ]);
  717. $actions = DB::getQueryLog();
  718. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  719. $nameMod = $this->functionsController->joinName($usrMod->USUA_NOMB, $usrMod->USUA_APPA, $usrMod->USUA_APMA);
  720. $idac = $this->functionsController->registerActivity(
  721. $user['linea'],
  722. $this->moduleCode,
  723. 'S002V01F04ELUS',
  724. '-',
  725. 'Eliminación',
  726. "El usuario $name (" . $usr->USUA_IDUS . ") eleimnó al usuario $nameMod (" . $usrMod->USUA_IDUS . ").",
  727. $idUser,
  728. $nowStr,
  729. 'S002V01S01GEUS'
  730. );
  731. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $user['linea']);
  732. return $this->responseController->makeResponse(false, "EXITO: Actualización correcta.");
  733. }
  734. public function getModules($idUser, $line){
  735. DB::enableQueryLog();
  736. $idUser = $this->encryptionController->decrypt($idUser);
  737. if(!$idUser){
  738. return $this->responseController->makeResponse(true, 'El ID del usuaio que realizó la solicitud no está encriptado correctamente', [], 400);
  739. }
  740. $usr = DB::table('S002V01TUSUA')->where([
  741. ['USUA_IDUS', '=', $idUser],
  742. ['USUA_NULI', '=', $line]
  743. ])->first();
  744. if(is_null($usr)){
  745. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  746. }
  747. $modules = DB::table('S002V01TMODU')->select(
  748. 'MODU_IDMO as IDMODULO',
  749. 'MODU_NOMO as NOMBREMODULO',
  750. 'MODU_ICON as ICONOMODULO',
  751. )->where('MODU_NULI', '=', $line)->get()->all();
  752. $now = $this->functionsController->now();
  753. $nowStr = $now->toDateTimeString();
  754. $actions = DB::getQueryLog();
  755. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  756. $idac = $this->functionsController->registerActivity(
  757. $line,
  758. '-',
  759. '-',
  760. '-',
  761. 'Consulta',
  762. "El usuario $name (" . $usr->USUA_IDUS . ") consultó los módulos del sistema.",
  763. $idUser,
  764. $nowStr
  765. );
  766. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  767. return $this->responseController->makeresponse(false, "EXITO", $modules);
  768. }
  769. public function getMenu($idUser, $line){
  770. DB::enableQueryLog();
  771. $idUser = $this->encryptionController->decrypt($idUser);
  772. if(!$idUser){
  773. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  774. }
  775. $usr = DB::table('S002V01TUSUA')->where([
  776. ['USUA_IDUS', '=', $idUser],
  777. ['USUA_NULI', '=', $line]
  778. ])->first();
  779. if(is_null($usr)){
  780. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  781. }
  782. $modules = DB::table('S002V01TMODU')->select(
  783. 'MODU_IDMO as IDMODULO',
  784. 'MODU_NOMO as NOMBREMODULO',
  785. 'MODU_ICON as ICONOMODULO',
  786. )->get()->all();
  787. $mods = [];
  788. foreach($modules as $module){
  789. $submodules = DB::table('S002V01TSUBM')->select(
  790. 'SUBM_IDSM as IDSUBMODULO',
  791. 'SUBM_NOMB as NOMBRESUBMODULO',
  792. 'SUBM_ICON AS ICONOSUBMODULO'
  793. )->where('SUBM_IDMO', '=', $module->IDMODULO)->get()->all();
  794. $menusMod = DB::table('S002V01TFUNC')->select(
  795. 'FUNC_IDFU AS IDFUNCION',
  796. 'FUNC_NOMB AS NOMBREFUNCION',
  797. 'FUNC_ICON AS ICONOFUNCION'
  798. )->where([
  799. ['FUNC_IDMO', '=', $module->IDMODULO],
  800. ['FUNC_IDSM', '=', null]
  801. ])->get()->all();
  802. $module->SUBMODULES = [];
  803. foreach($submodules as $submodule){
  804. $menus = DB::table('S002V01TFUNC')->select(
  805. 'FUNC_IDFU AS IDFUNCION',
  806. 'FUNC_NOMB AS NOMBREFUNCION',
  807. 'FUNC_ICON AS ICONOFUNCION'
  808. )->where([
  809. ['FUNC_IDMO', '=', $module->IDMODULO],
  810. ['FUNC_IDSM', '=', $submodule->IDSUBMODULO]
  811. ])->get()->all();
  812. $menuInd = 0;
  813. foreach($menus as $funcion){
  814. $pantallas = DB::table('S002V01TPANT')->select(
  815. 'PANT_IDPA AS IDPANTALLA',
  816. 'PANT_NOMB AS NOMBREPANTALLA'
  817. )->where([
  818. ['PANT_IDFU', '=', $funcion->IDFUNCION],
  819. ['PANT_NULI', '=', $line]
  820. ])->get()->all();
  821. $menus[$menuInd]->PANTALLAS = $pantallas;
  822. $menuInd++;
  823. }
  824. $submodule->FUNCIONES = [];
  825. foreach($menus as $menu){
  826. $submodule->FUNCIONES[] = $menu;
  827. }
  828. $module->SUBMODULES[] = $submodule;
  829. }
  830. $menModInd = 0;
  831. foreach($menusMod as $funcion){
  832. $pantallas = DB::table('S002V01TPANT')->select(
  833. 'PANT_IDPA AS IDPANTALLA',
  834. 'PANT_NOMB AS NOMBREPANTALLA'
  835. )->where([
  836. ['PANT_IDFU', '=', $funcion->IDFUNCION],
  837. ['PANT_NULI', '=', $line]
  838. ])->get()->all();
  839. $menusMod[$menModInd]->PANTALLAS = $pantallas;
  840. $menModInd++;
  841. }
  842. $module->MENUS = $menusMod;
  843. $mods[] = $module;
  844. }
  845. $now = $this->functionsController->now();
  846. $nowStr = $now->toDateTimeString();
  847. $actions = DB::getQueryLog();
  848. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  849. $idac = $this->functionsController->registerActivity(
  850. $line,
  851. '-',
  852. '-',
  853. '-',
  854. 'Consulta',
  855. "El usuario $name (" . $usr->USUA_IDUS . ") consultó el menú del sistema.",
  856. $idUser,
  857. $nowStr
  858. );
  859. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  860. return $this->responseController->makeResponse(false, "EXITO", $mods);
  861. }
  862. public function buildInitialPermissions($idUser, $line){
  863. DB::enableQueryLog();
  864. $idUser = $this->encryptionController->decrypt($idUser);
  865. if(!$idUser){
  866. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente', [], 400);
  867. }
  868. $usr = DB::table('S002V01TUSUA')->where([
  869. ['USUA_IDUS', '=', $idUser],
  870. ['USUA_NULI', '=', $line],
  871. ])->first();
  872. if(is_null($usr)){
  873. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no existe', [], 400);
  874. }
  875. $permissions = [];
  876. //Se obtienen todos los modulos
  877. $modules = DB::table('S002V01TMODU')->where('MODU_NULI', '=', $line)->get()->all();
  878. //se consultan los submodulos de cada modulo
  879. foreach($modules as $module){
  880. $submodules = DB::table('S002V01TSUBM')->where([
  881. ['SUBM_IDMO', '=', $module->MODU_IDMO],
  882. ['SUBM_NULI', '=', $line]
  883. ])->get()->all();
  884. $permissionsPerSubmodule = [];
  885. $hasSubmodules = false;
  886. foreach($submodules as $submodule){
  887. //Se consultan las funciones
  888. $functions = DB::table('S002V01TFUNC')->where([
  889. ['FUNC_IDMO', '=', $module->MODU_IDMO],
  890. ['FUNC_IDSM', '=', $submodule->SUBM_IDSM],
  891. ['FUNC_NULI', '=', $line],
  892. ])->get()->all();
  893. $permissionsPerFunction = [];
  894. foreach($functions as $function){
  895. $screens = DB::table('S002V01TPANT')->where([
  896. ['PANT_IDFU', '=', $function->FUNC_IDFU],
  897. ['PANT_NULI', '=', $line],
  898. ])->get()->all();
  899. $permissionsPerScreen = [];
  900. foreach($screens as $screen){
  901. $permissionsPerScreen[] = [
  902. 'id' => $screen->PANT_IDPA,
  903. 'name' => $screen->PANT_NOMB,
  904. 'access' => 0
  905. ];
  906. }
  907. $permissionsPerFunction[] = [
  908. 'id' => $function->FUNC_IDFU,
  909. 'name' => $function->FUNC_NOMB,
  910. 'access' => 0,
  911. 'children' => $permissionsPerScreen,
  912. ];
  913. }
  914. $permissionsPerSubmodule[] = [
  915. 'id' => $submodule->SUBM_IDSM,
  916. 'name' => $submodule->SUBM_NOMB,
  917. 'access' => 0,
  918. 'children' => $permissionsPerFunction,
  919. ];
  920. $hasSubmodules = true;
  921. }
  922. //Se consultan las funciones
  923. $functions = DB::table('S002V01TFUNC')->where([
  924. ['FUNC_IDMO', '=', $module->MODU_IDMO],
  925. ['FUNC_IDSM', '=', null],
  926. ['FUNC_NULI', '=', $line],
  927. ])->get()->all();
  928. $permissionsPerFunction = [];
  929. foreach($functions as $function){
  930. $screens = DB::table('S002V01TPANT')->where([
  931. ['PANT_IDFU', '=', $function->FUNC_IDFU],
  932. ['PANT_NULI', '=', $line],
  933. ])->get()->all();
  934. $permissionsPerScreen = [];
  935. foreach($screens as $screen){
  936. $permissionsPerScreen[] = [
  937. 'id' => $screen->PANT_IDPA,
  938. 'name' => $screen->PANT_NOMB,
  939. 'access' => 0
  940. ];
  941. }
  942. $permissionsPerFunction[] = [
  943. 'id' => $function->FUNC_IDFU,
  944. 'name' => $function->FUNC_NOMB,
  945. 'access' => 0,
  946. 'children' => $permissionsPerScreen,
  947. ];
  948. }
  949. $permissionsArr = [];
  950. foreach($permissionsPerSubmodule as $perm){ $permissionsArr[] = $perm; }
  951. foreach($permissionsPerFunction as $perm){ $permissionsArr[] = $perm; }
  952. $permissions[] = [
  953. 'id' => $module->MODU_IDMO,
  954. 'name' => $module->MODU_NOMO,
  955. 'access' => 0,
  956. 'children' => $permissionsArr,
  957. ];
  958. }
  959. $now = $this->functionsController->now();
  960. $nowStr = $now->toDateTimeString();
  961. $actions = DB::getQueryLog();
  962. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  963. $idac = $this->functionsController->registerActivity(
  964. $line,
  965. $this->moduleCode,
  966. 'S002V01F01ADPE',
  967. 'S002V01P02REPE',
  968. 'Consulta',
  969. "El usuario $name (" . $usr->USUA_IDUS . ") consultó el arreglo de permisos iniciales.",
  970. $idUser,
  971. $nowStr,
  972. 'S002V01S02GEPE'
  973. );
  974. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  975. return $this->responseController->makeresponse(false, "EXITO", ['permissions' => $permissions]);
  976. }
  977. public function updateProfile(Request $request){
  978. DB::enableQueryLog();
  979. $validator = Validator::make($request->all(), [
  980. 'id' => 'required|string',
  981. 'permissions' => 'required|json',
  982. 'name' => 'required|string|max:50',
  983. 'id_user' => 'required|string',
  984. 'linea' => 'required|integer'
  985. ]);
  986. if($validator->fails()){
  987. return $this->responseController->makeResponse(
  988. true,
  989. "Se encontraron uno o más errores.",
  990. $this->responseController->makeErrors(
  991. $validator->errors()->messages()
  992. ),
  993. 401
  994. );
  995. }
  996. $info = $request->all();
  997. $idUser = $this->encryptionController->decrypt($info['id_user']);
  998. if(!$idUser){
  999. return $this->responseController->makeResponse(true, "El ID del usuario que realizó la solicitud no fue encriptado correctamente", [], 400);
  1000. }
  1001. $usr = DB::table('S002V01TUSUA')->where([
  1002. ['USUA_IDUS', '=', $idUser],
  1003. ['USUA_NULI', '=', $info['linea']],
  1004. ])->first();
  1005. if(is_null($usr)){
  1006. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no existe', [], 404);
  1007. }
  1008. $idPerf = $this->encryptionController->decrypt($info['id']);
  1009. if(!$idPerf){
  1010. return $this->responseController->makeResponse(true, "El ID del perfil solicitado no fue encriptado correctamente", [], 400);
  1011. }
  1012. $perf = DB::table('S002V01TPERF')->where([
  1013. ['PERF_IDPE', '=', $idPerf],
  1014. ['PERF_NULI', '=', $info['linea']],
  1015. ])->first();
  1016. if(is_null($perf)){
  1017. return $this->responseController->makeResponse(true, 'El perfil solicitado no existe', [], 400);
  1018. }
  1019. $now = $this->functionsController->now();
  1020. $nowStr = $now->toDateTimeString();
  1021. DB::table('S002V01TPERF')->where([
  1022. ['PERF_IDPE', '=', $idPerf],
  1023. ['PERF_NULI', '=', $info['linea']],
  1024. ])->update([
  1025. 'PERF_PERM' => $info['permissions'],
  1026. 'PERF_NOPE' => $info['name'],
  1027. 'PERF_USMO' => $idUser,
  1028. 'PERF_FEMO' => $nowStr
  1029. ]);
  1030. $actions = DB::getQueryLog();
  1031. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1032. $idac = $this->functionsController->registerActivity(
  1033. $info['linea'],
  1034. $this->moduleCode,
  1035. 'S002V01F01ADPE',
  1036. 'S002V01P02REPE',
  1037. 'Actualización',
  1038. "El usuario $name (" . $usr->USUA_IDUS . ") actualizó el perfil " . $perf->PERF_NOPE . "(" . $perf->PERF_NOPE . ").",
  1039. $idUser,
  1040. $nowStr,
  1041. 'S002V01S02GEPE'
  1042. );
  1043. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $info['linea']);
  1044. return $this->responseController->makeResponse(false, "EXITO: Actualización correcta.");
  1045. }
  1046. public function deleteProfile(Request $request){
  1047. DB::enableQueryLog();
  1048. $validator = Validator::make($request->all(), [
  1049. 'id' => 'required|string',
  1050. 'id_user' => 'required|string',
  1051. 'linea' => 'required|integer'
  1052. ]);
  1053. if($validator->fails()){
  1054. return $this->responseController->makeResponse(
  1055. true,
  1056. "Se encontraron uno o más errores.",
  1057. $this->responseController->makeErrors(
  1058. $validator->errors()->messages()
  1059. ),
  1060. 401
  1061. );
  1062. }
  1063. $profile = $request->all();
  1064. $idUser = $this->encryptionController->decrypt($profile['id_user']);
  1065. if(!$idUser){
  1066. return $this->responseController->makeResponse(true, "El ID del usuario que solicitó la acción no está encriptado correctamente", [], 401);
  1067. }
  1068. $usr = DB::table('S002V01TUSUA')->where([
  1069. ['USUA_IDUS', '=', $idUser],
  1070. ['USUA_NULI', '=', $profile['linea']],
  1071. ])->first();
  1072. if(is_null($usr)){
  1073. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no existe', [], 404);
  1074. }
  1075. $idProf = $this->encryptionController->decrypt($profile['id']);
  1076. if(!$idProf){
  1077. return $this->responseController->makeResponse(true, "El ID del perfil seleccionado no está encriptado correctamente", [], 401);
  1078. }
  1079. $perf = DB::table('S002V01TPERF')->where([
  1080. ['PERF_IDPE', '=', $idProf],
  1081. ['PERF_NULI', '=', $profile['linea']],
  1082. ])->first();
  1083. if(is_null($perf)){
  1084. return $this->responseController->makeResponse(true, 'El perfil solicitado no existe', [], 404);
  1085. }
  1086. $users = DB::table('S002V01TUSUA')->where([
  1087. ['USUA_PERF', '=', $idProf],
  1088. ['USUA_NULI', '=', $profile['linea']]
  1089. ])->get()->all();
  1090. if(count($users) > 0){
  1091. return $this->responseController->makeResponse(true, "El perfil seleccionado no se puede eliminar porque hay usuarios asociados a él.", [], 401);
  1092. }
  1093. $now = $this->functionsController->now();
  1094. $nowStr = $now->toDateTimeString();
  1095. DB::table('S002V01TPERF')->where([
  1096. ['PERF_IDPE', '=', $idProf],
  1097. ['PERF_NULI', '=', $profile['linea']],
  1098. ])->update([
  1099. 'PERF_ESTA' => 'Eliminado',
  1100. 'PERF_USMO' => $idUser,
  1101. 'PERF_FEMO' => $nowStr
  1102. ]);
  1103. $actions = DB::getQueryLog();
  1104. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1105. $idac = $this->functionsController->registerActivity(
  1106. $profile['linea'],
  1107. $this->moduleCode,
  1108. 'S002V01F01ADPE',
  1109. '-',
  1110. 'Eliminación',
  1111. "El usuario $name (" . $usr->USUA_IDUS . ") eliminó el perfil " . $perf->PERF_NOPE . "($idProf).",
  1112. $idUser,
  1113. $nowStr,
  1114. 'S002V01S02GEPE'
  1115. );
  1116. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $profile['linea']);
  1117. return $this->responseController->makeResponse(false, "EXITO: Eliminación correcta.");
  1118. }
  1119. public function createProfile(Request $request){
  1120. DB::enableQueryLog();
  1121. $validator = Validator::make($request->all(), [
  1122. 'id_user' => 'required|string',
  1123. 'linea' => 'required|integer',
  1124. 'name' => 'required|string|min:8|max:50',
  1125. 'permissions' => 'required|json'
  1126. ]);
  1127. if($validator->fails()){
  1128. return $this->responseController->makeResponse(
  1129. true,
  1130. "Se encontraron uno o más errores.",
  1131. $this->responseController->makeErrors(
  1132. $validator->errors()->messages()
  1133. ),
  1134. 401
  1135. );
  1136. }
  1137. $profile = $request->all();
  1138. $idUser = $this->encryptionController->decrypt($profile['id_user']);
  1139. if(!$idUser){
  1140. return $this->responseController->makeResponse(true, 'El ID del usuario que realizó la solicitud no está encriptado correctamente.', [], 400);
  1141. }
  1142. $usr = DB::table('S002V01TUSUA')->where([
  1143. ['USUA_IDUS', '=', $idUser],
  1144. ['USUA_NULI', '=', $profile['linea']]
  1145. ])->first();
  1146. if(is_null($usr)){
  1147. return $this->responseController->makeResponse(true, 'El usuario que realizó la solicitud no está registrado', [], 404);
  1148. }else if($usr->USUA_ESTA == 'Eliminado'){
  1149. return $this->responseController->makeResponse(true, "El usuario que realiza el registro está eliminado.", [], 401);
  1150. }else if($usr->USUA_ESTA == 'Inactivo'){
  1151. return $this->responseController->makeResponse(true, "El usuario que realiza el registro está boloqueado.", [], 401);
  1152. }
  1153. $now = $this->functionsController->now();
  1154. $nowStr = $now ->toDateTimeString();
  1155. $newPerf = DB::table('S002V01TPERF')->insertGetId([
  1156. 'PERF_NULI' => $profile['linea'],
  1157. 'PERF_NOPE' => $profile['name'],
  1158. 'PERF_PERM' => $profile['permissions'],
  1159. 'PERF_USRE' => $idUser,
  1160. 'PERF_FERE' => $nowStr
  1161. ]);
  1162. $actions = DB::getQueryLog();
  1163. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1164. $idac = $this->functionsController->registerActivity(
  1165. $profile['linea'],
  1166. $this->moduleCode,
  1167. 'S002V01F01ADPE',
  1168. 'S002V01P02REPE',
  1169. 'Registro',
  1170. "El usuario $name (" . $usr->USUA_IDUS . ") registró el perfil $profile[name] ($newPerf).",
  1171. $idUser,
  1172. $nowStr,
  1173. 'S002V01S02GEPE'
  1174. );
  1175. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $profile['linea']);
  1176. return $this->responseController->makeResponse(false, 'EXITO');
  1177. }
  1178. public function getModule($idMod, $idUser, $line){
  1179. DB::enableQueryLog();
  1180. $idMod = $this->encryptionController->decrypt($idMod);
  1181. if(!$idMod){
  1182. return $this->responseController->makeResponse(true, 'El módulo solicitado no fue encriptado correctamente', [], 400);
  1183. }
  1184. $idUser = $this->encryptionController->decrypt($idUser);
  1185. if(!$idUser){
  1186. return $this->responseController->makeResponse(true, 'El ID del usuaio que realizó la solicitud no está encriptado correctamente', [], 400);
  1187. }
  1188. $usr = DB::table('S002V01TUSUA')->where([
  1189. ['USUA_IDUS', '=', $idUser],
  1190. ['USUA_NULI', '=', $line]
  1191. ])->first();
  1192. if(is_null($usr)){
  1193. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  1194. }
  1195. $module = DB::table('S002V01TMODU')->select(
  1196. 'MODU_IDMO as IDMODULO',
  1197. 'MODU_NOMO as NOMBREMODULO',
  1198. )->where([
  1199. ['MODU_NULI', '=', $line],
  1200. ['MODU_IDMO', '=', $idMod]
  1201. ])->first();
  1202. $now = $this->functionsController->now();
  1203. $actions = DB::getQueryLog();
  1204. $nowStr = $now->toDateTimeString();
  1205. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1206. $idac = $this->functionsController->registerActivity(
  1207. $line,
  1208. '-',
  1209. '-',
  1210. '-',
  1211. 'Consulta',
  1212. "El usuario $name (" . $usr->USUA_IDUS . ") consultó el módulo " . $idMod . " del sistema.",
  1213. $idUser,
  1214. $nowStr
  1215. );
  1216. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  1217. return $this->responseController->makeresponse(false, "EXITO", [
  1218. 'NOMBREMODULO' => is_null($module) ? '-' : $module->NOMBREMODULO
  1219. ]);
  1220. }
  1221. public function getSubmodule($idMod, $idSub, $idUser, $line){
  1222. DB::enableQueryLog();
  1223. $idMod = $this->encryptionController->decrypt($idMod);
  1224. if(!$idMod){
  1225. return $this->responseController->makeResponse(true, 'El módulo no fue encriptado correctamente', [], 400);
  1226. }
  1227. $idSub = $this->encryptionController->decrypt($idSub);
  1228. if(!$idSub){
  1229. return $this->responseController->makeResponse(true, 'El submódulo no fue encriptado correctamente', [], 400);
  1230. }
  1231. $idUser = $this->encryptionController->decrypt($idUser);
  1232. if(!$idUser){
  1233. return $this->responseController->makeResponse(true, 'El id del usuario que consulta no fue encriptado correctamente', [], 400);
  1234. }
  1235. $usr = DB::table('S002V01TUSUA')->where([
  1236. ['USUA_IDUS', '=', $idUser],
  1237. ['USUA_NULI', '=', $line]
  1238. ])->first();
  1239. if(is_null($usr)){
  1240. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  1241. }
  1242. $submodule = DB::table('S002V01TSUBM')->select(
  1243. 'SUBM_IDSM as IDSUBMODULO',
  1244. 'SUBM_NOMB as NOMBRESUBMODULO',
  1245. )->where([
  1246. ['SUBM_NULI', '=', $line],
  1247. ['SUBM_IDSM', '=', $idSub],
  1248. ['SUBM_IDMO', '=', $idMod],
  1249. ])->first();
  1250. $now = $this->functionsController->now();
  1251. $actions = DB::getQueryLog();
  1252. $nowStr = $now->toDateTimeString();
  1253. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1254. $idac = $this->functionsController->registerActivity(
  1255. $line,
  1256. '-',
  1257. '-',
  1258. '-',
  1259. 'Consulta',
  1260. "El usuario $name (" . $usr->USUA_IDUS . ") consultó el submódulo " . $idSub . " del sistema.",
  1261. $idUser,
  1262. $nowStr
  1263. );
  1264. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  1265. return $this->responseController->makeresponse(false, "EXITO", [
  1266. 'NOMBRESUBMODULO' => is_null($submodule) ? '-' : $submodule->NOMBRESUBMODULO
  1267. ]);
  1268. }
  1269. public function getFunction($idMod, $idSub, $idFun, $idUser, $line){
  1270. DB::enableQueryLog();
  1271. $idMod = $this->encryptionController->decrypt($idMod);
  1272. if(!$idMod){
  1273. return $this->responseController->makeResponse(true, 'El módulo no fue encriptado correctamente', [], 400);
  1274. }
  1275. $idSub = $this->encryptionController->decrypt($idSub);
  1276. if(!$idSub){
  1277. return $this->responseController->makeResponse(true, 'El submódulo no fue encriptado correctamente', [], 400);
  1278. }
  1279. $idFun = $this->encryptionController->decrypt($idFun);
  1280. if(!$idFun){
  1281. return $this->responseController->makeResponse(true, 'La función no fue encriptada correctamente', [], 400);
  1282. }
  1283. $idUser = $this->encryptionController->decrypt($idUser);
  1284. if(!$idUser){
  1285. return $this->responseController->makeResponse(true, 'El id del usuario que consulta no fue encriptado correctamente', [], 400);
  1286. }
  1287. $usr = DB::table('S002V01TUSUA')->where([
  1288. ['USUA_IDUS', '=', $idUser],
  1289. ['USUA_NULI', '=', $line]
  1290. ])->first();
  1291. if(is_null($usr)){
  1292. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  1293. }
  1294. $function = DB::table('S002V01TFUNC')->select(
  1295. 'FUNC_IDFU as IDFUNCION',
  1296. 'FUNC_NOMB as NOMBREFUNCION',
  1297. )->where([
  1298. ['FUNC_NULI', '=', $line],
  1299. ['FUNC_IDFU', '=', $idFun],
  1300. ['FUNC_IDMO', '=', $idMod],
  1301. ['FUNC_IDSM', '=', $idSub],
  1302. ])->first();
  1303. $now = $this->functionsController->now();
  1304. $nowStr = $now->toDateTimeString();
  1305. $actions = DB::getQueryLog();
  1306. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1307. $idac = $this->functionsController->registerActivity(
  1308. $line,
  1309. '-',
  1310. '-',
  1311. '-',
  1312. 'Consulta',
  1313. "El usuario $name (" . $usr->USUA_IDUS . ") consultó la función " . $idFun . " del sistema.",
  1314. $idUser,
  1315. $nowStr
  1316. );
  1317. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  1318. return $this->responseController->makeresponse(false, "EXITO", [
  1319. 'NOMBREFUNCION' => is_null($function) ? '-' : $function->NOMBREFUNCION
  1320. ]);
  1321. }
  1322. public function getScreen($idFun, $idPan, $idUser, $line){
  1323. DB::enableQueryLog();
  1324. $idFun = $this->encryptionController->decrypt($idFun);
  1325. if(!$idFun){
  1326. return $this->responseController->makeResponse(true, 'La función no fue encriptada correctamente', [], 400);
  1327. }
  1328. $idPan = $this->encryptionController->decrypt($idPan);
  1329. if(!$idPan){
  1330. return $this->responseController->makeResponse(true, 'La pantalla no fue encriptada correctamente', [], 400);
  1331. }
  1332. $idUser = $this->encryptionController->decrypt($idUser);
  1333. if(!$idUser){
  1334. return $this->responseController->makeResponse(true, 'El id del usuario que consulta no fue encriptado correctamente', [], 400);
  1335. }
  1336. $usr = DB::table('S002V01TUSUA')->where([
  1337. ['USUA_IDUS', '=', $idUser],
  1338. ['USUA_NULI', '=', $line]
  1339. ])->first();
  1340. if(is_null($usr)){
  1341. return $this->responseController->makeResponse(true, 'El usuario que realizó la consulta no está registrado', [], 404);
  1342. }
  1343. $screen = DB::table('S002V01TPANT')->select(
  1344. 'PANT_IDFU as IDPANTALLA',
  1345. 'PANT_NOMB as NOMBREPANTALLA',
  1346. )->where([
  1347. ['PANT_NULI', '=', $line],
  1348. ['PANT_IDPA', '=', $idPan],
  1349. ['PANT_IDFU', '=', $idFun],
  1350. ])->first();
  1351. $now = $this->functionsController->now();
  1352. $nowStr = $now->toDateTimeString();
  1353. $actions = DB::getQueryLog();
  1354. $name = $this->functionsController->joinName($usr->USUA_NOMB, $usr->USUA_APPA, $usr->USUA_APMA);
  1355. $idac = $this->functionsController->registerActivity(
  1356. $line,
  1357. '-',
  1358. '-',
  1359. '-',
  1360. 'Consulta',
  1361. "El usuario $name (" . $usr->USUA_IDUS . ") consultó la pantalla " . $idPan . " del sistema.",
  1362. $idUser,
  1363. $nowStr
  1364. );
  1365. $this->functionsController->registerLog($actions, $idUser, $nowStr, $idac, $line);
  1366. return $this->responseController->makeresponse(false, "EXITO", [
  1367. 'NOMBREPANTALLA' => is_null($screen) ? '-' : $screen->NOMBREPANTALLA
  1368. ]);
  1369. }
  1370. }