diff --git a/CHANGELOG.md b/CHANGELOG.md index 226c9bc1..d3dd6073 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## dev +- Fix: Social Network Auth (eluhr) + ## 1.6.2 Jan 4th, 2024 - Fix: Two Factor Authentication - Filter - Blocks even when two factor authentication is enabled diff --git a/docs/install/configuration-options.md b/docs/install/configuration-options.md index f448a70d..ba775e1c 100755 --- a/docs/install/configuration-options.md +++ b/docs/install/configuration-options.md @@ -143,6 +143,15 @@ List of urls that does not require explicit data processing consent to be access Setting this attribute allows the registration process. If you set it to `false`, the module won't allow users to register by throwing a `NotFoundHttpException` if the `RegistrationController::actionRegister()` is accessed. +#### enableSocialNetworkRegistration (type: `boolean`, default: `true`) + +Setting this attribute allows the registration process via social networks. If you set it to `false`, the module won't allow users to +register. + +#### sendWelcomeMailAfterSocialNetworkRegistration (type: `boolean`, default: `true`) + +Setting this attribute controls wether a confirmation mail should be send or not. + #### enableEmailConfirmation (type: `boolean`, default: `true`) If `true`, the module will send an email with a confirmation link that user needs to click through to complete its diff --git a/src/User/AuthClient/Facebook.php b/src/User/AuthClient/Facebook.php index b910539f..6a2cf7e4 100644 --- a/src/User/AuthClient/Facebook.php +++ b/src/User/AuthClient/Facebook.php @@ -12,10 +12,14 @@ namespace Da\User\AuthClient; use Da\User\Contracts\AuthClientInterface; +use Da\User\Traits\AuthClientUserIdTrait; use yii\authclient\clients\Facebook as BaseFacebook; class Facebook extends BaseFacebook implements AuthClientInterface { + + use AuthClientUserIdTrait; + /** * {@inheritdoc} */ diff --git a/src/User/AuthClient/GitHub.php b/src/User/AuthClient/GitHub.php index 1a298df5..19ccae62 100644 --- a/src/User/AuthClient/GitHub.php +++ b/src/User/AuthClient/GitHub.php @@ -12,10 +12,12 @@ namespace Da\User\AuthClient; use Da\User\Contracts\AuthClientInterface; +use Da\User\Traits\AuthClientUserIdTrait; use yii\authclient\clients\GitHub as BaseGitHub; class GitHub extends BaseGitHub implements AuthClientInterface { + use AuthClientUserIdTrait; /** * {@inheritdoc} */ diff --git a/src/User/AuthClient/Google.php b/src/User/AuthClient/Google.php index 42c24956..13fb6fec 100644 --- a/src/User/AuthClient/Google.php +++ b/src/User/AuthClient/Google.php @@ -12,10 +12,12 @@ namespace Da\User\AuthClient; use Da\User\Contracts\AuthClientInterface; +use Da\User\Traits\AuthClientUserIdTrait; use yii\authclient\clients\Google as BaseGoogle; class Google extends BaseGoogle implements AuthClientInterface { + use AuthClientUserIdTrait; /** * {@inheritdoc} */ diff --git a/src/User/AuthClient/LinkedIn.php b/src/User/AuthClient/LinkedIn.php index b0fc422e..fe5db731 100644 --- a/src/User/AuthClient/LinkedIn.php +++ b/src/User/AuthClient/LinkedIn.php @@ -12,10 +12,13 @@ namespace Da\User\AuthClient; use Da\User\Contracts\AuthClientInterface; +use Da\User\Traits\AuthClientUserIdTrait; use yii\authclient\clients\LinkedIn as BaseLinkedIn; class LinkedIn extends BaseLinkedIn implements AuthClientInterface { + use AuthClientUserIdTrait; + /** * {@inheritdoc} */ diff --git a/src/User/AuthClient/Twitter.php b/src/User/AuthClient/Twitter.php index 5a9f0a8e..166bb9ca 100644 --- a/src/User/AuthClient/Twitter.php +++ b/src/User/AuthClient/Twitter.php @@ -12,10 +12,13 @@ namespace Da\User\AuthClient; use Da\User\Contracts\AuthClientInterface; +use Da\User\Traits\AuthClientUserIdTrait; use yii\authclient\clients\Twitter as BaseTwitter; class Twitter extends BaseTwitter implements AuthClientInterface { + use AuthClientUserIdTrait; + /** * @return string */ diff --git a/src/User/AuthClient/VKontakte.php b/src/User/AuthClient/VKontakte.php index 69faa059..ac516c4d 100644 --- a/src/User/AuthClient/VKontakte.php +++ b/src/User/AuthClient/VKontakte.php @@ -12,11 +12,14 @@ namespace Da\User\AuthClient; use Da\User\Contracts\AuthClientInterface; +use Da\User\Traits\AuthClientUserIdTrait; use Yii; use yii\authclient\clients\VKontakte as BaseVKontakte; class VKontakte extends BaseVKontakte implements AuthClientInterface { + use AuthClientUserIdTrait; + /** * {@inheritdoc} */ diff --git a/src/User/AuthClient/Yandex.php b/src/User/AuthClient/Yandex.php index 35f3dfe2..262ec328 100644 --- a/src/User/AuthClient/Yandex.php +++ b/src/User/AuthClient/Yandex.php @@ -12,11 +12,14 @@ namespace Da\User\AuthClient; use Da\User\Contracts\AuthClientInterface; +use Da\User\Traits\AuthClientUserIdTrait; use Yii; use yii\authclient\clients\Yandex as BaseYandex; class Yandex extends BaseYandex implements AuthClientInterface { + use AuthClientUserIdTrait; + /** * {@inheritdoc} */ diff --git a/src/User/Contracts/AuthClientInterface.php b/src/User/Contracts/AuthClientInterface.php index 6aad0348..84e284b6 100644 --- a/src/User/Contracts/AuthClientInterface.php +++ b/src/User/Contracts/AuthClientInterface.php @@ -14,8 +14,9 @@ use yii\authclient\ClientInterface; /** - * @property-read string $email - * @property-read string $username + * @property-read string|null $email + * @property-read string|null $userName + * @property-read mixed|null $userId */ interface AuthClientInterface extends ClientInterface { @@ -28,4 +29,9 @@ public function getEmail(); * @return string|null username */ public function getUserName(); + + /** + * @return mixed|null user id + */ + public function getUserId(); } diff --git a/src/User/Controller/RegistrationController.php b/src/User/Controller/RegistrationController.php index 6abb039a..0cfe1953 100644 --- a/src/User/Controller/RegistrationController.php +++ b/src/User/Controller/RegistrationController.php @@ -17,6 +17,7 @@ use Da\User\Factory\MailFactory; use Da\User\Form\RegistrationForm; use Da\User\Form\ResendForm; +use Da\User\Helper\SecurityHelper; use Da\User\Model\SocialNetworkAccount; use Da\User\Model\User; use Da\User\Query\SocialNetworkAccountQuery; @@ -152,6 +153,10 @@ public function actionRegister() */ public function actionConnect($code) { + if (!$this->module->enableSocialNetworkRegistration) { + throw new NotFoundHttpException(); + } + /** @var SocialNetworkAccount $account */ $account = $this->socialNetworkAccountQuery->whereCode($code)->one(); if ($account === null || $account->getIsConnected()) { @@ -171,7 +176,11 @@ public function actionConnect($code) if ($user->load(Yii::$app->request->post()) && $user->validate()) { $this->trigger(SocialNetworkConnectEvent::EVENT_BEFORE_CONNECT, $event); - $mailService = MailFactory::makeWelcomeMailerService($user); + if ($this->module->sendWelcomeMailAfterSocialNetworkRegistration) { + $mailService = MailFactory::makeWelcomeMailerService($user); + } else { + $mailService = null; + } if ($this->make(UserCreateService::class, [$user, $mailService])->run()) { $account->connect($user); $this->trigger(SocialNetworkConnectEvent::EVENT_AFTER_CONNECT, $event); diff --git a/src/User/Module.php b/src/User/Module.php index 0e00f587..d8b4e03d 100755 --- a/src/User/Module.php +++ b/src/User/Module.php @@ -117,6 +117,14 @@ class Module extends BaseModule * @var bool whether to allow registration process or not */ public $enableRegistration = true; + /** + * @var bool whether to allow registration process for social network or not + */ + public $enableSocialNetworkRegistration = true; + /** + * @var bool whether to send a welcome mail after the registration process for social network + */ + public $sendWelcomeMailAfterSocialNetworkRegistration = true; /** * @var bool whether to force email confirmation to */ diff --git a/src/User/Service/SocialNetworkAccountConnectService.php b/src/User/Service/SocialNetworkAccountConnectService.php index 2d6a7a59..366c5f89 100644 --- a/src/User/Service/SocialNetworkAccountConnectService.php +++ b/src/User/Service/SocialNetworkAccountConnectService.php @@ -83,7 +83,7 @@ protected function getSocialNetworkAccount() [], [ 'provider' => $this->client->getId(), - 'client_id' => $data['id'], + 'client_id' => $this->client->getUserId(), 'data' => json_encode($data), ] ); diff --git a/src/User/Service/SocialNetworkAuthenticateService.php b/src/User/Service/SocialNetworkAuthenticateService.php index ded5f478..c2626848 100644 --- a/src/User/Service/SocialNetworkAuthenticateService.php +++ b/src/User/Service/SocialNetworkAuthenticateService.php @@ -48,7 +48,7 @@ public function __construct( public function run() { $account = $this->socialNetworkAccountQuery->whereClient($this->client)->one(); - if (!$this->controller->module->enableRegistration && ($account === null || $account->user === null)) { + if (!$this->controller->module->enableSocialNetworkRegistration && ($account === null || $account->user === null)) { Yii::$app->session->setFlash('danger', Yii::t('usuario', 'Registration on this website is disabled')); $this->authAction->setSuccessUrl(Url::to(['/user/security/login'])); @@ -97,7 +97,7 @@ protected function createAccount() [], [ 'provider' => $this->client->getId(), - 'client_id' => $data['id'], + 'client_id' => $this->client->getUserId(), 'data' => json_encode($data), 'username' => $this->client->getUserName(), 'email' => $this->client->getEmail(), @@ -106,7 +106,10 @@ protected function createAccount() if (($user = $this->getUser($account)) instanceof User) { $account->user_id = $user->id; - $account->save(false); + } + + if (!$account->save(false)) { + return null; } return $account; diff --git a/src/User/Service/UserCreateService.php b/src/User/Service/UserCreateService.php index 74ecc73d..491873e0 100644 --- a/src/User/Service/UserCreateService.php +++ b/src/User/Service/UserCreateService.php @@ -31,7 +31,7 @@ class UserCreateService implements ServiceInterface protected $securityHelper; protected $mailService; - public function __construct(User $model, MailService $mailService, SecurityHelper $securityHelper) + public function __construct(User $model, ?MailService $mailService, SecurityHelper $securityHelper) { $this->model = $model; $this->mailService = $mailService; @@ -70,7 +70,7 @@ public function run() } $model->trigger(UserEvent::EVENT_AFTER_CREATE, $event); - if (!$this->sendMail($model)) { + if ($this->mailService instanceof MailService && !$this->sendMail($model)) { $error_msg = Yii::t( 'usuario', 'Error sending welcome message to "{email}". Please try again later.', diff --git a/src/User/Traits/AuthClientUserIdTrait.php b/src/User/Traits/AuthClientUserIdTrait.php new file mode 100644 index 00000000..c1166fe1 --- /dev/null +++ b/src/User/Traits/AuthClientUserIdTrait.php @@ -0,0 +1,14 @@ +getUserAttributes()['id'] ?? null; + } +}