johnbillion/user-switching

WordPress中用户账户之间快速切换。

资助包维护!
johnbillion

安装数: 44 731

依赖项: 1

建议者: 0

安全: 0

星标: 187

关注者: 10

分支: 49

开放问题: 8

类型:wordpress-plugin


README

稳定标签: 1.8.0
测试到: 6.6
许可: GPL v2 或更高版本
标签: users, user switching, fast user switching, multisite, woocommerce
贡献者: johnbillion
捐赠链接: https://github.com/sponsors/johnbillion

WordPress中用户账户之间快速切换。

描述

此插件允许您通过点击按钮快速在WordPress中切换用户账户。您将立即注销并登录为所需用户。这对于帮助WooCommerce网站、会员网站、测试环境或需要在不同账户之间切换的管理员来说很有用。

特性

  • 切换用户:从“用户”屏幕立即切换到任何用户账户。
  • 切换回:立即切换回原始账户。
  • 切换关闭:注销账户但保留立即切换回的能力。
  • 与Multisite、WooCommerce、BuddyPress和bbPress兼容。
  • 与大多数会员和用户管理插件兼容。
  • 与大多数双因素认证解决方案兼容(有关更多信息,请参阅常见问题解答)。
  • 已批准在企业级WordPress平台(如AltisWordPress VIP)上使用。

注意:用户切换支持最高三年前的WordPress版本和7.4或更高版本的PHP版本。

安全

  • 只有具有编辑其他用户能力的用户才能切换用户账户。默认情况下,这是单站安装中的管理员,以及多站安装中的超级管理员。
  • 密码不会被(也不能)显示。
  • 在记住您切换的账户并切换回时使用WordPress的cookie身份验证系统。
  • 实施WordPress中的nonce安全系统,意味着只有那些有意切换用户的人才能切换。
  • 在适当的地方提供完整的用户会话验证支持。
  • 提供完整的HTTPS支持。
  • Patchstack漏洞披露计划支持

使用说明

  1. 访问WordPress中的“用户”菜单,您将在每个用户的操作链接列表中看到“切换到”链接。
  2. 点击此链接,您将立即切换到该用户账户。
  3. 您可以通过每个仪表板屏幕上的“切换回”链接或WordPress工具栏中的个人资料菜单切换回原始账户。

有关“切换关闭”功能的更多信息,请参阅常见问题解答

其他插件

我维护了几个为开发者准备的插件。查看它们

隐私声明

用户切换功能利用浏览器Cookie允许用户切换到另一个账户。其Cookie的工作机制与WordPress核心中的认证Cookie相同,这意味着它们的值包含用户的user_login字段明文,这应被视为可能涉及个人身份信息(PII),出于隐私和监管原因(GDPR、CCPA等)应予以处理。Cookie的名称为

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

用户切换功能不会向任何第三方发送数据,也不会包含任何第三方资源,将来也不会这么做。

有关用户切换时的隐私和安全问题,请参阅常见问题解答。

可访问性声明

用户切换旨在让所有用户都能完全访问。它实施了网络可访问性的最佳实践,输出语义和结构化的标记,遵循WordPress的默认样式和可访问性指南,在适当的情况下使用WordPress和浏览器提供的可访问性API,并通过键盘完全可访问。

当与WordPress的最新版本一起使用时,用户切换应遵循Web内容可访问性指南(WCAG)2.0 AA级别,其中其管理区域本身遵循这些指南。如果您在使用用户切换时遇到了或发现了可访问性问题,请在一个用户切换插件支持论坛上发帖,我将迅速处理。

屏幕截图

  1. 用户屏幕上的“切换到”链接
    The Switch To link on the Users screen
  2. 用户个人资料上的“切换到”链接
    The Switch To link on a user's profile

常见问题解答

此插件与PHP 8兼容吗?

是的,它正在积极测试,并已支持到PHP 8.3。

“关闭”是什么意思?

关闭会将您从账户注销,但保留您的用户ID在认证Cookie中,这样您可以直接切换回,而无需再次手动登录。这就像切换到无用户,并能够切换回来。

您可以在WordPress工具栏中的个人资料菜单中找到“关闭”链接。一旦您关闭了,您会在几个地方看到一个“切换回”链接

  • 在网站页脚中
  • 在登录屏幕上
  • 在“元”小工具中

此插件与WordPress多站点兼容吗?

是的,您还可以在网络管理员中的用户屏幕上切换用户。

此插件与WooCommerce兼容吗?

是的,您还可以在以商店管理员或管理员用户身份登录时,从各种WooCommerce管理屏幕切换用户。

此插件与BuddyPress兼容吗?

是的,您还可以从成员个人资料屏幕和成员列表屏幕切换用户。

此插件与bbPress兼容吗?

是的,您还可以从成员个人资料屏幕切换用户。

如果我的网站使用双因素认证插件,此插件还能工作吗?

