Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // module.config.php (adapt the module name and the entity name
- 'doctrine' => array(
- 'authentication' => array(
- 'odm_default' => array(
- 'object_manager' => 'doctrine.documentmanager.odm_default',
- 'identity_class' => 'Users\Entity\User',
- 'identity_property' => 'email',
- 'credential_property' => 'password',
- /*'credential_callable' => function(User $user, $passwordGiven) {
- return any_check_test($user->getPassword(), $passwordGiven);
- }, /*_*/
- ),
- ),
- // module-name:Users and entity declared : Users\Entity\Users
- 'driver' => array(
- 'users' => array(
- 'class' => 'Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver',
- 'cache' => 'array',
- 'paths' => array(
- __DIR__ . '/../src/Users/Entity'
- ),
- ),
- 'odm_default' => array(
- 'drivers' => array(
- 'Users\Entity' => 'users',
- ),
- ),
- ),
- )
- // inside the controller
- public function loginAction() {
- /* use-case :
- * create a form
- * if incoming data (from a login form) fill out the form with that data
- * if form is valid : authenticate the user (+redirect him)
- * if not valid : re-show the form (with data + validation errors )
- * if not data received : show a new (empty) form
- * send the form to the view
- */
- $loginForm = $this->getServiceLocator()->get('LoginForm');
- //
- if ($this->request->isPost())
- {
- $loginForm->setData($this->request->getPost());
- if ($loginForm->isValid())
- {
- $data = $loginForm->getData();
- $authService = $this->getServiceLocator()
- ->get('doctrine.authenticationservice.odm_default');
- $adapter = $authService->getAdapter();
- $adapter->setIdentityValue($data['email']); // i am using email
- $adapter->setCredentialValue($data['password']);
- $authResult = $authService->authenticate();
- if ($authResult->isValid()) {
- return $this->redirect()->toRoute('home'); // or last viewed page
- }
- return array(
- 'loginForm' => $loginForm,
- 'errors' => 'Email or password is not valid',
- );
- $this->redirect()->toRoute('home');
- } /* else {
- //
- // LOG Event ( login|password not valide )
- //
- //Zend\Debug\Debug::dump("not valid data");
- //Zend\Debug\Debug::dump($loginForm->getMessages());
- }//* */
- }
- //
- return new ViewModel(array(
- 'loginForm' => $loginForm,
- ));
- }
- public function logoutAction()
- {
- $identity = null;
- $authService = $this->getServiceLocator()->get('doctrine.authenticationservice.odm_default');
- if ( $authService->hasIdentity() ) {
- // in case you need it (log/etc... )
- $identity = $authService->getIdentity();
- }
- $authService->clearIdentity(); // clear anyway
- return $this->redirect()->toRoute('home');
- // or return array('msg'=>'disconnected'); // ajax?...
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement