当没有足够的权限时,带有ACL的Cakephp不会重定向

时间:2014-01-20 03:58:06

标签: cakephp permissions acl redirect

我一直在Cakephp网站上工作,差不多完成了,除了标题上的问题,我需要做的是如果用户没有足够的访问级别继续实际工作,则重定向到一个页面好吧,如果你在地址栏中写了网址,但是当按下应该指向同一地址的按钮时却没有。

这是我的AppController类

class AppController extends Controller {
public $components = array(
    'Acl',
    'Auth' => array(
        'authorize' => array(
            'Actions' => array('actionPath' => 'controllers')
        )
    ),
    'Session'
);
public $helpers = array('Html', 'Form', 'Session');

public function beforeFilter() {
    //Configure AuthComponent
    //debug($this->params,true);
    $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
    $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login');
    $this->Auth->loginRedirect = array('controller' => 'posts', 'action' => 'add');
    $this->Auth->authError = "Error";
            if (isset($this->params['requested'])) $this->Auth->allow($this->action); 
}

这是一个链接到“添加”页面的示例,如果不允许用户执行此操作,则应该重定向到我的AppController中配置的页面

<?php echo $this->Html->link($this->Html->image("add.png", array("alt" => "Agregar nueva encuesta","title" => "Agregar nueva encuesta")),
        array('controller'=>'Surveys','action'=>'add',$beneficiary['Beneficiary']['id']),
        array('escape' => false));?>

因此,要明确的是,如果用户写入地址“myserver / Beneficiaries / add / parameter”被重定向到显示他无法执行该操作的页面,但如果他按下该链接则没有任何反应,他无法添加任何内容,但我需要重定向。

我很感激任何建议。

我希望我的解释清楚,我的英语远非完美:)

编辑:

我决定遵循用户Ben Hitchcock关于使用网络调试器“Charles”的建议。这就是我得到的

在地址栏写作

请求 GET / Projects / SUISEPV2 / Beneficiaries / edit / 1 HTTP / 1.1 主机localhost User-Agent Mozilla / 5.0(Windows NT 6.3; WOW64; rv:26.0)Gecko / 20100101 Firefox / 26.0 接受text / html,application / xhtml + xml,application / xml; q = 0.9, / ; q = 0.8 Accept-Language es-ES,es; q = 0.8,en-US; q = 0.5,en; q = 0.3 Accept-Encoding gzip,deflate Cookie CAKEPHP = 7399cvgls9gurk37ubes1fknf0 连接保持活力

Responde 找到HTTP / 1.1 302 日期星期一,2014年1月20日16:54:56 GMT 服务器Apache / 2.4.7(Win32)OpenSSL / 0.9.8y PHP / 5.4.22 X-Powered-By PHP / 5.4.22 位置myserver / Projects / SUISEPV2 / posts / add 内容长度0 保持活动超时= 5,最大= 100 连接保持活跃 Content-Type text / html;字符集= UTF-8

按链接

请求: GET / Projects / SUISEPV2 / Beneficiaries / edit / 1 HTTP / 1.1 主机localhost User-Agent Mozilla / 5.0(Windows NT 6.3; WOW64; rv:26.0)Gecko / 20100101 Firefox / 26.0 接受text / html,application / xhtml + xml,application / xml; q = 0.9, / ; q = 0.8 Accept-Language es-ES,es; q = 0.8,en-US; q = 0.5,en; q = 0.3 Accept-Encoding gzip,deflate Referer myserver / Projects / SUISEPV2 / Beneficiaries Cookie CAKEPHP = 7399cvgls9gurk37ubes1fknf0 连接保持活力

响应: 找到HTTP / 1.1 302 日期:星期一,2014年1月20日16:55:33 GMT 服务器Apache / 2.4.7(Win32)OpenSSL / 0.9.8y PHP / 5.4.22 X-Powered-By PHP / 5.4.22 位置myserver / Projects / SUISEPV2 / Beneficiaries 内容长度0 保持活动超时= 5,最大= 100 连接保持活跃 Content-Type text / html;字符集= UTF-8

不幸的是我仍然看不出有什么问题。

1 个答案:

答案 0 :(得分:0)

我找到了一个解决方案,它比我想要的更好。

在我的布局中,我添加了下一行

<?php echo $this->Session->flash('auth'); ?>

现在我可以在AppController类

中显示我在beforeFilter函数中配置的消息
$this->Auth->authError = "Custom error message here";

结果如下:

如果我按下链接按钮,我没有被重定向,但是我收到一条消息,显示我想告诉用户的内容。 如果我写的地址我被重定向到错误页面,我也得到了flash消息。