public function authenticate(ClientInterface $client)
 {
     // find existing user by service
     if ($this->userService !== null) {
         /** @var User $user */
         $user = Yii::createObject(ModelMapHelper::User());
         $user = $user->loadModel($this->userService->user_id);
         $user->login(UsersModule::module()->loginDuration);
     } else {
         // no user for this pair
         // this is the most hard part
         // create user
         /** @var SocialServiceInterface|BaseClient $client */
         $client->retrieveAdditionalData();
         /** @var RegistrationForm $registrationForm */
         $registrationForm = Yii::createObject(ModelMapHelper::RegistrationForm());
         $this->mapServiceAttributes($client, $registrationForm);
         $user = $registrationForm->socialRegister($client);
         if ($user === false) {
             throw new ErrorException("Unable to register user");
         }
         $userService = $this->createService();
         if ($user->save() === false) {
             throw new ErrorException("Unable to save user:" . var_export($user->errors, true));
         }
         $user->link('services', $userService);
         // check if we need to run post-registration
         $user->login(UsersModule::module()->loginDuration);
         // check if there's some required or recommended fields missing
         foreach (UsersModule::module()->requiredUserAttributes as $attribute) {
             if (empty($user->{$attribute})) {
                 Yii::$app->session->setFlash('info', Yii::t('users', 'Please fill required profile fields.'));
                 $this->redirectToProfileUpdate();
                 return;
             }
         }
         foreach (UsersModule::module()->recommendedUserAttributes as $attribute) {
             if (empty($user->{$attribute})) {
                 //! @todo Add limitation on UsersModule::recommendedFieldsMaxPrompts
                 Yii::$app->session->setFlash('info', Yii::t('users', 'Please fill recommended profile fields.'));
                 $this->redirectToProfileUpdate();
                 return;
             }
         }
     }
 }
 public function beforeRun()
 {
     $this->model = Yii::createObject(ModelMapHelper::RegistrationForm());
     return parent::beforeRun();
 }