2011-08-28 107 views
3

我一直在阅读堆栈溢出问题,整个下午都试图弄清楚这一点..CakePHP的验证拒绝管理路由页面

我有一个指数/登录/注销/注册功能的用户控制器,但也有admin_index/admin_add/admin_edit/admin_delete等

我启用了验证组件,并在我的users_controller我试图拒绝访问ADMIN_ *页面如果Auth.User.role != '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');将用户重定向,据我可以告诉它重定向到/但我需要它重定向回用户控制器。

希望这一切有意义,我已经提供了足够的信息..

回答

8

问题的根源可能是您的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; 
} 
?> 

users_controller.php中

<?php 
class UsersController extends AppController { 

var $name = 'Users'; 

function beforeFilter(){ 
    parent::beforeFilter(); 
    $this->Auth->allow('register'); 
} 
?> 

我将所有Auth设置移动到$ components变量中的声明中,因为它是s eems更清晰,并更有意义地在那里声明默认值。但这更多的是个人偏好问题,它不应该对代码的功能产生真正的影响。

另外,请注意,如果您将autoRedirect设置为false,则必须在Users :: login()操作中手动重定向登录用户,并获取loginRedirect$this->Auth->redirect()

我没有看到任何理由为什么你应该被发送到/当你没有登录,你试图访问一个被阻止的动作,但也许会更容易找出解决上述问题后。* *

+0

工程治疗!谢谢!我做了一个用户测试登录,并尝试去site.com/admin/users/它否认我,但仍然重定向到/而不是显示/索引...任何想法?或者我应该开一个新的问题? – medoix

+0

您是以管理员用户身份登录的用户,角色是“admin”吗?如果您希望用户在登录时将其重定向到site.com/shows/,请从AuthComponent配置中删除读取'autoRedirect'=> false的行。另外,我只注意到你的logoutRedirect很奇怪。这应该是用户在注销后重定向到的位置。也许尝试删除AppController中的autoRedirect和logoutRedirect行,然后看看事情是如何工作的。 – mtnorthrop

+0

如果普通用户试图访问'/ admin /'页面,则无法正确重定向。任何想法如何解决这个问题? – waspinator

-1

你应该这样做,就像...

 function beforeFilter() 
     { 
     if($this->Auth->user('role')=='admin'){ 
     $this->Auth->allow('admin_view','admin_controls');//put your all admin actions separated by comma 

     } 
else 
{ 
$this->Auth->allow('home');//put your all non-admin actions separated by comma 
} 


    } 

希望它会工作...如果任何问题,让我知道。 ...

+0

我刚刚试过这个解决方案,现在我不能登录页面,如果没有Auth'd和登录时无法访问任何admin_页面。 – medoix

+0

如果用户使用任何其他角色登录,他也可以看到这些页面。我发现的方式是通过使用重定向,如果用户不具有管理角色。认证 - >允许或认证 - >拒绝都失败 – Liko