是的,大部分情况下可以。

我所知道的一个例外是Duo安全。如果您使用此插件,您应该安装用户切换Duo安全插件,这将防止在您切换用户时出现双因素认证提示。

用户需要什么权限才能切换账户?

用户需要edit_users权限才能切换用户账户。默认情况下,只有管理员拥有此权限,在启用多站点的情况下,只有超级管理员才有此权限。

具体来说,用户需要编辑目标用户的能力,以便切换到他们。这意味着如果你已经设置了自定义用户能力映射,使用edit_usersedit_user能力来影响用户编辑他人的能力,那么用户切换应该尊重这一点。

多站点安装上的常规管理员能否切换账户?

不行。但可以通过安装常规管理员用户切换插件来启用此功能。

能否将切换账户的能力授予其他用户或角色?

可以。可以通过明确授予用户或角色switch_users元能力来允许他们切换用户,无论他们是否有edit_users能力。出于实用目的,用户或角色还需要list_users能力,以便他们可以访问WordPress管理区域中的用户菜单。

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 );

请注意,这需要在用户切换自己的能力过滤之前发生,因此优先级为9

能否从用户中拒绝切换账户的能力?

可以。WordPress中的用户能力可以设置为false以拒绝它们。拒绝switch_users能力将阻止用户切换用户,即使他们有edit_users能力。

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 );

注意

  • 这需要在用户切换自己的能力过滤之前发生,因此优先级为9
  • 目标用户的ID可以在$args[2]中找到。

我能否在我的插件或主题中添加自定义的“切换到”链接?

可以。使用user_switching::maybe_switch_url()方法来完成此操作。它负责认证,并返回一个用于切换到提供的用户账户的带nonce保护的URL。

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 )
		);
	}
}

如果您想指定用户切换后重定向到的URL,请在URL中添加一个redirect_to参数,如下所示

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 )
		);
	}
}

上述代码也适用于显示切换回原始用户的链接,但如果您想为此提供一个显式的链接,可以使用以下代码

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 )
		);
	}
}

我能否确定当前用户是否切换到了他们的账户?

可以。使用current_user_switched()函数来完成此操作。如果当前用户从另一个账户切换到当前账户,则返回一个表示原始用户的WP_User对象,否则返回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.
	}
}

我能否记录每次用户切换到另一个账户?

您可以安装审计跟踪插件,如Simple HistoryWP Activity LogStream,所有这些插件都内置了对用户切换的支持,并在用户切换到另一个账户时记录条目。

此插件是否允许用户为另一项操作定罪另一用户?

可能可以,但用户切换包含一些针对此情况的安全保护,并且作为网站管理员,您还可以采取进一步的预防措施

  • 您可以安装审计跟踪插件,如Simple HistoryWP Activity LogStream,所有这些插件都内置了对用户切换的支持,并在用户切换到另一个账户时记录条目。
  • 用户切换将切换到的用户的原始用户ID存储在新的WordPress用户会话中。尽管默认情况下,他们在随后切换回时,该会话不会持续,但如果您的数据库服务器启用了查询日志记录,则将记录此ID。
  • 用户切换将原始用户的登录名存储在认证cookie中(有关更多信息,请参阅隐私声明)。如果您的服务器访问日志存储cookie数据,则将为每个访问请求记录此登录名(以及IP地址)。
  • 用户切换触发动作,当用户切换账户、关机或切换回时(见下文)。根据您的需求,您可以使用这些动作进行额外的日志记录,以确保安全。

以上之一应允许您在用户切换账户时关联动作与原始用户,如果您需要的话。

请注意,即使不使用用户切换插件,任何能够编辑其他用户的用户仍然可以通过更改密码和手动登录该账户等方式,将其他用户构陷为某个动作。如果您担心用户滥用他人,那么在授予用户管理权限时应格外小心。

能否直接从管理员工具栏切换用户?

是的,有一个第三方插件可以做到这一点:管理员工具栏用户切换

当用户切换账户时,是否有任何插件动作被调用?

是的。当用户切换到另一个账户时,将调用 switch_to_user 钩子。

/**
 * 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 );

当用户切换回原始账户时,将调用 switch_back_user 钩子。

/**
 * 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 );

当用户关机时,将调用 switch_off_user 钩子。

/**
 * 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 );

当用户切换账户、关机或切换回时,将应用 user_switching_redirect_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 );

此外,当适用时,用户切换会尊重以下来自WordPress核心的过滤器:

  • 在切换到另一个用户时,使用 login_redirect
  • 在关机时,使用 logout_redirect

我该如何报告安全漏洞?

您可以通过Patchstack官方用户切换漏洞披露计划报告安全漏洞。Patchstack团队将帮助验证、分类和处理任何安全漏洞。

您接受捐赠吗?

我正在通过GitHub Sponsors计划接受赞助,您提供的任何支持都将帮助我维护此插件,使其对所有人免费。