Thứ Năm, 24 tháng 4, 2014

Zend\Authentication [Creating a Communication Application]

Zend\Authentication is an authentication component provided bZend 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 componentvalidate 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 treturn 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 tvalidate 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'));
}

3.       The ConfirmAction function will render the logged in user's welcome screen:
public function confirmAction()
{
$user_email = $this->getAuthService()->getStorage()->read();
$viewModel  = new ViewModel(array( 'user_email' => $user_email));
return $viewModel;
}

4.       The viefor 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 trto log in with the credentials thayou 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 tvalidate 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 tstore 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 datto 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 thawe havcovered in this chapter.

Không có nhận xét nào:

Đăng nhận xét