예제 #1
0
 /**
  * @return \Zend\Http\Response|ViewModel
  */
 public function loginAction()
 {
     $data = $this->getRequest()->getPost();
     $form = new Form\LoginForm(null, $this->getServiceLocator());
     $flashMessenger = new FlashMessenger();
     if ($this->getRequest()->isPost()) {
         // If you used another name for the authentication service, change it here
         $form->setData($this->getRequest()->getPost());
         if ($form->isValid()) {
             /**
              * @var \User\Service\Auth $userAuth
              */
             $userAuth = $this->getServiceLocator()->get('\\User\\Service\\Auth');
             try {
                 $userAuth->authenticateEquals($data['email'], $data['password']);
                 $flashMessenger->addSuccessMessage('You\'re successfully logged in');
                 $session = new Container('location');
                 $location = $session->location;
                 if ($location) {
                     $session->getManager()->getStorage()->clear('location');
                     return $this->redirect()->toUrl($location);
                 }
                 return $this->redirect()->toRoute('home');
             } catch (AuthException $exception) {
                 $flashMessenger->addErrorMessage($exception->getMessage());
             }
         }
     }
     return new ViewModel(array('form' => $form, 'serviceLocator' => $this->getServiceLocator()));
 }
예제 #2
0
 /**
  * check if user is correctly login
  *
  * @return \Zend\Http\Response |ViewModel | redirect
  */
 public function processAction()
 {
     $data = $this->params()->fromPost();
     $form = new LoginForm();
     $form->setData($data);
     $viewModel = new ViewModel(["form" => $form]);
     $viewModel->setTemplate('user/login/index');
     if ($form->isValid()) {
         $this->authService = $this->getAuth();
         $adapter = $this->authService->getAdapter();
         $adapter->setIdentityValue($data['email']);
         $adapter->setCredentialValue($data['password']);
         $authResult = $this->authService->authenticate();
         if ($authResult->isValid()) {
             $identity = $authResult->getIdentity();
             $this->authService->getStorage()->write($identity);
             //TODO move bellow  logic to specific service update last login time
             return $this->redirect()->toRoute('board');
         } else {
             $viewModel->setVariable("isLoginError", true);
         }
     } else {
         $viewModel->setVariable("isFormError", true);
     }
     return $viewModel;
 }
예제 #3
0
 public function indexAction()
 {
     $form = new LoginForm();
     $authService = $this->getServiceLocator()->get('Zend\\Authentication\\AuthenticationService');
     $request = $this->getRequest();
     if ($request->isPost()) {
         $form->setData($request->getPost());
         if ($form->isValid()) {
             $data = $form->getData();
             $adapter = $authService->getAdapter();
             $adapter->setIdentityValue($data['email']);
             $adapter->setCredentialValue($data['password']);
             $authResult = $authService->authenticate();
             if ($authResult->isValid()) {
                 $identity = $authResult->getIdentity();
                 $authService->getStorage()->write($identity);
                 $time = 1209600;
                 // 14 days 1209600/3600 = 336 hours => 336/24 = 14 days
                 //-					if ($data['rememberme']) $authService->getStorage()->session->getManager()->rememberMe($time); // no way to get the session
                 if ($data['remember-me']) {
                     $sessionManager = new SessionManager();
                     $sessionManager->rememberMe($time);
                 }
                 return $this->redirect()->toRoute('user_dashboard');
             } else {
                 $this->flashMessenger()->addErrorMessage("Invalid login credentials provided. Try again, or sign up!");
             }
         }
     }
     $view = new ViewModel(array('form' => $form));
     return $view;
 }
