使用 WP Discourse SSO 管理 Discourse 中的群组成员

Holy :cow:! How’d you manage to find it!? Nice work.

By adding log statments to my development site. This should be an easy fix, but I’d rather not push the change to WordPress before I test it out some more.

3 个赞

Sure you have a reasonable development environment. Like a real developer. That I understand (in spite of my not having such!) But even thinking to try usernames that had changed… I guess the 2 was the clue.

Here’s a slightly different method to handle Paid Memberships Pro group sync. Pop this into your functions.php file. Expected behavior is add upon subscribe and remove upon cancellation. Thanks to @dfriestedt for sponsoring.

https://github.com/justindirose/dc-pmp-group-sync

8 个赞

Awesome, anything stopping this being wrapped into a plugin for customers who aren’t confident editing PHP?

1 个赞

No, probably not, but PHP/WP development isn’t my forte, and I don’t plan on personally actively maintaining this code long-term.

3 个赞

I am implementing the PMPro code above and am hung up on something. My understanding is that this code only fires when users buy subscription or cancel a subscription.

Is there a way that I can auto-sync users as they are created on Discourse? In my case, I have some users who are subscribers but don’t have a Discourse accounts (will be created via SSO).

If wordpress is the SSO master then you can have it update groups at every login.

1 个赞

Is there a code snippet that shows an example of updating groups on WP login?

The add_user_to_discourse_groups function will create a Discourse user if it doesn’t already exist. If that function is being called for your users, then you should be fine. Where you might run into problems will be for existing WordPress users who got their membership before the code was added to your site. If that is the case, you will probably need to add the users to Discourse groups through the SSO parameters that are passed to Discourse on login.

To pass additional SSO parameters with the WP Discourse plugin, you can hook into the 'wpdc_sso_params' filter. The parameter you need to use to add users to a Discourse group is called add_groups. You can use it with something like this:

add_filter( 'wpdc_sso_params', 'wpdc_custom_sso_params', 10, 2 );
function wpdc_custom_sso_params( $params, $user ) {
    if ( /* some condition that returns true if groups should be added for the user */ ) {
        $params['add_groups'] = 'comma,separated,group,names'; // Don't use spaces between names.
    }

    return $params;
}
7 个赞

我意识到这只是示例代码,但它似乎有一个相当严重的错误。

如果付款未完成,并且会员资格状态为“token”

该组仍被添加到 discourse 用户中 :open_mouth:

我稍后会对此进行研究,但我觉得值得一提


1 个赞

我希望使用此代码将我的 Discourse 社区限于使用 Paid Memberships Pro 的付费会员。我有一个免费会员级别和两个付费会员级别。

我该如何修改此代码:Manage group membership in Discourse with WP Discourse SSO

以便付费会员能够访问社区。否则,他们将被重定向到付费会员注册页面。

谢谢。

为了方便我自己参考,这是您想要修改的代码:

add_action( 'wpdc_sso_provider_before_sso_redirect', 'wpdc_custom_check_user_membership', 10, 2 );
function wpdc_custom_check_user_membership( $user_id, $user ) {
    if ( /* some condition */ ) {
	    wp_safe_redirect( home_url() );

	    exit;
    }
}

您需要做的是将代码中的 /* some condition */ 注释替换为一个条件,该条件对于没有付费会员资格的用户返回 true。然后,这些用户可以被重定向到您的会员注册页面。

我目前在我的测试站点上没有安装 Paid Memberships Pro 插件,但根据他们的文档,您可以使用他们的 pmpro_hasMembershipLevel 函数来检查用户是否具有给定的会员级别:https://www.paidmembershipspro.com/documentation/content-controls/require-membership-function/。

要使用 pmpro_hasMembershipLevel 函数,您需要知道两个付费会员级别的 ID(或名称)。您可以从 pmp 的“会员级别”管理页面获取这些 ID。例如,如果您的两个付费级别的 ID 是 1 和 2,您可以使用以下条件:

if (! pmpro_hasMembershipLevel(array(1, 2), $user_id))

将其替换到代码中,将是:

add_action( 'wpdc_sso_provider_before_sso_redirect', 'wpdc_custom_check_user_membership', 10, 2 );
function wpdc_custom_check_user_membership( $user_id, $user ) {
    if (! pmpro_hasMembershipLevel( array( 1, 2 ), $user_id ) ) {
	    wp_safe_redirect( home_url() );

	    exit;
    }
}

您还需要更改的另一行是:

wp_safe_redirect( home_url() );

它目前设置为将用户重定向到站点的首页。您需要将其更改为重定向到您站点注册页面的路径或完整 URL:

wp_safe_redirect( /* path_to_your_signup_page */ );

请注意,我尚未测试此代码。如果您直接修改实时站点的 functions.php 文件,请确保您可以通过站点的后端访问站点的 functions.php 文件,以防代码中出现任何错误或拼写错误。

4 个赞

在此留下一个与 https://meta.discourse.org/t/manage-group-membership-in-discourse-with-wp-discourse-sso/74724#restricting-access-to-discourse-when-a-membership-doesnt-exist-4 相关的笔记。如果启用了 WP Discourse 的“登录时创建或同步 Discourse 用户”选项,则所有 WordPress 站点的用户都将自动创建 Discourse 帐户,即使他们通过 OP 中概述的方法无法登录 Discourse。有关如何阻止为不具有适当会员级别的用户自动创建帐户的详细信息,请参阅此帖子的第二部分:How to prevent some WP users from being able to login to Discourse - #2 by simon

基本上:

add_filter( 'wpdc_bypass_sync_sso', 'wpdc_custom_bypass_sync_sso', 10, 3 );
function wpdc_custom_bypass_sync_sso( $bypass_sync, $user_id, $user ) {
    if ( /* Some condition that returns true if the user doesn't meet the membership requirement */  ) {

        $bypass_sync = true;
    }

    return $bypass_sync;
}

我在此给自己设置一个提醒,以便将这些详细信息更新到 OP 中。

2 个赞

谢谢西蒙。我已按照您的说明为我的网站设置好了。

1 个赞

澄清一下。

无级别(非用户):0
免费级别 = 1
我有两个付费级别:2,3

你说

你需要做的是用一个返回true的条件替换代码中的/* some condition */注释,这个条件适用于没有付费会员的用户。然后,这些用户可以被重定向到你的会员注册页面。

要使用pmpro_hasMembershipLevel函数,你需要知道你的两个付费会员级别的ID(或名称)。你可以从pmp“会员级别”管理页面获取这些ID。例如,如果你的两个付费级别的ID是1和2,你可以使用以下条件:

if (! pmpro_hasMembershipLevel(array(1, 2), $user_id))

这些陈述是否矛盾?

根据我的理解:
我应该使用以下:

if (! pmpro_hasMembershipLevel(array(0,1), $user_id))

将没有付费会员的用户重定向到会员注册页面。

如果我错了,请纠正我。

谢谢。

如果条件返回 true,用户将被重定向到您的注册页面。

我认为最安全的方法是使用一个条件,当用户既没有级别 2 也没有级别 3 时返回 true

if (! pmpro_hasMembershipLevel(array(2, 3), $user_id))

1 个赞

谢谢。我错过了你第二个代码片段中的“!”号。

1 个赞