The presence of an event changes the variable in the background

I have the following script presented to me:

  • Authentication with AD
  • AD server returns the identification string "foo \ bar"
  • Upon successful authentication, the LOGIN_SUCCESS event is fired with an identification string and an adapter as parameters

Once this event is fired, there are several triggers.

  • The first listener checks to see if a user row exists in the database with the matching "line foo \ bar". If not, User-Row will be created.
  • Each time you log in, another Listener will update the User-Metadata provided by the adapter.
  • I would like to have a third listener that changes the identity string "foo \ bar" to a user object from User-Row to DB

Currently mine AuthenticationServicelooks like this:

if ($result->isValid()) {
    $currentIdentity = $result->getIdentity();

    $eventManager = new EventManager(__CLASS__);
    $eventResult = $eventManager->trigger(self::DUIT_USER_LOGIN_SUCCESSFUL, $this, [
        'identity' => $currentIdentity,
        'adapter' => $adapter
    ]);

    // This whole stuff sucks!
    if ($eventResult->last() instanceof EventInterface) {
        $identityObject = $eventResult->last()->getParam('identity');

        if ($identityObject instanceof User) {
            $this->getStorage()->write($identityObject);

            return $result;
        }
    }

    $this->getStorage()->write($result->getIdentity());
}

As you can see, I am doing this here, which does not actually belong AuthenticationService. Is there a way Eventthis can change parameter(in this case identity) so that the parameter value is changed in the background?

I really don't want to do this event check in AuthenticationService. This assumption simply should not be, but I really see no other way to do something.

//Edit

Currently, the trigger does this in the background:

public function handleWorkflow(EventInterface $event)
{
    $identityString = $event->getParam('identity');

    $userObject = $this->userService->findByActiveDirectoryId($identityString);

    if ($userObject instanceof User) {
        return $event->setParam('identity', $userObject);
    }

    return $event->setParam('identity', $identityString);
}
+3
source share
1 answer

As discussed in the IRC, this type of “conversion” applied to event data is subtle and will bite you.

string , a IdentityFinder - AuthenticationService:

if ($result->isValid()) {
    $currentIdentity = $result->getIdentity();

    // we use some sort of locator to find the identity
    $identity = $this->identityLocator->find($currentIdentity);

    $this->getStorage()->write($identity);

    // triggering is still OK, but not to manipulate the data here.
    // it should also happen _AFTER_ successful authentication
    // I also assume you injected the EventManager here, instead of building it
    // result is also being ignored, it shouldn't contain data required to us
    $this->eventManager->trigger(
        self::DUIT_USER_LOGIN_SUCCESSFUL, 
        $this,
        [
            'identity' => $identity,
            'adapter' => $adapter
        ]
    );

}

, , finder no-op, :

class NoopIdentityFinder implements IdentityFinderInterface
{
    public function find($identity)
    {
        return $identity; // no real "find" going on
    }
}

, "" , .

+2

All Articles