User storage and setting namespace
- kleinpetr
- Member | 480
Good idea, because if I have in config this code
myAuthenticator:
class: App\Model\MyAuthenticator
autowired: no
But inject no working I must register authenticator as normal service:
- App\Model\MyAuthenricator
So what is the setting autowired ??
Last edited by kleinpetr (2015-11-16 16:21)
- kleinpetr
- Member | 480
Hi
inject is beautiful but if I have for example 2 authenticators Inject not working because implement multiple service of IAuthenticator. I know I can use addon where I implement User instead of IAuthenticator but I dont like it.. So in this situation is better autowired: no and getting authenticator over $this->context.. Although my phpStorm doesnt like it, but use addon ‘multiple authenticator’ seems to me unnecessarily complex.
Last edited by kleinpetr (2015-11-17 11:35)
- David Matějka
- Moderator | 6445
Hi, $this->context
is (almost) always wrong. You should pass
specific authenticator to the presenter using neon configuration:
services:
myAuthenticator: App\Model\MyAuthenticator
- App\MyPresenter(@myAuthenticator)
- kleinpetr
- Member | 480
So I can set authenticator only for some Presenters ? If I try this
#authenticators
frontAuthenticator: App\Model\FrontAuthenticator
- App\FrontModule\Presenters\SignPresenter(@frontAuthenticator)
adminAuthenticator: App\Model\AdminAuthenticator
- App\AdminModule\Presenters\SignPresenter(@adminAuthenticator)
But still not working:
Service 'security.user': Multiple services of type Nette\Security\IAuthenticator
Last edited by kleinpetr (2015-11-17 12:09)
- CZechBoY
- Member | 3608
Register it as service, without implements IAuthenticator
, then
set authenticator you want in the presenter.
/**
* @var \App\Model\ModuleXXXAuthenticator
*/
public $authenticator;
public function injectAuthenticator (\App\Model\ModuleXXXAuthenticator $authenticator)
{
$this->authenticator = $authenticator;
$this->getUser()->setAuthenticator($authenticator);
}
- David Matějka
- Moderator | 6445
@CZechBoY that won't work, there is a typehint on setAuthenticator. And also, it is not guaranteed that User will be injected before injectAuthenticator method is called.
@kleinpetr either you can disable autowiring for both services and
then set the authenticator you need using
$this->user->setAuthenticator()
. Or you can omit
implements IAuthenticator
, inject authenticator and
login using
$identity = $this->authenticator->authenticate(array($username, $password));
$this->user->login($identity);
And since you do not implement IAuthenticator anymore, you can change the
signature of the authenticate method to
e.g. authenticate($username, $password)
- kleinpetr
- Member | 480
Yes, that's working ! But I can't disable autowiring.. I register authenticators as normally service like this:
#authenticators
- App\Model\FrontAuthenticator
- App\Model\AdminAuthenticator
And injecting this services in BasePresenter and where I need some
authenticator so I use it.
This solution is working, but how you use multiple authenticators you do ? Also
this ?