예제 #1
0
 public function indexAction()
 {
     $form = new FormLogin();
     $error = false;
     $request = $this->getRequest();
     if ($request->isPost()) {
         $form->setData($request->getPost());
         if ($form->isValid()) {
             $data = $request->getPost()->toArray();
             $auth = new AuthenticationService();
             $sessionStorage = new SessionStorage('User');
             $auth->setStorage($sessionStorage);
             $authAdapter = $this->getServiceLocator()->get('User\\Auth\\Adapter');
             $authAdapter->setUsername($data['email']);
             $authAdapter->setPassword($data['password']);
             $result = $auth->authenticate($authAdapter);
             if ($result->isValid()) {
                 $sessionStorage->write($auth->getIdentity()['user'], null);
                 return $this->redirect()->toRoute('user-admin/default', ['controller' => 'users']);
             } else {
                 $error = true;
             }
         }
     }
     return new ViewModel(['form' => $form, 'error' => $error]);
 }
 public function loginAction()
 {
     if ($this->authenticationService->getIdentity()) {
         return $this->redirect()->toRoute('home');
     }
     $form = new Login($this->getServiceLocator()->get('MvcTranslator'));
     $messages = [];
     $this->layout('layout/1-col');
     if ($this->getRequest()->isPost()) {
         $post = $this->params()->fromPost();
         $form->setData($post);
         if ($form->isValid()) {
             $data = $form->getData();
             $adapter = $this->getAuthenticationService()->getAdapter();
             $storage = $this->getAuthenticationService()->getStorage();
             $adapter->setIdentity($data['email']);
             $adapter->setCredential($data['password']);
             $storage->setRememberMe($data['remember']);
             $result = $this->getAuthenticationService()->authenticate();
             if ($result->isValid()) {
                 $user = $this->getUserManager()->getUser($result->getIdentity()->getId());
                 $user->updateLoginData();
                 $this->getUserManager()->persist($user);
                 $this->getUserManager()->flush();
                 $url = $this->params()->fromQuery('redir', $this->referer()->fromStorage());
                 return $this->redirect()->toUrl($url);
             }
             $messages = $result->getMessages();
         }
     } else {
         $this->referer()->store();
     }
     $view = new ViewModel(['form' => $form, 'errorMessages' => $messages, 'redir' => $this->params()->fromQuery('redir')]);
     $view->setTemplate('authentication/login');
     return $view;
 }