Skip to content

Instantly share code, notes, and snippets.

@loicfrering
Created June 4, 2010 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save loicfrering/425504 to your computer and use it in GitHub Desktop.
Save loicfrering/425504 to your computer and use it in GitHub Desktop.
Practical exemple of what can be done with a DI Container in ZF.
parameters:
auth.adapter.entityName: Application_Model_User
auth.adapter.identityField: email
auth.adapter.credentialField: password
services:
auth.adapter:
class: LoSo_Zend_Auth_Adapter_Doctrine2
arguments: [@em, %auth.adapter.entityName%, %auth.adapter.identityField%, %auth.adapter.credentialField%]
auth:
class: Zend_Auth
constructor: getInstance
<?php
/**
* @Service
*/
class Application_Service_AuthService
{
/**
* @var Zend_Auth
* @Inject("auth")
*/
protected $auth;
public function setAuth($auth)
{
$this->auth= $auth;
return $this;
}
/**
* @var Application_Service_Doctrine_UserService
* @Inject
*/
protected $userService;
public function setUserService($userService)
{
$this->userService = $userService;
return $this;
}
/**
* @var Zend_Auth_Adapter_Interface
* @Inject("auth.adapter")
*/
protected $authAdapter;
public function setAuthAdapter($authAdapter)
{
$this->authAdapter = $authAdapter;
return $this;
}
protected $user;
public function authenticate($username, $password)
{
$this->authAdapter->setIdentity($username)
->setCredential($password);
return $this->auth->authenticate($this->authAdapter);
}
public function hasIdentity()
{
return $this->auth->hasIdentity();
}
public function getIdentity()
{
return $this->auth->getIdentity();
}
public function getIdentityAsUser()
{
if (null === $this->user) {
$this->user = $this->userService->findOneByEmail($this->getIdentity());
}
return $this->user;
}
public function isIdentifiedUser($user)
{
return $user->getEmail() == $this->getIdentity();
}
}
<?php
/**
* @Service
*/
class AuthController extends LoSo_Zend_Controller_Action
{
/**
* @var Application_Service_Doctrine_UserService
* @Inject
*/
protected $userService;
public function setUserService($userService)
{
$this->userService = $userService;
return $this;
}
/**
* @var Application_Service_AuthService
* @Inject
*/
protected $authService;
public function setAuthService($authService)
{
$this->authService = $authService;
return $this;
}
public function loginAction()
{
$loginForm = new Application_Form_Login();
$loginForm->setAction($this->view->url(array('action' => 'authenticate')));
$this->view->loginForm = $loginForm;
}
public function authenticateAction()
{
$loginForm = new Application_Form_Login();
if ($this->getRequest()->isPost()) {
if ($loginForm->isValid($_POST)) {
$result = $this->authService->authenticate($loginForm->username->getValue(), $loginForm->password->getValue());
if ($result->isValid()) {
$this->_helper->flashMessenger->setNamespace('success')->addMessage($this->view->translate('Authentication successfull.'));
return $this->_helper->redirector('index', 'index');
} else {
switch($result->getCode()) {
case Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID:
$message = 'Invalid password.';
break;
case Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND:
case Zend_Auth_Result::FAILURE_IDENTITY_AMBIGUOUS:
$message = 'Invalid username.';
break;
default:
$message = 'Authentication failed.';
}
$this->_helper->flashMessenger->setNamespace('error')->addMessage($this->view->translate($message));
return $this->_helper->redirector('login');
}
} else {
$this->view->loginForm = $loginForm;
return $this->render('login');
}
}
return $this->_helper->redirector('login');
}
public function logoutAction()
{
Zend_Auth::getInstance()->clearIdentity();
$this->_helper->redirector('index', 'index');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment