Are there alternative methods or best practices for restricting access to certain content based on user permissions in PHP, especially in an intranet setting?

In an intranet setting, restricting access to certain content based on user permissions can be crucial for maintaining security and privacy. One common method is to use role-based access control (RBAC) where users are assigned specific roles with corresponding permissions. This can be implemented by checking the user's role against the required permission before allowing access to the content.

// Check user's role against required permission
function hasPermission($userRole, $requiredPermission) {
    // Define roles and permissions
    $roles = [
        'admin' => ['manage_users', 'view_reports'],
        'editor' => ['edit_content', 'publish_content'],
        'viewer' => ['view_content']
    ];

    // Check if user has the required permission
    if (isset($roles[$userRole]) && in_array($requiredPermission, $roles[$userRole])) {
        return true;
    } else {
        return false;
    }
}

// Example usage
$userRole = 'editor';
$requiredPermission = 'edit_content';

if (hasPermission($userRole, $requiredPermission)) {
    // Allow access to content
    echo "You have permission to edit content.";
} else {
    // Deny access to content
    echo "Access denied. You do not have permission to edit content.";
}