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 user role mapping #91

Merged
merged 1 commit into from
Jan 15, 2025
Merged

Fix user role mapping #91

merged 1 commit into from
Jan 15, 2025

Conversation

roborourke
Copy link
Contributor

  • Ensures role mapping occurs on each login in case of updates
  • Fixes network role mapping where both sites and network level settings are supplied
  • Uses add_user_to_blog() so that the current blog is properly restored and other defaults and caches are created

* Ensures role mapping occurs on each login in case of updates
* Fixes network role mapping where both sites and network level settings are supplied
* Uses `add_user_to_blog()` so that the current blog is properly restored and other defaults and caches are created
Copy link
Member

@mattheu mattheu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 I think this is a good improvement and makes sense. Could this cause unexpected behaviour if people update the plugin? Will sites have the assumption that roles are only mapped on initial creation and not updated - is this possibly a breaking change 

@roborourke
Copy link
Contributor Author

@mattheu possibly breaking for some use cases or instances, however that's ok for minor releases below a 1.0.0 release. We're not promising 100% stability yet.

I will bump to 0.6.0 with this though, so properly scoped composer dependencies should continue to use the appropriate version.

@roborourke roborourke merged commit 83961f3 into master Jan 15, 2025
1 check failed
@roborourke roborourke deleted the role-mapping branch January 15, 2025 09:42
Copy link
Member

@rmccue rmccue left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was merged while I was reviewing it, but I don't think it should have been merged just yet.

@@ -3,7 +3,7 @@
Plugin Name: WP Simple SAML
Description: Integrate SAML 2.0 IDP without the hassle
Author: Shady Sharaf, Human Made
Version: 0.1
Version: 0.6.0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the very large version update? The version in master is 0.4.1, why the jump?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The latest release tag was 0.5.1, this introduces a new action so I opted for a minor release. Semantic versioning of a plugin below 0.x allows for this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file now says 0.6.0 (which is a major release under 0.x), but the actual plugin version is in plugin.php not this file, which is 0.4.1:

Version: 0.4.1

Seems like we need to remove it from this file, fix the main one, and make sure we keep those in sync.

* @param \WP_User $user User object
* @param array $attributes SAML Attributes passed from IdP
*/
do_action( 'wpsimplesaml_user_matched', $user, $attributes );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the functional difference in this PR? It's not clear to me why the existing action is not good enough here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one acts on each authentication, the other action only works when a user account is initially created.

Changes to settings in active directory once a user has logged in once will never be reflected in WordPress unless the roles mapping is checked again on each authentication.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a fairly major change in behaviour that might have subtle effects; is there an issue where we can discuss that behaviour in more depth?

}

return $network_roles ?? (array) $roles;
return $roles;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this change here? There shouldn't be any functional difference?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was just weirdly written, $network_roles is only defined in the if block above this return statement, and $roles is already cast to an array.

@mattheu
Copy link
Member

mattheu commented Jan 15, 2025

but I don't think it should have been merged just yet.

Yeah I just commented didn't approve.

@roborourke
Copy link
Contributor Author

Sorry I thought you'd approved it Matt. My fault.

Comment on lines +593 to +594
foreach ( $roles['network'] as $role ) {
add_user_to_blog( (int) $site_id, $user->ID, $role );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In terms of #90, the minimal change here I think would have been to restore_current_blog(), although add_user_to_blog() might make sense overall. Need to check the difference here.

Also, the previous code skips the first role in $roles['network'], why the difference here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I think this breaks the current behaviour if you have multiple roles.

Internally, add_user_to_blog() does a few things this code did not do, including setting the user's primary site and clearing caches, which I think is good. However, internally it calls ->set_role() not ->add_role(), so using it in a loop like this will wipe previous roles.

However, I do think the current behaviour is probably also kind of broken in that it never removes roles they shouldn't have if the upstream changes. That's a fairly significant change though, so might warrant an option of some sort - as some users may want to allow adding roles purely in WP.

Let's open an issue to discuss.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created #94 for the follow up discussion.

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