1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Return secondary usergroup a current user is a member

Discussion in 'XenForo Development Discussions' started by LPH, Feb 10, 2014.

  1. LPH

    LPH Well-Known Member

    The following returns the usergroup ID but now I'd like to match the current user logged in with their secondary usergroup.

    Code:
    $user_groups = explode(',', $secondary_group_ids );
    $user_groups[]= $user_group_id;
    

    Basically ... along this line of thinking....

    If the currently logged in user is a member of usergroup 2 then do something.

    Is this possible?
     
  2. Chris D

    Chris D XenForo Developer Staff Member

    PHP:
    $userModel XenForo_Model::create('XenForo_Model_User');
    if (
    $userModel->isMemberOfUserGroup($user2true))
    {
        
    // Do something
    }
    $user is your user which must include the user_group_id and secondary_group_ids.

    2 is the user group you want to check. This can actually also be an array of usergroup IDs.

    It's worth noting that the "true" in the callback is un-needed here, it is true by default. i.e. without it it will check the user against secondary usergroups. If you set it to false, however, it would only check primary groups.
     
    Aayush and LPH like this.
  3. LPH

    LPH Well-Known Member

  4. LPH

    LPH Well-Known Member

    I'm sorry. I'm staring at the code and thinking ... how do I get the secondary usergroup for the current user? :)

    In copying the code, the following error was returned:

    Code:
    Catchable fatal error: Argument 1 passed to XenForo_Model_User::isMemberOfUserGroup() must be of the type array, null given, called in /path/to/wp-content/plugins/xenword-user-info.php on line 43 and defined in/path/to/community/library/XenForo/Model/User.php on line 1659

    Am I changing the 2 to a "6" or "5" or "7"?
    Do I define a variable?

    Update: And I tried:

    Code:
    echo '<br />The XF secondary usergroup' . strpos( $user['secondary_group_ids'] ) . '<br />';
    But that returned NULL.
     
    Last edited: Feb 11, 2014
  5. Vincent Gabriel

    Vincent Gabriel Active Member

    Dump the $user variable it seems that it's empty. it can be either there is no logged in user or you didn't pull the user info. Looking at the the error code and line if you are trying to incorporate this into a third party script try using the XenForo SDK.
     
    LPH likes this.
  6. LPH

    LPH Well-Known Member

    The plan was to start using the SDK after this latest version of the script was finished but I'm tracking down a really nasty issue with matching the secondary usergroup and WordPress roles.
     
  7. LPH

    LPH Well-Known Member

    Code:
            $user_groups = explode( ',', $secondary_group_ids );
            $user_groups[] = $user_group_id;
            $user_roles = array();
            foreach( $user_groups as $user_group ) {
                $user_roles[] = $XF->options['xf_user_role'][$user_group];
                wp_die( 'This is the role:' . var_dump( $XF->options['xf_user_role'] ) );
            }
    
    returns

    Code:
    array(7) { [3]=> string(13) "administrator" [4]=> string(6) "author" [2]=> string(10) "subscriber" [1]=> string(10) "subscriber" [5]=> string(6) "author" [7]=> string(11) "contributor" [6]=> string(6) "editor" }
    
    Now I just need to figure out how to get the current user's user_group, return the paired role in quotes, then set_role ('').

    I can pencil it out, write about it here, but the code escapes me. I've tried different ways to get the current user group. I realize @Chris Deeming gave a huge push in the right direction with public function isMemberOfUserGroup(array $user, $userGroupId, $includeSecondaryGroups = true) in User.php ...

    Update: Almost there !
    Code:
    $XF->options['xf_user_role'][$user_group]
    Returns string(13) "administrator" for the user with that XF secondary group. ... so I need to strip out the string(13) as well as the quotes ...
     
    Last edited: Feb 11, 2014

Share This Page