UsersProfilesController.php 79 KB

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