public function loginAction() { $redirect = 'dashboard'; if ($this->auth->hasIdentity()) { return $this->redirect()->toRoute($redirect); } $request = $this->getRequest(); if ($request->isPost()) { $this->authForm->setData($request->getPost()); if ($this->authForm->isValid()) { $authAdapter = new AuthAdapter($this->getServiceLocator()->get('Zend\\Db\\Adapter\\Adapter')); $authAdapter->setTableName('admins')->setIdentityColumn('email')->setCredentialColumn('password')->setIdentity($request->getPost('email'))->setCredential($request->getPost('password'))->setCredentialTreatment('md5(?)'); $Site_Id = 1; $authAdapter->getDbSelect()->where('site_id=' . $Site_Id); $result = $this->auth->authenticate($authAdapter); if ($result->isValid()) { if ($request->getPost('remember_me') == 1) { $storage = $this->getServiceLocator()->get('Application\\Model\\MyAuthStorage'); $storage->setRememberMe(1); } $this->flashmessenger()->addMessage('You are looged in successfully'); return $this->redirect()->toRoute($redirect); } else { $this->flashmessenger()->addErrorMessage('Invalid username or password, try again.'); return $this->redirect()->toRoute('home'); } } } $viewModel = new ViewModel(array('form' => $this->authForm)); $viewModel->setTerminal(true); return $viewModel; }
public function authenticate() { // connection $dbAdapter = new DbAdapter(array('driverClass' => 'Doctrine\\DBAL\\Driver\\PDOMySql\\Driver', 'dbname' => 'RioVerdeShopping')); // set identification $authAdapter = new AuthAdapter($dbAdapter); $authAdapter->setTableName($this->entity)->setIdentityColumn($this->identidade)->setCredentialColumn($this->credencial); $authAdapter->setIdentity($this->login)->setCredential($this->senha); //authenticate $result = $authAdapter->authenticate(); switch ($result->getCode()) { case Result::FAILURE_IDENTITY_NOT_FOUND: $msg = "Login inexistente!"; break; case Result::FAILURE_CREDENTIAL_INVALID: $msg = "Senha inválida!"; break; case Result::SUCCESS: $registro = $result->getIdentity(); $msg = "Seja bem vindo(a) " . $registro['nome']; break; default: $msg = "Falha na tentativa de autenticação!"; break; } return $msg; }
public function authenticate() { $authAdapter = new AuthAdapter($this->adapter); $authAdapter->setTableName('administrators')->setIdentityColumn('username')->setCredentialColumn('password'); $authAdapter->setIdentity($this->adapterUsername)->setCredential($this->adapterPassword); $authService = $this->authConnect->authenticate($authAdapter); if ($authService->isValid()) { $this->authConnect->getStorage()->write($authAdapter->getResultRowObject()); return array('status' => 'success', 'code' => '200', 'msg' => $authService->getMessages()); } else { return array('status' => 'failure', 'code' => '301', 'msg' => $authService->getMessages()); /* switch ($authService->getCode()) { case Result::FAILURE_CREDENTIAL_INVALID: break; case Result::FAILURE_IDENTITY_NOT_FOUND: break; default: break; } */ } }
/** * Ensure that exceptions are caught */ public function testCatchExceptionBadSql() { $this->setExpectedException('Zend\\Authentication\\Adapter\\Exception\\RuntimeException', 'The supplied parameters to'); $this->_adapter->setTableName('bad_table_name'); $this->_adapter->setIdentity('value'); $this->_adapter->setCredential('value'); $this->_adapter->authenticate(); }
public function login(PasswordModel $password, $dbAdapter) { $authAdapter = new AuthAdapter($dbAdapter); $authAdapter->setTableName('password')->setIdentityColumn('staff_id')->setCredentialColumn('password'); $authAdapter->setIdentity($password->staffId)->setCredential($password->password); $result = $this->auth->authenticate($authAdapter); if ($result->isValid()) { $storage = $this->auth->getStorage(); $storage->write($authAdapter->getResultRowObject()); } else { throw new \Exception('パスワードが間違いました。'); } }
public function auth(User $user) { $auth = new AuthenticationService(); $authAdapter = new DbTable($this->dbAdapter); $authAdapter->setTableName('user')->setIdentityColumn('username')->setCredentialColumn('upassword'); $authAdapter->setIdentity($user->getUsername())->setCredential($user->getUpassword()); $result = $auth->authenticate($authAdapter); if ($result->isValid()) { $storage = $auth->getStorage(); $storage->write($authAdapter->getResultRowObject(array('userID', 'username', 'schoolID'))); return true; } else { print_r($result->getMessages()); } }
public function getServiceConfig() { return array('factories' => array('log' => function ($sm) { $log = new Logger(); $writer = new FirePhpWriter(new FirePhpBridge(new \FirePHP())); $log->addWriter($writer); return $log; }, 'Application\\Storage\\Login' => function ($sm) { return new \Application\Storage\Login('nhpress'); }, 'Zend\\Session\\SessionManager' => function ($sm) { $config = $sm->get('config'); if (isset($config['session'])) { $session = $config['session']; $sessionConfig = null; if (isset($session['config'])) { $class = isset($session['config']['class']) ? $session['config']['class'] : 'Zend\\Session\\Config\\SessionConfig'; $options = isset($session['config']['options']) ? $session['config']['options'] : array(); $sessionConfig = new $class(); $sessionConfig->setOptions($options); } $sessionStorage = null; if (isset($session['storage'])) { $class = $session['storage']; $sessionStorage = new $class(); } $sessionSaveHandler = null; if (isset($session['save_handler'])) { $sessionSaveHandler = $sm->get($session['save_handler']); } $sessionManager = new SessionManager($sessionConfig, $sessionStorage, $sessionSaveHandler); } else { $sessionManager = new SessionManager(); } Container::setDefaultManager($sessionManager); return $sessionManager; }, 'AuthService' => function ($sm) { $dbAdapter = $sm->get('Zend\\Db\\Adapter\\Adapter'); $dbTableAuthAdapter = new DbTableAuthAdapter($dbAdapter); $dbTableAuthAdapter->setTableName('correspondent'); $dbTableAuthAdapter->setIdentityColumn('username'); $dbTableAuthAdapter->setCredentialColumn('password'); $authService = new AuthenticationService(); $authService->setAdapter($dbTableAuthAdapter); $authService->setStorage($sm->get('Application\\Storage\\Login')); return $authService; })); }
/** * Faz a autenticação dos usuários * * @param array $params * @return array */ public function authenticate($params) { if (!isset($params['username']) || !isset($params['password'])) { throw new \Exception("Parâmetros inválidos"); } $password = md5($params['password']); $auth = new AuthenticationService(); $authAdapter = new AuthAdapter($this->dbAdapter); $authAdapter->setTableName('users')->setIdentityColumn('username')->setCredentialColumn('password')->setIdentity($params['username'])->setCredential($password); $result = $auth->authenticate($authAdapter); if (!$result->isValid()) { throw new \Exception("Login ou senha inválidos"); } //salva o user na sessão $session = $this->getServiceManager()->get('Session'); $session->offsetSet('user', $authAdapter->getResultRowObject()); return true; }
public function accountAction() { $session = new Container('user'); $logged = $session->offsetGet('username'); if ($logged === null) { $this->redirect()->toRoute('user', array('action' => 'signin')); } $id = (int) $this->params()->fromRoute('id', 0); if (!$id) { return $this->redirect()->toRoute('user'); } $user = $this->getUserTable()->getUser($id); $form = new AccountForm(); $form->bind($user); $form->get('submit')->setAttribute('value', 'Save changes'); $form->get('password')->setAttribute('readonly', 'true'); $request = $this->getRequest(); if ($request->isPost()) { $form->setInputFilter($user->getInputFilter()); $form->setData($request->getPost()); if ($form->isValid()) { $username = $this->getUserTable()->getUserByName($user->username); if (!empty($username)) { $this->redirect()->toRoute('user', array('action' => 'account', 'id' => $user->id)); echo '<div class="alert alert-error" id="change-alert"> <button type="button" class="close" data-dismiss="alert">×</button> <h4>Warning!</h4> User already exist!. </div>'; } else { $this->getUserTable()->modifyAccount($user); $sm = $this->getServiceLocator(); $dbAdapter = $sm->get('Zend\\Db\\Adapter\\Adapter'); $authAdapter = new AuthAdapter($dbAdapter); $authAdapter->setTableName('user')->setIdentityColumn('username')->setCredentialColumn('password'); $authAdapter->setIdentity($user->username)->setCredential($user->password); $authService = new AuthenticationService(); $authService->setAdapter($authAdapter); $result = $authService->authenticate(); $this->redirect()->toRoute('user'); } } } return array('id' => $id, 'user' => $user, 'form' => $form); }
/** * This action is called when a user is to be authenticated by their username and password * * @return \Zend\View\Model\ViewModel */ public function authenticateAction() { $Logform = new LoginForm(); //Gets the username $email = $this->request->getPost('email'); //Get the password and encrypt it using md5 $password = md5($this->request->getPost('password')); //Create a connection to the database $db = $this->getServiceLocator()->get('dbcon'); if ($this->request->isPost()) { //Perform a check to see if username and password are not empty if ($email != null and $password != null) { //Create an instance of the Auth Adapter $auth = new AuthAdapter($db); //Set the user name $auth->setIdentity($email); //Set the password $auth->setCredential($password); //Set the Table name $auth->setTableName('users'); //Set the user name colum $auth->setIdentityColumn('email'); //Set the password column $auth->setCredentialColumn('password'); //Authenticate the user $auth->authenticate(); //If authentication is valid if ($auth->authenticate()->isValid()) { //Convert the user credentials from an object into an array $array = get_object_vars($auth->getResultRowObject()); //Set the username and store it in session $this->session->offsetSet('email', $array['email']); $this->session->offsetSet('username', $array['username']); //Set the user id and store in session $this->session->offsetSet('id', $array['id']); //Set the user full name and store in session $this->session->offsetSet('fullname', $array['full_name']); $this->AuthenticationLogger("user logged in successfully at " . date('y-m-d H:i:s')); $this->ActivityLogs("user logged in successfully at " . date('Y-m-d H:i:s')); //Redirect the user to the admin page $this->getUrl('ekontact', 'Ekontact', 'dashboard'); } else { $msg = $this->flashMessenger()->addMessage(sprintf(" %s Invalid email or password %s", '<div class="error">', '</div>')); return $this->redirect()->toRoute('authentication', array('controller' => 'Authentication', 'action' => 'login')); } } else { $msg = $this->flashMessenger()->addMessage(sprintf(" %s Please make sure both email and password fields are not empty %s", '<div class="error">', '</div>')); $this->getUrl('authentication', 'Authentication', 'login'); } } $view = new ViewModel(array('form' => $Logform)); $this->layout('layout/login_layout'); return $view; }
<?php require_once './autoloader.php'; use Zend\Authentication\Adapter\DbTable as AuthAdapter; use Zend\Authentication\AuthenticationService; $adapter = new Zend\Db\Adapter\Adapter(array('driver' => 'pdo', 'dsn' => 'mysql:dbname=eva;hostname=localhost', 'username' => 'root', 'password' => 'password', 'driver_options' => array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''))); // Configure the instance with constructor parameters... $authAdapter = new AuthAdapter($adapter); $authAdapter->setTableName('eva_user_users')->setIdentityColumn('userName')->setCredentialColumn('password'); $authAdapter->setIdentity('AlloVince')->setCredential('$2y$14$WmZhcUFqblVqaU1ieUVqZOOKNVv3GZS8DmRLfBOkD53OvG6fvbBjy'); // instantiate the authentication service $auth = new AuthenticationService(); // Attempt authentication, saving the result $result = $auth->authenticate($authAdapter); if (!$result->isValid()) { // Authentication failed; print the reasons why foreach ($result->getMessages() as $message) { echo "{$message}\n"; } } else { p($auth->getIdentity()); }
public function loginAction() { $view = new ViewModel(); // $view->setTerminal(true); $this->layout('layout/layout-login'); $form = new LoginForm(); $request = $this->getRequest(); if ($request->isPost()) { // get post data $post = $request->getPost(); // get the db adapter $sm = $this->getServiceLocator(); $dbAdapter = $sm->get('Zend\\Db\\Adapter\\Adapter'); // create auth adapter $authAdapter = new AuthAdapter($dbAdapter); // configure auth adapter $authAdapter->setTableName('ta_usuario')->setIdentityColumn('va_nombre')->setCredentialColumn('va_contrasenia'); // pass authentication information to auth adapter $authAdapter->setIdentity($post->get('va_nombre'))->setCredential($post->get('va_contrasenia')); // create auth service and set adapter // auth services provides storage after authenticate $authService = new AuthenticationService(); $authService->setAdapter($authAdapter); // authenticate // var_dump($authAdapter->authenticate());Exit; $result = $authService->authenticate(); // var_dump($result->isValid());Exit; // check if authentication was successful // if authentication was successful, user information is stored automatically by adapter if ($result->isValid()) { // redirect to user index page return $this->redirect()->toUrl($this->getRequest()->getBaseUrl() . '/restaurante/index/index'); } else { 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: /** do stuff for successful authentication * */ break; default: /** do stuff for other failure * */ break; } } } $view->setVariables(array('form' => $form)); return $view; // return array('form' => $form); }