PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/myCore/controladores/usuarioController.php

https://gitlab.com/fabian.morales/Spencial
PHP | 264 lines | 227 code | 37 blank | 0 comment | 45 complexity | 98df4e6efbcf2beb429b3df1c5dc024b MD5 | raw file
  1. <?php
  2. class usuarioController extends myController{
  3. function index(){
  4. $paises = Pais::all();
  5. $estadosCorp = [];
  6. $estadosFact = [];
  7. $jusuario = JFactory::getUser();
  8. $compania = new Compania();
  9. if ($jusuario->id){
  10. $compania = Compania::where("id_usuario", $jusuario->id)->first();
  11. $estadosCorp = Departamento::where("id_pais", $compania->id_pais_corp)->get();
  12. $estadosFact = Departamento::where("id_pais", $compania->id_pais_fact)->get();
  13. }
  14. return myView::render("usuario.form_usuario", ["compania" => $compania, "jusuario" => $jusuario, "paises" => $paises, "estadosCorp" => $estadosCorp, "estadosFact" => $estadosFact]);
  15. }
  16. function mostrarLogin($redirect = ""){
  17. if (empty($redirect)){
  18. $redirect = myApp::getRequest()->getVar("redirect", "", "base64");
  19. }
  20. else{
  21. $redirect = base64_encode($redirect);
  22. }
  23. $usuario = JFactory::getUser();
  24. if (!$usuario->id){
  25. return myView::render("usuario.form_login", ["redirect" => $redirect]);
  26. }
  27. else{
  28. return $this->index();
  29. }
  30. }
  31. function login($username="", $password="", $redirect="", $noredir = false){
  32. $req = myApp::getRequest();
  33. if (!$redirect){
  34. if ($redirect = $req->getVar("redirect", "", "base64")) {
  35. $redirect = base64_decode($redirect);
  36. if (!JURI::isInternal($redirect)) {
  37. $redirect = '';
  38. }
  39. }
  40. }
  41. if (!$username){
  42. $username = $req->getVar('username_login', '', 'username');
  43. }
  44. if (!$password){
  45. $password = $req->getVar('password_login', '', "RAW");
  46. }
  47. $opciones = array();
  48. $opciones['remember'] = false;
  49. $opciones['return'] = $redirect;
  50. $credenciales = array();
  51. $credenciales['username'] = $username;
  52. $credenciales['password'] = $password;
  53. $error = myApp::login($credenciales, $opciones);
  54. if(!JError::isError($error)){
  55. if (!$noredir){
  56. if (!$redirect) {
  57. $redirect = 'index.php?option=com_my_component&controller=usuario';
  58. }
  59. myApp::redirect($redirect);
  60. }
  61. }
  62. else{
  63. myApp::mostrarMensaje($error->message, "error");
  64. }
  65. }
  66. function logout($noredir = false){
  67. $error = myApp::logout();
  68. $req = myApp::getRequest();
  69. if (!$noredir){
  70. if(!JError::isError($error)){
  71. if ($redirect = $req->getVar('redirect', '', 'base64')){
  72. $redirect = base64_decode($redirect);
  73. if (!JURI::isInternal($redirect)) {
  74. $redirect = 'index.php';
  75. }
  76. }
  77. else{
  78. $redirect = 'index.php';
  79. }
  80. if ($redirect && !(strpos($redirect, 'com_my_component'))){
  81. myApp::redirect($redirect);
  82. }
  83. }
  84. else{
  85. myApp::redirect('index.php?option=com_my_component&controller=usuario');
  86. }
  87. }
  88. }
  89. function guardarUsuario(){
  90. jimport('joomla.user.helper');
  91. $modelo = myApp::getModelo();
  92. $request = myApp::getRequest();
  93. $nuevo = false;
  94. $fecha = date('Y-m-d H:i:s');
  95. $usuario = new Usuario();
  96. $compania = new Compania();
  97. $usuarioJoomla = JFactory::getUser();
  98. if ($usuarioJoomla->id){
  99. $usuario = Usuario::find($usuarioJoomla->id);
  100. $compania_aux = Compania::where("id_usuario", $usuarioJoomla->id)->first();
  101. if (sizeof($compania_aux)){
  102. $compania = $compania_aux;
  103. }
  104. }
  105. else{
  106. $usuario->name = $request->getVar("nombre");
  107. $usuario->activation = substr(uniqid(), 1, 100);
  108. $usuario->registerDate = $fecha;
  109. $usuario->params = '';
  110. $nuevo = true;
  111. }
  112. $passwordOrig = $usuario->password;
  113. $usuario->username = $request->getVar("email");
  114. $usuario->lastvisitDate = $fecha;
  115. $password = $request->getVar('password', '', JREQUEST_ALLOWRAW);
  116. $usuario->fill($request->all());
  117. $compania->fill($request->all());
  118. if (is_numeric($compania->id_estado_corp)){
  119. $cnt = Departamento::where("id", $compania->id_estado_corp)->count();
  120. if ($cnt == 0){
  121. $compania->estado_corp = $compania->id_estado_corp;
  122. $compania->id_estado_corp = null;
  123. }
  124. }
  125. else{
  126. $compania->estado_corp = $compania->id_estado_corp;
  127. $compania->id_estado_corp = null;
  128. }
  129. if (is_numeric($compania->id_estado_fact)){
  130. $cnt = Departamento::where("id", $compania->id_estado_fact)->count();
  131. if ($cnt == 0){
  132. $compania->estado_fact = $compania->id_estado_fact;
  133. $compania->id_estado_fact = null;
  134. }
  135. }
  136. else{
  137. $compania->estado_fact = $compania->id_estado_fact;
  138. $compania->id_estado_fact = null;
  139. }
  140. $grupo = $modelo->getGrupoUser();
  141. if ($redirect = $request->getVar('_redirect', '', 'base64')) {
  142. $redirect = base64_decode($redirect);
  143. if (!JURI::isInternal($redirect)) {
  144. $redirect = '';
  145. }
  146. }
  147. if (!$password && !$usuarioJoomla->id){
  148. myApp::mostrarMensaje("Enter a password", "error");
  149. return false;
  150. }
  151. $usuarioEmail = Usuario::where("email", $usuario->email)->first();
  152. if (sizeof($usuarioEmail) && $usuarioEmail->id != $usuarioJoomla->id){
  153. myApp::mostrarMensaje("The email address is already in use", "error");
  154. return;
  155. }
  156. if ($password){
  157. $salt = JUserHelper::genRandomPassword(32);
  158. $crypt = JUserHelper::getCryptedPassword($password, $salt);
  159. $usuario->password = $crypt.':'.$salt;
  160. }
  161. else{
  162. $usuario->password = $passwordOrig;
  163. }
  164. $exito = false;
  165. if ($usuario->save()){
  166. $compania->id_usuario = $usuario->id;
  167. if ($compania->save()){
  168. if ($nuevo){
  169. if ($modelo->guardarUsuarioGrupo($usuario->id, $grupo["id"])){
  170. $exito = true;
  171. $urlImagenes = JUri::root()."images/";
  172. $mensaje = myView::render("usuario.correo_cuenta_nueva", ["urlImagenes" => $urlImagenes, "urlSitio" => JUri::root(), "usuario" => $usuario, "compania" => $compania]);
  173. $jcfg = new JConfig();
  174. $mail =& JFactory::getMailer();
  175. $mail->addRecipient($usuario->email);
  176. $mail->setSender(array($jcfg->mailfrom, $jcfg->fromname));
  177. $mail->setSubject("New account");
  178. $mail->IsHTML(1);
  179. $mail->setBody($mensaje);
  180. $mail->Send();
  181. }
  182. }
  183. else{
  184. $exito = true;
  185. }
  186. }
  187. }
  188. if ($exito){
  189. if ($usuario->id){
  190. myApp::mostrarMensaje("Your user account was updated successfully","message");
  191. }
  192. else{
  193. myApp::mostrarMensaje("Your user account was created successfully. Shortly, you will receive a email message to validate you account.","message");
  194. }
  195. }
  196. else{
  197. myApp::mostrarMensaje("Your user account was not created","error");
  198. }
  199. return myView::render("usuario.blanco");
  200. }
  201. function activarUsuario(){
  202. $idUsuario = myApp::getRequest()->getVar("idUsuario");
  203. $token = myApp::getRequest()->getVar("token");
  204. $user = Usuario::find($idUsuario);
  205. if (!$user->id){
  206. myApp::mostrarMensaje("This account is not valid", "error");
  207. return false;
  208. }
  209. if ($user->activation == 0){
  210. myApp::mostrarMensaje("This account is already activated", "error");
  211. return false;
  212. }
  213. if ($user->activation != $token){
  214. myApp::mostrarMensaje("The validation code is incorrect", "error");
  215. return false;
  216. }
  217. $user->activation = 0;
  218. if ($user->save()){
  219. myApp::mostrarMensaje("Your account was activated successfully", "message");
  220. }
  221. else{
  222. myApp::mostrarMensaje("Your account was not activated", "error");
  223. }
  224. return myView::render("usuario.blanco");
  225. }
  226. }
  227. ?>