Advertisement
aromatix

ZF2 MongoDB ODM authentication

Sep 14th, 2013
453
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.27 KB | None | 0 0
  1. // module.config.php (adapt the module name and the entity name
  2. 'doctrine'  => array(
  3.     'authentication'    => array(
  4.         'odm_default'   => array(
  5.             'object_manager'        => 'doctrine.documentmanager.odm_default',
  6.             'identity_class'        => 'Users\Entity\User',
  7.             'identity_property'     => 'email',
  8.             'credential_property'   => 'password',
  9.             /*'credential_callable' => function(User $user, $passwordGiven) {
  10.                 return any_check_test($user->getPassword(), $passwordGiven);
  11.             }, /*_*/
  12.         ),
  13.     ),
  14.     // module-name:Users and entity declared : Users\Entity\Users
  15.     'driver'    => array(  
  16.         'users' => array(
  17.             'class' => 'Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver',
  18.             'cache' => 'array',
  19.             'paths' => array(
  20.                 __DIR__ . '/../src/Users/Entity'
  21.             ),
  22.         ),
  23.         'odm_default'   => array(
  24.             'drivers'   => array(
  25.                 'Users\Entity' => 'users',
  26.             ),
  27.         ),
  28.     ),
  29. )
  30.  
  31. // inside the controller
  32. public function loginAction() {
  33.     /* use-case :
  34.      * create a form
  35.      *     if incoming data (from a login form) fill out the form with that data
  36.      *         if form is valid    : authenticate the user (+redirect him)
  37.      *             if not valid    : re-show the form (with data + validation errors )
  38.      *     if not data received : show a new (empty) form
  39.      * send the form to the view
  40.      */
  41.     $loginForm = $this->getServiceLocator()->get('LoginForm');
  42.     //
  43.     if ($this->request->isPost())
  44.     {
  45.         $loginForm->setData($this->request->getPost());
  46.         if ($loginForm->isValid())
  47.         {
  48.             $data = $loginForm->getData();
  49.             $authService = $this->getServiceLocator()
  50.                         ->get('doctrine.authenticationservice.odm_default');
  51.  
  52.             $adapter = $authService->getAdapter();
  53.             $adapter->setIdentityValue($data['email']);  // i am using email
  54.             $adapter->setCredentialValue($data['password']);
  55.             $authResult = $authService->authenticate();
  56.  
  57.             if ($authResult->isValid()) {
  58.                 return $this->redirect()->toRoute('home'); // or last viewed page
  59.             }
  60.  
  61.             return array(
  62.                 'loginForm' => $loginForm,
  63.                 'errors' => 'Email or password is not valid',
  64.             );
  65.  
  66.             $this->redirect()->toRoute('home');
  67.         } /* else {
  68.             //
  69.             // LOG Event ( login|password not valide )
  70.             //
  71.             //Zend\Debug\Debug::dump("not valid data");
  72.             //Zend\Debug\Debug::dump($loginForm->getMessages());
  73.         }//* */
  74.     }
  75.     //
  76.     return new ViewModel(array(
  77.             'loginForm' => $loginForm,
  78.     ));
  79. }
  80.  
  81. public function logoutAction()
  82. {
  83.     $identity  = null;
  84.     $authService = $this->getServiceLocator()->get('doctrine.authenticationservice.odm_default');
  85.         if ( $authService->hasIdentity() ) {
  86.             // in case you need it (log/etc... )
  87.             $identity = $authService->getIdentity();
  88.         }
  89.         $authService->clearIdentity(); // clear anyway
  90.  
  91.     return $this->redirect()->toRoute('home');
  92.     // or return array('msg'=>'disconnected'); // ajax?...
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement