views:

43

answers:

2

Hi All,

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

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
}
jostster
Thank you for the reply. The problem i have when redirect is, I want to make the user stay in the same page where he requested from. For example he requests /user/delete from /user/view. In my beforefilter the request is /user/delete. How i can get the "/user/view" url - the one from where he requested ? So dat i can redirect him to /user/view
metalhawk
I was unable to figure out a way to just cancel it so what I ended up doing is checking in my action if they were authenticated and if so then I would continue with the processing of the page. If not then I would not display the info that the user request but instead display the flash message. See answer for example
jostster
Thank you for the reply. The "edit 2" idea works, but there is a lot of code redundancy. I have to perform this check for all the methods, so i was thinking if i could somehow do it in the beforefilter(), then it be more single point and modular.
metalhawk
@metalhawk I added edit 3
jostster
Thank you Joster. But it behaves as the last comment says. "Returning false keeps the flow running (cake will execute further actions).".
metalhawk
@metalhawk updated to edit 4
jostster
Hi Joster, Deny is to set(kinda) permissions. The action "delete" is already denied. When i perform Acl->check(..). It returns false, as it is denied.
metalhawk
I'm confused at what your wanting then...
jostster
I'm exactly looking for the one listed in "edit3"
metalhawk
@metalhawk ... I am sorry I miss understood your first comment. The redirect will work fine but you just wanted to get the url the user came from. Use the `$this->redirect($this->referrer());` This will redirect the user to the page they came from. But before doing the redirect I would do $this->Session->setFlash("message"); so they can see why they were redirected.
jostster
Thank you for your time Joster. $this->referrer() did not work. But $_SERVER['HTTP_REFERER'] worked. Thanks again. :)
metalhawk
@metalhawk sorry i had a typo $this->referer()
jostster
A: 

In your controller:

if (!$this->Acl->check(.....)){
    //User do not have permission to view the page.
    $this->Session->setFlash('Unauthorized.', 'auth_error');
}

Create view app/views/layouts/auth_error.ctp In this new view file add:

<?php $session->flash(); ?>
webbiedave
Thank you for your prompt reply. I get the following error... Fatal error: Allowed memory size of 209715200 bytes exhausted (tried to allocate 523800 bytes) in ..../cake/libs/debugger.php on line 612
metalhawk