【问题标题】:cakephp manage correctly permission in beforeFiltercakephp 在 beforeFilter 中正确管理权限
【发布时间】:2013-11-12 22:31:32
【问题描述】:

我的控制器有点问题。 我希望用户只能在某些页面内访问,而 andmin 用户只能在更多页面内访问。

我有一个名为 UsersController 的控制器 这是它的 beforeFilter 方法

public function beforeFilter () {
        parent::beforeFilter(); // chiamo anche il callback beforeFilter dal parent per ottenere un'autorizzazione per l'utente loggato da tutte le viste $this->Auth->allow('index','view'); per tutti i Model 
        $views = array ('login','register','activate');
        if ($this->Session->read('is_logged')) {
            $views = array_merge ($views, array ('login','logout', 'change_password'));
            if ($user_type == 'admin') {
                $views = array_merge ($views, array ('add','delete','edit','create','index'));
            }
        }
        $this->Auth->allow($views);
    }

在此功能中客人可以进入内部登录、注册和激活。
登录的用户可以访问内部登录。注销和更改密码,管理其他页面。

但这不起作用。例如,登录的用户可以访问索引视图或添加视图。

为什么会这样?

这是我在 appController 中的 beforeFilter:

public function beforeFilter () {
        $this->setReleaseData();

        $this->checkUserStatus();
        $this->updateTimezone();
        $this->setRedirect();

        if($this->Session->read('is_logged')){
            $auth_user = $this->Auth->user();
            $this->set('user_type', $auth_user['group']);
        }
    }

如何正确管理进入页面的权限?

谢谢

【问题讨论】:

    标签: php cakephp


    【解决方案1】:

    我看到您没有使用授权处理程序,因此您必须手动拒绝对操作的访问

    $this->Auth->deny(array('index', 'add', 'edit', 'etc'));
    

    编辑

    我实际上会首先在您的 beforeFilter (AppController) 中拒绝访问所有内容

    $this->Auth->deny();
    

    然后在特定控制器的 beforeFilter() 中

    if ($user_type == 'admin') {
        $this->Auth->allow('actionThatYouWantToGrantAccess');
    }
    

    【讨论】:

    • 我已经尝试过您的解决方案但不起作用,如果我打印数组视图,我可以看到例如索引不在数组中,但用户可以在此页面访问?为什么?我错过了什么吗?
    • @AlessandroMinoccheri 您能否更新您的问题并粘贴您定义组件的 AppController 部分
    【解决方案2】:

    我会进一步研究 Auth Controller。尝试使用管理员路由(在 App/Config/core.php 中打开)和 $this->Auth->allow() 一起使用,可以在 AppController.php 中默认设置 beforeFilter() 然后在每个控制器的 beforeFilter 中设置也是。

        /**
     * @var mixed[mixed]
     */
    public $components = array(
        'Auth' => array(
            'autoRedirect' => false,
            'loginRedirect' => array(
                'admin' => true,
                'controller' => 'homes',
                'action' => 'index',
            ),
            'loginAction' => array(
                'controller' => 'users',
                'action' => 'login',
                'admin' => false,
                'plugin' => false,
            ),
            'authenticate' => array(
                'Form',
            ),
        ),
        'Session',
        'Cookie',
    );
    
    /**
     * Before Filter callback
     */
    public function beforeFilter() {    
        // Allow public views
        $this->Auth->allow('index', 'view', 'display');
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-01
      • 2018-04-22
      • 1970-01-01
      • 1970-01-01
      • 2019-06-15
      • 1970-01-01
      • 2011-11-18
      • 2014-04-18
      相关资源
      最近更新 更多