I’ve set up a database table like so:
table: group
id name subGroupOf
1 grandparent NULL
2 parent 1
3 child 2
Here’s what I’m trying to do in php:
When a user accesses a page, the page tells the auth() function they require ‘child’ permissions. Because ‘child’ is a subgroup of ‘parent’, members of both groups should get permission. But, parent is a subgroup of ‘grandparent’, so members of all three groups should have access.
Since there is no limit to how many subgroups could be nested, I knew I would need a loop. But I’m totally drawing a blank.
I know it needs to check if the group is a subGroupOf, and if so, validate the parent group. Here’s what I have so far:
// Get group of current user
$group = mysqli_query($cxn,'SELECT group FROM user WHERE id='.$userID);
// Compare group to permissions
if($group == $permissions)
return TRUE;
// Check if group is a sub group
$subGroupOf = mysqli_query($cxn,'SELECT subGroupOf FROM group WHERE id="'.$group.'"');
if($subGroupOf == NULL)
{
// Wrong permissions
}
// Check if the parent group matches permissions
if($subGroupOf == $permissions)
return TRUE;
Somehow I need to loop that last part, and stop when it gets to
$subGroupOf == NULL
I’m fairly new to programming, so I’m still figuring out the logic…Any ideas? I don’t need the whole thing written for me (that code is summarized anyways), I just need help figuring out the structure..
Another method, but you still need a function for recursion:
The function
Calling the function, and checking against permissions: