0

I have registration form in my Yii2 advanced application. In my application only admin can register users. But when I register new user, admin session data are destroyed and new user's session data are set. What I am trying is not to change session data when I register new user. Admin user is still should be set to session data. How to solve this problem. This is my action in controller:

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;

        }
    }
Scott
  • 4,974
  • 6
  • 35
  • 62
  • Please update your question with your code. How are you registering this users? Show us the action in your controller and any methods of your User model that is being used. – Clyff Mar 23 '16 at 16:37
  • In my action everything works fine. I mean data will be added to database and user will be signed up when you create new user. But, for example I want to add user named Johnny. Action will be executed and then logged in user in my site will Johnny not admin. But I am trying to achieve that admin should be still logged after adding Johnny – Scott Mar 23 '16 at 17:14

2 Answers2

0

Checkout the documentation for \yii\web\User::login(). When this is run in your code by calling Yii::$app->getUser()->login($user), the session data is set to match your $user.

If it's always the admin user that's signing up new users, I'm not sure you even need to run the login method.

smsalisbury
  • 345
  • 4
  • 15
0

I have solved this problem. In order to make answer clearly and simple I will show you default signup action in site controller (Yii2 advanced template):

    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,
         ]);
    }

Only I have done was adding this

Yii::$app->user->enableSession = false

inside the if statement

Scott
  • 4,974
  • 6
  • 35
  • 62
  • If you decide to set enalbeSession to `false`, there is no need of doing that inside that if (this will only makes that statement never occur). By the way take a look at http://stackoverflow.com/questions/2063480/the-3-different-equals. And looking at your code, you only needed remove any login attempt as @spencer4of6 said. – Clyff Mar 23 '16 at 21:02