我正在使用带有Controller_Template的Kohana 3.2。基本上我想做的是检查每个action_method的ACL。如果失败,请加载拒绝访问视图并跳过其余action_method代码;否则继续加载。
我知道我可以使用如果...else 语句来执行简单的布尔检查(甚至执行if(check_permission())return;
(,但我希望有一种更优雅的方法来处理它,action_page()
中多余的代码最少......如果可能的话,只需check_permission();
.我可以在function check_permission()
中添加更多代码
function check_permission() {
$this->template->content = View::factory('system/access_denied')
->bind('title', $title);
$title = 'Access Denied';
}
function action_page() {
check_permission();
$this->template->content = View::factory('page/index')
->bind('title', $title);
->bind('data', $data);
$title = 'Page loaded';
.
.
.
}
也许在 kohana 中有一些特定的方法可以实现这一点?原生 php 也很好...
如果你需要一个很好的ACL模块,你可以使用Zend Framework的ACL。这是包含 Zend Framework 的 Kohana 模块。
你可以像这样使用它:
开始:
$acl = new Zend_Acl();
添加角色:
$acl->addRole(new Zend_Acl_Role('guest'))
->addRole(new Zend_Acl_Role('member'))
->addRole(new Zend_Acl_Role('admin'));
添加资源(控制器(:
$acl->add(new Zend_Acl_Resource('someController'));
允许访问资源(控制器(的角色和特权(操作(:
$acl->allow('member', 'someController' array('page'));
然后签入您的方法,以获取用户的权限:
public function befor()
{
$role = .... // role from user
$resource = $this->request->controller();
$action = $this->request->action();
if ($acl->isAllowed($role, $resource, $action))
{
//...redirect
}
}
这是你要找的吗?
我认为Request::current()->redirect('...')
befor()
会帮助你。
喜欢这个:
public function befor()
{
parent::befor();
if (... have no access)
{
Request::current()->redirect('access/denied');
}
}
。
Class Access extends Controller_Template {
public function action_denied()
{
$this->template->content = View::factory('system/access_denied')
->bind('title', $title);
$title = 'Access Denied';
}
}