PageRenderTime 92ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/php5-3/app/controllers/backoffice/Authentication.class.php

https://bitbucket.org/ronaldobrandini/framework
PHP | 168 lines | 130 code | 25 blank | 13 comment | 17 complexity | 5beb461212d42cd7ffeb0d7d4f72d936 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception
  1. <?php
  2. namespace app\controllers\backoffice;
  3. /**
  4. *
  5. * Public Class Authentication
  6. *
  7. * Classe responsavel pelo login do sistema
  8. *
  9. *
  10. * @package app.controllers.backoffice
  11. * @access public
  12. * @author Ronaldo Silva <ronaldo.silva@xsystems.com.br>
  13. * @version 1.0 08/05/2014
  14. *
  15. */
  16. class Authentication extends \core\controllers\BackOfficeController{
  17. public $auth = false;
  18. public $authRedirection = 'Authentication';
  19. public function initContent(){
  20. parent::initContent();
  21. $cookie = new \core\lib\Cookie();
  22. if($cookie->cookieExists('XXloginXX') && \core\lib\Tools::isSubmit('superlogout')){
  23. $cookie->destroy('XXloginXX');
  24. \core\lib\Tools::redirect('?controller=authentication&redirect=' . \core\lib\Tools::getValue('redirect'));
  25. }
  26. $this->context->smarty->assign('redirect', \core\lib\Tools::getValue('redirect'));
  27. if(\core\lib\Tools::getValue('action') == 'forget'){
  28. $this->setTemplate(_ADMIN_THEME_DIR . 'forget-password.tpl');
  29. $this->context->smarty->assign('email', trim(\core\lib\Tools::getValue('email')));
  30. }else{
  31. if($cookie->cookieExists('XXloginXX')){
  32. $this->setTemplate(_ADMIN_THEME_DIR . 'lock-screen.tpl');
  33. $cookieData = $cookie->read('XXloginXX');
  34. $this->context->smarty->assign('id', $cookieData->id);
  35. $this->context->smarty->assign('redirect', \core\lib\Tools::getValue('redirect'));
  36. $this->context->smarty->assign('token', base64_encode(md5($cookieData->ipAddress)));
  37. $this->context->smarty->assign('bodyClass', 'lock-screen');
  38. }else{
  39. $this->context->smarty->assign('username', \core\lib\Tools::getValue('username'));
  40. $this->setTemplate(_ADMIN_THEME_DIR . 'authentication.tpl');
  41. }
  42. }
  43. $this->liveEdit[] = 'Authentication.init();';
  44. $this->context->smarty->assign('liveEdit', $this->liveEdit);
  45. }
  46. public function setMedia(){
  47. parent::setMedia();
  48. $this->addJs('plugins/form/jquery.form.min.js');
  49. $this->addJs('plugins/validate/jquery.validate.min.js');
  50. $this->addJs('Authentication.js');
  51. }
  52. public function postProcess(){
  53. parent::postProcess();
  54. switch(\core\lib\Tools::getValue('action')){
  55. case 'login' :
  56. $this->processLogin();
  57. break;
  58. case 'forget' :
  59. $this->forgetPassword();
  60. break;
  61. case 'unlock' :
  62. $this->unlock();
  63. break;
  64. }
  65. }
  66. public function forgetPassword(){
  67. $validator = new \core\lib\DataValidator();
  68. $validator->set('Email', \core\lib\Tools::getValue('email'))->isEmail()->required();
  69. if($validator->validate()){
  70. if(\core\lib\Tools::getValue('email') === 'ronaldo.silva@xsystems.com.br'){
  71. $this->msgs[] = 'Enviamos um email com o link para você redefinir a sua senha.';
  72. if($this->ajax){
  73. die(\core\lib\Tools::jsonEncode(array('hasErrors' => false, 'msg' => $this->msgs)));
  74. }else{
  75. \core\lib\Tools::displayMessage($this->msgs, 'success');
  76. }
  77. }else{
  78. $this->msgs[] = 'Desculpe mas o email que você digitou não foi encontrado em nosso servidor!';
  79. if($this->ajax){
  80. die(\core\lib\Tools::jsonEncode(array('hasErrors' => true, 'errors' => $this->msgs)));
  81. }else{
  82. \core\lib\Tools::displayMessage($this->msgs, 'danger');
  83. }
  84. }
  85. }else{
  86. if($this->ajax){
  87. die(\core\lib\Tools::jsonEncode(array('hasErrors' => true, 'errors' => $validator->getErrors())));
  88. }else{
  89. \core\lib\Tools::displayMessage($validator->getErrors(), 'danger');
  90. }
  91. }
  92. }
  93. public function unlock(){
  94. $dataValidator = new \core\lib\DataValidator();
  95. $dataValidator->set('id', \core\lib\Tools::getValue('id'))->isInteger()->required();
  96. }
  97. public function processLogin(){
  98. $dataValidator = new \core\lib\DataValidator();
  99. $dataValidator->set('Usuário', trim(\core\lib\Tools::getValue('username')))
  100. ->required()->isAlphaNum('-_.');
  101. $dataValidator->set('Senha', trim(\core\lib\Tools::getValue('password')))
  102. ->required()->isAlphaNum('&@#$!,.;:/?\|~^Çç][{}áéíóúâêîôûãõàèìòù');
  103. if(!$dataValidator->validate()){
  104. $this->msgs = $dataValidator->getErrors();
  105. }
  106. $email = 'ronaldobrandini';
  107. $senha = \core\lib\Tools::passEncrypt('metallica');
  108. if(!count($this->msgs)){
  109. $criteria = new \core\data\dbquery\SqlCriteria();
  110. $criteria->add(new \core\data\dbquery\SqlFilter('user', '=', trim(\core\lib\Tools::getValue('username'))));
  111. $criteria->add(new \core\data\dbquery\SqlFilter('password', '=', \core\lib\Tools::passEncrypt(trim(\core\lib\Tools::getValue('password')))));
  112. if(trim(\core\lib\Tools::getValue('username')) === $email &&
  113. \core\lib\Tools::passEncrypt(trim(\core\lib\Tools::getValue('password'))) === $senha){
  114. $session = new \core\lib\Session();
  115. $session->createUserSession(1);
  116. if(\core\lib\Tools::getValue('remember')){
  117. $cookie = new \core\lib\Cookie();
  118. $cookie->setContent('id', 1);
  119. $cookie->setContent('ipAddress', \core\lib\Tools::getIpAddress());
  120. $cookie->write('XXloginXX', 'dashboard/', strtotime('+30 days'));
  121. }
  122. if($this->ajax){
  123. die(\core\lib\Tools::jsonEncode(array('hasErrors' => false, 'redirect' => '?controller=' . \core\lib\Tools::getValue('redirect'))));
  124. }else{
  125. \core\lib\Tools::redirect('?controller=' . \core\lib\Tools::getValue('redirect'));
  126. }
  127. }else{
  128. $this->msgs[] = 'Não encontramos um usuário válido com os dados fornecidos.';
  129. if($this->ajax){
  130. die(\core\lib\Tools::jsonEncode(array('hasErrors' => true, 'errors' => $this->msgs)));
  131. }else{
  132. \core\lib\Tools::displayMessage($this->msgs, 'warning');
  133. }
  134. }
  135. }else{
  136. if($this->ajax){
  137. die(\core\lib\Tools::jsonEncode(array('hasErrors' => true, 'errors' => $this->msgs)));
  138. }else{
  139. \core\lib\Tools::displayMessage($dataValidator->getErrors(), 'warning');
  140. }
  141. }
  142. }
  143. }