duanlan7903 2014-05-30 19:16
浏览 72
已采纳

错误的重定向CakePHP

Redirect AuthComponent::$unauthorizedRedirect

when a user accesses an action where it is not permitted to access the _unauthorized method redirects incorrectly

correct: localhost / project / index

where he's redirecting: localhost / project / project / index

I am using 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
}
}
?>
  • 写回答

4条回答 默认 最新

  • dongshuxi3105 2014-12-04 11:36
    关注

    Ok, I found the answer for this. dude you can just add unauthorize redirect in AppController like this:

    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'
        );
    

    Here you can specify any unauthorized redirection or custom unauthorized page

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么