Esempio n. 1
0
 public function googleSigninAction()
 {
     $backofficeAuthenticationService = $this->getServiceLocator()->get('library_backoffice_auth');
     try {
         $request = $this->getRequest();
         $router = $this->getEvent()->getRouter();
         $loginUrl = $router->assemble([], ['name' => 'backoffice_user_login']);
         $lastRequestUrl = $request->getQuery('request_url', null);
         $dbAdapter = $this->getServiceLocator()->get('dbadapter');
         $error = $this->params()->fromQuery('error', false);
         if (!empty($error)) {
             return $this->redirect()->toUrl('/');
         }
         if (!is_null($lastRequestUrl)) {
             $session = new Container('requestUrl');
             $session->lastRequestUrl = $lastRequestUrl;
         }
         $this->layout('layout/login');
         $googleAuth = $this->getServiceLocator()->get('library_service_google_auth');
         $response = $googleAuth->authenticate($this->getServiceLocator());
         if ($response[0] == 'verified') {
             $userManager = new UserManager($this->getServiceLocator());
             $userInfo = $userManager->getUserByEmail($response[1]);
             if (!$userInfo instanceof \DDD\Domain\User\User) {
                 $requestUrl = new Container('requestUrl');
                 if (!empty($requestUrl)) {
                     $lastRequestUrl = $requestUrl->lastRequestUrl;
                 } else {
                     $lastRequestUrl = null;
                 }
                 $session = new Container('authFailed');
                 $session->authFailed = true;
                 return $this->redirect()->toRoute("backoffice_user_login", ["action" => "login"], ['query' => ['request_url' => $lastRequestUrl]]);
             }
             $userData = [];
             foreach ((array) $userInfo as $key => $row) {
                 $rawKey = preg_replace('/\\0\\*\\0/', '', $key);
                 $userData[$rawKey] = $row;
             }
             $userData = (object) $userData;
             $backofficeAuthenticationService->getStorage()->write($userData);
             $appConfig = $this->getServiceLocator()->get('config');
             $backofficeAuthenticationService->setAsBackofficeUser($appConfig['session']['config']['options']['cookie_domain']);
             $backofficeAuthenticationService->setRememberMyEmail($userData->email, $appConfig['session']['config']['options']['cookie_domain']);
             // update user last login date and time
             $userManagerService = $this->getServiceLocator()->get('service_user');
             $userManagerService->updateLastLogin($userData->id);
             $redirectUrl = $backofficeAuthenticationService->getUrlForRedirect();
             $requestUrl = new Container('requestUrl');
             if (!empty($requestUrl)) {
                 $lastRequestUrl = $requestUrl->lastRequestUrl;
             } else {
                 $lastRequestUrl = null;
             }
             if (!is_null($lastRequestUrl) && $lastRequestUrl != $loginUrl) {
                 $redirect = $lastRequestUrl;
             } else {
                 $redirect = $redirectUrl;
             }
             return $this->redirect()->toUrl($redirect);
         } else {
             header('Location: ' . $response);
             exit;
         }
     } catch (\Exception $e) {
         $session = new Container('authFailed');
         $session->authFailed = self::CONNECTION_TIMEDOUT;
         return $this->redirect()->toRoute('backoffice_user_login');
     }
 }