【问题标题】:working with session on zf2 (recover container)在 zf2 上使用会话(恢复容器)
【发布时间】:2014-01-29 11:46:32
【问题描述】:

先例:

  • 使用自定义 hack OF ZfcUserLdap 对 LDAP 服务器进行身份验证(也包括 zfcUser 作为依赖项)
  • hack 是由于 Ldap 服务器使用 ldapc 包装器,因此绑定和搜索过程不属于 Ldap 标准,而是通过 ldapc 库进行的
  • 通过修改 bind 和 findbyuser 方法,登录/密码框非常适合 Ldap 服务器

需要:

  • 在登录步骤中添加国家/地区选择
  • 检查用户是否有权限在这个国家工作(所以这里的国家是有意义的,不需要ACL,它将通过LDAP用户组检查)
  • 存储所选国家/地区以在整个应用程序中使用

正在进行中:

  • 将带有可用国家/地区的 SELECT 下拉列表添加到登录框 [OK]
  • 获取在登录表单中选择的国家[OK]

    -> 在 ZfcUserLdap\Authentication\Adapter\Ldap.php 类的身份验证方法中,我正确地获得了表单中设置的国家/地区

问题:

  • 如何将国家存储到会话变量中,

    -> 因为 zfcUser 定义了一个存储并且在登录步骤中定义了国家,我想使用那个存储

我将不胜感激任何类型的澄清或提示来完成这项任务。

解决方案:

逻辑更多在 zfcUserLdap 模块,因为身份验证是针对 LDAP 服务器的。 我向在 zfcUserLdap 扩展的实体添加了一个新属性 country,该属性通过 findByUsername 方法设置为实体对象。

public function findByUsername($username, $country = null)
{
    $pUser = $this->ldap->findByUsername($username);

    if (isObjectNotNull($pUser))
    {
        $this->entity->setDisplayName(getLdapUserFirstName($pUser) . ' ' . getLdapUserLastName($pUser));
        $this->entity->setEmail(getLdapUserMail($pUser));
        $this->entity->setId(getLdapUserUid($pUser));
        $this->entity->setUsername(getLdapUserUid($pUser));
        $this->entity->setLdapcObject($pUser);
        $this->entity->setUserCountry($country);

        return $this->entity;
    }
    else {
        return null;
    }
}

在此处输入国家/地区将很有用,因为身份验证过程可能会检查用户名是否有权在该国家/地区工作。我需要稍后添加该检查。

像这样,国家是实体对象的一部分,所以我可以像获取用户名一样获取国家。

目前,我已经创建了一个与 ZfcUserDisplayName 非常相似的 View Helper。我只是更新 get 方法来获取 country 属性。

 $countryName = $user->getUserCountry();

我计划创建一个控制器插件来从任何控制器获取国家/地区。

【问题讨论】:

    标签: session zend-framework2


    【解决方案1】:

    ZFCUser 有一个身份验证事件,您应该对此加以利用。在模块的主引导程序中:

    $sm = $e->getApplication()->getServiceManager();
    $zfcAuthEvents = $e->getApplication()->getServiceManager()->get('ZfcUser\Authentication\Adapter\AdapterChain')->getEventManager();
    
    $zfcAuthEvents->attach( 'authenticate', function( $authEvent ) use( $sm ){
        try
        {
            // use $authEvent->getIdentity() to get country and stick it in a session
    
            return true;
        }
        catch( \Exception $x )
        {
            // handle it
        }
    });
    

    如何在会话中存储取决于您,有 400 种方法可以给猫剥皮。

    【讨论】:

    • 我添加了我的解决方案作为编辑,顺便说一句,您的解释为我在 ZF 世界的研究提供了一个新领域。
    猜你喜欢
    • 2015-06-21
    • 2013-06-22
    • 2019-02-16
    • 2010-12-09
    • 1970-01-01
    • 2013-06-14
    • 1970-01-01
    • 1970-01-01
    • 2017-06-13
    相关资源
    最近更新 更多