/** * @return \Zend\Http\Response|ViewModel */ public function indexAction() { if ($this->getAuthService()->hasIdentity()) { return $this->redirect()->toRoute('home'); } $helper = new AuthHelper(); if ($this->sessionCount() == 0) { $this->sessionCount(1); } $hasCaptcha = $this->sessionCount() >= 3; if ($hasCaptcha) { $plugin = $this->plugin('url'); $url = $plugin->fromRoute('auth', array('action' => 'captcha')); $form = $helper->getForm($hasCaptcha, $url, self::CAPTCHA_DIR); } else { $form = $helper->getForm($hasCaptcha); } $request = $this->getRequest(); $message = ""; if ($request->isPost()) { $form->setInputFilter($helper->getInputFilter()); $form->setData($request->getPost()); if ($form->isValid()) { $user = $request->getPost('username'); $pass = $request->getPost('password'); $authAdapter = $this->getAuthService()->getAdapter(); $authAdapter->setIdentity($user)->setCredential($pass); $result = $authAdapter->authenticate(); if ($result->isValid()) { if ($request->getPost('remember') == 1) { $this->getSessionStorage()->setRememberMe(1); $this->getAuthService()->setStorage($this->getSessionStorage()); } $data = (array) $authAdapter->getResultRowObject(); $user = new User(); $user->exchangeArray($data); $user->setLastLogin(date('Y-m-d H:i:s')); $this->userTable()->saveUser($user); $columnsToOmit = array('password'); $authUser = $authAdapter->getResultRowObject(null, $columnsToOmit); $userRoles = array(); foreach ($this->userRoleTable()->grantRoles($authUser->userId) as $userRole) { array_push($userRoles, $userRole->roleId); } $authUser->roles = $userRoles; $this->getAuthService()->getStorage()->write($authUser); $this->sessionCount(-$this->sessionCount()); return $this->redirect()->toRoute('home'); } $message = "Invalid user/password"; $this->sessionCount(1); } } $this->layout('layout/empty'); return new ViewModel(array('form' => $form, 'message' => $message, 'hasCaptcha' => $hasCaptcha)); }
/** * {@inheritDoc} */ public function exchangeArray(array $array) { $this->__initializer__ && $this->__initializer__->__invoke($this, 'exchangeArray', array($array)); return parent::exchangeArray($array); }