Giter VIP home page Giter VIP logo

user-switching's Introduction

User Switching

Stable tag: 1.7.3
Tested up to: 6.5
License: GPL v2 or later
Tags: users, user switching, fast user switching, multisite, woocommerce
Contributors: johnbillion
Donate link: https://github.com/sponsors/johnbillion

Instant switching between user accounts in WordPress.

Description

This plugin allows you to quickly swap between user accounts in WordPress at the click of a button. You'll be instantly logged out and logged in as your desired user. This is handy for for helping customers on WooCommerce sites, membership sites, testing environments, or for any site where administrators need to switch between multiple accounts.

Features

  • Switch user: Instantly switch to any user account from the Users screen.
  • Switch back: Instantly switch back to your originating account.
  • Switch off: Log out of your account but retain the ability to instantly switch back in again.
  • Compatible with Multisite, WooCommerce, BuddyPress, and bbPress.
  • Compatible with most membership and user management plugins.
  • Compatible with most two-factor authentication solutions (see the FAQ for more info).
  • Approved for use on enterprise-grade WordPress platforms such as Altis and WordPress VIP.

Note: User Switching supports versions of WordPress up to three years old, and PHP version 7.4 or higher.

Security

  • Only users with the ability to edit other users can switch user accounts. By default this is only Administrators on single site installations, and Super Admins on Multisite installations.
  • Passwords are not (and cannot be) revealed.
  • Uses the cookie authentication system in WordPress when remembering the account(s) you've switched from and when switching back.
  • Implements the nonce security system in WordPress, meaning only those who intend to switch users can switch.
  • Full support for user session validation where appropriate.
  • Full support for HTTPS.
  • Backed by the Patchstack Vulnerability Disclosure Program

Usage

  1. Visit the Users menu in WordPress and you'll see a Switch To link in the list of action links for each user.
  2. Click this and you will immediately switch into that user account.
  3. You can switch back to your originating account via the Switch back link on each dashboard screen or in your profile menu in the WordPress toolbar.

See the FAQ for information about the Switch Off feature.

Other Plugins

I maintain several other plugins for developers. Check them out:

  • Query Monitor is the developer tools panel for WordPress
  • WP Crontrol lets you view and control what's happening in the WP-Cron system

Privacy Statement

User Switching makes use of browser cookies in order to allow users to switch to another account. Its cookies operate using the same mechanism as the authentication cookies in WordPress core, which means their values contain the user's user_login field in plain text which should be treated as potentially personally identifiable information (PII) for privacy and regulatory reasons (GDPR, CCPA, etc). The names of the cookies are:

  • wordpress_user_sw_{COOKIEHASH}
  • wordpress_user_sw_secure_{COOKIEHASH}
  • wordpress_user_sw_olduser_{COOKIEHASH}

User Switching does not send data to any third party, nor does it include any third party resources, nor will it ever do so.

See also the FAQ for some questions relating to privacy and safety when switching between users.

Accessibility Statement

User Switching aims to be fully accessible to all of its users. It implements best practices for web accessibility, outputs semantic and structured markup, adheres to the default styles and accessibility guidelines of WordPress, uses the accessibility APIs provided by WordPress and web browsers where appropriate, and is fully accessible via keyboard.

User Switching should adhere to Web Content Accessibility Guidelines (WCAG) 2.0 at level AA when used with a recent version of WordPress where its admin area itself adheres to these guidelines. If you've experienced or identified an accessibility issue in User Switching, please open a thread in the User Switching plugin support forum and I'll address it swiftly.

Screenshots

  1. The Switch To link on the Users screen
    The Switch To link on the Users screen
  2. The Switch To link on a user's profile
    The Switch To link on a user's profile

Frequently Asked Questions

Does this plugin work with PHP 8?

Yes, it's actively tested and working up to PHP 8.3.

What does "Switch off" mean?

Switching off logs you out of your account but retains your user ID in an authentication cookie so you can switch straight back without having to log in again manually. It's akin to switching to no user, and being able to switch back.

The Switch Off link can be found in your profile menu in the WordPress toolbar. Once you've switched off you'll see a Switch back link in a few places:

  • In the footer of your site
  • On the Log In screen
  • In the "Meta" widget

Does this plugin work with WordPress Multisite?

Yes, and you'll also be able to switch users from the Users screen in Network Admin.

Does this plugin work with WooCommerce?

Yes, and you'll also be able to switch users from various WooCommerce administration screens while logged in as a Shop Manager or an administrative user.

Does this plugin work with BuddyPress?

Yes, and you'll also be able to switch users from member profile screens and the member listing screen.

Does this plugin work with bbPress?

Yes, and you'll also be able to switch users from member profile screens.

Does this plugin work if my site is using a two-factor authentication plugin?

Yes, mostly.

One exception I'm aware of is Duo Security. If you're using this plugin, you should install the User Switching for Duo Security add-on plugin which will prevent the two-factor authentication prompt from appearing when you switch between users.

What capability does a user need in order to switch accounts?

A user needs the edit_users capability in order to switch user accounts. By default only Administrators have this capability, and with Multisite enabled only Super Admins have this capability.

Specifically, a user needs the ability to edit the target user in order to switch to them. This means if you have custom user capability mapping in place which uses the edit_users or edit_user capabilities to affect ability of users to edit others, then User Switching should respect that.

Can regular admins on Multisite installations switch accounts?

No. This can be enabled though by installing the User Switching for Regular Admins plugin.

Can the ability to switch accounts be granted to other users or roles?

Yes. The switch_users meta capability can be explicitly granted to a user or a role to allow them to switch users regardless of whether or not they have the edit_users capability. For practical purposes, the user or role will also need the list_users capability so they can access the Users menu in the WordPress admin area.

add_filter( 'user_has_cap', function( $allcaps, $caps, $args, $user ) {
	if ( 'switch_to_user' === $args[0] ) {
		if ( my_condition( $user ) ) {
			$allcaps['switch_users'] = true;
		}
	}
	return $allcaps;
}, 9, 4 );

Note that this needs to happen before User Switching's own capability filtering, hence the priority of 9.

Can the ability to switch accounts be denied from users?

Yes. User capabilities in WordPress can be set to false to deny them from a user. Denying the switch_users capability prevents the user from switching users, even if they have the edit_users capability.

add_filter( 'user_has_cap', function( $allcaps, $caps, $args, $user ) {
	if ( 'switch_to_user' === $args[0] ) {
		if ( my_condition( $user ) ) {
			$allcaps['switch_users'] = false;
		}
	}
	return $allcaps;
}, 9, 4 );

Notes:

  • This needs to happen before User Switching's own capability filtering, hence the priority of 9.
  • The ID of the target user can be found in $args[2].

Can I add a custom "Switch To" link to my own plugin or theme?

Yes. Use the user_switching::maybe_switch_url() method for this. It takes care of authentication and returns a nonce-protected URL for the current user to switch into the provided user account.

if ( method_exists( 'user_switching', 'maybe_switch_url' ) ) {
	$url = user_switching::maybe_switch_url( $target_user );
	if ( $url ) {
		printf(
			'<a href="%1$s">Switch to %2$s</a>',
			esc_url( $url ),
			esc_html( $target_user->display_name )
		);
	}
}

If you want to specify the URL that the user gets redirected to after switching, add a redirect_to parameter to the URL like so:

if ( method_exists( 'user_switching', 'maybe_switch_url' ) ) {
	$url = user_switching::maybe_switch_url( $target_user );
	if ( $url ) {
		// Redirect to the home page after switching:
		$redirect_to = home_url();
		printf(
			'<a href="%1$s">Switch to %2$s</a>',
			esc_url( add_query_arg(
				'redirect_to',
				rawurlencode( $redirect_to ),
				$url
			) ),
			esc_html( $target_user->display_name )
		);
	}
}

The above code also works for displaying a link to switch back to the original user, but if you want an explicit link for this you can use the following code:

if ( method_exists( 'user_switching', 'get_old_user' ) ) {
	$old_user = user_switching::get_old_user();
	if ( $old_user ) {
		printf(
			'<a href="%1$s">Switch back to %2$s</a>',
			esc_url( user_switching::switch_back_url( $old_user ) ),
			esc_html( $old_user->display_name )
		);
	}
}

Can I determine whether the current user switched into their account?

Yes. Use the current_user_switched() function for this. If the current user switched into their account from another then it returns a WP_User object for their originating user, otherwise it returns false.

if ( function_exists( 'current_user_switched' ) ) {
	$switched_user = current_user_switched();
	if ( $switched_user ) {
		// User is logged in and has switched into their account.
		// $switched_user is the WP_User object for their originating user.
	}
}

Can I log each time a user switches to another account?

You can install an audit trail plugin such as Simple History, WP Activity Log, or Stream, all of which have built-in support for User Switching and all of which log an entry when a user switches into another account.

Does this plugin allow a user to frame another user for an action?

Potentially yes, but User Switching includes some safety protections for this and there are further precautions you can take as a site administrator:

  • You can install an audit trail plugin such as Simple History, WP Activity Log, or Stream, all of which have built-in support for User Switching and all of which log an entry when a user switches into another account.
  • User Switching stores the ID of the originating user in the new WordPress user session for the user they switch to. Although this session does not persist by default when they subsequently switch back, there will be a record of this ID if your database server has query logging enabled.
  • User Switching stores the login name of the originating user in an authentication cookie (see the Privacy Statement for more information). If your server access logs store cookie data, there will be a record of this login name (along with the IP address) for each access request.
  • User Switching triggers an action when a user switches account, switches off, or switches back (see below). You can use these actions to perform additional logging for safety purposes depending on your requirements.

One or more of the above should allow you to correlate an action with the originating user when a user switches account, should you need to.

Bear in mind that even without the User Switching plugin in use, any user who has the ability to edit another user can still frame another user for an action by, for example, changing their password and manually logging into that account. If you are concerned about users abusing others, you should take great care when granting users administrative rights.

Can I switch users directly from the admin toolbar?

Yes, there's a third party add-on plugin for this: Admin Bar User Switching.

Are any plugin actions called when a user switches account?

Yes. When a user switches to another account, the switch_to_user hook is called:

/**
 * Fires when a user switches to another user account.
 *
 * @since 0.6.0
 * @since 1.4.0 The `$new_token` and `$old_token` parameters were added.
 *
 * @param int    $user_id     The ID of the user being switched to.
 * @param int    $old_user_id The ID of the user being switched from.
 * @param string $new_token   The token of the session of the user being switched to. Can be an empty string
 *                            or a token for a session that may or may not still be valid.
 * @param string $old_token   The token of the session of the user being switched from.
 */
do_action( 'switch_to_user', $user_id, $old_user_id, $new_token, $old_token );

When a user switches back to their originating account, the switch_back_user hook is called:

/**
 * Fires when a user switches back to their originating account.
 *
 * @since 0.6.0
 * @since 1.4.0 The `$new_token` and `$old_token` parameters were added.
 *
 * @param int       $user_id     The ID of the user being switched back to.
 * @param int|false $old_user_id The ID of the user being switched from, or false if the user is switching back
 *                               after having been switched off.
 * @param string    $new_token   The token of the session of the user being switched to. Can be an empty string
 *                               or a token for a session that may or may not still be valid.
 * @param string    $old_token   The token of the session of the user being switched from.
 */
do_action( 'switch_back_user', $user_id, $old_user_id, $new_token, $old_token );

When a user switches off, the switch_off_user hook is called:

/**
 * Fires when a user switches off.
 *
 * @since 0.6.0
 * @since 1.4.0 The `$old_token` parameter was added.
 *
 * @param int    $old_user_id The ID of the user switching off.
 * @param string $old_token   The token of the session of the user switching off.
 */
do_action( 'switch_off_user', $old_user_id, $old_token );

When a user switches to another account, switches off, or switches back, the user_switching_redirect_to filter is applied to the location that they get redirected to:

/**
 * Filters the redirect location after a user switches to another account or switches off.
 *
 * @since 1.7.0
 *
 * @param string       $redirect_to   The target redirect location, or an empty string if none is specified.
 * @param string|null  $redirect_type The redirect type, see the `user_switching::REDIRECT_*` constants.
 * @param WP_User|null $new_user      The user being switched to, or null if there is none.
 * @param WP_User|null $old_user      The user being switched from, or null if there is none.
 */
return apply_filters( 'user_switching_redirect_to', $redirect_to, $redirect_type, $new_user, $old_user );

In addition, User Switching respects the following filters from WordPress core when appropriate:

  • login_redirect when switching to another user.
  • logout_redirect when switching off.

How can I report a security bug?

You can report security bugs through the Patchstack Vulnerability Disclosure Program. The Patchstack team helps validate, triage, and handle any security vulnerabilities. Report a security vulnerability here.

Do you accept donations?

I am accepting sponsorships via the GitHub Sponsors program and any support you can give will help me maintain this plugin and keep it free for everyone.

user-switching's People

Contributors

1naveengiri avatar andyburnsco avatar johnbillion avatar mikelittle avatar mphillips avatar skaeser avatar soderlind avatar swissspidy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

user-switching's Issues

Conflict with Sucuri Security plugin

Multisite on Local by flywheel.
WordPress 4.9.8
Sucuri Security plugin (1.8.18) is network activated
User Switching (1.4.0 ) is network activated.

When the Sucuri Security plugin is activated, I get Sorry, you are not allowed to access this page when I switch to user.

If I go back and refreshes the page, it says I'm switched to the user. So, switching works but the redirect fails(?).

Switching back to the original user works fine.

UI to quickly switch between users

It's a bit of a pain to head to Manage Users each time. What about a UI, maybe tied to the toolbar, for quickly switching between users?

Ideally, the UI would persist when you're in a switched user, so you can quickly switch to another user without reverting to your original user.

Fatal error on author archives

Since 3bb5468 the plugin uses current_user_can( 'switch_to_user', get_queried_object_id() ) and self::switch_to_url( get_queried_object() ) ) in the admin bar on author archives.

For some reason, queried_object is false in my environment when viewing an author archive and queried_object_id is 0.

This leads to a fatal error because of the strict type checking:

Uncaught TypeError: Argument 1 passed to user_switching::switch_to_url() must be an instance of WP_User, boolean given, called in /wp-content/plugins/user-switching/user-switching.php on line 487

While I am trying to figure out why this is the case (and why current_user_can( 'switch_to_user', 0 ) works), I thought I'd raise this here just in case.

It can't hurt to have some type checking before calling that function.

BP user check can generate PHP errors

The BuddyPress current user checks at

if ( ! empty( $members_template ) && empty( $bp->displayed_user->id ) ) {
can cause PHP notices when not looking at a single user profile or a member directory. I think what's needed is something like this:

$user_id = null;
if ( bp_is_user() ) {
    $user_id = bp_displayed_user_id();
} elseif ( bp_is_members_directory() ) {
    $user_id = bp_get_member_user_id();
}

if ( ! $user_id ) {
   return;
}

$user = get_userdata( $user_id );

// ...

Allow switching between some accounts?

Hello!

First of all, thank you very much for your time! This plugin is great!

I am working on a project and I need to allow managers to switch only to the accounts associated with each of them.

Is there any way to achieve it? Any clue with a custom development?

Thanks so much in advance!

Best Regards!

Could not switch users

*** on develop branch ***

screen shot 2019-02-15 at 9 33 26 pm

I switched off my user, but still have access to the admin...? Clicking "Switch back" throws the above error.

Maybe QM and User Switching are having a sibling feud? ;)

COOKIE_DOMAIN causing 404

Hello,

We are setting the COOKIE_DOMAIN in wp-config.php to allow support for subdomains. For example:

define( 'COOKIE_DOMAIN', '.mysite.com' );

This appears to break the user switching plugin. Specifically, we receive a 404 or "could not switch users" error when trying to switch.

Do you know of any workaround for this?

Thanks!
Neil

How to create custom link in plugin

I want to create 2 custom buttons im wp custom profiles plugin:

`
/profile/login/?action=switch_to_user&user_id=17&nr=1&_wpnonce=215bbb149b
to:
< a href="/profile/< ? php switch_to_user code ? >">Swith to user< /a>

/profile/login/?action=switch_to_olduser&nr=1&_wpnonce=a7c824e394
to:
< a href="/profile/< ? php switch_to_olduser code ? >">Swith to back< /a>
`

Use of undefined constant when clearing old user cookies

Hi,

I'm hitting the following error message on PHP 7.3.5:
Use of undefined constant USER_SWITCHING_OLDUSER_COOKIE - assumed 'USER_SWITCHING_OLDUSER_COOKIE' (this will throw an Error in a future version of PHP)

That constant is defined in the plugins_loaded action, so it should be defined just fine ahead of time. Here is the relevant part of the stack, with the plugins_loaded call being run way back:

stack: Array(11)
0: "user_switching_clear_olduser_cookie()"
1: "do_action('wp_login')"
2: "xxxxxxxxxxxxxxxxxxxxxxx"
3: "apply_filters('determine_current_user')"
4: "_wp_get_current_user()"
5: "wp_get_current_user()"
6: "get_user_locale()"
7: "determine_locale()"
8: "load_plugin_textdomain()"
9: "Freemius::_load_textdomain()"
10: "do_action('plugins_loaded')"

The blanked out part above is a custom function of mine that checks for an SSO cookie and autologins the user if the cookie is valid. Here it is, simplified:

add_action('determine_current_user', function ( $user_id ) {
	$found_user = get_user_from_sso_cookie();
	if ( !empty( $found_user->ID ) ) {
		error_log( '<sso> LoggedIn: '.esc_attr( $found_user->user_email ) );
		wp_set_current_user( $found_user->ID );
		wp_set_auth_cookie( $found_user->ID );
		do_action( 'wp_login', $found_user->user_login, $found_user );
		return $found_user->ID;
	}
});

Note that the plugins works correctly apart from this error, I can still switch between users just fine, I just get this error in my logs from time to time. Ping me if you need more information, and thanks for your work on this plugin.

Cheers,
Thomas

permanent error by returning from switched user

Bug details:

An error of type E_ERROR was caused in line 721 of the file /usr/www/users/buchbim/neu/wp-content/plugins/user-switching/user-switching.php.

Error message: Uncaught TypeError: Argument 1 passed to user_switching :: filter_removable_query_args () must be of the type array, null given,
called in /usr/www/users/buchbim/neu/wp-includes/class-wp-hook.php on line 287
and defined in /usr/www/users/buchbim/neu/wp-content/plugins/user-switching/user-switching.php:721

Stack trace:

#0 /usr/www/users/buchbim/neu/wp-includes/class-wp-hook.php(287): user_switching->filter_removable_query_args(NULL)
#1 /usr/www/users/buchbim/neu/wp-includes/plugin.php(206): WP_Hook->apply_filters(NULL, Array)
#2 /usr/www/users/buchbim/neu/wp-includes/functions.php(1216): apply_filters('removable_query...', Array)
#3 /usr/www/users/buchbim/neu/wp-content/plugins/user-switching/user-switching.php(804): wp_removable_query_args()
#4 /usr/www/users/buchbim/neu/wp-content/plugins/user-switching/user-switching.php(268): user_switching::remove_query_args('https://neu.buc...')
#5 /usr/www/users/buchbim/neu/wp-content/plugins/user-switching/user-switching.php(209): user_switching::get_redirect(Object(WP_Us ....

My software is:
WordPress version 5.5.3
Current theme: GeneratePressChild (Version 1.0)
Current plugin: User Switching (Version 1.5.6)
PHP version 7.4.13

Else I like your plugin very handy. Thanks for it. Cheers KaTho

Ampersands are removed from URLs when switching to a user

Here is an example URL: https://www.example.com/checkout/order-pay/100/?pay_for_order=true&key=order_x82jhsx71

Then after switching the user the URL changes to: https://www.example.com/checkout/order-pay/100/?pay_for_order=true&user_switched=true

This breaks the location each time returning a 404 because the &key=order_x82jhsx71 is missing. I've looked at the code for quite some time and can't find out what is causing this.

Any help would be appreciated :)

Not working on production

It does not working on the production website. Instead it works for the staging environment (same server).

How can I debug it?

Thank you so much!

Best practices for including user switching functionality in another plugin?

We're discussing adding user switching links to Restrict Content Pro when this plugin is active.

Do you have any suggestions as far as best practices for implementing something like that, and/or are you aware of another plugin that has already done so? I'm sure I could muddle through it, but you're obviously the expert, so I decided to check with you first 😄.

Consider using $_GET instead of $_REQUEST

First, thank you for this great plugin.

I opened a support ticket on WordPress that I will put here:

We’ve used the plugin on our website for 2 year… never had an issue.

A couple of days ago, we updated the following plugins:
WooCommerce => 5.3 to 5.4.1
Admin Columns Pro => 5.5.2
Storefront => 3.7.0

Since then, clicking on “Switch to” produces 404 not every time… but really often.

I’ve noticed that when the 404s happen, the plugin did not create the cookies (wordpress_user_sw_*).

There are absolutely no errors in debug.log.

I’ve created a dev website but cannot reproduce, at this point, the error.

Any tips or suggestions about that problem? Are we the only ones?

Your answer:

Is the 404 shown for the wp-login.php page or for the page you get redirected to afterwards?

Not sure what the problem could be but the best advise is to try deactivating your other plugins one by one to see if you can identify the culprit.

My answer:

I see this link in the address bar:
https://xxxxxx/wp-login.php?action=switch_to_user&user_id=1145&nr=1&_wpnonce=d6b4fb8b2

… and the 404 page is displayed.

When it works, we are redirected to WooCommerce account page.

I thought updating a plugin had fixed the issue. It did not.

I did more digging and edited your code to trace it as this happens only on a production server I do not control. In dev, I wasn't able to reproduce the problem:

image

Then, I tried a "Switch to" and logs showed this:

image

The line in red should have triggered step 2 and step 3 in the logs. It did not.

I replaced all $_REQUEST by $_GET in the code. So far, the problem is gone... but I will try it for multiple days before claiming it really solved it.

Any reason why you are using $_REQUEST instead of $_GET?

Switch back fails when Wordpress is in subdirectory and using custom login page

I'm using Bedrock which puts Wordpress in it's own subdirectory. Part of this setup is to change the site url like the following:

define('WP_SITEURL', "http://site.com/wp");

This works fine along with the user swithing plugin. However I have also defined a custom login page by implementing the login_url filter.

So instead of the built-in login page http://site.com/wp/wp-login.php my login url becomes http://site.com/login and shows a custom post with a login form.

This combination breaks the "Switch back" functionality in this plugin. The reason seems to be that the auth cookie saved by the plugin uses SITECOOKIEPATH to save the cookie. Since this value is fetched from the WP_SITEURL setting, the value becomes /wp. So the cookie is only saved in the /wp path, but my login page is in the root path. This means that the user switching auth cookie (prefixed wordpress_user_sw_) is not available in the login page and the user switch fails with a "Could not switch off." message.

Changing all instances of SITECOOKIEPATH in your plugin to COOKIEPATH fixes the problem since COOKIEPATH is fetched from the home_url setting. But I guess there is some reason for the auth cookie to be saved in SITECOOKIEPATH while the old_user cookie is saved in COOKIEPATH?

Another solution would be to add a filter in you plugin so that it was possible to override the url used for switching from wp_login_url() to something else. if you think that sounds like a good idea I'd be happy to send a pull request.

Or maybe this could be solved in some other way?

Running into an issue where User-switching interferes with Woocommerce

When ever we leave user switching enabled and try add a product to cart (which fails) we see this error in the php log:

[02-Jun-2019 17:22:16 UTC] PHP Fatal error:  Uncaught TypeError: Argument 2 passed to user_switching::filter_user_has_cap() must be of the type array, string given, called in /home/okanag8/public_html/wp-includes/class-wp-hook.php on line 286 and defined in /home/okanag8/public_html/wp-content/plugins/user-switching/user-switching.php:817
Stack trace:
#0 /home/okanag8/public_html/wp-includes/class-wp-hook.php(286): user_switching->filter_user_has_cap(Array, 'edit_other_vend...', Array, Object(WP_User))
#1 /home/okanag8/public_html/wp-includes/plugin.php(208): WP_Hook->apply_filters(Array, Array)
#2 /home/okanag8/public_html/wp-includes/class-wp-user.php(768): apply_filters('user_has_cap', Array, 'edit_other_vend...', Array, Object(WP_User))
#3 /home/okanag8/public_html/wp-includes/capabilities.php(649): WP_User->has_cap('edit_post', 103182)
#4 /home/okanag8/public_html/wp-content/plugins/js_composer/include/classes/core/access/abstract-class-vc-access.php(105): current_user_can('edit_post', 103182)
#5 /home/okanag8/public_html/wp-content/plugins/js_comp in /home/okanag8/public_html/wp-content/plugins/user-switching/user-switching.php on line 817

User switching fails on certain providers

I have your awesome plugin running on several different hosts and it works great! Recently, we were hired to do a site on Dreamhost. For whatever reason, the user switching works some of the time and not the others. Specifically, logged in as an admin, I go to switch to a user and it takes me to the admin Dashboard saying switched to my admin account. I find this usually happens after I switch to one user, go back to myself and then try to go to another. If I wait a few minutes, it works.
Any thoughts as to why this may be?

Everything is the most current version.

Switch back function not working.

Hi,

I am having a problem with switching back from the customer account to the admin dashboard. When I click the switch back at the bottom of the web page, the page flashes briefly and the switch back link is gone but I am still on the customer's profile. I have tried disabling all plugins and turned off cloudflare but the plugin is still not working. Sometimes, the "The link you followed has expired." error has also popped up after trying to switch back or we end up in a loop where we keep getting redirected back to the wordpress my-account page, Currently we are using a run-around solution where we use the toolbar at the top to return to the dashboard, but this is only a temporary solution. We do not want to enable the toolbar for everyone, only the admins. Could it be a cookies or a caching problem? Please help.

Add option to permanently switch users

I'm not sure how many people would find this useful, but sometimes I wish there was an option to make the current switch permanent, e.g. in the notice displayed by user_switching::action_admin_notices().

This could be a simple link that, when clicked, calls user_switching_set_olduser_cookie() to remove information about the old user.

There could also be a new $permanent param for switch_to_user().

If such an addition doesn't sound like a good fit for the plugin, a filter inside user_switching::action_admin_notices() could already be useful to add such a link on my own.

PHP notice when user-switching is used together with WooCommerce and MailPoet

Over at MailPoet, we got a report from one of our users that they get the following PHP notice when using user-switching to switch to a different user:

PHP Notice: get_cart was called <strong>incorrectly</strong>. Get cart should not be called before the wp_loaded action.

Backtrace:

require('wp-load.php'), 
require_once('wp-config.php'),
require_once('wp-settings.php'), 
do_action('init'), 
WP_Hook->do_action, 
WP_Hook->apply_filters, 
user_switching->action_init, 
switch_to_user, 
do_action('switch_to_user'), 
WP_Hook->do_action, 
WP_Hook->apply_filters, 
user_switching->forget_woocommerce_session, 
WC_Session_Handler->forget_session, 
wc_empty_cart, 
WC_Cart->empty_cart, 
do_action('woocommerce_cart_emptied'), 
WP_Hook->do_action, 
WP_Hook->apply_filters, 
MailPoet\AutomaticEmails\WooCommerce\Events\AbandonedCart->handleCartChange, 
WC_Cart->is_empty, 
WC_Cart->get_cart, 
wc_doing_it_wrong 

Please see <a href="https://wordpress.org/support/article/debugging-in-wordpress/">Debugging in WordPress</a> for more information. (This message was added in version 2.3.) in /var/www/html/wp-includes/functions.php:5311

user-switching calls WC_Session_Handler::forget_session() before the wp_loaded action which per se is not a problem (https://github.com/johnbillion/user-switching/blob/develop/user-switching.php#L852). But when this method is called, WooCommerce calls WC_Cart::empty_cart() and MailPoet hooks into an action that is executed inside this method to call WC_Cart::get_cart(). That is why the user is seeing the notice.

I don't have a strong opinion on whether this should be fixed in WooCommerce, MailPoet, or User Switching. If we fix this in MallPoet, there is a chance that other users will see the same notice when WooCommerce and User Switching are used in combination with another plugin. I found another user reporting the same, but they didn't provide enough details for us to tell if they were using MailPoet or something else: https://wordpress.org/support/topic/get-cart-should-not-be-called-before-the-wp_loaded-action/

I'm opening this issue to start a conversation and to ask if, in your opinion, it would make sense to change User Switching to execute user_switching::forget_woocommerce_session() after wp_loaded?

Shop Managers getting 502 error on Switch To - Admins do not

Hello - I'm sorry to say, I don't know what was updated recently, but the site is currently operating with all up-to-date plugins, and Woo Commerce Shop Managers trying to use User Switching is now failing with 502 Bad Gateway error. Server logs aren't showing anything obvious that I can say relates to these events.

Add 'Switch Back' link to the dodgy network site listing screen

On Multisite, it's possible to end up on the dodgy site listing screen which says:

You attempted to access the "{site}" dashboard, but you do not currently have privileges on this site.

There should be a Switch Back link on here. Need to check to see if there are any hooks on this screen. Might not be. Add one to core if so.

Sessions not cleared

Could it be that after few switches I have multiple sessions?

Using User Session Control.

Logout not possible after switching & deleting users

Just noticed this behaviour on a site running WordPress 4.3 und User Switching 1.0.7.

There were only two users registered on the site, A and B (both admins). What I did:

  1. Log in as user A.
  2. Switch to user B.
  3. Delete user A.

What I expected:

Either to be logged out completely, or the switch being permanent.

What happened:

Immediately after deleting user A, the User Switching notice in the admin was saying "Switched to B. Switch back to B." After reloading the page it now only says "Switch back to B."

The problem:

I cannot log out anymore. Everytime I try to log out (not Switch Off!), the login form says "You are now logged out. Switch back to B."

Only deleting the cookies helps.

Changing users on the front end

Hello, I'm trying to use the plugin on the front end, because I have a restricted area and wanted to leave it available for the user to do this.

I have any tips on how I can do?

Sorry about my English.

Capability switch_users doesn't exist by default

We have a user role "Shop Manager" for those who actually manage orders for customers. They do not have full admin privileges.

After 1.4.0 they did not see "Switch to" link anymore.

Long story short it turns out that their account needed "switch_users" capability which however did not exist. After we created that capability and enabled it for the user role Shop Manager they can now user Switch to feature again.

It was not at all obvious that a "switch_users" capability must be created manually in order for this functionality to work.

Is there a specific change from version 1.3.1 to 1.4.0?

I use a plugin called WooCommerce Phone Orders & Manual Orders. After updating User Switching from 1.3.1 to 1.4.0, the switch user ability in this other plugin fails. However, reverting to User Switching 1.3.1 (or even deleting the plugin entirely) doesn't fix the issue.

Is there something about the functions in User Switching that might cause it to interfere with plugins that also make use of the switch_to_user hook? Since reverting and deleting didn't correct the issue, I assume it could be a cookie or database change that isn't reverted on deletion of the User Switching plugin.

Thank you for your help

Strange Error

WP_Admin_Bar::add_node was called with an argument that is deprecated since version 3.3! Use my-account as the parent for the switch-off admin bar node instead of my-account-with-avatar.

I saw there was a wordpress.org forum post about this a few years ago and it looked to be resolved but I ran into it today. I'm using the TGM Plugin Activation Class to install some plugins and I noticed this error was being thrown on the pages that TGM generates. I've gone into TGM to try and figure this out, but it seems that TGM is only calling WP_List_Table.

Thanks to your other handy plugin, Query Monitor, I can see the stack trace is going back to user_switching->action_admin_bar_menu() which doesn't make a lot of sense since the admin bar is supposed to be decoupled from the admin page's content (right?). Calling remove_action on that function gets rid of the error. I can add it to the plugin I'm working on but it seems like a better approach to find the root cause whatever it is. :)

Do you know what causes that error to be thrown? I tried to make sense of the logic but I'm afraid I don't know enough about WP's past to make sense of what's happening.

Great plugin btw :) not a super huge issue, just curious is all!

No 'Switch back to ...' links

I've updated to 1.0 across all my sites, it's working fine on most, but on some sites when I switch into a user I see the 'Switched to...' message but there is no 'Switch back to...' link, either in the message div or in the toolbar user dropdown. The message div is also not showing the icon.

I put some error logging in there and it looks like the conditional in the user_switching_get_olduser_cookie function is always failing.

I've cleared out all cookies and tried again, but it's still not working.

I can see that the two cookies (wordpress_user_switching_HASH and wordpress_user_switching_olduser_HASH) both exist and neither is empty.

PHP 5.4 strict

