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

Allow to modify UserQuery in getByUsernameOrEmail #216

Merged
merged 5 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/events/UserQueryCriteria.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace flipbox\saml\sp\events;

use craft\elements\db\UserQuery;
use yii\base\Event;

class UserQueryCriteria extends Event
{
public UserQuery $userQuery;

public string $usernameOrEmail;

public bool $applyDefaultCriteria = true;

public bool $archived;
}
26 changes: 21 additions & 5 deletions src/services/login/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
use craft\models\FieldLayout;
use flipbox\saml\core\exceptions\InvalidMessage;
use flipbox\saml\core\helpers\ProviderHelper;
use flipbox\saml\sp\events\UserQueryCriteria;
use flipbox\saml\sp\helpers\UserHelper;
use flipbox\saml\sp\models\Settings;
use flipbox\saml\sp\records\ProviderIdentityRecord;
use flipbox\saml\sp\records\ProviderRecord;
use flipbox\saml\sp\Saml;
use SAML2\Response as SamlResponse;
use yii\base\Event;
use yii\base\UserException;

/**
Expand All @@ -32,6 +34,8 @@ class User extends Component

const EVENT_BEFORE_USER_SAVE = 'eventBeforeUserSave';

const EVENT_GET_CUSTOM_USER_CRITERIA = 'eventGetCustomUserCriteria';

/**
* @var FieldLayout|null
*/
Expand Down Expand Up @@ -435,18 +439,30 @@ protected function forceGet($username)
*/
protected function getByUsernameOrEmail($usernameOrEmail, $archived = false)
{
$event = new UserQueryCriteria([
'userQuery' => UserElement::find(),
'usernameOrEmail' => $usernameOrEmail,
'archived' => $archived,
]);

if (Event::hasHandlers(self::class, self::EVENT_GET_CUSTOM_USER_CRITERIA)) {
Event::trigger(
self::class,
self::EVENT_GET_CUSTOM_USER_CRITERIA,
$event
);
}

return UserElement::find()
return ($event->applyDefaultCriteria ? $event->userQuery
->where(
[
'or',
['username' => $usernameOrEmail],
['email' => $usernameOrEmail],
['username' => $event->usernameOrEmail],
['email' => $event->usernameOrEmail],
]
)
->status(null)
->archived($archived)
->one();
->archived($event->archived) : $event->userQuery)->one();
}

private function getAttributeValue($attributeValue)
Expand Down
Loading