简体   繁体   English

CakePHP Auth允许JSON扩展

[英]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)? 基本上,我想知道的是,您是否可以使用Auth组件来允许某些扩展(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 . 所以网址是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. 如果我们转到该URL,则内容类型将是HTML,应限制为登录用户(管理员),以便他们可以拥有编辑/删除按钮。 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 . 但是,当你将.json扩展名放在它的末尾时,我们也会使用这个动作来呈现json,所以url将是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? 那么,Auth组件是否可以允许某些扩展,这是最好的方法吗?

Thanks and Cheers! 谢谢,干杯!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM