Page 1 of 4

Add phpBB users to custom WordPress groups/roles

Posted: Thu May 03, 2018 1:54 pm
by mLgz0rn
Is there any way to do so when you give users a custom group in phpbb then they also get into a custom role that I have created in wordpress?

Phpbb Groups:
Image

Wordpress Roles:
Image

Re: Add users to custom groups/roles

Posted: Thu May 03, 2018 3:49 pm
by axew3
Yes but not with actual plugin code only as it is. It consider only default phpBB groups and treated as explained on install help, and as you can see/understand on code below in a very easy matter (easy to be improved as you like).

The best you can hope is that this plugin you use in WordPress to create users groups/roles, is coded in the way that after these groups/roles, are also existent into the default WP roles array, that also at moment i ignore if it is possible, i should check, but if it is, then you could just
on file
class.wp.w3all-phpbb.php
inside
private static function verify_phpbb_credentials(){
where code:

Code: Select all

if ( $phpbb_user_session[0]->group_name == 'ADMINISTRATORS' ){
      	      
      	      $role = 'administrator';
      	      
            } elseif ( $phpbb_user_session[0]->group_name == 'GLOBAL_MODERATORS' ){
        
            	   $role = 'editor';
          	  
               }  else { $role = 'subscriber'; }  // for all others phpBB Groups default to WP subscriber
assuming that the var
$phpbb_user_session[0]->group_name
is stored in phpBB for group Raider as RAIDER (check this into yourDBprefix_groups -> group_name column)
you'll change the code above into:

Code: Select all

if ( $phpbb_user_session[0]->group_name == 'ADMINISTRATORS' ){
      	      
      	      $role = 'administrator';
      	      
            } elseif ( $phpbb_user_session[0]->group_name == 'GLOBAL_MODERATORS' ){
        
            	   $role = 'editor';
          	  
               }  elseif ( $phpbb_user_session[0]->group_name == 'RAIDER' ){
        
            	   $role = '????';
          	  
               }
               else { $role = 'subscriber'; }  // for all others phpBB Groups default to WP subscriber
check that in this code the role assignment where line
$role = '????';
you should change giving it the correct value
$role='related-wp-role';
... but this is ok if the logic above is in the way we have assume.

If not, that i assume will be, then it is also easy to do this instead:
where code:

Code: Select all

if ( ! is_wp_error( $user_id ) ) {
just after add something like:

Code: Select all

if ( $phpbb_user_session[0]->group_name == 'RAIDER' ){
// execute sql to update this user role to what you want
 }
add the code, that can be an easy sql insert instruction (or update it depend on how this plugin you use is done), to update/insert the user role/group into what needed.
You need to know in this case how the plugin you use archive these data to assign users/roles-groups.

Re: Add users to custom groups/roles

Posted: Fri May 04, 2018 9:48 pm
by mLgz0rn
Do I also need to change the

Code: Select all

        if ( $phpbb_user[0]->group_name == 'ADMINISTRATORS' ){
      	      $role = 'administrator';
      	      $u_role = 'a:1:{s:13:"administrator";b:1;}';
            } elseif ( $phpbb_user[0]->group_name == 'GLOBAL_MODERATORS' ){
            	   $role = 'editor';
            	   $u_role = 'a:1:{s:6:"editor";b:1;}';
               } else { 
               	$role = 'subscriber';  // for all others phpBB Groups default to WP subscriber
               	$u_role = 'a:1:{s:10:"subscriber";b:1;}'; 
               }          
Part?
Because I can't seem to get it to work.
The plugin for wordpress is called "User Roles and Capabilities" and I checked the database.
The new roles do get added into where the default roles are "wp_user_roles"

Re: Add users to custom groups/roles

Posted: Sat May 05, 2018 8:19 am
by axew3
Hi, ok i've just install on fly and all work as expected at point 1 (the best you can hope).
Steps i've do/check:
i have a test group in phpBB, named
Atestgroup
so i've move a test user named aguest to this Atestgroup group in phpBB AND i've set it as default group for this user.

So into WP, after installation of this plugin you use, i've create a group: you note that when you create a group you have the field
Role ID:
the role id, is so stored for each user as capabilities in WP, i've create one giving it role-id: custom-editor
you can check this value after,
in WP database -> usermeta -> [column] meta_key -> capabilities
how this value is stored for each user.
So i've change the code of class.wp.w3all-phpbb.php where:

Code: Select all

  if ( $phpbb_user_session[0]->group_name == 'ADMINISTRATORS' ){
      	      
      	      $role = 'administrator';
      	      
            } elseif ( $phpbb_user_session[0]->group_name == 'GLOBAL_MODERATORS' ){
        
            	   $role = 'editor';
          	  
               }  else { $role = 'subscriber'; }  // for all others phpBB Groups default to WP subscriber
into this:

Code: Select all

if ( $phpbb_user_session[0]->group_name == 'ADMINISTRATORS' ){
      	      
      	      $role = 'administrator';
      	      
            } elseif ( $phpbb_user_session[0]->group_name == 'GLOBAL_MODERATORS' ){
        
            	   $role = 'editor';
          	  
               }  elseif ( $phpbb_user_session[0]->group_name == 'Atestgroup' ){
        
            	   $role = 'custom-editor';
          	  
               } else { $role = 'subscriber'; }  // for all others phpBB Groups default to WP subscriber
I've delete the aguest user in WP and reactivated him in phpBB to test the addition in WP another time (because you know that when you delete an user in wp it become deactivated in phpBB).
So when aguest user logged in phpBB, and come in WP side, or login WP first time, it is correctly added into needed custom-editor group. Work OK.

Note:
$phpbb_user_session[0]->group_name == 'Atestgroup'
Atestgroup is the group_name assigned for this phpBB group on db table yourPrefix_groups.

You'll go to add as above, any groups you want in the same way adding more elseif:

Code: Select all

elseif ( $phpbb_user_session[0]->group_name == 'AnotherNamedphpBBGroup' ){
            	   $role = 'another-custom-role-id';
 }
NOTE: i've note also this: if i have an user, and it belong to registered or newly registered group, then i add him to another custom group, you see that it belong to all these groups BUT, the one resulting as it is, can be still registered or newly registered:
you need to click -> Make group default for member to set it as default group for this member!
Setup phpBB to assign the default group which the user belong to, as you want.

On this online example, if i'm not wrong, i've setup phpBB users to: be newly registered when they register, then they are moved into registered as default, after first post approved.

I think this is also the problem about this recurrent question:
"i setup widgets to retrieve posts based on phpBB user permissions, but this user can see posts from forums where he should not."

This things could be coded to be an option, but in the wild (and in the while) this is the joke to apply.

I've discovered a little bug, when an new user is added in WP coming from phpBB:
PHP Notice: Undefined variable: wp_lang_x_phpbb in wp46/wp-content/plugins/wp-w3all-phpbb-integration/class.wp.w3all-phpbb.php on line 997
going to fix it as soon.

Hope ive been clear
cheers

ps: the the file class.wp.w3all-phpbb.php has been patched to fix the php notice error, check it here
https://wordpress.org/support/topic/1-8 ... t-10249334

[EDITED]

Re: Add users to custom groups/roles

Posted: Sun May 06, 2018 11:06 pm
by mLgz0rn
Yeah seems to be working! thanks.
Just didnt know they had to actually login before the role would change in wordpress.

Re: Add phpBB users to custom WordPress groups/roles

Posted: Mon May 07, 2018 7:24 am
by axew3
If anything still obscure or that need to be adjusted let know.
Note: if you disable the user in WordPress (not delete), then you re-enable him in phpBB, then the user return to login in WordPress, it will be re-activated also in WordPress, but as subscriber. If you'll need the user re-activated with proper role (ok maybe this is a secondary not so important thing, maybe yes), then you'll need to change the code into class.wp.w3all-phpbb.php file, where this code:

Code: Select all

  if ( is_multisite() ) {

	 	 $wpu_db_utab = (is_multisite()) ? WPW3ALL_MAIN_DBPREFIX . 'usermeta' : $wpdb->prefix . 'usermeta';
	 	 $subscriber = 'a:1:{s:10:"subscriber";b:1;}';
	   $wpdb->query("UPDATE $wpu_db_utab SET meta_value = '$subscriber' WHERE user_id = '$user_id' AND meta_key = 'wp_capabilities'");

	  
  } else {
	 	 // user should be re-activated with proper role maybe: subscriber as default as it is 
	 	 // here the db tab will be the one of the site, the user will login first (or come as logged)
	 	 // so not switch the db table prefix here
	 	    $wpu_db_utab = $wpdb->prefix . 'usermeta';
	 	    $subscriber = 'a:1:{s:10:"subscriber";b:1;}';
	      $wpdb->query("UPDATE $wpu_db_utab SET meta_value = '$subscriber' WHERE user_id = '$user_id' AND meta_key = 'wp_capabilities'");
	    }
This "notable" topic and your previous have been added to this list:
Advanced How to phpBB WordPress integration "notable" topics list