Sync only specific user roles
-
Hello,
May I know if there is a way to just send users with just specific user roles. I tried to scan the code but it looks like there is no way to do it.
I hope on the Wprus_Api_Abstract->fire_action method there is a filter that it is a condition to fire the action
$can_fire_action = apply_filters(‘wprus_can_fire_action’, true, $data, $endpoint);
if( ! $can_fire_action ) return;
-
Hi @jhayghost !
Thank you for using WPRUS!Indeed, such filter is not available in the current version – however, it is possible to achieve this with custom code in a small plugin:
For synchronized actions:
– Usewprus_before_firing_action
to check the parameters and determine whether the action should be sent (save the state in an attribute of the plugin’s class, for example)
– Usepre_http_request
and set the response manually to short-circuit the HTTP request and return early with that value if the state saved in your plugin is matching your conditions.For async actions (only without performing redirects!):
– Usewprus_before_async_firing_action
to check the parameters and determine whether the action should be sent (save the state in an attribute of the plugin’s class, for example)
– Usewprus_action_data
to alter the data sent if the state saved in your plugin is matching your conditions.This is not ideal because some code is executed unnecessarily, browser support breaks in case it needs redirection, and logs may show false negatives, but this would be a way to somewhat achieve your goal with the current plugin.
I will add such filter in the next version (release date undetermined).
Thanks @frogerme for your prompt response. If you add a filter kindly add also the $url to be more flexible.
I might fork it your repo for now. Thanks for sharing your plugin.
Same problem here. Have you find a working solution?
Thanks.It would be super useful to have this hook.
In our case, we don’t want admins to be synced at all between sites. One site is a cart/WooCommerce site for purchases and one is for course material. The course material people shouldn’t have access to the cart site since that could be problematic–and same in reverse.
The common setup for this plugin would likely be similar to this. Granted, many sites are small and don’t have more than one admin and they handle both, but that’s not the case for us. If we could filter by role it would simplify things greatly.
Thanks!
Hi there,
We’d really love to see this functionality added too. We only want the sync to happen for one user role.
Meanwhile, is this the right idea to implement what you’re suggesting above?
add_action( 'wprus_before_firing_action', 'check_whether_user_sync_between_sites_should_proceed', 10, 2 ); function check_whether_user_sync_between_sites_should_proceed( $endpoint, $url, $data ) { global $block_http_requests; $block_http_requests = true; if( ... user role checks out ... ){ $block_http_requests = false; } return; }
add_filter( 'pre_http_request', 'short_circuit_http_request', 10, 3 ); function short_circuit_http_request( $preempt, $parsed_args, $url ){ global $block_http_requests; if ( ! $block_http_requests ) { return false; // i.e. Don't intervene so it can proceed as planned } // Otherwise, block the request: return new WP_Error( 'blocked', __( "HTTP request manually blocked due to disallowed user roles.", $this->plugin_name ) ); }
add_action( 'wprus_after_firing_action', 'reset_http_blocking_after_checking_whether_user_sync_between_sites_should_proceed', 10, 2 ); function reset_http_blocking_after_checking_whether_user_sync_between_sites_should_proceed( $endpoint, $url, $data, $response ) { global $block_http_requests; $block_http_requests = false; }
I ask because we’re getting an error when we update the user profile, so must have misunderstood something. … or it’s just too late at night as I write this ??
Would be grateful if you could let me know if anything jumps out at you.
Thank you!
Alex.
- This reply was modified 3 years ago by theinnographer.
- This reply was modified 3 years ago by theinnographer.
- This reply was modified 3 years ago by theinnographer.
Hi again,
Yikes. It was definitely too late at night. I was using the wrong number of arguments in my add_action statements, e.g. should be:
add_action( 'wprus_before_firing_action', 'check_whether_user_sync_between_sites_should_proceed', 10, 3 );
not:
add_action( 'wprus_before_firing_action', 'check_whether_user_sync_between_sites_should_proceed', 10, 2 );
And should be 4 for wprus_after_firing_action.
I would welcome and appreciate any other feedback on taking this approach.
Sincerely,
Alex.One more thought, to support the request for adding the user and/or user role filters suggested above.
It’s quite possible to lock an administrator out of a remote site by mistake.
Sounds silly, I know. And sounds like something you have to be clumsy or stupid to do.
But consider the following example:
1. You set up role sync between sites with a role being synched, e.g. editor
2. You don’t setup to sync the administrator role between those sites, but you also don’t “merge with existing roles”
3. You add the editor role to an administratorIf that administrator also exists on any synched remote sites, then after the actions are fired they will only have role editor. And not the administrator role any longer!
This may be a fringe example and I know it’d be odd to add the editor role to administrator user, but something quite like this happened to us. And I believe we’ve been very careful and thoughtful.
I guess my points are that: 1) it might be better not to allow administrators to sync by default in any event; and 2) it’d be great to have the ability requested above to select which users / user roles are impacted – globally for all sync pairs, or on a case by case basis for each sync pair.
All that said, thank you again for this wonderful plugin Alexandre.
We really appreciate you providing it and hope our experiences and comments are helpful to you.
Sincerely,
Alex.One more thought ??
This functionality would also avoid workarounds such as having to use the same administrator passwords on all synched sites if you’re synching passwords.
Thanks again and have a great day!
Alex.
- The topic ‘Sync only specific user roles’ is closed to new replies.