Setting the user id directly on your storage has some downsides. It (obviously) won't work if you use another storage engine. And events registered on ZFCUser's AuthService
will not be executed.
I would solve this issue by using a custom authentication adapter. This Adapter can be called from a controller in the following way:
$this->zfcUserAuthentication()->getAuthService()->authenticate(new ForceLogin($user));
The implementation looks as follows:
class ForceLogin implements Zend\Authentication\Adapter\AdapterInterface
{
/**
* @var UserInterface
*/
protected $user;
/**
* @param $user
*/
function __construct($user)
{
$this->user = $user;
}
public function authenticate()
{
return new Zend\Authentication\Result(
Result::SUCCESS,
$this->getUser()->getId()
);
}
}