【问题标题】:Zend Framework double Acl Resources with ModulesZend 框架双 Acl 资源与模块
【发布时间】:2012-02-16 11:12:18
【问题描述】:

我设置了一个新的模块管理员,所以我可以通过 www.example.com/admin 完美地访问这个模块 - 但我当然想让这个只有管理员可以访问。现在,控制器显然是“索引”以及动作。但是因为我希望每个人都访问 www.example.com,它也获得了控制器和操作“索引”,所以我的 Acl 插件中有以下几行:

$acl->add(new Zend_Acl_Resource('index'));
$acl->add(new Zend_Acl_Resource('admin:index'));

$acl->allow(null, array('index'));

// admins can do anything
$acl->allow('administrator', null);

但这似乎也适用于管理模块。如何从管理模块限制普通用户?我已经试过了

$acl->deny('guest', 'admin:index', 'index');

但这似乎不起作用。非常感谢帮助。谢谢你,对不起我的英语不好。

最好的问候。

【问题讨论】:

  • 您是否在任何地方定义了角色?即:访客和管理员

标签: zend-framework acl


【解决方案1】:

我经常为我的管理员和用户前端使用单独的会话命名空间,以及单独的 ACL。

通过为我的管理面板设置一个单独的会话,登录用户仍然无法访问管理部分,而不管 ACL 是什么,因为管理的 Zend_Auth 会话与用户会话完全隔离。

这是我用于管理的类,它扩展了 Zend_Auth。请注意在 getInstance 中它如何将存储设置为与默认 Zend_Auth 不同的会话命名空间。

<?php

class My_Admin_Auth extends Zend_Auth
{
    protected function __construct()
    {}

    protected function __clone()
    {}

    public static function getInstance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();
            self::$_instance->setStorage(new Zend_Auth_Storage_Session('Zend_Auth_admin'));
        }

        return self::$_instance;
    }

    /**
     * Get an auth adapater instance suitable for authenticating agains
     * the administrator database.
     * @param string $username
     * @param string $password
     * @return Zend_Auth_Adapter_DbTable Adapter used to call $auth->authenticate($adapter);
     */
    public static function getAdapter($username, $password, $usersalt)
    {
        $db = Zend_Controller_Front::getInstance()
                                     ->getParam('bootstrap')
                                     ->getResource('db');

        $authAdapter = new Zend_Auth_Adapter_DbTable($db,
                                                     'administrators',
                                                     'username',
                                                     'password');

        $authAdapter->setIdentity($username)
                    ->setCredential($password)
                    ->setCredentialTreatment(
                        'SHA1(CONCAT(?,"' . $usersalt . '"))'
                    );

        return $authAdapter;
    }

    /**
     * Return a SHA-1 hashed and salted version of the entered password
     * @param string $plaintext  Password to hash, a static salt is also applied
     * @return string the hashed password
     */
    public static function hashAdminPassword($plaintext, $usersalt)
    {
        return sha1($plaintext . $usersalt);
    }
}

除此之外,您可以为管理区域定义一个单独的 ACL,默认情况下拒绝所有内容,然后仅在实例化类时用户是管理员时添加允许规则。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-29
    • 2012-05-18
    • 2011-01-19
    相关资源
    最近更新 更多