【发布时间】:2023-03-23 13:00:03
【问题描述】:
我看到了以下场景:
- 针对 AD 运行身份验证
- AD 服务器返回标识字符串“foo\bar”
- 身份验证成功后,将触发事件“LOGIN_SUCCESS”,并将身份字符串和适配器作为参数
一旦触发此事件,就会附加多个触发器。
- 第一个侦听器检查数据库中是否已经存在具有匹配“身份 foo\bar”的用户行。如果没有,将创建一个用户行
- 每次登录时,另一个侦听器都会更新适配器提供的用户元数据
- 我想要第三个侦听器,将身份字符串“foo\bar”从数据库中的用户行更改为用户对象
目前我的AuthenticationService 看起来像这样:
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());
}
如您所见,我在这里做的事情并不真正属于AuthenticationService。 Event 有没有办法修改给定的parameter(在本例中为identity),以便在后台更改参数的值?
我真的不想在AuthenticationService 中检查这个 eventResult。这个假设不应该存在,但我真的没有看到任何不同的做事方式。
//编辑
目前触发器正在后台执行此操作:
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);
}
【问题讨论】:
标签: php zend-framework2