0

我有一个小问题是我的控制器。我希望用户只能在某些页面内访问一个 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);
    }

在此功能中,客人可以进入内部登录、注册和激活。
登录的用户可以访问内部登录。logout 和 change_password, admin 到其他页面更多。

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

为什么这个?

这是我在 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']);
        }
    }

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

谢谢

4

2 回答 2

1

我会更多地研究 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');
    }
于 2013-11-13T01:10:23.877 回答
1

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

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

编辑

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

$this->Auth->deny();

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

if ($user_type == 'admin') {
    $this->Auth->allow('actionThatYouWantToGrantAccess');
}
于 2013-11-13T01:58:57.543 回答