gpt4 book ai didi

php - 错误的重定向 CakePHP

转载 作者:行者123 更新时间:2023-12-01 16:26:47 26 4
gpt4 key购买 nike

重定向 AuthComponent::$unauthorizedRedirect

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

正确:本地主机/项目/索引

他重定向到的位置: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
}
}
?>

最佳答案

好的,我找到了这个问题的答案。伙计,你可以像这样在 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'
);

您可以在此处指定任何未经授权的重定向或自定义未经授权的页面

关于php - 错误的重定向 CakePHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23961920/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com