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'); } } }
/** * * @SWG\Api( * path="/login", * description="User Login API", * produces="['application/json']", * @SWG\Operations( * @SWG\Operation( * method="POST", * summary="Login by password", * @SWG\Parameters( * @SWG\Parameter( * name="Login json", * description="{ identify : username or email, password : password}", * paramType="body", * required=true, * type="string" * ) * ) * ) * ) * ) */ public function indexAction() { Login::setLoginMode(Login::LOGIN_MODE_TOKEN); $data = $this->request->getRawBody(); if (!$data) { throw new Exception\InvalidArgumentException('No data input'); } if (!($data = json_decode($data, true))) { throw new Exception\InvalidArgumentException('Json data parsing failed'); } $form = new LoginForm(); if ($form->isValid($data) === false) { return $this->showInvalidMessagesAsJson($form); } $user = new Login(); $apikey = new Apikey(); $loginUser = $user->loginByPassword($data['identify'], $data['password']); $userinfo = $loginUser->dump(User::$simpleDump); $userinfo['roles'] = Login::getAuthStorage()->get(Login::AUTH_KEY_ROLES); $userinfo['token'] = Login::getAuthStorage()->get(Login::AUTH_KEY_TOKEN); return $this->response->setJsonContent($userinfo); }
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'); } } }