我是 Yii 的新手,我需要解决一些问题:有一些控制器有一些动作,如果用户尝试加载一些动作,我需要检查他是否被授权;如果是,则执行一些操作,如果不是 - 重定向到登录页面。我该怎么做?我希望我不需要检查 Yii::user 并在每个操作中手动重定向?提前致谢。
4 回答
使用动作过滤器,创建 yii crud 时有基本的访问控制过滤器。以下是有关过滤器的文档:
http ://www.yiiframework.com/doc/guide/1.1/en/basics.controller#filter
访问控制的现成解决方案的主题在这里: http ://www.yiiframework.com/doc/guide/1.1/pl/topics.auth#sec-3
简而言之,在控制器中创建方法:
    public function filters()
    {
        return array(
            'accessControl',
        );
    }
然后使用方法定义操作的访问规则:
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;
    }
}然后,我还能够从每个管理模块控制器中删除 filters() 中返回的 accessControl 数组(但必须保留空的 filter() 函数),并完全删除 accessRules()。例如,这里是 DefaultController:
class DefaultController extends Controller
{
    public function filters()
    {
    }
    public function actionIndex()
    {
        $this->render('index');
    }
}值得一提的是,除了简单的访问控制之外,建议使用 RBAC,如果我们在这里使用 RBAC,我们会调用 Yii::app()->user->checkAccess() 而不是检查 Yii:: AdminModule 中的 app()->user->id。
只需添加这个
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帖子已经存在,您可以参考您的案例
希望这可以帮助