Skip to content

Commit

Permalink
Allow to modify UserQuery in getByUsernameOrEmail (#216)
Browse files Browse the repository at this point in the history
  • Loading branch information
nvdkemp authored Aug 15, 2024
1 parent eeab9bf commit f23af0a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
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

0 comments on commit f23af0a

Please sign in to comment.