checkMenu (function)

Last updated: Sat, Apr 15, 2023 6:35 pm
Return to Knowledgebase

Purpose

Check if a user has a permission level

Location

users/helpers/permissions.php

See Also:

hasPerm (function)

Parameters

# Parameter Data Type Required Description
1 $permission int Yes The permission ID of the menu item to be checked.
2 $id = 0 int No The user ID of the user whose permission needs to be checked

Returns

Data Type Description of Returned Data
bool If the user has the permission, the function returns true. If not, the function checks if the user is logged in and has an ID of 1 (which is the ID of the master user in UserSpice). If the user is the master user, the function returns true. Otherwise, the function returns false.

Example

checkMenu(2)

Further Documentation:

This poorly named function comes all the way back from the UserCake days.
It was primarily used to see if a user could see a menu item
It has primarily been replaced by hasPerm which accepts an array

$permission: The permission ID of the menu item to be checked.
$id: The user ID of the user whose permission needs to be checked. This parameter is optional and defaults to 0 if not specified. If the user ID is 0, the function will check if the currently logged-in user has the specified permission.

The function first checks if the $id parameter is 0 and if the user is currently logged in. If both of these conditions are true, the function sets $id to the ID of the currently logged-in user. Next, the function queries the database to see if the user has the specified permission. If the user has the permission, the function returns true. If not, the function checks if the user is logged in and has an ID of 1 (which is the ID of the master user in UserSpice). If the user is the master user, the function returns true. Otherwise, the function returns false.

Here is an example of how the checkMenu function can be used:


if (checkMenu(2)) {
// Display the link to the "Admin" page
echo 'Admin';
}

In this example, the function is called with a permission ID of 3. If the currently logged-in user (or the user with an ID of 0 if no user is logged in) has the permission, the link to the "Admin" page will be displayed. If not, the link will not be displayed.