Notice Undefined offset: 0 on line 519 in file /var/www/clients/client1/web2/web/wp-content/plugins/user-switching/user-switching.php
which relates to
public function filter_map_meta_cap( array $required_caps, $cap, $user_id, array $args ) {
in the code.

Just to let you know.

Hook to filter the switch to user link

Need a filter to sort the switch to URL link, So if we custom admin do not want to allow the switch to this user role or user then they can use this filter

Critical Error

Hi
I have started to experience critical errors with User Switching activated. I also have Woocommerce, Woo Product Vendor and Woo Bookings installed. When I create a booking and click book now (effectively adding to basket) the critical errors happens. If I deactivate User Switching it works ok. I have a dump of the log below. any ideas please?
cheers
Tim

Using User Switching 1.5.4
Woocommerce 3.9.2
Woo Bookings 1.15.15
Woo Product Vendor 2.1.28

[02-Apr-2020 09:43:46 UTC] PHP Warning: Invalid argument supplied for foreach() in /home/c11pluss/public_html/wp-content/plugins/woocommerce-memberships/includes/class-wc-memberships-capabilities.php on line 71
[02-Apr-2020 09:43:46 UTC] PHP Fatal error: Uncaught TypeError: Argument 2 passed to user_switching::filter_user_has_cap() must be of the type array, string given, called in /home/c11pluss/public_html/wp-includes/class-wp-hook.php on line 288 and defined in /home/c11pluss/public_html/wp-content/plugins/user-switching/user-switching.php:867
Stack trace:
#0 /home/c11pluss/public_html/wp-includes/class-wp-hook.php(288): user_switching->filter_user_has_cap(Array, 'edit_other_vend...', Array, Object(WP_User))
#1 /home/c11pluss/public_html/wp-includes/plugin.php(206): WP_Hook->apply_filters(Array, Array)
#2 /home/c11pluss/public_html/wp-includes/class-wp-user.php(780): apply_filters('user_has_cap', Array, 'edit_other_vend...', Array, Object(WP_User))
#3 /home/c11pluss/public_html/wp-includes/capabilities.php(658): WP_User->has_cap('edit_post', 27230)
#4 /home/c11pluss/public_html/wp-content/themes/focusblog/inc/extra/meta-options.php(32): current_user_can('edit_post', 27230)
#5 /home/c11pluss/public_html/wp-includes/class-wp-hook.php(290): thrive_save_post_f in /home/c11pluss/public_html/wp-content/plugins/user-switching/user-switching.php on line 867

Can I disable this for Admins and Leave open for WOO Accounts?

Plugin Works Flawlessly - however, just a quick question about - limits - disabling it from admins that might be working on something sensitive - like inventory.

example - I have 2FA installed for my peace of mind.

again for the woo store - 5.7.2. WP it works like a beast - great plugin high 5!
any feedback would be appreciated.

Release post lock when switching user from the post editing screen

A slightly annoying behaviour is that when switching users from the post editing screen, the post lock isn't released and you see the "This post is being edited by X" modal, and you need to take over.

User Switching should release the post lock when switching between users from this screen.

Steps to reproduce:

  • Switch to a user
  • Edit a post
  • Use the "Switch back" option in the user profile menu
  • See the post locked modal

Intermittent patchy performance

I'm not sure if this is a bug with this plugin, an issue with BuddyPress/BBPress, a multi-site thing, or a cache problem. So I'm just mentioning it in case there is anything that can be done.

When I switch users some areas of the main site, BBPress and BuddyPress mostly, show me as logged out. Hitting refresh a lot or adding in a random query string busts the problem (which makes me think it could be a cache issue). However, some pages do this more than others. For example, the forum overview is almost always "logged out" while the forums themselves - only sometimes.

As I say, I cannot be sure this is a problem at this end of things but the issues only show up while I am using this plugin to act as another user.

Switching back fails when using BackupBuddy from iThemes

Hi
Thanks for a great plugin that I enjoy using.
But I noticed that iThemes BackupBuddy (or iThemes Security PRO) is preventing me from returning back to my admin account.
The combination with Yoast SEO Pro could also be an issue - due to the roles mix match! (Yoast creates some new roles "SEO Admin" etc. and mixed with Danish and some other language - iThemes interpret them different? So normal access "No Role on this Board" or subscriber becomes "SEO Admin" !!!

Have a good one

Netz

Sorry to open another ticket - this is not server config related

Hello - I'm sorry to say, I don't know what was updated recently, but the site is currently operating with all up-to-date plugins, and Woo Commerce Shop Managers trying to use User Switching is now failing with 502 Bad Gateway error. Server logs aren't showing anything obvious that I can say relates to these events.

Crucially, we have users like me, who can login as admin, switch users perfectly, and it works, but if I switch to Shop Manager and try to use Switch To, I'll get the exact same error.

If it's server config, why would it only affect Shop Manager roles?

Thanks

Error 502

I am using cloudflare on my Dokan marketplace and when trying to switch to a vendor i have a 502 gateway error. Any specific parameters to add on cloudflare or have you ever experienced such issue ?

Even if I switched the user, I remain the origin user

Hi John,

using your plugin User Switching for years on our internal corporate page (Intranet) with pleasure as it made my work supporting the users easier. Unfortunately, the plugin is not working on our page since some months and I do not exactly why.

Description:

  • Going to the user section and click for another user on "Switch to" (Screenshot 1)
  • The page reloads, no error message and in WP Admin on the top of the page, there appears the message "Switch back to my user", so it seems that it worked, but, looking on the wpadminbar and also the current user infos, it's my origin user shown and not the user I switched to (Screenshot 2)

001

002

As I've tested your plugin in a clean wordpress installation, it worked well there and I assume that the reason is linked to my page. So I switched to a WP Origin Theme, disabled all plugins (with exception of ACF Pro and your up-to-date User Switching), removed all users with exception of mine and another for testing purposes. But it's still the same situation.

Don't know how to proceed. Do you have any ideas what might be the reason?

Best
Oli

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.