. */ namespace SP\Modules\Web\Controllers; use SP\Core\Events\Event; use SP\Core\Events\EventMessage; use SP\Core\SessionFactory; use SP\Core\SessionUtil; use SP\Html\Html; use SP\Http\Request; use SP\Http\Response; use SP\Modules\Web\Controllers\Helpers\LayoutHelper; use SP\Modules\Web\Controllers\Traits\JsonTrait; use SP\Services\Auth\LoginService; /** * Class LoginController * * @package SP\Modules\Web\Controllers */ class LoginController extends ControllerBase { use JsonTrait; /** * Login action * * @throws \Psr\Container\ContainerExceptionInterface * @throws \Psr\Container\NotFoundExceptionInterface */ public function loginAction() { try { $loginService = $this->dic->get(LoginService::class); $loginResponmse = $loginService->doLogin(); $forward = Request::getRequestHeaders('X-Forwarded-For'); if ($forward) { $this->eventDispatcher->notifyEvent('login.info', new Event($this, EventMessage::factory() ->addDetail('X-Forwarded-For', $this->configData->isDemoEnabled() ? '***' : $forward)) ); } $this->returnJsonResponseData(['url' => $loginResponmse->getRedirect()]); } catch (\Exception $e) { processException($e); $this->eventDispatcher->notifyEvent('exception', new Event($e)); $this->returnJsonResponse($e->getCode(), $e->getMessage()); } } /** * Logout action * * @throws \Psr\Container\ContainerExceptionInterface * @throws \Psr\Container\NotFoundExceptionInterface * @throws \SP\Core\Dic\ContainerException */ public function logoutAction() { if ($this->session->isLoggedIn()) { $inactiveTime = abs(round((time() - $this->session->getLastActivity()) / 60, 2)); $totalTime = abs(round((time() - $this->session->getStartActivity()) / 60, 2)); $this->eventDispatcher->notifyEvent('logout', new Event($this, EventMessage::factory() ->addDescription(__u('Finalizar sesión')) ->addDetail(__u('Usuario'), $this->session->getUserData()->getLogin()) ->addDetail(__u('Tiempo inactivo'), $inactiveTime . ' min.') ->addDetail(__u('Tiempo total'), $totalTime . ' min.')) ); SessionUtil::cleanSession(); $this->session->setLoggedOut(true); $layoutHelper = $this->dic->get(LayoutHelper::class); $layoutHelper->getCustomLayout('logout', 'logout'); $this->view(); } else { Response::redirect('index.php?r=login'); } } /** * Index action * * @throws \Psr\Container\ContainerExceptionInterface * @throws \Psr\Container\NotFoundExceptionInterface * @throws \SP\Core\Dic\ContainerException */ public function indexAction() { $layoutHelper = $this->dic->get(LayoutHelper::class); $layoutHelper->getCustomLayout('index', 'login'); if (SessionFactory::getLoggedOut() === true) { SessionFactory::setLoggedOut(); $this->view->assign('loggedOut', 1); } else { $this->view->assign('loggedOut', 0); } $this->view->assign('mailEnabled', $this->configData->isMailEnabled()); $this->view->assign('updated', SessionFactory::getAppUpdated()); SessionFactory::setAppUpdated(false); $getParams = []; // Comprobar y parsear los parámetros GET para pasarlos como POST en los inputs if (count($_GET) > 0) { foreach ($_GET as $param => $value) { $getParams['g_' . Html::sanitizeFull($param)] = Html::sanitizeFull($value); } } $this->view->assign('getParams', $getParams); $this->view(); } }