Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow entire controller in CakePHP Auth and allow() doesn't work

I have tried to perform

$this->Auth->allow() in beforeFilter() but, I need to add entire controller as an exception, i.e. it needs to be public and does not require user to be signed in.

Just a short-cut way to perform $this->Auth->allow( every-function-in-this-controller )

Answers?

Edit:

I have this:

<?php

App::uses('AppController','Controller');

class AllzonesController extends AppController {

    public function __beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('index');
    }

    public function index() {
        $this->layout = 'main';
        $this->set('Hello',"Hello world");
    }
}

It is transferring Auth-login()

like image 705
Karma Avatar asked Jun 13 '26 15:06

Karma


2 Answers

What you proposed is the appropiate way of what you want to do

public function beforeFilter() {
    $this->Auth->allow();
}

Reading the API docs

Takes a list of actions in the current controller for which authentication is not required, or no parameters to allow all actions.

So the function with no parameters should allow a normal user (not logged in) to access every action of that controller.

EDIT :

Sorry, missed the version reference in your tag. In here it says

 $this->Auth->allow('*');

is the appropiate way for Cake 2.0 (and previous versions, as noted by @mark)

like image 110
Nunser Avatar answered Jun 15 '26 05:06

Nunser


For versions beyond CakePHP 2.1

$this->Auth->allow() instead of $this->Auth->allow('*')

and

To my other question __beforeFiter is not a magic function!

<?php

App::uses('AppController','Controller');

class AllzonesController extends AppController {

    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('index');
    }

    public function index() {
        $this->layout = 'main';
        $this->set('Hello',"Hello world");
    }
}
like image 32
Karma Avatar answered Jun 15 '26 06:06

Karma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!