So i've just test a way to switch users roles/groups between wp and phpBB, and it is very easy to do in "one way", wp to phpBB (example: when an user switched from subscriber to editor, make him global moderator in phpBB, and the contrary etc).
I hooked it for the test into the
profile_update action, so into the phpbb_update_profile() function that fire when the user's update occur, but the code can be added to run without hooking it into
profile_update action, but maybe into
init.
This is especially true if we want some membership plugin like Memberpress interact with this feature. With something like (but there are many ways to achieve same or more complex results)
Code: Select all
if(current_user_can('mepr-active','rules:111')){
echo 'yes MEPR membership2 active, move this user to a phpBB group'; exit;
} else { remove the user from a group }
anyway, as said many times, it is very easy to do for default wp roles/phpBB groups (and it will be added on 2.6.8 as option) and specific scenarios, but to code something that can provide an UI where would be possible to setup options, is not so easy, and as said before i will not code something like this.
WP roles applies to an user, an user can be subscriber or editor or admin etc in wp, but in phpBB an user can belong to more than one group.
So what the code should do when an user is demoted from editor role in wp, to subscriber, and in phpBB the user belong to global admins group and some other group? Remove from these groups or not? May from someone and not some other. It can be complex as more we can imagine, mixing memberships and roles.
It is very easy to do per case, so it will be provided the basic that just will do +- this on code, based on WP roles:
Code: Select all
#$uid = $w3all_phpbb_connection->get_var("SELECT user_id FROM ".$w3all_config["table_prefix"]."users WHERE LOWER(user_email) = '$old_user_data->user_email'");
$phpbbug = $w3all_phpbb_connection->get_results("SELECT user_id, group_id FROM ".$w3all_config["table_prefix"]."users WHERE LOWER(user_email) = '$old_user_data->user_email'");
if(empty($phpbbug[0])){ return; } // if the user (still?) do not exist
$uid = $phpbbug[0]->user_id;
$ugid = $phpbbug[0]->group_id;
############################################
### START
# Roles -> Groups switches
// **** MemberPress: (do not know if it is a bug of the version i am testing) but when an user is set as NoRole in WordPress,
// set/leave the user as subscriber by the way.
// So that it is necessary another way to detect that the user may have no active mepr subscriptions/memberships and deactivate the user in phpBB if the case
// ** note that here, may you would add memberships from external wp plugins and more complex roles/groups/memberships switches
include( WPW3ALL_PLUGIN_DIR.'common/wpRoles_phpbbGroups.php' );
// before we check if this user has been set as NoRole in WP
if( empty($wpu->roles) )
{ // no role for this site, deactivate in phpBB
$w3all_phpbb_connection->query("UPDATE ".$w3all_config["table_prefix"]."users SET user_type = '1' WHERE user_id = '$uid'");
}
// if old user's data role do not match, then there is an user role change: update/activate in phpBB based on Roles/Groups array
// check that the user had a role before
if( empty($old_user_data->roles[0]) && !empty($wpu->roles) && !empty($wpRoles_phpBBGroups)
OR !empty($wpu->roles) && $old_user_data->roles[0] != $wpu->roles[0] && !empty($wpRoles_phpBBGroups) ) // **
{ // retrieve all groups which the user belong to (for more complex roles/groups switches)
$uid_groups = $w3all_phpbb_connection->get_results("SELECT * FROM ".$w3all_config["table_prefix"]."user_group WHERE user_id = $uid");
if( $wpu->roles[0] == 'subscriber' OR $wpu->roles[0] == 'contributor' )
{
if( $ugid != 2 )
{
$w3all_phpbb_connection->query("UPDATE ".$w3all_config["table_prefix"]."users SET user_type = '0', group_id = '2' WHERE user_id = '$uid'");
// on duplicate key is not possible, there is no index into the user_group table: remove the record if exist before to insert again
// even if the user_group table can contain duplicated values
// *** also note that here, may the phpBB user should be removed from any other group, which may belong to into phpBB
$w3all_phpbb_connection->query("DELETE FROM ".$w3all_config["table_prefix"]."user_group WHERE user_id = '$uid' AND group_id IN('2','$ugid')");
$w3all_phpbb_connection->query("INSERT INTO ".$w3all_config["table_prefix"]."user_group (group_id, user_id, group_leader, user_pending) VALUES ('2','$uid','0','0')");
}
} elseif( $wpu->roles[0] == 'editor' && $ugid != 4 )
{
$w3all_phpbb_connection->query("UPDATE ".$w3all_config["table_prefix"]."users SET user_type = '0', group_id = '4' WHERE user_id = '$uid'");
// same as above
$w3all_phpbb_connection->query("DELETE FROM ".$w3all_config["table_prefix"]."user_group WHERE user_id = '$uid' AND group_id IN('4','$ugid')");
$w3all_phpbb_connection->query("INSERT INTO ".$w3all_config["table_prefix"]."user_group (group_id, user_id, group_leader, user_pending) VALUES ('4','$uid','0','0')");
} elseif( $wpu->roles[0] == 'administrator' && $ugid != 5 )
{
$w3all_phpbb_connection->query("UPDATE ".$w3all_config["table_prefix"]."users SET user_type = '0', group_id = '5' WHERE user_id = '$uid'");
// same as above
$w3all_phpbb_connection->query("DELETE FROM ".$w3all_config["table_prefix"]."user_group WHERE user_id = '$uid' AND group_id IN('5','$ugid')");
$w3all_phpbb_connection->query("INSERT INTO ".$w3all_config["table_prefix"]."user_group (group_id, user_id, group_leader, user_pending) VALUES ('5','$uid','0','0')");
}
}
# if(current_user_can('mepr-active','rules:111')){
# # the user belong and is active on mepr Rule ID 111
# }
# Roles -> Groups switches
### END
############################################
in words:
if an user is promoted to author in wp, will be moved to global moderator group as primary group in phpBB, the previous primary group membership will be removed.
If an user is demoted from author to contributor or subscriber, will be moved to registered group in phpBB, the previous primary group membership will be removed.
Same goes for administrator.
All works so smooth, but what else can be considered?
1) should be the user removed from any other group which belong to in phpBB, when role updated in WP (so that the primary in phpBB update also, and should be the unique one?
Considering the one way, the default code will NOT remove the user from more groups, but only update the primary (which maybe is the unique one normally).
Set the user as deactivated when NoRole in WP.
2) About MemberPress:
// **** MemberPress: (do not know if it is a bug of the version i am testing) but when an user is set as NoRole in WordPress,
// set/leave the user as subscriber by the way.
// So that it is necessary another way to detect that the user may have no active mepr subscriptions/memberships and deactivate the user in phpBB if the case
will follow more logs...