Yii2注册而不保存会话数据

时间:2016-03-23 16:22:15

标签: authentication yii2 authorization yii2-advanced-app

我在Yii2高级应用程序中有注册表。在我的应用程序中,只有admin可以注册用户但是当我注册新用户时,会破坏管理员会话数据并设置新用户的会话数据。我正在尝试的是在注册新用户时不更改会话数据。管理员用户仍应设置为会话数据。如何解决这个问题呢。这是我在控制器中的行为:

public function actionSignup()
{
        $model = new SignupForm();

        if(Yii::$app->user->can('admin'))
        {
            if (($response = self::ajaxValidate($model)) !== false)
                return $response;

            if (self::postValidate($model))
            {                
                try
                {
                    $trans = Yii::$app->db->beginTransaction();

                    if ($user = $model->signup()) {
                        Yii::$app->getUser()->login($user);
                    }                

                    //tagidan tushgan odamining child_left, child_right tini o'zgartirish
                    $under = UserModel::findOne($user->id_parent_under);

                    if ($under->id_child_left == 0)
                        $under->id_child_left = $user->id;
                    else
                        $under->id_child_right = $user->id;

                    $under->update(false);

                    ChildrenBinaryModel::insertNewUser($user);
                    ChildrenClassicModel::insertNewUser($user);

                    $parents = ChildrenBinaryModel::find()->with('user')->where(["id_child" => $user->id])->orderBy(['depth' => SORT_ASC])->all();

                    $c_user = $user;
                    foreach($parents as $p)
                    {
                        $p_user = $p->user;

                        if ($p_user->id_child_left == $c_user->id)
                            $p_user->ball_left += 100;
                        else
                            $p_user->ball_right += 100;

                        $p_user->update(false);

                        $c_user = $p_user;
                    }

                    $trans->commit();
                }
                catch(\Exception $e)
                {
                    $trans->rollBack();
                    return $this->renderContent($e->getMessage());
                }

                return $this->render('index');
            }

            return $this->render('signup', [
                'model' => $model,
            ]);
        }else{
            throw new ForbiddenHttpException;

        }
    }

2 个答案:

答案 0 :(得分:0)

结帐\yii\web\User::login() Yii::$app->getUser()->login($user)。通过调用$user在您的代码中运行此功能后,会话数据将设置为与您的// create constraint Constraint usersOnly = new Constraint(Constraint.__FORM_AUTH, "user"); usersOnly.setAuthenticate(true); ConstraintMapping requireAuthentication = new ConstraintMapping(); requireAuthentication.setConstraint(usersOnly); requireAuthentication.setPathSpec("/*"); // create login service LoginService loginService = new HashLoginService("realm"); loginService.setConfig("users.txt"); // create form authentication FormAuthenticator formAuthenticator = new FormAuthenticator("/login", "/login", true); // create /login route ServletHolder loginServlet = new ServletHolder(new DefaultServlet() { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().append("<html>\n<head>\n<title>Login</title>\n</head>\n<body>\n" + "<form method='POST' action='/j_security_check'>\n" + "<input type='text' name='j_username'/>\n" + "<input type='password' name='j_password'/>\n" + "<input type='submit' value='Login'/>\n</form>\n</body>\n</html>\n"); } }); ConstraintSecurityHandler securityHandler = new ConstraintSecurityHandler(); securityHandler.addMapping(requireAuthentication); securityHandler.setLoginService(loginService); securityHandler.setAuthenticator(formAuthenticator); // assign security to each webapp for (WebAppContext webapp : webapps) { webapp.setSecurityHandler(securityHandler); webapp.addServlet(loginServlet, "/login"); } 匹配。

如果管理员用户始终注册新用户,我甚至不确定您是否需要运行登录方式。

答案 1 :(得分:0)

我已经解决了这个问题。为了使答案清晰简单,我将在站点控制器(Yii2高级模板)中显示默认注册操作:

    public function actionSignup()
    {
         $model = new SignupForm();
         if ($model->load(Yii::$app->request->post())) {
             if ($user = $model->signup()) {
                 if (Yii::$app->user->enableSession = false && 
                     Yii::$app->getUser()->login($user)) {
                     return $this->goHome();
                 }
             }
         }

         return $this->render('signup', [
             'model' => $model,
         ]);
    }

我只是添加了这个

Yii::$app->user->enableSession = false
if语句中的