Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Social Network Auth #535

Merged
merged 9 commits into from
Jan 10, 2024
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions docs/install/configuration-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/User/AuthClient/Facebook.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}
*/
Expand Down
2 changes: 2 additions & 0 deletions src/User/AuthClient/GitHub.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}
*/
Expand Down
2 changes: 2 additions & 0 deletions src/User/AuthClient/Google.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}
*/
Expand Down
3 changes: 3 additions & 0 deletions src/User/AuthClient/LinkedIn.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}
*/
Expand Down
3 changes: 3 additions & 0 deletions src/User/AuthClient/Twitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
3 changes: 3 additions & 0 deletions src/User/AuthClient/VKontakte.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}
*/
Expand Down
3 changes: 3 additions & 0 deletions src/User/AuthClient/Yandex.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}
*/
Expand Down
10 changes: 8 additions & 2 deletions src/User/Contracts/AuthClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -28,4 +29,9 @@ public function getEmail();
* @return string|null username
*/
public function getUserName();

/**
* @return mixed|null user id
*/
public function getUserId();
}
11 changes: 10 additions & 1 deletion src/User/Controller/RegistrationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()) {
Expand All @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions src/User/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
2 changes: 1 addition & 1 deletion src/User/Service/SocialNetworkAccountConnectService.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ protected function getSocialNetworkAccount()
[],
[
'provider' => $this->client->getId(),
'client_id' => $data['id'],
'client_id' => $this->client->getUserId(),
'data' => json_encode($data),
]
);
Expand Down
9 changes: 6 additions & 3 deletions src/User/Service/SocialNetworkAuthenticateService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']));

Expand Down Expand Up @@ -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(),
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/User/Service/UserCreateService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.',
Expand Down
14 changes: 14 additions & 0 deletions src/User/Traits/AuthClientUserIdTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Da\User\Traits;

trait AuthClientUserIdTrait
{
/**
* @see \Da\User\Contracts\AuthClientInterface::getUserId()
*/
public function getUserId()
{
return $this->getUserAttributes()['id'] ?? null;
}
}
Loading