Php Yii授权和重定向
我是Yii新手,我需要修正一些事情:有一些控制器有一些动作,如果用户试图加载一些动作,我需要检查他是否被授权;如果是,则执行一些操作,如果不是,则重定向到登录页面。我怎么做?我希望我不需要在每次操作中通过Yii::user进行检查并手动重定向?提前谢谢 只要加上这个Php Yii授权和重定向,php,yii,Php,Yii,我是Yii新手,我需要修正一些事情:有一些控制器有一些动作,如果用户试图加载一些动作,我需要检查他是否被授权;如果是,则执行一些操作,如果不是,则重定向到登录页面。我怎么做?我希望我不需要在每次操作中通过Yii::user进行检查并手动重定向?提前谢谢 只要加上这个 if(Yii::app()->user->getId()===null) { // This is the case where user is not logged in so redirect
if(Yii::app()->user->getId()===null) {
// This is the case where user is not logged in so redirect
$this->redirect(array('site/login'));
} else {
// this is the case where user is logged in
// do your business
}
有一个非常好的SO帖子已经存在,你可以参考你的案例
希望这有助于使用操作过滤器,在创建yii crud时有基本的访问控制过滤器。以下是关于过滤器的文档:
访问控制就绪解决方案的主题如下: 简而言之,在控制器中创建方法:
public function filters()
{
return array(
'accessControl',
);
}
class DefaultController extends Controller
{
public function filters()
{
}
public function actionIndex()
{
$this->render('index');
}
}
然后使用方法定义操作的访问规则:
public function accessRules()
{
return array(
array('deny',
'actions'=>array('create', 'edit'),
'users'=>array('?'),
),
array('allow',
'actions'=>array('delete'),
'roles'=>array('admin'),
),
array('deny',
'actions'=>array('delete'),
'users'=>array('*'),
),
);
}
编辑
若要定义用户未经身份验证时重定向的url,请在用户组件中定义它:
'user' => [
'class' => 'CWebUser', // or custom
'allowAutoLogin' => true, // for autologin
'loginUrl' => ['/ua/user/login'], // if not authenticated go here
]
我已经创建了AdminModule.php。在本文中,我创建了一个函数来检查用户是否登录。还有一些公共页面,不需要登录
public function beforeControllerAction($controller, $action)
{
if(parent::beforeControllerAction($controller, $action))
{
// this overwrites everything in the controller
$controller->layout = 'admin';
// this method is called before any module controller action is performed
// you may place customized code here
$route=$controller->id.'/'.$action->id;
$publicPages=array(
'user/login',
'default/error',
);
if($this->password!==false && Yii::app()->user->isGuest && !in_array($route,$publicPages))
Yii::app()->user->loginRequired();
else
return true;
}
else
return false;
}
作为Mohit Bhansali示例的简化版本,我在AdminModule中实现了以下代码:
public function beforeControllerAction($controller, $action)
{
if(parent::beforeControllerAction($controller, $action)) {
if (Yii::app()->user->isGuest) {
Yii::app()->user->loginRequired();
} elseif (Yii::app()->user->id != 'myadminuser') {
throw new CHttpException(403, 'You are not authorized to perform this action.');
}
return true;
} else {
return false;
}
}
然后,我还能够从每个管理模块控制器中删除accessControl数组return in filters()(但必须保留空的filter()函数),并完全删除accessRules()。例如,以下是DefaultController:
public function filters()
{
return array(
'accessControl',
);
}
class DefaultController extends Controller
{
public function filters()
{
}
public function actionIndex()
{
$this->render('index');
}
}
还值得一提的是,除了简单的访问控制外,建议使用RBAC。如果我们在这里使用RBAC,我们将调用Yii::app()->user->checkAccess(),而不是在AdminModule中检查Yii::app()->user->id。您不应该这样做。推荐的方法是使用
if(Yii::app()->user->isGuest)Yii::app()->user->loginRequired()代码>。它将重定向到登录页面并为您存储返回URL。但他只是想重定向。请告诉我,我应该在哪里设置授权操作?我的意思是配置我很抱歉@user2218845我没有得到你想要的,我可以说你需要在你的操作中添加上面的代码,如果你需要重定向用户,如果他没有登录,还需要阅读我发布的访问控制配置和其他设置的链接谢谢,但访问控制限制区域,我需要另一件事,查看用户组件配置中的上一条注释,这里有'loginUrl'=>['/ua/user/login']
它将未经授权的用户重定向到您应该阅读的URL中。除此之外,你的问题还不是很清楚,因为你说如果用户没有被授权,你想把他发送到登录页面。你是说认证?这是两件不同的事情。如果用户已通过身份验证(=登录),但未经授权,他应在登录页面上执行什么操作?他已经登录了。哦,对不起,我的意思是,若用户并没有登录,他应该进入登录页面。您的评论很好,而且很有效,但是请告诉我,我如何为loginRequired()方法设置控制器/操作?您可以使用accessRules
来完成,就像PeterM解释的那样。不过,您不需要添加这么多规则。添加一个带有'users'=>'@'
的allow
和一个不带任何其他参数的deny
。阅读所有细节。不,你不理解我。现在我调用Yii::app()->user->loginRequired()并将我移动到“site/login”页面。但我需要使用另一个控制器/动作链接。我没有测试/验证这段代码(包括公共页面),但我可以验证这种方法的基础工作得很好。