Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP中beforefilter()中的取消请求_Php_Acl_Before Filter - Fatal编程技术网

PHP中beforefilter()中的取消请求

PHP中beforefilter()中的取消请求,php,acl,before-filter,Php,Acl,Before Filter,我使用CakePHP、CAS进行身份验证,使用ACL进行授权。 如果用户没有查看页面的权限,我需要刷新一条消息,说明不允许或重定向到另一个页面 例如:如果用户正在查看/users/view/1。现在用户请求/users/delete/1。用户没有删除的权限。所以我想在他从/users/view/1请求的页面上显示一条flash消息 在我的app_控制器中,我具有以下功能: function beforeFilter() { $this->__initPhpCas(); if (is

我使用CakePHP、CAS进行身份验证,使用ACL进行授权。 如果用户没有查看页面的权限,我需要刷新一条消息,说明不允许或重定向到另一个页面

例如:如果用户正在查看/users/view/1。现在用户请求/users/delete/1。用户没有删除的权限。所以我想在他从/users/view/1请求的页面上显示一条flash消息

在我的app_控制器中,我具有以下功能:

function beforeFilter() {
  $this->__initPhpCas();
  if (isset($_SESSION['loggedIn'])){
    if(!$this->Acl->check(.....){
        //User do not have permission to view the page.
        // Need to cancel this request and flash a message 
   }
  }
如有任何建议,我们将不胜感激

最后的答案是

function beforeFilter() {
      $this->__initPhpCas();
      if (isset($_SESSION['loggedIn'])){
        if(!$this->Acl->check(.....){
            //User do not have permission to view the page.
            // Need to cancel this request and flash a message 
            $this->Session->setFlash(__('You are not authorized to view this page.', true));
        $this->redirect($_SERVER['HTTP_REFERER']);
       }
      }
使用$this->redirect;通过使用$this->Session->setFlash;。我已经包括了向您展示的链接

编辑:

我建议设置flash消息,然后执行重定向。然后在重定向页面上,用$session->flash;显示flash消息

编辑2:

因为你不想做重定向,你需要做这样的事情

function view() {
    if($this->Acl->check(.....){
        //display the page and continue with the view action
    }
    else {
        $this->Session->setFlash("You do not have access to use this feature");
    }
}
编辑3:

。看看链接中的最后一篇文章

编辑4:尝试使用

编辑5:

如果我理解正确,您希望使用beforeFilter检查他们是否有访问权限,如果没有,则不要继续运行操作。CakePHP实际上不允许这样做,但有一个解决办法

function beforeFilter() {
    if($this->Acl->check(.....){
        //display the page and continue with the view action
    }
    else {
        $this->Session->setFlash("You do not have access to use this feature");
        $this->params['action'] = "failedCheck";
    }
}

function failedCheck() {
    //blah blah blah
}

谢谢你的回复。重定向时的问题是,我想让用户停留在他请求的页面上。例如,他请求/user/delete from/user/view。在我的beforefilter中,请求是/user/delete。我如何获得/user/view url—他请求的url?所以dat我可以将他重定向到/user/view我无法找到一种方法来取消它,所以我最终要做的是检查我的操作是否经过身份验证,如果是,那么我将继续处理页面。如果不是,我将不显示用户请求的信息,而是显示flash消息。参见答案示例谢谢您的回复。edit2的想法是可行的,但是有很多代码冗余。我必须对所有的方法进行检查,所以我在想,如果我能在beforefilter中进行检查,那么它将更加单点和模块化。谢谢Joster。但它的行为正如最后一条评论所说。返回false将保持流运行,cake将执行进一步的操作..嗨,Joster,Deny是为了设置Kinda权限。删除操作已被拒绝。当我执行Acl->检查时。。。它返回false,因为它被拒绝。