med*_*oix 3 authentication cakephp roles
我整个下午一直在阅读Stack Overflow问题,试图解决这个问题.
我有一个带索引/登录/注销/注册功能的用户控制器,但也有admin_index/admin_add/admin_edit/admin_delete等.
我启用了Auth组件,在我的users_controller中,我试图拒绝访问admin_*页面Auth.User.role != 'admin',如果我启用$this->Auth->authorize = 'controller';它拒绝访问site.com/admin/users/页面,并且似乎甚至杀死了注销功能我的帐户将角色设置为admin.
但是,如果我在i中键入url,则会重定向回主页面.
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');
}
}
Run Code Online (Sandbox Code Playgroud)
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');
}
Run Code Online (Sandbox Code Playgroud)
我的第二个问题涉及$this->Auth->deny('page');重定向用户的方式,据我所知它重定向到/但我需要它重定向回用户控制器.
希望这一切都有意义,我提供了足够的信息..
问题的根源可能是你的isAuthorized()方法.这应该只返回true或false,并指示经过身份验证的用户是否已授权访问特定操作.
很难说为什么要重定向到主页而不是登录页面.但是你可能在某个地方有其他代码搞砸了.
尝试修改您的代码,如下所示,看看这是否有助于让事情顺利进行:
app_controller.php
<?php
class AppController extends Controller {
var $components = array('Session', 'Auth' => array(
'loginAction' => array('controller'=>'users','action'=>'login', 'admin'=>false),
'logoutRedirect' => array('controller'=>'users','action'=>'logout'),
'loginRedirect' => array('controller'=>'shows', 'action'=>'index'),
'autoRedirect' => false,
'authorize' => 'controller'
);
function beforeFilter() {
$this->Auth->allow('home');
}
function isAuthorized() {
if (!empty($this->params['prefix']) && $this->params['prefix'] == 'admin') {
if ($this->Auth->user('role') != 'admin') {
return false;
}
}
return true;
}
?>
Run Code Online (Sandbox Code Playgroud)
users_controller.php中
<?php
class UsersController extends AppController {
var $name = 'Users';
function beforeFilter(){
parent::beforeFilter();
$this->Auth->allow('register');
}
?>
Run Code Online (Sandbox Code Playgroud)
我将所有Auth设置移动到$ components变量中的声明,因为它看起来更干净,并且在那里声明默认值更有意义.但这更多的是个人偏好问题,它不应该对代码的功能产生实际影响.
另请注意,如果设置autoRedirect为false,则必须在Users :: login()操作中手动重定向登录用户,并获取loginRedirect值$this->Auth->redirect().
我没有看到任何理由为什么你应该被发送到/当你没有登录并且你试图访问被阻止的动作,但是在你修复上述内容之后可能会更容易理解.**
| 归档时间: |
|
| 查看次数: |
9269 次 |
| 最近记录: |