Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allowing a Specific Page in Cakephp

I understand how to allow certain controller actions for non-logged in users. But, I can't find any documentation on how to allow access to specific pages. The controller is pages and the action is display. But, I don't want to allow the user to see all pages, just the about page.

So, what is the correct way to allow guests access to some, but not all, pages?

like image 888
Amy Anuszewski Avatar asked Apr 29 '11 19:04

Amy Anuszewski


2 Answers

I'm afraid you can't do that using the standard functions that AuthComponent gives you. You have to create your own logic for that in the pages_controller's display action.

Something like (pseudo-code style)

# in app/controllers/pages_controller.php
var $allowedPages = array('one', 'two');

function display($page) {
    if(in_array($page, $allowedPages) || $this->User->loggedin) {
        $this->render($page);
    } else {
        $this->render('not_allowed');
    }
}
like image 159
vindia Avatar answered Sep 27 '22 16:09

vindia


In CakePHP 3.x you can accomplish your goal by specifying the full action in the PagesController beforeFilter action:

public function beforeFilter(Event $event) {
  parent::beforeFilter($event);

  $this->Auth->allow(
    ['controller' => 'pages', 'action' => 'display', 'about']
  );
}
like image 34
ericgio Avatar answered Sep 27 '22 16:09

ericgio