From dd407f50b482b472f290fdff741b85e8b52bacce Mon Sep 17 00:00:00 2001 From: tonydspaniard Date: Mon, 17 Jun 2019 17:35:22 +0200 Subject: [PATCH] re #309 remove the creation of a user on social auth and force creation of account through RegistrationController --- src/User/Bootstrap.php | 11 +++---- .../SocialNetworkAuthenticateService.php | 31 ++----------------- .../resources/views/registration/register.php | 2 +- 3 files changed, 8 insertions(+), 36 deletions(-) diff --git a/src/User/Bootstrap.php b/src/User/Bootstrap.php index 90ca741..5a22a9c 100644 --- a/src/User/Bootstrap.php +++ b/src/User/Bootstrap.php @@ -13,22 +13,21 @@ namespace Da\User; use Da\User\Component\AuthDbManagerComponent; use Da\User\Contracts\AuthManagerInterface; +use Da\User\Controller\SecurityController; +use Da\User\Event\FormEvent; use Da\User\Helper\ClassMapHelper; use Da\User\Model\User; use Yii; use yii\authclient\Collection; use yii\base\Application; use yii\base\BootstrapInterface; +use yii\base\Event as YiiEvent; use yii\base\Exception; use yii\base\InvalidConfigException; use yii\console\Application as ConsoleApplication; use yii\i18n\PhpMessageSource; use yii\web\Application as WebApplication; -use yii\base\Event as YiiEvent; -use Da\User\Event\FormEvent; -use Da\User\Controller\SecurityController; - /** * Bootstrap class of the yii2-usuario extension. Configures container services, initializes translations, * builds class map, and does the other setup actions participating in the application bootstrap process. @@ -127,7 +126,7 @@ class Bootstrap implements BootstrapInterface foreach ($map as $class => $definition) { $di->set($class, $definition); $model = is_array($definition) ? $definition['class'] : $definition; - $name = (substr($class, strrpos($class, '\\') + 1)); + $name = substr($class, strrpos($class, '\\') + 1); $modelClassMap[$class] = $model; if (in_array($name, ['User', 'Profile', 'Token', 'SocialNetworkAccount'])) { $di->set( @@ -152,7 +151,7 @@ class Bootstrap implements BootstrapInterface } // Attach an event to check if the password has expired - if (!is_null(Yii::$app->getModule('user')->maxPasswordAge)) { + if (null !== Yii::$app->getModule('user')->maxPasswordAge) { YiiEvent::on(SecurityController::class, FormEvent::EVENT_AFTER_LOGIN, function (FormEvent $event) { $user = $event->form->user; if ($user->password_age >= Yii::$app->getModule('user')->maxPasswordAge) { diff --git a/src/User/Service/SocialNetworkAuthenticateService.php b/src/User/Service/SocialNetworkAuthenticateService.php index 1e621f9..e1ec09d 100644 --- a/src/User/Service/SocialNetworkAuthenticateService.php +++ b/src/User/Service/SocialNetworkAuthenticateService.php @@ -104,40 +104,13 @@ class SocialNetworkAuthenticateService implements ServiceInterface if (($user = $this->getUser($account)) instanceof User) { $account->user_id = $user->id; $account->save(false); - - return $account; } - return false; + return $account; } protected function getUser(SocialNetworkAccount $account) { - $user = $this->userQuery->whereEmail($account->email)->one(); - if (null !== $user) { - return $user; - } - /** @var User $user */ - $user = $this->controller->make( - User::class, - [], - [ - 'scenario' => 'connect', - 'username' => $account->username, - 'email' => $account->email, - ] - ); - - if (!$user->validate(['email'])) { - $user->email = null; - } - - if (!$user->validate(['username'])) { - $user->username = null; - } - - $mailService = MailFactory::makeWelcomeMailerService($user); - - return $this->controller->make(UserCreateService::class, [$user, $mailService])->run() ? $user : false; + return $this->userQuery->whereEmail($account->email)->one(); } } diff --git a/src/User/resources/views/registration/register.php b/src/User/resources/views/registration/register.php index 9e5dab1..29d1e75 100644 --- a/src/User/resources/views/registration/register.php +++ b/src/User/resources/views/registration/register.php @@ -41,7 +41,7 @@ $this->params['breadcrumbs'][] = $this->title; field($model, 'username') ?> - generatePasswords == false): ?> + generatePasswords === false): ?> field($model, 'password')->passwordInput() ?>