rgk
rgk

Reputation: 810

Username changes with every new registration in YII framework

I am working on an application built with YII framework. Once the admin logins, the app redirects to dashboard where in the top right corner we can find the user name as SUPERADMIN. However, when i created a new registration form and adds a user, and on refreshing the dashboard page, instead of superadmin, i am seeing the newly registered user name. how to resolve this? below is the code.

dashboard url : http://localhost/myAPP/frontend/web/dashboard
Registration form URL: http://localhost/myApp/frontend/web/site/signup

in dashboard using <?php echo ucfirst(Yii::$app->user->identity->firstname); ?> prints the username as SUPERADMIN. however the same code after new user registration showing the new user name. Please help.

Here is my signup code.

public function actionSignup()
    {
        $session = Yii::$app->session;  
        $labId = $session->get('labId');               
        if ($labId) {
            if ($session->get('role') == 'super admin') {
                $model = new SignupForm();
                $success = NULL;
                if ($model->load(Yii::$app->request->post())) {
                    if ($user = $model->signup()) {
                        if (Yii::$app->getUser()->login($user)) {
                            $success = "User registered successfully.";
                            $model = new SignupForm();
                            return $this->render('signup', [
                                'model' => $model, 'success' => $success,
                            ]);
                        }
                    }
                }

                return $this->render('signup', [
                    'model' => $model,'success' => $success,
                ]);
            } else {                
                return $this->goBack('../dashboard');
            }                       
        } else {          
            return $this->goBack('site/login');
        }          
    }

Upvotes: 0

Views: 76

Answers (1)

Nana Partykar
Nana Partykar

Reputation: 10548

Problem is in this line, if (Yii::$app->getUser()->login($user))

Why, I'm saying is. Because, Superadmin is registering user. So, there is no need of this line if (Yii::$app->getUser()->login($user)) {, because that new user no need to get login (as already Superadmin is logged in.) Remove that line and see.

if ($user = $model->signup()) {
  $success = "User registered successfully.";
  $model = new SignupForm();
  return $this->render('signup', [
      'model' => $model, 'success' => $success,
  ]);
}

Upvotes: 1

Related Questions