【问题标题】:CakePHP Auth only for Admin PrefixCakePHP Auth 仅用于管理前缀
【发布时间】:2014-02-02 17:20:22
【问题描述】:

我有管理员前缀,我们有一个基于 CMS 的部分,网站所有者可以在其中维护应用内容。

有没有办法通过 Auth 组件自动限制这些部分。而其他部分不需要认证。

// other wise i will have to add a lot of actions like below
$this->Auth->allow('home', 'about', 'contacts);

【问题讨论】:

    标签: cakephp authentication cakephp-2.4


    【解决方案1】:

    只要$this->Auth->allow();prefix != 'admin'

    【讨论】:

    • 老兄,我的要求是允许具有“admin”角色的用户能够访问 CakePHP 3.x 中具有“Admin”前缀的所有操作......请你帮帮我在这里?
    【解决方案2】:

    您可以使用基于控制器的授权非常简单地完成此操作: http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#using-controllerauthorize

    这个想法是,您将在您的控制器(以及您需要特定规则的任何控制器)中有一个 isAuthorized() 方法,该方法根据用户的授权返回 true 或 false。

    因此,您可以通过在 AppController 中添加类似内容来禁用所有管理路径(经过身份验证的路径除外):

    public function isAuthorized($user) {
        // * Admin section control
        if (empty($this->params['admin'])) {
            // ** DEFAULT: All users can access public functions
            return true;
        } else if(AuthComponent::user('role') == 'admin'){
            // ** Allow admin users access to everything.
            return true;
        }
        // * DEFAULT: Deny all
        return false;
    }
    

    编辑:糟糕——这只有在用户已经通过身份验证(登录)时才有用,抱歉。

    【讨论】:

    • 所以将它添加到 AppController 中,我需要在所有控制器的 beforeFilter() 中调用它来检查授权?
    • 授权仍然需要用户登录
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-30
    • 1970-01-01
    相关资源
    最近更新 更多