/** * Handle successful authentication * * @param \yii\authclient\BaseClient $authClient * @return Response */ public function onAuthSuccess(\yii\authclient\BaseClient $authClient) { $attributes = $authClient->getUserAttributes(); // User already logged in - Add new authclient to existing user if (!Yii::$app->user->isGuest) { AuthClientHelpers::storeAuthClientForUser($authClient, Yii::$app->user->getIdentity()); return $this->redirect(['/user/account/connected-accounts']); } // Login existing user $user = AuthClientHelpers::getUserByAuthClient($authClient); if ($user !== null) { return $this->login($user, $authClient); } if (!$authClient instanceof ApprovalBypass && !Yii::$app->getModule('user')->settings->get('auth.anonymousRegistration')) { Yii::$app->session->setFlash('error', Yii::t('UserModule.base', "You're not registered.")); return $this->redirect(['/user/auth/login']); } // Check if E-Mail is given if (!isset($attributes['email'])) { Yii::$app->session->setFlash('error', "Missing E-Mail Attribute from AuthClient."); return $this->redirect(['/user/auth/login']); } if (!isset($attributes['id'])) { Yii::$app->session->setFlash('error', "Missing ID AuthClient Attribute from AuthClient."); return $this->redirect(['/user/auth/login']); } // Check if e-mail is already taken if (User::findOne(['email' => $attributes['email']]) !== null) { Yii::$app->session->setFlash('error', Yii::t('UserModule.base', 'User with the same email already exists but isn\'t linked to you. Login using your email first to link it.')); return $this->redirect(['/user/auth/login']); } // Try automatically create user & login user $user = AuthClientHelpers::createUser($authClient); if ($user !== null) { return $this->login($user, $authClient); } // Make sure we normalized user attributes before put it in session (anonymous functions) $authClient->setNormalizeUserAttributeMap([]); // Store authclient in session - for registration controller Yii::$app->session->set('authClient', $authClient); // Start registration process return $this->redirect(['/user/registration']); }
/** * Registers users * * @return boolean state */ public function register(\yii\authclient\ClientInterface $authClient = null) { $this->models['User']->language = Yii::$app->language; if ($this->enableUserApproval) { $this->models['User']->status = User::STATUS_NEED_APPROVAL; $this->models['User']->registrationGroupId = $this->models['GroupUser']->group_id; } if ($this->models['User']->save()) { // Save User Profile $this->models['Profile']->user_id = $this->models['User']->id; $this->models['Profile']->save(); if ($this->models['GroupUser']->validate()) { $this->models['GroupUser']->user_id = $this->models['User']->id; $this->models['GroupUser']->save(); } if ($this->enablePasswordForm) { // Save User Password $this->models['Password']->user_id = $this->models['User']->id; $this->models['Password']->setPassword($this->models['Password']->newPassword); $this->models['Password']->save(); } if ($authClient !== null) { \humhub\modules\user\authclient\AuthClientHelpers::storeAuthClientForUser($authClient, $this->models['User']); $authClient->trigger(\humhub\modules\user\authclient\BaseClient::EVENT_CREATE_USER, new \yii\web\UserEvent(['identity' => $this->models['User']])); } return true; } return false; }