Ok. Here is an explained solution of how to get the lowest user permission value for specific user (many thanks to
@Lukas W. The instructions below are based on his code).
----
Imagine we have a User entity under
$user
variable.
First of all, we need to get all users groups our user belongs to:
PHP:
$userGroups = $user->secondary_group_ids;
$userGroups[] = $user->user_group_id;
User permission values are stored in
xf_permission_entry
table. That is why we need to create a
XF:PermissionEntry
finder:
PHP:
/** @var Finder $permissionFinder */
$permissionFinder = \XF::finder('XF:PermissionEntry');
Now, it is time to specify what exactly we are looking for in the table:
PHP:
$permissionFinder
->where('permission_group_id', '**PERMISSION_GROUP_ID**')
->where('permission_id', '**PERMISSION_ID**')
->whereOr([
['user_group_id', $userGroups],
['user_id', $user->user_id]
]);
Change
**PERMISSION_GROUP_ID**
and
**PERMISSION_ID**
to a desired values.
Next, we need to retrieve selected entities from the database:
PHP:
$permissions = $permissionFinder->fetch();
Finally, we want to store the lowest permission value in
$lowestValue
variable. In order to do so, we are to iterate
$permissions
:
PHP:
// Getting the first permission value
$lowestValue = $permissions->first()->premission_value_int;
// Iterating retrieved permissions
/** @var PermissionEntry $permission */
foreach ($permissions as $permission)
{
// If current permission int value is lower than $lowestValue, assign it to $lowestValue
$lowestValue = min($lowestValue, $permission->permission_value_int);
}
That's all! You now have the lowest int permission value for current user in
$lowestValue
variable.
Keep in mind, that users can choose "No limits" option instead of providing an int value. "No limits" option equals -1. If you want to work with positive (+ zero) integers only, you will need to perform an additional check after iterating the array:
PHP:
if ($lowestValue === -1)
{
$lowestValue = 0;
}
A zero (0) integer permission value is treaded as default "No" permission value by XenForo and, therefore, will not appear in $lowestValue
. Make sure to use "No limits" option instead of using 0 as value!