PHP 中的 beforefilter() 取消请求

发布于 2024-09-25 03:51:07 字数 1002 浏览 3 评论 0原文

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

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

在我的 app_controller 中,我有以下功能:

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']);
       }
      }

I'm using CakePHP , CAS for Authentication and ACL for Authorization.
If the user donot have permission to view the page, i need to flash a message stating Not permitted OR redirect to another page.

Ex: If the user is viewing /users/view/1 .Now the user requests /users/delete/1. The user donot have permission to delete. So I want to display a flash message on the page he requested from (/users/view/1).

In my app_controller, i have the following function:

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 
   }
  }

Any suggestions are appreciated

Final answer is

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']);
       }
      }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

芯好空 2024-10-02 03:51:07

重定向 使用 $this->redirect();并使用 $this->Session->setFlash 添加消息 ();。我已经添加了链接来向您展示。

编辑:

我建议设置闪存消息,然后进行重定向。然后在重定向的页面上,使用 $session->flash(); 显示 Flash 消息。

EDIT2:

由于您不想进行重定向,因此您需要执行类似的操作。

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:尝试使用 deny()

编辑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
}

to redirect use $this->redirect(); and add a message by using $this->Session->setFlash();. I have included links to show you.

EDIT:

I would recommend setting the flash message then doing the redirect. Then on the redirected page, display the flash message with $session->flash();.

EDIT2:

Since you are not wanting to do a redirect you will need to do something like this.

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");
    }
}

EDIT 3:

Try this. Take a look at the last post in the link.

Edit 4: Try using deny()

Edit 5:

If I understand you correctly you want to use beforeFilter to check if they have access and if not then don't continue running the actions. CakePHP doesn't really allow this but a work around is.

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文