예제 #4
0
 public function loginAction()
 {
     $auth = new AuthenticationService();
     if ($auth->hasIdentity()) {
         return $this->redirect()->toRoute('home');
     }
     // process the form
     $form = new LoginForm();
     $request = $this->getRequest();
     if ($this->getRequest()->isPost()) {
         $form->setData($request->getPost());
         if ($form->isValid()) {
             $data = $form->getData();
             // check if the user exists
             $sm = $this->getServiceLocator();
             $mapper = $sm->get('User\\Model\\UserMapper');
             $params = array('where' => 'username = "******"');
             $users = $mapper->select($params);
             if ($users) {
                 $user = $users[0];
                 /**
                  * If the account is not active, prompt the user to activate
                  * the account
                  */
                 if (!$user->getActive()) {
                     return $this->redirect()->toRoute('registration', array('action' => 'confirm', 'id' => $user->getId()));
                 }
                 // authenticate the user
                 $dbAdapter = $sm->get('Zend\\Db\\Adapter\\Adapter');
                 $adapter = new AuthAdapter($dbAdapter, 'user', 'username', 'password_hash');
                 $adapter->setIdentity($data['credential']);
                 $adapter->setCredential(hash('sha256', $user->getPassword_salt() . $data['password']));
                 $result = $auth->authenticate($adapter);
                 if ($result->isValid()) {
                     // store session information in database
                     $mapper = $sm->get('User\\Model\\SessionMapper');
                     $session = new Session(array('user_id' => $user->getId(), 'ip_address' => $_SERVER['REMOTE_ADDR'], 'login_timestamp' => date('Y-m-d H:i:s')));
                     $mapper->save($session);
                     // store user information in session variable
                     $container = new Container('user');
                     $container->user = $user->get_array();
                     return $this->redirect()->toRoute('home');
                 } else {
                     foreach ($result->getMessages() as $message) {
                         print "{$message}\n";
                     }
                 }
             } else {
                 print "Invalid username/email";
             }
         }
     }
     return new ViewModel(array('form' => $form));
 }
예제 #5
0
 public function loginAction()
 {
     //        $p = new Bcrypt();
     //        $p->setSalt('xTiH$dg0913oJ.ceveiRfewFEBFEbh892e34.wfeguuq12332rtuBYCPLZvcwgams');
     //        $p1= $p->create('1234');
     //        echo $p1;
     //        exit();
     //
     if ($this->getServiceLocator()->get('AuthService')->hasIdentity()) {
         return $this->redirect()->toUrl('/');
     }
     $form = new LoginForm('form');
     $form->setInputFilter(new LoginFilter());
     $request = $this->getRequest();
     if ($request->isPost()) {
         $data = $request->getPost();
         //            print "<pre>";
         //            print_r($data);
         //            exit();
         $form->setData($data);
         if ($form->isValid()) {
             $data = $form->getData();
             //$userPass = new UserPassword();
             //                print "<pre>";
             //                echo strlen($userPass->salt);
             //                exit();
             // $encryptPass = $userPass->create($data['password']);
             $authService = $this->getServiceLocator()->get('AuthService');
             $authService->getAdapter()->setIdentity($data['username'])->setCredential($data['password']);
             $result = $authService->authenticate();
             if ($result->isValid()) {
                 return $this->redirect()->toUrl('/');
             }
         } else {
         }
     }
     return new ViewModel(array('form' => $form));
 }
예제 #6
0
 public function signinAction()
 {
     $form = new LoginForm();
     $request = $this->getRequest();
     if ($request->isPost()) {
         $user = new User();
         $form->setInputFilter($user->getInputFilter());
         $form->setData($request->getPost());
         if ($form->isValid()) {
             $post = $request->getPost();
             $sm = $this->getServiceLocator();
             $dbAdapter = $sm->get('Zend\\Db\\Adapter\\Adapter');
             $authAdapter = new AuthAdapter($dbAdapter);
             $authAdapter->setTableName('user')->setIdentityColumn('username')->setCredentialColumn('password');
             $authAdapter->setIdentity($post->get('username'))->setCredential(sha1($post->get('password')));
             $authService = new AuthenticationService();
             $authService->setAdapter($authAdapter);
             $result = $authService->authenticate();
             if ($result->isValid()) {
                 $user = $this->getUserTable()->getUserByName($post->get('username'));
                 $session = new Container('user');
                 $session->offsetUnset('username');
                 $session->offsetSet('username', $user->username);
                 $session->offsetUnset('id');
                 $session->offsetSet('id', $user->id);
                 return $this->redirect()->toRoute('image');
             } else {
                 echo '<div class="alert alert-error">
                         <button type="button" class="close" data-dismiss="alert">&times;</button>
                         <h4>Warning!</h4>
                         Log in failed!.
                       </div>';
             }
         }
     }
     return array('form' => $form);
 }
