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

Pulling secondary group id's from database

Discussion in 'XenForo Development Discussions' started by lasertits, Oct 26, 2011.

  1. lasertits

    lasertits Active Member

    Hopefully one of you many talented and knowledgable individuals on here can point me in the right direction on this. Basically I'm trying to figure out how the secondary groups for a user are stored in the database, and also how to get it back.

    I'm writing a plugin that will need to know what a user's secondary groups are in order to determine what level of access said user has, only issue is I can't figure out how to pull that info from the database.

    I suppose I could parse through the xenforo php and figure out how it enters the data in the code somewhere, but I'm hoping someone on here already knows or can provide some insight.

    I know in the xf_user_group_relation table it has user id's and their primary group id's, but not secondary.

    In xf_user it has a secondary_group_ids column but the data appears to be in hex value (see screenshot here: http://i.imgur.com/UMxYJ.png), might in turn be a bitmask or some other value that through an algorithm can be split into multiple digits...

    So again, to reiterate - how the heck do I get individual group id's from that secondary_group_ids field? :confused:

    Any ideas / help / suggestions is greatly appreciated. I really don't want to go through the php and try to figure this out if I don't have to. Thanks!
  2. James

    James Well-Known Member

    I'm mobile so I can't check but I thought the secondary group ids column was a serialised array.

    You can check if a user belongs to a usergroup by using XenForo_Model_User->isMemberOfUserGroup(array $user, array $ids, true);
  3. James

    James Well-Known Member

    After double-checking I was wrong. The secondary_group_ids column is a comma-separated list of the secondary group IDs, so to get them you'd just explode(',', $secondaryGroupIds);

    If you do want to just validate that the user is a member of a particular usergroup, you can use the isMemberOfUserGroup function, see (line 1367):
         * Determines if a user is a member of a particular user group
         * @param array $user
         * @param integer|array $userGroupId either a single user group ID or an array thereof
         * @param boolean Also check secondary groups
         * @return boolean
    public function isMemberOfUserGroup(array $user$userGroupId$includeSecondaryGroups true)
    Fuhrmann likes this.
  4. MGSteve

    MGSteve Well-Known Member

    Whichever program you're using to view the data interpreted the string field as a really big number and converted it to a hex display. As James said, they're in clear text (csv) in the db.
  5. Luke F

    Luke F Well-Known Member

    Wish they wouldn't store text in blobs like that :<

    Whatever rare encoding issue it solves isn't worth all the hassle of trying to work with the database while tools (correctly) show the data in hex format
    ragtek and jmurrayhead like this.
  6. Scott

    Scott Active Member

    The reason is serialized data isn't always UTF-8. PHP can add null bytes in when you serialize an object in PHP 5.3+. Also when its in text form people get tempted to edit the structure directly and bad things (TM) happen.

    The solution is just to pull out the row and unserialize it in PHP.
    Floris likes this.
  7. lasertits

    lasertits Active Member

    Thanks for all the input everyone, really appreciate it! You've definitely pointed me in a solid direction which I'm going to pursue in hopes I can pull this off without any hiccups. :D
  8. MGSteve

    MGSteve Well-Known Member

    Yeah, I noticed it doing the hex thing in phpMyAdmin today - In row view it shows the column data fine, go it edit it and its in Hex. The fields are varbinary rather than varchar, which as you say must be for UTF-8 compliance.
  9. Nnirvi

    Nnirvi Member

    Can someone explain the solution to this? I'm trying to get the secondary group of a user in my custom plugin. I'd like it to do stuff if user belongs to a certain secondary group. James probably has the proper function but how do I call it?
  10. Snog

    Snog Well-Known Member

    $visitor = XenForo_Visitor::getInstance();
    $user_Model = XenForo_Model::create('XenForo_Model_User');
    $currentuser = $visitor->toArray();
      ...your function here
    Last edited: Dec 30, 2014
  11. Nnirvi

    Nnirvi Member

    $visitor->toarray(); was the key here, thanks!
  12. Robert9

    Robert9 Active Member

    $newGroupIds = 12;
    $oldGroupIds = unserialize($user['secondaryGroupIds']);
    $newGroupIds, $oldGroupIds);
    My need is just to add another new group to a single user, and this is my solution.
    I adds the group to user.secondary_group_ids and adds a new set to xf_user_group_relation.

    Is there anything easy to add a subscription to xf_user_upgrade_active, please?

Share This Page