UsersProfilesController.php 61 KB

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