예제 #7
0
 /**
  * check if user is correctly login
  * @return \Zend\Http\Response|ViewModel | redirect
  */
 public function processAction()
 {
     $data = $this->params()->fromPost();
     $form = new LoginForm();
     $form->setData($data);
     $viewModel = new ViewModel(["form" => $form]);
     $viewModel->setTemplate('user/login/index');
     if ($form->isValid()) {
         $this->authService = $this->getAuth();
         $adapter = $this->authService->getAdapter();
         $adapter->setIdentityValue($data['email']);
         $adapter->setCredentialValue($data['password']);
         $authResult = $this->authService->authenticate();
         if ($authResult->isValid()) {
             $identity = $authResult->getIdentity();
             $this->authService->getStorage()->write($identity);
             //last login time
             $em = $this->getServiceLocator()->get('Doctrine\\ORM\\EntityManager');
             $user = $em->find('Application\\Entity\\User', $identity);
             $user->setLastLogin(time());
             $em->persist($user);
             $em->flush();
             $trajectSession = new Container('traject');
             if ($trajectSession->trajectData) {
                 return $this->redirect()->toRoute('traject/default', ['controller' => 'manage', 'action' => 'success']);
             }
             return $this->redirect()->toRoute('board');
         } else {
             $viewModel->setVariable("isLoginError", true);
             return $viewModel;
         }
     } else {
         $viewModel->setVariable("isFormError", true);
         return $viewModel;
     }
 }
 /**
  * Action for logging in a user
  * @return JsonModel
  */
 public function loginAction()
 {
     if ($this->request->isOptions()) {
         return new JsonModel();
     }
     if ($this->request->isPost()) {
         $loginForm = new LoginForm();
         $loginFilter = new LoginFilter();
         $loginForm->setInputFilter($loginFilter);
         $post = get_object_vars(json_decode($this->request->getContent()));
         $loginForm->setData($post);
         if (!$loginForm->isValid()) {
             $errorMessages = array();
             foreach ($loginForm->getMessages() as $elementName => $messages) {
                 foreach ($messages as $message) {
                     $errorMessages[$elementName] = $message;
                 }
             }
             return new JsonModel(array('error' => 1, 'message' => 'You have an error in your form. Please try again.', 'formErrors' => $errorMessages));
         }
         $formData = $loginForm->getData();
         $authAdapter = new AuthAdapter($this->getServiceLocator()->get('Zend\\Db\\Adapter\\Adapter'));
         $authAdapter->setTableName('lt_user');
         $authAdapter->setIdentityColumn('email');
         $authAdapter->setCredentialColumn('password');
         $authAdapter->setIdentity($formData['email']);
         $authAdapter->setCredential($formData['password']);
         $result = $authAdapter->authenticate();
         if (!$result->isValid()) {
             return new JsonModel(array('error' => 1, 'message' => 'Error while logging in. Please try again'));
         } else {
             $auth = $this->getServiceLocator()->get('AuthService');
             $objectManager = $this->getServiceLocator()->get('Doctrine\\ORM\\EntityManager');
             $user = $objectManager->getRepository('Application\\Entity\\LtUser')->findOneBy(array('email' => $formData['email']));
             $date = new \DateTime();
             $hashRandomize = uniqid('swetea', true);
             $authToken = md5($formData['email'] . $date->format('Y-m-d') . $hashRandomize);
             $user->setAuthtoken($authToken);
             $storage = $auth->getStorage();
             $storage->write(array('contactName' => $user->getContactname(), 'userGroup' => $user->getUsergroup(), 'email' => $user->getEmail(), 'userId' => $user->getUserId()));
             $objectManager->persist($user);
             $objectManager->flush();
             return new JsonModel(array('error' => 0, 'message' => 'Login successful', 'authToken' => $authToken, 'userGroup' => $user->getUsergroup()));
         }
     } else {
         $this->response->setStatusCode(405);
         return new JsonModel(array('error' => 1, 'message' => 'Request Method not allowed'));
     }
 }