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

Conversation

nvdkemp
Copy link
Contributor

@nvdkemp nvdkemp commented May 7, 2024

We have some CraftCMS sites that use Azure as IDP. We need to match those users based on the objectidentifier (http://schemas.microsoft.com/identity/claims/objectidentifier) which we save in a Craft field in the users fieldlayout. However the code always assumes the NameID override is for the username or email attribute.

There currently is an open issue with someone having the same problem: #204.

This fix will make it so that you are able to influence the UserQuery and return a different result. It is also possible to only change the $usernameOrEmail or $archived variable with this event.

Example how you could modify the UserQuery (in a plugin or module):

<?php

use craft\elements\User as UserElement;
use flipbox\saml\sp\events\UserQueryCriteria;
use flipbox\saml\sp\services\login\User;
use yii\base\Event;

Event::on(User::class, User::EVENT_GET_CUSTOM_USER_CRITERIA, static function (UserQueryCriteria $event): void {
    $event->applyDefaultCriteria = false;
    $event->userQuery            = UserElement::find()
        ->microsoftIdentifier($event->usernameOrEmail) // the usernameOrEmail is the microsoftIdentifier. This can be configured in the SAML-SL plugin in the Username/NameID Override section (admin/saml-sp/metadata/3#configure)
        ->status(null)
        ->archived($event->archived);
});

I branched from the 2.7.5 tag, since some of our websites are still running on Craft 3.

Edit: The master has been merged into the branch and there are no more remaining conflicts.

If you accept this fix: can you make a new tag for this? For both Craft3/Craft4/Craft5. All of those versions have the same code.
Thanks!

@dsmrt dsmrt self-assigned this May 7, 2024
@nvdkemp nvdkemp changed the title Allow to modify userQuery in getByUsernameOrEmail Allow to modify UserQuery in getByUsernameOrEmail May 7, 2024
@JensvdHeydt
Copy link

Is there any chance to merge this? I am having the same problem and would like to use this modification.

@dsmrt dsmrt merged commit f23af0a into flipboxfactory:master Aug 15, 2024
2 checks passed
@dsmrt
Copy link
Contributor

dsmrt commented Aug 15, 2024

New release at 5.1.1
https://packagist.org/packages/flipboxfactory/saml-sp#5.1.1

I'll work on craft 3 and 4 tonight too

@dsmrt
Copy link
Contributor

dsmrt commented Aug 15, 2024

Also, push 2.8.0 for v3 and 4.3.0 for v4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants