Example #1
0
 public function loginAction()
 {
     /**
      * @var BackofficeAuthenticationService $backofficeAuthenticationService
      */
     $this->layout('layout/login');
     $session = new Container('authFailed');
     $router = $this->getEvent()->getRouter();
     $request = $this->getRequest();
     $lastRequestUrl = $request->getQuery()->request_url;
     $loginUrl = $router->assemble([], ['name' => 'backoffice_user_login']);
     if ($session->authFailed) {
         $form = new LoginForm();
         $form->setInputFilter(new LoginFilter());
         $error = $session->authFailed === self::CONNECTION_TIMEDOUT ? 'Connection Timed Out.' : 'Authentication failed.';
         $session->getManager()->getStorage()->clear('authFailed');
         return new ViewModel(['form' => $form, 'error' => $error, 'backofficeVersion' => Constants::APP_VERSION]);
     }
     $serviceLocator = $this->getServiceLocator();
     $backofficeAuthenticationService = $serviceLocator->get('library_backoffice_auth');
     if ($backofficeAuthenticationService->hasIdentity()) {
         $redirectHome = $backofficeAuthenticationService->getHomeUrl();
         if (!empty($lastRequestUrl) && $lastRequestUrl != $loginUrl) {
             $redirectUrl = $lastRequestUrl;
         } else {
             $redirectUrl = $redirectHome;
         }
         return $this->redirect()->toUrl($redirectUrl);
     }
     $failure = '';
     $request = $this->getRequest();
     $form = new LoginForm();
     if ($request->isPost()) {
         $postData = $request->getPost();
         $form->setInputFilter(new LoginFilter());
         $form->setData($postData);
         if ($form->isValid()) {
             $formData = $form->getData();
             $result = $backofficeAuthenticationService->authenticate(null, $formData['identity'], $formData['credential']);
             if ($result->isValid()) {
                 $auth = $this->getServiceLocator()->get('library_backoffice_auth');
                 $userIdentity = $auth->getIdentity();
                 // update user last login date and time
                 $userManagerService = $serviceLocator->get('service_user');
                 $userManagerService->updateLastLogin($userIdentity->id);
                 $appConfig = $serviceLocator->get('config');
                 $backofficeAuthenticationService->setAsBackofficeUser($appConfig['session']['config']['options']['cookie_domain']);
                 $backofficeAuthenticationService->setRememberMyEmail($formData['identity'], $appConfig['session']['config']['options']['cookie_domain']);
                 $redirect = $backofficeAuthenticationService->getUrlForRedirect();
                 if (!empty($lastRequestUrl) && $lastRequestUrl != $loginUrl) {
                     $redirect = $lastRequestUrl;
                 } else {
                     $redirect = $redirect;
                 }
                 return $this->redirect()->toUrl($redirect);
             } else {
                 $failure = 'Authentication failed.';
             }
         } else {
             $failure = 'Authentication failed.';
         }
     }
     $session1 = Helper::getSessionContainer('logout');
     if ($session1->offsetExists('loggedOut') && $session1->offsetGet('loggedOut')) {
         $session1->getManager()->getStorage()->clear();
     }
     return new ViewModel(['form' => $form, 'error' => $failure, 'lastRequestUrl' => $lastRequestUrl, 'backofficeVersion' => Constants::APP_VERSION]);
 }