为什么Yii2 user-> save()会重定向?

时间:2016-11-08 07:43:33

标签: php yii2

我有一个用户模型,扩展AciveRecord实现IdentityInterface。我应该用OpenId检查用户,如果存在,我将在我的数据库中复制用户条目。一切正常,但我有问题。

创建用户后,我想为他分配权限,但是当我在用户/登录时调用user-> save()页面重定向时。

行为:

public function behaviors()
{
    return [
        'access' => [
            'class' => AccessControl::className(),
            'only' => ['rbac','users','logout', 'index', 'whoami', 'user-change-data-handler','login', 'openid', 'showoid', 'openid-signup', ''],
            'rules' => [
                [
                    'actions' => ['login', 'openid', 'showoid', 'openid-signup'],
                    'allow' => true,
                    'roles' => ['?'],
                ],
                [
                    'actions' => ['logout', 'index', 'showoid', 'whoami', 'user-change-data-handler', 'showoid'],
                    'allow' => true,
                    'roles' => ['@'],
                ],
                [
                    'actions' => ['users',],
                    'allow' => true,
                    'roles' => ['UsersAdministrator'],
                ],
                [
                    'actions' => ['rbac',],
                    'allow' => true,
                    'roles' => ['RBACAdministrator'],
                ],
            ],
        ],
        // 'verbs' => [
        //     'class' => VerbFilter::className(),
        //     'actions' => [
        //         'logout' => ['post'],
        //     ],
        // ],
    ];
}

输入登录用户后,重定向到

public function actionOpenid()
{
  if (empty($_GET['username']))
      return $this->render('openid');
  $client = new OpenId();
  $client->authUrl = 'http://*******/openidserver/'.$_GET['username']; // Setup provider endpoint
  $client->setReturnUrl('http://127.0.0.1/backend/web/site/showoid');
  $client->setUserAttributes('user/email');
  $url = $client->buildAuthUrl(); // Get authentication URL
  return Yii::$app->getResponse()->redirect($url); // Redirect to authentication URL
}

OpenID重定向到

public function actionShowoid()
{
    $client = new OpenId();
    if (!$client->validate()){
        $this->redirect(Url::to('site/login'));
    }
    else  { // validate response
        $strings=explode('&', $_SERVER["REDIRECT_QUERY_STRING"]);
        $openid = [];
        foreach ($strings as $k => $v) {
            if (!preg_match('/.+=.+/', $v))
                continue;
            $vals=[explode('.',explode('=',$v)[0])[1], explode('=',$v)[1]];
            $openid[$vals[0]] = $vals[1];
        }
        $openid['username'] = explode('%2F',$openid["identity"]);
        $openid['username'] = $openid['username'][count($openid['username'])-1];
        $user = \common\models\User::findOne(['username' => $openid['username']]);
        if ($user ==null){
            $user = new \common\models\User;
            $user->username = $openid['username'];
            $user->password_hash = 'Debugging';
            $user->save();
//------------all below ignores
            if (1){
                    $id = $user->id;
                    $assignment = 'AccessAdministrator';
                    $url = Url::to(['api/add-assignments', 'user_id' => $id, 'item_name' =>$assignment, 'apiKey'=>$this->apiKey]);
                    file_get_contents($url);
                }


            $user->save();
            \Yii::$app->user->login($user);
            $this->redirect(['site/index']);

        }
        else{
            \Yii::$app->user->login($user);
            $this->redirect(['site/index']);
        }
    //var_dump(\common\models\User::findOne(['username' => $openid['username']]));
    }
}

1 个答案:

答案 0 :(得分:0)

尝试删除if(1)并最终以这种方式执行

islower((unsigned char)'ÿ')

并记住,当代码结束时......你应该渲染或重定向到某个页面,否则可以重定向到默认目标页面,例如:site / login