Skip to content

Commit

Permalink
Merge branch 'master' into v2.0.0-dev
Browse files Browse the repository at this point in the history
  • Loading branch information
maxxer committed Feb 15, 2024
2 parents de84979 + bb917cd commit 7e5583a
Show file tree
Hide file tree
Showing 32 changed files with 457 additions and 296 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:

services:
mariadb:
image: mariadb:latest
image: mariadb:10
ports:
- 3306:3306
env:
Expand Down
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,19 @@

## dev

- Enh: Keycloak auth client (e.luhr)
- Fix: Social Network Auth (eluhr)

## 1.6.2 Jan 4th, 2024

- Fix: Two Factor Authentication - Filter - Blocks even when two factor authentication is enabled
- Fix: update Dutch (nl) translations (squio)
- Enh: possibility to limit the depth of the recursion when getting user ids from roles (mp1509)
- Fix: UserSearch avoid fields name conflict if joined with other tables (liviuk2)
- Fix: PasswordExpireService return false when user model attribute "password_changed_at" is already set at null.
- Enh #524: Two Factor - Authenticator App - offer a "Can't scan?" fallback
- Fix #530: Welcome email: reported Password is now HTML-encoded
- Ehn: updated french translation by @arollmann

## 1.6.1 March 4th, 2023

Expand Down
1 change: 1 addition & 0 deletions docs/guides/social-network-authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ The following is the list of clients supported by the module:
- **Facebook** - `Da\User\AuthClient\Facebook`
- **Github** - `Da\User\AuthClient\Github`
- **Google** - `Da\User\AuthClient\Google`
- **Keycloak** - `Da\User\AuthClient\Keycloak`
- **LinkedIn** - `Da\User\AuthClient\LinkedIn`
- **Twitter** - `Da\User\AuthClient\Twitter`
- **VKontakte** - `Da\User\AuthClient\VKontakte`
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
55 changes: 55 additions & 0 deletions src/User/AuthClient/Keycloak.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Da\User\AuthClient;

use Da\User\Contracts\AuthClientInterface;
use yii\authclient\OpenIdConnect;

/**
* Example application configuration:
*
* ```php
* 'components' => [
* 'authClientCollection' => [
* 'class' => 'yii\authclient\Collection',
* 'clients' => [
* 'keycloak' => [
* 'class' => 'yii\authclient\clients\Keycloak',
* 'clientId' => 'keycloak_client_id',
* 'clientSecret' => 'keycloak_client_secret',
* 'issuerUrl' => 'http://keycloak/realms/your-realm',
* ],
* ],
* ]
* // ...
* ]
* ```
*/
class Keycloak extends OpenIdConnect implements AuthClientInterface
{
/**
* {@inheritdoc}
*/
public function getEmail()
{
// claim from email scope
return $this->getUserAttributes()['email'] ?? null;
}

/**
* {@inheritdoc}
*/
public function getUserName()
{
// claim from profile scope
return $this->getUserAttributes()['preferred_username'] ?? $this->getEmail();
}

/**
* {@inheritdoc}
*/
public function getUserId()
{
return $this->getUserAttributes()['sub'] ?? null;
}
}
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
2 changes: 1 addition & 1 deletion src/User/Controller/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ public function actionTwoFactor($id)
switch ($choice) {
case 'google-authenticator':
$uri = $this->make(TwoFactorQrCodeUriGeneratorService::class, [$user])->run();
return $this->renderAjax('two-factor', ['id' => $id, 'uri' => $uri]);
return $this->renderAjax('two-factor', ['id' => $id, 'uri' => $uri, 'user' => $user]);
case 'email':
$emailCode = $this->make(TwoFactorEmailCodeGeneratorService::class, [$user])->run();
return $this->renderAjax('two-factor-email', ['id' => $id, 'code' => $emailCode]);
Expand Down
6 changes: 4 additions & 2 deletions src/User/Filter/TwoFactorAuthenticationEnforceFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ public function beforeAction($action)
}

$permissions = $module->twoFactorAuthenticationForcedPermissions;
$itemsByUser = array_keys($this->getAuthManager()->getItemsByUser(Yii::$app->user->identity->id));
if (!empty(array_intersect($permissions, $itemsByUser))) {

$user = Yii::$app->user->identity;
$itemsByUser = array_keys($this->getAuthManager()->getItemsByUser($user->id));
if (!empty(array_intersect($permissions, $itemsByUser)) && !$user->auth_tf_enabled) {
Yii::$app->session->setFlash('warning', Yii::t('usuario', 'Your role requires 2FA, you won\'t be able to use the application until you enable it'));
return Yii::$app->response->redirect(['/user/settings/account'])->send();
}
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/Query/SocialNetworkAccountQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function whereClient(AuthClientInterface $client)
return $this->andWhere(
[
'provider' => $client->getId(),
'client_id' => (string)$client->getUserAttributes()['id'],
'client_id' => (string)$client->getUserId(),
]
);
}
Expand Down
18 changes: 12 additions & 6 deletions src/User/Search/UserSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@

namespace Da\User\Search;

use Da\User\Model\User;
use Da\User\Query\UserQuery;
use Da\User\Traits\ContainerAwareTrait;
use Yii;
use yii\base\InvalidParamException;
use yii\base\Model;
use yii\data\ActiveDataProvider;

class UserSearch extends Model
{
use ContainerAwareTrait;

/**
* @var string
*/
Expand Down Expand Up @@ -106,21 +110,23 @@ public function search($params)
return $dataProvider;
}

$userClass = $this->getClassMap()->get(User::class);

if ($this->created_at !== null) {
$date = strtotime($this->created_at);
$query->andFilterWhere(['between', 'created_at', $date, $date + 3600 * 24]);
$query->andFilterWhere(['between', $userClass::tableName().'.created_at', $date, $date + 3600 * 24]);
}

if ($this->last_login_at !== null) {
$date = strtotime($this->last_login_at);
$query->andFilterWhere(['between', 'last_login_at', $date, $date + 3600 * 24]);
$query->andFilterWhere(['between', $userClass::tableName().'.last_login_at', $date, $date + 3600 * 24]);
}

$query
->andFilterWhere(['like', 'username', $this->username])
->andFilterWhere(['like', 'email', $this->email])
->andFilterWhere(['registration_ip' => $this->registration_ip])
->andFilterWhere(['last_login_ip' => $this->last_login_ip]);
->andFilterWhere(['like', $userClass::tableName().'.username', $this->username])
->andFilterWhere(['like', $userClass::tableName().'.email', $this->email])
->andFilterWhere([$userClass::tableName().'.registration_ip' => $this->registration_ip])
->andFilterWhere([$userClass::tableName().'.last_login_ip' => $this->last_login_ip]);

return $dataProvider;
}
Expand Down
7 changes: 6 additions & 1 deletion src/User/Service/MailService.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,16 @@ public function getType()
*/
public function run()
{
return $this->mailer
$result = $this->mailer
->compose(['html' => $this->view, 'text' => "text/{$this->view}"], $this->params)
->setFrom($this->from)
->setTo($this->to)
->setSubject($this->subject)
->send();

if (!$result) {
Yii::error("Email sending failed to '{$this->to}'.", 'mailer');
}
return $result;
}
}
3 changes: 2 additions & 1 deletion src/User/Service/PasswordExpireService.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ public function __construct(User $model)

public function run()
{
return $this->model->updateAttributes([
$this->model->updateAttributes([
'password_changed_at' => null,
]);
return true;
}
}
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
Loading

0 comments on commit 7e5583a

Please sign in to comment.