Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
错误重定向CakePHP 重定向AuthComponent::$unauthorizedRedirect_Php_Cakephp_Redirect_Acl_Unauthorized - Fatal编程技术网

错误重定向CakePHP 重定向AuthComponent::$unauthorizedRedirect

错误重定向CakePHP 重定向AuthComponent::$unauthorizedRedirect,php,cakephp,redirect,acl,unauthorized,Php,Cakephp,Redirect,Acl,Unauthorized,当用户访问不允许访问的操作时,未经授权的方法会错误地重定向 正确:localhost/project/index 他重定向的位置:localhost/project/project/index 我正在使用acl AppController.php <?php App::uses('Controller', 'Controller'); class AppController extends Controller { public $components = array('Acl','Se

当用户访问不允许访问的操作时,未经授权的方法会错误地重定向

正确:localhost/project/index

他重定向的位置:localhost/project/project/index

我正在使用acl

AppController.php

<?php
App::uses('Controller', 'Controller');
class AppController extends Controller {

public $components = array('Acl','Session','DebugKit.Toolbar','RequestHandler','Auth');
public $helpers = array('Html','Form','Session');
public $uses = array('Role');
public $roleId;
public $UAP;
public $aroId;

public function beforeFilter()
{
    if ($this->Session->check('Config.language')) {
        Configure::write('Config.language', $this->Session->read('Config.language'));
    }
    $this->Auth->authorize = array(
        AuthComponent::ALL => array('actionPath' => 'controllers/','userModel' => 'Role'),
        'Actions',
        );
    $this->Auth->authenticate = array(
        'Blowfish' => array(
            'userModel' => 'User'
            )
        );
    if(!$this->_isAdmin()){
        $this->roleId = $this->getRoleId();
        $this->UAP = $this->Role->find('first',array('conditions'=>array('Role.id'=>$this->roleId)));
        $aro = $this->Acl->Aro->find('first',array(
            'conditions'=>array(
                'Aro.model'=>'Role',
                'Aro.foreign_key'=>$this->roleId)));
        $this->aroId = $aro['Aro']['id'];
        $allow = array_merge($this->_getAllowed(), array('display'));
        $this->Auth->allowedActions = $allow;
    }
    //Configure AuthComponent
    $this->Auth->loginAction = array(
        'controller' => 'users',
        'action' => 'login'
        );
    $this->Auth->logoutRedirect = array(
        'controller' => 'users',
        'action' => 'login'
        );
    $this->Auth->loginRedirect = array(
        'controller' => 'pages',
        'action' => 'display',
        'home'
        );
    $this->Auth->authError = __('Not Authorized');
    return parent::beforeFilter();
}

protected function _getAllowed($actionsIds = null, $controllerActions = null){
    if(is_null($actionsIds)){
        $actionsIds = $this->_getAllowedActionsIds();
    }
    if(is_null($controllerActions)){
        $controllerActions = $this->_getControllerActions();
    }
    $allow = array();
    foreach ($actionsIds as $value) {
        array_push($allow, $controllerActions[$value]);
    }
    return $allow;
}

protected function _getAllowedActionsIds($allowedActions = null){
    if(is_null($allowedActions)){
        $allowedActions = $this->_getAllowedActions();
    }
    return array_values($allowedActions);
}

protected function _getAllowedActions($aroId = null, $acoId = null){
    if(is_null($aroId)){
        $aroId = $this->aroId;
    }
    if(is_null($acoId)){
        $acoId = $this->_getControllerActionsIds();
    }
    $result = $this->Acl->Aco->Permission->find('list',array(
        'conditions'=>array(
            'Permission.aro_id'=>$aroId,
            'Permission.aco_id'=>$acoId,
            'Permission._create'=>1,
            'Permission._read'=>1,
            'Permission._update'=>1,
            'Permission._delete'=>1,
            ),
        'fields'=>array('id','aco_id'),
        'recursive'=>'-1'));
    return $result;
}

protected function _getControllerActionsIds($controllerActions = null){
    if(is_null($controllerActions)){
        $controllerActions = $this->_getControllerActions();
    }
    return array_keys($controllerActions);
}

protected function _getControllerActions($node = null){
    if(is_null($node)){
        $node = $this->_getNodeController();
    }
    return $this->Acl->Aco->find(
        'list',array(
            'conditions'=>array('Aco.parent_id'=>$node['0']['Aco']['id']),
            'fields'=>array('Aco.id','Aco.alias'),
            'recursive'=>'-1',
            ));
}

protected function _getNodeController(){
    return $this->Acl->Aco->node("controllers/{$this->name}");  
}

protected function _isAdmin(){
    if($this->Auth->user() && $this->Auth->user('role_id') == 1){
        $this->Auth->allow();
        return true;
    }
    return false;
}

public function getRoleId(){
    if(!is_null($this->Auth->user('role_id'))){
        return $this->Auth->user('role_id');
    }
    return 9; //Usuário não cadastrado
}
}
?>

我在Acl上也遇到了同样的问题

据我所知,当经过身份验证的用户试图访问他们无权访问的对象时,CakePHP会首先尝试将他们重定向到他们的引用者URL,或者
$loginDirect
,或者只是普通根目录

出于某种原因(我并不假装理解),这不起作用,并且输出了一个乱码版本的
$loginRedirect
。在我的例子中,CakePHP安装在
localhost/CakePHP
中,因此我收到了对
localhost/CakePHP/CakePHP
的请求。如果
$loginRedirect
指向控制器,它将指向
localhost/cakephp/cakephp/controller/method

解决方法是进入AuthComponent.php(在CakePHP库中)并从中编辑
$UnauthorizedDirect

public $unauthorizedRedirect = true;


控制未经授权访问的处理。 *-对于默认值
true
未经授权的用户被重定向到推荐人URL *或AuthComponent::$loginRedirect或“/”。 *-如果设置为字符串或数组,则该值将用作重定向到的URL。 *-如果设置为false,将引发禁止异常,而不是重定向

所以我同意Charles Barry说的话好的, 我找到了答案。伙计,你可以在AppController中添加未经授权的重定向,如下所示:

public $components = array(
        'Acl',
        'Auth' => array(
            'authorize' => array(
                'Actions' => array('actionPath' => 'controllers')
            ),
            'authError' => 'Did you really think you are allowed to see that?',
            'unauthorizedRedirect' => array(
                'controller' => 'users',
                'action' => 'index',
                'prefix' => false)
        ),
        'Session'
    );

在这里,您可以指定任何未经授权的重定向或自定义未经授权的页面。似乎只有当您的项目位于子目录中时,才会发生这种行为

文档声明“默认情况下,未经授权的用户被重定向到引用者URL或
AuthComponent::$loginDirect
'/'
”,顺序如下

如果
$loginDirect
是一个数组,则在
AuthComponent::redirectUrl
中,该数组是:

此参数
'base'=>false
去除了URL的底部,因此登录后的重定向即使在子目录中也有效

不幸的是,在AuthComponent的
函数中,使用
Controller::referer
$loginDirect
中的URL从数组转换为字符串,并且未使用特殊参数
'base'=>false

解决方案可以是确保始终剥离基础,并在AppController中使用特殊参数定义
$loginDirect
,例如

$this->Auth->loginRedirect = array(
          'controller' => 'posts',
          'action' => 'index',
          'base' => false
          );
如果您决定按照Manoj Sharma的建议设置
$unauthorizedRedirect
,则每个未经授权的请求都会重定向到此URL,而不会重定向到推荐人URL。如果用户在单击未经授权的链接后刚刚收到authError消息,则这可能是不需要的,但如果键入未经授权的URL,则应重定向用户

Router::url($redir + array('base' => false));
$this->Auth->loginRedirect = array(
          'controller' => 'posts',
          'action' => 'index',
          'base' => false
          );