Step 1
You will need to define your user entity. The relevant part of my entity is as follows:
/**
* Class Player
* @ORM\Entity
*/
class Player
{
/**
* @var integer
* @ORM\Id @ORM\GeneratedValue @ORM\Column(type="integer", name="player_id")
*/
protected $playerId;
/**
* @var string
* @ORM\Column(type="string")
*/
protected $username = '';
/**
* @var string
* @ORM\Column(type="string")
*/
protected $password = '';
public function getPassword()
{
return $this->password;
}
public function setPassword($plaintextPassword, $salt)
{
$this->password = crypt($plaintextPassword, '$5$rounds=5000$'.$salt.'$');
return $this;
}
public static function hashPassword($player, $password)
{
return ($player->getPassword() === crypt($password, $player->getPassword()));
}
}
Step 2
We need to setup the dependancy manager inside zend to give us back a properly configured Authenication service in our controller. (This is the bit I couldn't find any documentation on) A quick read through the code in the doctrine ORM module and a bit of experimentation suggested that the structure of the config should be as follows:
'doctrine' => array(
'driver' => array(
'application_models' => array(
'class' =>'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(__DIR__ . '/../src/Application/Model')
),
'orm_default' => array(
'drivers' => array(
'Application\Model' => 'application_models'
)
)
),
'authentication' => array(
'orm_default' => array(
//should be the key you use to get doctrine's entity manager out of zf2's service locator
'objectManager' => 'Doctrine\ORM\EntityManager',
//fully qualified name of your user class
'identityClass' => 'Application\Model\Player',
//the identity property of your class
'identityProperty' => 'username',
//the password property of your class
'credentialProperty' => 'password',
//a callable function to hash the password with
'credentialCallable' => 'Application\Model\Player::hashPassword'
),
),
),
Step 3
Once that is all setup, you are ready to authenticate your user in your controller. For berevity I have not included any of the form setup or handling.
$auth = $this->getServiceLocator()->get('doctrine.authenticationservice.orm_default');
$auth->getAdapter()->setIdentityValue($username);
$auth->getAdapter()->setCredentialValue($password);
$result = $auth->authenticate();
Its not much use getting your user to login if you can't later find out who is logged in, the following lines of code will return the user entity to your application.
$auth = $this->getServiceLocator()->get('doctrine.authenticationservice.orm_default');
$player = $auth->getIdentity();
Message