Retrieve the permissions assigned to a user with the given user ID.
Location
users/helpers/permissions.php
Parameters
#
Parameter
Data Type
Required
Description
1
$user_id
integer
Yes
representing the ID of the user whose permissions need to be fetched
Returns
Data Type
Description of Returned Data
array of objects
It returns an array of permission objects, where each object contains information about a permission assigned to the user.
Example
fetchUserPermissions(2)
Further Documentation:
* Fetches the permissions assigned to a given user.
And here is an example usage of the fetchUserPermissions() function:
// Assume the user ID is 1 $user_id = 1;
// Call the function to fetch the permissions assigned to the user $user_permissions = fetchUserPermissions($user_id);
//returns
array(2) {
[0]=>
object(stdClass)#64 (3) {
["id"]=>
int(100)
["user_id"]=>
int(1)
["permission_id"]=>
int(1)
}
[1]=>
object(stdClass)#63 (3) {
["id"]=>
int(101)
["user_id"]=>
int(1)
["permission_id"]=>
int(2)
}
}
In this example, the function fetches the permissions assigned to the user with the ID 1 and displays them in an HTML list. Note that the DB class and query() method referenced in the function should be defined elsewhere in your code.
So, if you have a user with 10 permission levels, you will get an array of 10 objects.
If you would like the permission name, you can query that directly
$user_id = 1; $user_permissions = $db->query("SELECT
m.*,
p.name as permission_name
FROM user_permission_matches m
LEFT OUTER JOIN permissions p ON m.permission_id = p.id
WHERE m.user_id = ?", [$user_id])->results();
//returns
array(2) {
[0]=>
object(stdClass)#64 (4) {
["id"]=>
int(100)
["user_id"]=>
int(1)
["permission_id"]=>
int(1)
["permission_name"]=>
string(8) "Everyone"
}
[1]=>
object(stdClass)#63 (4) {
["id"]=>
int(101)
["user_id"]=>
int(1)
["permission_id"]=>
int(2)
["permission_name"]=>
string(13) "Administrator"
}
}