User storage and setting namespace

4 years ago

kleinpetr
Backer | 481
+
0
-

Hello, I using several storages of user. Everything works fine, but my IDE (phpStorm) marked ->setNamespace method as not found.

The same problem I have with
$this->context->getService('*authenticator')

How do I fix this ?

Last edited by kleinpetr (2015-11-16 14:39)

4 years ago

CZechBoY
Member | 3409
+
0
-

Inject services via constructor or @inject annotation.

4 years ago

kleinpetr
Backer | 481
+
0
-

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)

4 years ago

kleinpetr
Backer | 481
+
0
-

So what is the best practice with using authenticators ?? When in nette manual is $this->context ?

If i understand correctly, so if I set autowired: yes I can inject this service. ?

Thanks for help.

Last edited by kleinpetr (2015-11-16 17:56)

4 years ago

kleinpetr
Backer | 481
+
0
-

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)

4 years ago

David Matějka
Moderator | 5977
+
0
-

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)

4 years ago

kleinpetr
Backer | 481
+
0
-

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)

4 years ago

kleinpetr
Backer | 481
+
0
-

So, how can I use multiple authenticator without $this->context Thanks for help.

4 years ago

CZechBoY
Member | 3409
+
0
-

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);
}

4 years ago

David Matějka
Moderator | 5977
+
0
-

@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)

4 years ago

kleinpetr
Backer | 481
+
0
-

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 ?