alvincrespo
alvincrespo

Reputation: 9334

CakePHP Auth Allow JSON Extension

Essentially, what I would like to know is if you can use the Auth Component to allow certain extensions (JSON/HTML)?

Basically, lets say we have one action, the action is index. In this action all we do is list authors (Users). So the url is http://somewebsite.com/authors/index. If we go to that url the content type would be HTML, which should be restricted to logged in users (Admins) so that they can have Edit/Delete buttons. However, we also use this action to present json when you put the .json extension at the end of it, so the url will be http://somewebsite.com/authors/index.json. In this case, you wouldn't need to be logged in because you just want to access that information.

So, is it possible for the Auth Component to allow certain extensions, and is this the best way to go about it?

Thanks and Cheers!

Upvotes: 3

Views: 1037

Answers (1)

deceze
deceze

Reputation: 522024

Something along these lines should work (including explicitly "unlocking" only specific methods):

public function beforeFilter() {
    $methods = array('index', 'foo', 'bar');

    // please forgive the terrible indentation
    if (in_array($this->action, $methods) &&
        isset($this->params['ext']) && $this->params['ext'] == 'json'
    ) {
        $this->Auth->allow($this->action);
    }
}

Upvotes: 3

Related Questions