Zend\Authentication is an authentication component provided by Zend Framework which can be used for authentication against a wide number of authentication mechanisms including database table, HTTP authentication, and LDAP authentication. The component also lets you store the session information to a wide range of storages.
In this example, we will be using the Zend\Authentication component to validate the user credentials submitted in the login form.
In this task we will be authenticating the login form using the Zend\Authentication
component using the following steps:
1. Add a function to return the authentication service in the login controller src/ Users/Controller/LoginController.php:
// References
use Zend\Authentication\AuthenticationService;
use Zend\Authentication\Adapter\DbTable as DbTableAuthAdapter;
// Class definition
public function getAuthService()
{
if (! $this->authservice) {
$dbAdapter = $this->getServiceLocator()->get('Zend\Db\Adapter\ Adapter');
$dbTableAuthAdapter = new DbTableAuthAdapter($dbAdapter, 'user','email','password', 'MD5(?)');
$authService = new AuthenticationService();
$authService->setAdapter($dbTableAuthAdapter);
$this->authservice = $authService;
}
return $this->authservice;
}
2. In the processAction() method for LoginController, check if the form submission is valid, and use the AuthService method to validate the credentials using the authenticate method:
public function processAction()
//
$this->getAuthService()->getAdapter()
->setIdentity($this->request-
>getPost('email'))
>getPost('password'));
->setCredential($this->request-
$result = $this->getAuthService()->authenticate(); if ($result->isValid()) {
$this->getAuthService()->getStorage()->write($this->request-
>getPost('email'));
return $this->redirect()->toRoute(NULL , array('controller' => 'login', 'action' => 'confirm'));
}
public function confirmAction()
{
$user_email = $this->getAuthService()->getStorage()->read();
$viewModel = new ViewModel(array( 'user_email' => $user_email));
return $viewModel;
}
4. The view for the user's home page created under /view/users/login/confirm.
phtml will be as follows:
<section class="login-confirm">
<h2>Login Successful</h2>
<p> Welcome! <?php echo $this->user_email; ?> </p>
</section>
5. Open the login page in your browser and try to log in with the credentials that you used during registration. The login form should look like the following:
Upon successful login, you will be redirected to the login success page as shown below.
What just happened?
We created a new database table authentication adapter for the user table to validate the email and password fields. Using the authentication adapter we have been able to perform authentication for registered users.
Q1. Which file should be modified to store the database credentials application-wide?
1. <App>/module/<Module>/config.inc
2. <App>/config/autoload/global.php
3. <App>/module/<Module>/module.config.php
4. <App>/module/<Module>/config/module.config.php
Q2. What is the correct method to assign an input filter to a form?
1. $form->setInputFilter($inputFilter)
2. $form->useInputFilter($inputFilter)
3. $form->assignInputFilter($inputFilter)
4. $form->mapInputFilter($inputFilter)
Summary
In this chapter we have learned creating forms, doing basic validations, storing form data to the database, using models, and authenticating with the database. In the next chapter we will be learning about advanced database operations, which will be based on the TableGateway pattern that we have covered in this chapter.
Không có nhận xét nào:
Đăng nhận xét