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; })); }
/** * 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; }