我目前正在开发一个需要ACL的网站,因为我使用Zend,我使用他们的ACL类是有道理的,但我对如何做到这一点几乎没有任何想法.我已经阅读了文档,但它让我更加困惑...基本上我想要做的就是设置两个用户组,例如"normal"和"admin",普通用户可以访问所有具有非管理员控制器的页面admin显然可以访问管理控制器页面.
我有很多问题:
我该如何设置?
我应该通过DB或config.ini运行它吗?
我在哪里放置我的ACL.php?
我该如何编写这样的脚本?
我如何调用,这是在索引中完成的吗?
如果你引导我访问一个网站或一个很好的教程,我将非常感激.
不久前我实施了类似的事情.基本概念在示例代码中.
我创建了自己的configAcl.php文件,该文件加载到bootstrap文件中,在我的例子中是index.php.这是根据你的情况如何:
$acl = new Zend_Acl(); $roles = array('admin', 'normal'); // Controller script names. You have to add all of them if credential check // is global to your application. $controllers = array('auth', 'index', 'news', 'admin'); foreach ($roles as $role) { $acl->addRole(new Zend_Acl_Role($role)); } foreach ($controllers as $controller) { $acl->add(new Zend_Acl_Resource($controller)); } // Here comes credential definiton for admin user. $acl->allow('admin'); // Has access to everything. // Here comes credential definition for normal user. $acl->allow('normal'); // Has access to everything... $acl->deny('normal', 'admin'); // ... except the admin controller. // Finally I store whole ACL definition to registry for use // in AuthPlugin plugin. $registry = Zend_Registry::getInstance(); $registry->set('acl', $acl);
另一种情况是,如果您想在所有控制器上仅允许普通用户"列表"操作.这很简单,你可以添加这样的行:
$acl->allow('normal', null, 'list'); // Has access to all controller list actions.
接下来,您应该创建一个新插件,当有一些控制器操作请求时,它会自动处理凭证检查.此检查在preDispatch()方法中进行,该方法在每次调用控制器操作之前调用.
这是AuthPlugin.php:
class AuthPlugin extends Zend_Controller_Plugin_Abstract { public function preDispatch(Zend_Controller_Request_Abstract $request) { $loginController = 'auth'; $loginAction = 'login'; $auth = Zend_Auth::getInstance(); // If user is not logged in and is not requesting login page // - redirect to login page. if (!$auth->hasIdentity() && $request->getControllerName() != $loginController && $request->getActionName() != $loginAction) { $redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('Redirector'); $redirector->gotoSimpleAndExit($loginAction, $loginController); } // User is logged in or on login page. if ($auth->hasIdentity()) { // Is logged in // Let's check the credential $registry = Zend_Registry::getInstance(); $acl = $registry->get('acl'); $identity = $auth->getIdentity(); // role is a column in the user table (database) $isAllowed = $acl->isAllowed($identity->role, $request->getControllerName(), $request->getActionName()); if (!$isAllowed) { $redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('Redirector'); $redirector->gotoUrlAndExit('/'); } } } }
最后的步骤是加载configAcl.php并在引导文件(可能是index.php)中注册AuthPlugin.
require_once '../application/configAcl.php'; $frontController = Zend_Controller_Front::getInstance(); $frontController->registerPlugin(new AuthPlugin());
所以这是基本概念.我没有测试上面的代码(复制,粘贴和重写只是为了展示目的)所以它不是防弹.只是想一个主意.
编辑
为了清晰起见.AuthPlugin中的上述代码假设$ identity对象填充了用户数据(数据库中的"role"列).这可以在登录过程中完成,如下所示:
[...] $authAdapter = new Zend_Auth_Adapter_DbTable($db); $authAdapter->setTableName('Users'); $authAdapter->setIdentityColumn('username'); $authAdapter->setCredentialColumn('password'); $authAdapter->setIdentity($username); $authAdapter->setCredential(sha1($password)); $authAdapter->setCredentialTreatment('? AND active = 1'); $auth = Zend_Auth::getInstance(); $result = $auth->authenticate($authAdapter); if ($result->isValid()) { $data = $authAdapter->getResultRowObject(null, 'password'); // without password $auth->getStorage()->write($data); [...]