re #309 remove the creation of a user on social auth and force creation of account through RegistrationController
This commit is contained in:
@ -13,22 +13,21 @@ namespace Da\User;
|
|||||||
|
|
||||||
use Da\User\Component\AuthDbManagerComponent;
|
use Da\User\Component\AuthDbManagerComponent;
|
||||||
use Da\User\Contracts\AuthManagerInterface;
|
use Da\User\Contracts\AuthManagerInterface;
|
||||||
|
use Da\User\Controller\SecurityController;
|
||||||
|
use Da\User\Event\FormEvent;
|
||||||
use Da\User\Helper\ClassMapHelper;
|
use Da\User\Helper\ClassMapHelper;
|
||||||
use Da\User\Model\User;
|
use Da\User\Model\User;
|
||||||
use Yii;
|
use Yii;
|
||||||
use yii\authclient\Collection;
|
use yii\authclient\Collection;
|
||||||
use yii\base\Application;
|
use yii\base\Application;
|
||||||
use yii\base\BootstrapInterface;
|
use yii\base\BootstrapInterface;
|
||||||
|
use yii\base\Event as YiiEvent;
|
||||||
use yii\base\Exception;
|
use yii\base\Exception;
|
||||||
use yii\base\InvalidConfigException;
|
use yii\base\InvalidConfigException;
|
||||||
use yii\console\Application as ConsoleApplication;
|
use yii\console\Application as ConsoleApplication;
|
||||||
use yii\i18n\PhpMessageSource;
|
use yii\i18n\PhpMessageSource;
|
||||||
use yii\web\Application as WebApplication;
|
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,
|
* 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.
|
* 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) {
|
foreach ($map as $class => $definition) {
|
||||||
$di->set($class, $definition);
|
$di->set($class, $definition);
|
||||||
$model = is_array($definition) ? $definition['class'] : $definition;
|
$model = is_array($definition) ? $definition['class'] : $definition;
|
||||||
$name = (substr($class, strrpos($class, '\\') + 1));
|
$name = substr($class, strrpos($class, '\\') + 1);
|
||||||
$modelClassMap[$class] = $model;
|
$modelClassMap[$class] = $model;
|
||||||
if (in_array($name, ['User', 'Profile', 'Token', 'SocialNetworkAccount'])) {
|
if (in_array($name, ['User', 'Profile', 'Token', 'SocialNetworkAccount'])) {
|
||||||
$di->set(
|
$di->set(
|
||||||
@ -152,7 +151,7 @@ class Bootstrap implements BootstrapInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Attach an event to check if the password has expired
|
// 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) {
|
YiiEvent::on(SecurityController::class, FormEvent::EVENT_AFTER_LOGIN, function (FormEvent $event) {
|
||||||
$user = $event->form->user;
|
$user = $event->form->user;
|
||||||
if ($user->password_age >= Yii::$app->getModule('user')->maxPasswordAge) {
|
if ($user->password_age >= Yii::$app->getModule('user')->maxPasswordAge) {
|
||||||
|
|||||||
@ -104,40 +104,13 @@ class SocialNetworkAuthenticateService implements ServiceInterface
|
|||||||
if (($user = $this->getUser($account)) instanceof User) {
|
if (($user = $this->getUser($account)) instanceof User) {
|
||||||
$account->user_id = $user->id;
|
$account->user_id = $user->id;
|
||||||
$account->save(false);
|
$account->save(false);
|
||||||
|
|
||||||
return $account;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return $account;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getUser(SocialNetworkAccount $account)
|
protected function getUser(SocialNetworkAccount $account)
|
||||||
{
|
{
|
||||||
$user = $this->userQuery->whereEmail($account->email)->one();
|
return $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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -41,7 +41,7 @@ $this->params['breadcrumbs'][] = $this->title;
|
|||||||
|
|
||||||
<?= $form->field($model, 'username') ?>
|
<?= $form->field($model, 'username') ?>
|
||||||
|
|
||||||
<?php if ($module->generatePasswords == false): ?>
|
<?php if ($module->generatePasswords === false): ?>
|
||||||
<?= $form->field($model, 'password')->passwordInput() ?>
|
<?= $form->field($model, 'password')->passwordInput() ?>
|
||||||
<?php endif ?>
|
<?php endif ?>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user