What are the best practices for verifying user access rights in PHP when dealing with multiple user types?

When dealing with multiple user types in PHP, it is important to verify user access rights to ensure that each user can only access the appropriate resources. One common practice is to use role-based access control (RBAC) where each user is assigned a specific role with corresponding permissions. By checking the user's role and permissions before granting access to certain functionalities or data, you can ensure that users only have the necessary access rights.

// Example of verifying user access rights based on user role
function checkAccessRights($userRole, $requiredPermission) {
    $rolePermissions = [
        'admin' => ['manage_users', 'manage_posts', 'manage_comments'],
        'editor' => ['manage_posts', 'manage_comments'],
        'user' => ['view_posts', 'view_comments']
    ];

    if (isset($rolePermissions[$userRole]) && in_array($requiredPermission, $rolePermissions[$userRole])) {
        return true;
    } else {
        return false;
    }
}

// Example usage
$userRole = 'admin';
$requiredPermission = 'manage_users';

if (checkAccessRights($userRole, $requiredPermission)) {
    echo 'Access granted';
} else {
    echo 'Access denied';
}