首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在BjyAuthorize之前执行代码

在BjyAuthorize之前执行代码
EN

Stack Overflow用户
提问于 2014-11-16 22:29:43
回答 1查看 127关注 0票数 0

我已经创建了一个基于cookie的“记住我”的登录系统,我使用的是ZF2和ZfcUser和BjyAuthorize。那么,在登录后,如果缓存过期,BjyAuthorize触发器将出现在我的“记住我”系统之前,用户将无法识别为已登录。

这就是我在我的AbstractController:https://gist.github.com/Gamempire/2b6b6388cedca9491d5f#file-abstractcontroller-php-L18 (每个控制器都扩展了我的AbstractController)中称为“记住我”的cookie系统的地方。

我如何在BjyAuthorize之前调用它,这样BjyAuthorize就知道有人登录了?

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-11-22 20:20:42

要正确地做到这一点,我不只是尝试在bjyAuthorize之前注入自己,最好添加到ZfcUser功能中,并使用storageChain来获取用户的身份。

若要通过重写ZfcUser的服务名称来执行此操作,请执行以下操作

代码语言:javascript
复制
'service_manager' => array(
    'factories' => array(
        'zfcuser_auth_service' => 'myNamespace\Factory\AuthenticationServiceFactory',
    ),
),

之后,您需要创建您自己的工厂,您可以复制ZfcUser并对其进行更改。

代码语言:javascript
复制
public function createService(ServiceLocatorInterface $serviceLocator)
{
    $storageChain = new \Zend\Authentication\Storage\Chain();

    // dummy storage interface
    $nonPersistent = new \Zend\Authentication\Storage\NonPersistent(); // replace this with your own storage interface.
    $nonPersistent->write(1); // just to test 

    /* @var $authStorage Storage\StorageInterface */
    $authStorage = $serviceLocator->get('ZfcUser\Authentication\Storage\Db');

    $storageChain->add($authStorage, 10); // Check this interface first.
    $storageChain->add($nonPersistent, 0); // Check this interface Second.

    /* @var $authAdapter Adapter\AdapterInterface */
    $authAdapter = $serviceLocator->get('ZfcUser\Authentication\Adapter\AdapterChain');

    return new AuthenticationService(
        $storageChain,
        $authAdapter
    );
}

使用此示例代码,当没有登录用户的会话时,$this->zfcUserAuthentication()->getIdentity()将返回1。

在此之后,您需要创建自己的存储接口。它实现Zend\Authentication\Storage\StorageInterface。我不知道你的cookie实现是怎样的,你需要让它适应这一点。此外,您还需要实现何时使用cookie和何时不使用cookie。还有一个为zfcuser创建的模块,它实现了一个名为GoalioRememberMe的me函数。希望这能帮到你。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26962805

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档