1

I'm trying to implement authentication module in a ZF2 application, i did exactly as i found in official docs, but i'm getting this error:

Fatal error: Call to undefined method DoctrineModule\Authentication\Adapter\ObjectRepository::setIdentityValue() in DOCROOT/module/Login/src/Login/Controller/IndexController.php on line 33

I put this in my module.config.php:

'doctrine' => array(
    'driver' => array(
        __NAMESPACE__ . '_driver' => array(
            'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
            'cache' => 'array',
            'paths' => array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Entity')
        ),
        'orm_default' => array(
            'drivers' => array(
                __NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver'
            )
        )
    ),
    'authentication' => array(
        'orm_default' => array(
            'object_manager' => 'Doctrine\ORM\EntityManager',
            'identity_class' => 'Login\Entity\User',
            'identity_property' => 'email',
            'credential_property' => 'password',
        ),
    ),
)

in my module.php:

public function getServiceConfig()
{
    return array(
        'factories' => array(
            'Zend\Authentication\AuthenticationService' => function($serviceManager) {
                return $serviceManager->get('doctrine.authenticationservice.orm_default');
            }
        )
    );
}

And this is my controller:

namespace Login\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class IndexController extends AbstractActionController{

public function indexAction(){
    if ($this->getRequest()->isPost())  {
        if($this->authenticate()){
            return new ViewModel(array(
                'error' => 'Your authentication credentials is valid!',
            ));
        }else{
            return new ViewModel(array(
                'error' => 'Your authentication credentials are not valid',
            ));
        }

    }else{
        return new ViewModel(array('error' => ''));
    }
}

public function authenticate(){

    $data = $this->getRequest()->getPost();

    $authService = $this->getServiceLocator()->get('Zend\Authentication\AuthenticationService');

    $adapter = $authService->getAdapter();
    $adapter->setIdentityValue($data['email']);
    $adapter->setCredentialValue($data['password']);
    $authResult = $authService->authenticate();
    if ($authResult->isValid()) {
        return true;
    }else{
        return false;
    }
}

}

Any glue?

Arthur Mastropietro
  • 673
  • 1
  • 7
  • 22
  • Take a look at this for configuration [Zend 2 + doctrine 2 Auth Adapter](http://stackoverflow.com/questions/12092091/zend-2-doctrine-2-auth-adapter) – ArrowHead Feb 05 '16 at 16:09
  • I followed along but none help. My configuration seems to be pretty right. Look that it doesn't find this function: `DoctrineModule\Authentication\Adapter\ObjectRepository::setIdentityValue()` – Arthur Mastropietro Feb 05 '16 at 17:29

3 Answers3

8

Ok, i found the answer. Seems like the method setIdentityValue() and setCredentialValue() (getters as well) doesn't exists anymore. Instead to set these values you have to call setIdentity() and setCredential(). Seems this was changed recently, cause i found nothing about it, and my application used to work and suddenly, stop working cause these changes. This is how i realized the methods changed their sign:

$authService = $this->getServiceLocator() >get('Zend\Authentication\AuthenticationService');`
$adapter = $authService->getAdapter();
$class_methods = get_class_methods($adapter);
echo "<pre>";print_r($class_methods);exit;

And the output:

Array
(
    [0] => __construct
    [1] => setOptions
    [2] => getOptions
    [3] => authenticate
    [4] => getCredential
    [5] => setCredential
    [6] => getIdentity
    [7] => setIdentity
)
Arthur Mastropietro
  • 673
  • 1
  • 7
  • 22
0

Take a look at this configuration :

Zend2 + Doctrine2 Authentication

ArrowHead
  • 609
  • 5
  • 16
  • I followed along but none help. My configuration seems to be pretty right. Look that it doesn't find this function: `DoctrineModule\Authentication\Adapter\ObjectRepository::setIdentityValue()` – Arthur Mastropietro Feb 05 '16 at 17:30
0

Yes, I'll confirm your point (and error) Arthur. I've just updated to Zend 2.5, my actual version. Methods 'setIdentityValue' and 'setCredentialValue' need to be changed in: - setIdentity() - setCredential(). With this change my code works fine.