【发布时间】:2011-11-05 10:59:59
【问题描述】:
我整个下午都在阅读 Stack Overflow 问题,试图弄清楚这一点..
我有一个具有索引/登录/注销/注册功能的用户控制器,但也有 admin_index/admin_add/admin_edit/admin_delete 等。
我启用了 Auth 组件,并且在我的 users_controller 中,如果 Auth.User.role != 'admin',我试图拒绝访问 admin_* 页面,当我启用 $this->Auth->authorize = 'controller'; 时,它拒绝访问 site.com/admin/users/ 页面即使我的帐户的角色设置为管理员,似乎也终止了注销功能。
但是,如果我输入网址,我会被重定向回主页。
users_controller.php
<?php
class UsersController extends AppController {
var $name = 'Users';
function beforeFilter(){
parent::beforeFilter();
$this->Auth->authorize = 'controller';
$this->Auth->allow('register');
}
function isAuthorized() {
if ($this->Auth->user('role') != 'admin') {
$this->Auth->deny('admin_index','admin_view', 'admin_add', 'admin_edit','admin_delete');
}
}
app_controller.php
<?php
class AppController extends Controller {
var $components = array('Auth', 'Session');
function beforeFilter() {
$this->Auth->loginAction = array('controller'=>'users','action'=>'login', 'admin'=>false);
$this->Auth->logoutRedirect = array('controller'=>'users','action'=>'logout');
$this->Auth->loginRedirect = array('controller'=>'shows', 'action'=>'index');
$this->Auth->autoRedirect = false;
$this->Auth->allow('home');
}
我的第二个问题与 $this->Auth->deny('page'); 重定向用户的方式有关,据我所知它重定向到 / 但我需要它重定向回用户控制器。
希望这一切都有意义,我已经提供了足够的信息..
【问题讨论】:
标签: cakephp roles authentication