• 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

lasertits

Active member
#1
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!
 

James

Well-known member
#2
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);
 

James

Well-known member
#3
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):
PHP:
	/**
	 * 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)
 

MGSteve

Well-known member
#4
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...
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.
 

Luke F

Well-known member
#5
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...
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
 

Scott

Active member
#6
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.
 

lasertits

Active member
#7
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
 

MGSteve

Well-known member
#8
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.
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
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?
 

Snog

Well-known member
#10
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?
Code:
$visitor = XenForo_Visitor::getInstance();
$user_Model = XenForo_Model::create('XenForo_Model_User');
$currentuser = $visitor->toArray();
$allowed = YOUR SECONDARY USERGROUP ID;

if($user_Model->isMemberOfUserGroup($currentuser,$allowed))
{
  ...your function here
}
 
Last edited:

Robert9

Active member
#12
Code:
$newGroupIds = 12;
$oldGroupIds = unserialize($user['secondaryGroupIds']);
XenForo_DataWriter_Helper_User::updateSecondaryUserGroupIds($user['user_id'],
$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?