Пример #1
0
 public function indexAction()
 {
     if (!$this->request->isPost()) {
         return;
     }
     if ($this->request->isAjax()) {
         $form = new Forms\LoginForm();
         if ($form->isValid($this->request->getPost()) === false) {
             return $this->showInvalidMessagesAsJson($form);
         }
         $user = new Login();
         try {
             $loginUser = $user->loginByPassword($this->request->getPost('identify'), $this->request->getPost('password'));
             if ($this->request->getPost('remember')) {
                 $token = $user->getRememberMeToken();
                 if ($token) {
                     $this->cookies->set(Login::LOGIN_COOKIE_REMEMBER_KEY, $token, time() + $user->getRememberMeTokenExpires());
                 }
             }
             return $this->showResponseAsJson(Login::getCurrentUser());
         } catch (\Exception $e) {
             return $this->showExceptionAsJson($e, $user->getMessages());
         }
     } else {
         $form = new Forms\LoginForm();
         if ($form->isValid($this->request->getPost()) === false) {
             $this->showInvalidMessages($form);
             return $this->redirectHandler($this->getDI()->getConfig()->user->loginFailedRedirectUri, 'error');
         }
         $user = new Login();
         try {
             $user->loginByPassword($this->request->getPost('identify'), $this->request->getPost('password'));
             if ($this->request->getPost('remember')) {
                 $token = $user->getRememberMeToken();
                 if ($token) {
                     $this->cookies->set('realm', $token, time() + $user->getRememberMeTokenExpires());
                 } else {
                     $this->flashSession->error($user->getMessages());
                 }
             }
             //$this->flashSession->success('SUCCESS_USER_LOGGED_IN');
             return $this->redirectHandler($this->getDI()->getConfig()->user->loginSuccessRedirectUri);
         } catch (\Exception $e) {
             $this->showException($e, $user->getMessages());
             return $this->redirectHandler($this->getDI()->getConfig()->user->loginFailedRedirectUri, 'error');
         }
     }
 }
Пример #2
0
 public function indexAction()
 {
     if (!$this->request->isPost()) {
         return;
     }
     if ($this->request->isAjax() || $this->request->get('ajax')) {
         $form = new Forms\LoginForm();
         if ($form->isValid($this->request->getPost()) === false) {
             return $this->showInvalidMessagesAsJson($form);
         }
         $user = new Login();
         try {
             $loginUser = $user->loginByPassword($this->request->getPost('identify'), $this->request->getPost('password'));
             $cookieDomain = $this->getDI()->getConfig()->session->sso_domain;
             if ($loginUser->id && $this->request->getPost('remember')) {
                 $token = $user->getRememberMeToken();
                 if ($token) {
                     $cookies = $this->cookies->set(Login::LOGIN_COOKIE_REMEMBER_KEY, $token, time() + $user->getRememberMeTokenExpires());
                     if ($cookieDomain) {
                         $cookie = $cookies->get(Login::LOGIN_COOKIE_REMEMBER_KEY);
                         $cookie->setDomain($cookieDomain);
                     }
                 }
             }
             if (!empty($_SERVER['HTTP_ORIGIN'])) {
                 $this->response->setHeader('Access-Control-Allow-Credentials', 'true');
                 $this->response->setHeader('Access-Control-Allow-Origin', $_SERVER['HTTP_ORIGIN']);
                 $this->response->setHeader('Access-Control-Allow-Methods', 'POST');
                 $this->response->setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Requested-With');
             }
             return $this->showResponseAsJson(Login::getCurrentUser());
         } catch (\Exception $e) {
             return $this->showExceptionAsJson($e, $user->getMessages());
         }
     } else {
         $loginFailedRedirectUri = $this->dispatcher->getParam('loginFailedRedirectUri');
         $loginFailedRedirectUri = $loginFailedRedirectUri ? $loginFailedRedirectUri : $this->getDI()->getConfig()->user->loginFailedRedirectUri;
         $loginFailedRedirectUri = $loginFailedRedirectUri ? $loginFailedRedirectUri : $this->request->getURI();
         $form = new Forms\LoginForm();
         if ($form->isValid($this->request->getPost()) === false) {
             $this->showInvalidMessages($form);
             return $this->redirectHandler($loginFailedRedirectUri, 'error');
         }
         $user = new Login();
         try {
             $user->loginByPassword($this->request->getPost('identify'), $this->request->getPost('password'));
             if ($this->request->getPost('remember')) {
                 $token = $user->getRememberMeToken();
                 if ($token) {
                     $ssoDomain = $this->getDI()->getConfig()->session->sso_domain;
                     $this->cookies->set('realm', $token, time() + $user->getRememberMeTokenExpires());
                     if ($ssoDomain) {
                         $cookie = $this->cookies->get(Login::LOGIN_COOKIE_REMEMBER_KEY);
                         $cookie->setDomain($ssoDomain);
                     }
                 } else {
                     $this->flashSession->error($user->getMessages());
                 }
             }
             //$this->flashSession->success('SUCCESS_USER_LOGGED_IN');
             $loginSuccessRedirectUri = $this->dispatcher->getParam('loginSuccessRedirectUri');
             if (empty($loginSuccessRedirectUri)) {
                 $loginSuccessRedirectUri = '/';
             }
             return $this->response->redirect($loginSuccessRedirectUri);
         } catch (\Exception $e) {
             $this->showException($e, $user->getMessages());
             // $this->getDI()->getConfig()->user->loginFailedRedirectUri
             return $this->response->redirect($loginFailedRedirectUri, 'error');
         }
     }
 }