duanqian8867 2013-11-12 22:31
浏览 31
已采纳

cakephp在beforeFilter中正确管理权限

I have a little problem isnisde my controller. I want that a user can access only inside some pages an andmin user inside more pages.

I have a controller called UsersController this is its beforeFilter method

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

in this function guest can enter inside login, register and activate.
user logged can access inside login. logout and change_password, admin to the other pages more.

But this not works. For example a user logged can access inside the index view or add view.

Why this?

This is my beforeFilter inside appController:

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

How can I manage correctly permission to enter in pages?

Thanks

  • 写回答

2条回答 默认 最新

  • douji2520 2013-11-13 01:58
    关注

    I see you are not using an Authorization Handler, thus you will have to manually deny the access to actions

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

    EDIT

    I would actually start by denying access to everything, in your beforeFilter (AppController)

    $this->Auth->deny();
    

    and then in the beforeFilter() of you specific controller

    if ($user_type == 'admin') {
        $this->Auth->allow('actionThatYouWantToGrantAccess');
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助