-
-
Save wilmoore/426672 to your computer and use it in GitHub Desktop.
Practical exemple of what can be done with a DI Container in ZF.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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