public function loginAction() { $messages = null; $form = new AuthForm(); $form->get('submit')->setvalue('Login'); $request = $this->getRequest(); if ($request->isPost()) { $authFormFilters = new Auth(); $form->setInputFilter($authFormFilters->getInputFilter()); $form->setData($request->getPost()); if ($form->isValid()) { $data = $form->getData(); $sm = $this->getServiceLocator(); $dbAdapter = $sm->get('Zend\\Db\\Adapter\\Adapter'); $config = $this->getServiceLocator()->get('Config'); $staticSalt = $config['static_salt']; $authAdapter = new AuthAdapter($dbAdapter, 'users', 'usr_name', 'usr_password', "MD5(CONCAT('{$staticSalt}', ?, usr_password_salt)) AND usr_active = 1"); $authAdapter->setIdentity($data['usr_name'])->setCredential($data['usr_password']); $auth = new AuthenticationService(); // or prepare in the globa.config.php and get it from there. Better to be in a module, so we can replace in another module. // $auth = $this->getServiceLocator()->get('Zend\Authentication\AuthenticationService'); // $sm->setService('Zend\Authentication\AuthenticationService', $auth); // You can set the service here but will be loaded only if this action called. $result = $auth->authenticate($authAdapter); // echo '<pre>'; // print_r($result); // echo '</pre>'; switch ($result->getCode()) { case Result::FAILURE_IDENTITY_NOT_FOUND: // do stuff for nonexistent identity break; case Result::FAILURE_CREDENTIAL_INVALID: // do stuff for invalid credential break; case Result::SUCCESS: $storage = $auth->getStorage(); $storage->write($authAdapter->getResultRowObject(null, 'usr_password')); $time = 1209600; // 14 days 1209600/3600 = 336 hours => 336/24 = 14 days // if ($data['rememberme']) $storage->getSession()->getManager()->rememberMe($time); // no way to get the session // if ($data['rememberme']) { // $sessionManager = new \Zend\Session\SessionManager(); // $sessionManager->rememberMe($time); // } break; default: // do stuff for other failure break; } foreach ($result->getMessages() as $message) { $messages .= "{$message}\n"; } } else { echo '<h1> The form is NOT valid </h1>'; } } // echo '<pre>'; // print_r($_SESSION); // echo '</pre>'; return new ViewModel(array('form' => $form, 'messages' => $messages)); }
public function loginAction() { $messages = null; $form = new AuthForm(); $form->get('submit')->setValue('Login'); $request = $this->getRequest(); if ($request->isPost()) { $authFormFilters = new Auth(); $form->setInputFilter($authFormFilters->getInputFilter()); $form->setData($request->getPost()); if ($form->isValid()) { $data = $form->getData(); $sm = $this->getServiceLocator(); $dbAdapter = $sm->get('Zend\\Db\\Adapter\\Adapter'); $config = $this->getServiceLocator()->get('Config'); $staticSalt = $config['static_salt']; $authAdapter = new AuthAdapter($dbAdapter, 'users', 'usr_name', 'usr_password', "MD5 (CONCAT('{$staticSalt}', ?, usr_password_salt)) AND usr_active = 1"); $authAdapter->setIdentity($data['usr_name'])->setCredential($data['usr_password']); $auth = new AuthenticationService(); $result = $auth->authenticate($authAdapter); switch ($result->getCode()) { case Result::FAILURE_IDENTITY_NOT_FOUND: // do stuff for nonexistent identity break; case Result::FAILURE_CREDENTIAL_INVALID: // do stuff for invalid credential break; case Result::SUCCESS: $storage = $auth->getStorage(); $storage->write($authAdapter->getResultRowObject(null, 'usr_password')); /*$time = 1209600; // 14 days 1209600/3600 = 336 hours => 336/24 = 14 days // if ($data['rememberme']) $storage->getSession()->getManager()->rememberMe($time); // no way to get the session if ($data['rememberme']) { $sessionManager = new \Zend\Session\SessionManager(); $sessionManager->rememberMe($time); }*/ break; default: // do stuff for other failure break; } foreach ($result->getMessages() as $message) { $messages .= "{$message}\n"; } //echo '<pre>'; //print_r($_SESSION); //echo '</pre>'; } else { //echo 'Form is not valid!'; } } return new viewModel(array('form' => $form, 'messages' => $messages)); }
public function indexAction() { $form = new RegistrationForm(); $form->get('submit')->setValue('Register'); $request = $this->getRequest(); // съдържа данните преди валидация if ($request->isPost()) { $form->setInputFilter(new RegistrationFilter($this->getServiceLocator())); $form->setData($request->getPost()); if ($form->isValid()) { $data = $form->getData(); //съдържа данните след валидация $data = $this->prepareData($data); $auth = new Auth(); $auth->exchangeArray($data); //echo '<pre>'; //print_r($auth); //echo '</pre>'; /* //Manualy, without ServiceManager $dbAdapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter'); $resultSetPrototype = new ResultSet(); $resultSetPrototype->setArrayObjectPrototype(new Auth()); $tableGateway = new TableGateway('users', $dbAdapter, null, $resultSetPrototype); $usersTable = new UsersTable($tableGateway); $usersTable->saveUser($auth); $userIvan = $usersTable->getUser(5); //without $userTable $rowset = $tableGateway->select(array('usr_id' => 5)); $user3 = $rowset->current(); echo '<pre>'; print_r($user3); echo '</pre>'; */ //using the ServiceManager $this->getUsersTable()->saveUser($auth); $this->sendConfirmationEmail($auth); $this->flashMessenger()->addMessage($auth->usr_email); return $this->redirect()->toRoute('auth/default', array('controller' => 'registration', 'action' => 'registration-success')); } } return new viewModel(array('form' => $form)); }
public function indexAction() { // A test instantiation to make sure it works. Not used in the application. You can remove the next line // $myValidator = new ConfirmPassword(); $form = new RegistrationForm(); $form->get('submit')->setValue('Register'); $request = $this->getRequest(); if ($request->isPost()) { $form->setInputFilter(new RegistrationFilter($this->getServiceLocator())); $form->setData($request->getPost()); if ($form->isValid()) { $data = $form->getData(); $data = $this->prepareData($data); $auth = new Auth(); $auth->exchangeArray($data); /* // this is replaced by // 1) Manualy composing (wiring) the objects $dbAdapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter'); $resultSetPrototype = new \Zend\Db\ResultSet\ResultSet(); $resultSetPrototype->setArrayObjectPrototype(new \Auth\Model\Auth()); $tableGateway = new \Zend\Db\TableGateway\TableGateway('users', $dbAdapter, null, $resultSetPrototype); $usersTable = new \Auth\Model\UsersTable($tableGateway); // $usersTable->saveUser($auth); // $user7 = $usersTable->getUser(7); $rowset = $tableGateway->select(array('usr_id' => 7)); $user7 = $rowset->current(); echo '<pre>'; var_dump($user7); echo '</pre>'; */ // OR // 2) Using the service Locator $this->getUsersTable()->saveUser($auth); $this->sendConfirmationEmail($auth); $this->flashMessenger()->addMessage($auth->usr_email); return $this->redirect()->toRoute('auth/default', array('controller' => 'registration', 'action' => 'registration-success')); } } return new ViewModel(array('form' => $form)); }
public function loginAction() { if (isset($_SESSION['bareos']['authenticated']) && $_SESSION['bareos']['authenticated']) { return $this->redirect()->toRoute('dashboard', array('action' => 'index')); } $this->layout('layout/login'); $config = $this->getServiceLocator()->get('Config'); $form = new LoginForm($config['directors']); $request = $this->getRequest(); if ($request->isPost()) { $auth = new Auth(); $form->setInputFilter($auth->getInputFilter()); $form->setData($request->getPost()); if ($form->isValid()) { $director = $form->getInputFilter()->getValue('director'); $username = $form->getInputFilter()->getValue('consolename'); $password = $form->getInputFilter()->getValue('password'); $config = $this->getServiceLocator()->get('Config'); $this->director = $this->getServiceLocator()->get('director'); $this->director->set_config($config['directors'][$director]); $this->director->set_user_credentials($username, $password); if ($this->director->auth($username, $password)) { $_SESSION['bareos']['director'] = $director; $_SESSION['bareos']['username'] = $username; $_SESSION['bareos']['password'] = $password; $_SESSION['bareos']['authenticated'] = true; $_SESSION['bareos']['idletime'] = time(); return $this->redirect()->toRoute('dashboard', array('action' => 'index')); } else { session_destroy(); $err_msg = "Sorry, can not authenticate. Wrong username and/or password."; return new ViewModel(array('form' => $form, 'err_msg' => $err_msg)); } } else { // given credentials in login form could not be validated in this case $err_msg = "Please provide a director, username and password."; session_destroy(); return new ViewModel(array('form' => $form, 'err_msg' => $err_msg)); } } return new ViewModel(array('form' => $form)); }