What are the best practices for implementing role-based access control (RBAC) in PHP user administration to avoid logic issues related to admin privileges?

When implementing role-based access control (RBAC) in PHP user administration, it is important to carefully define roles and permissions to avoid logic issues related to admin privileges. One way to prevent these issues is to create a separate role for super admins with elevated privileges that cannot be easily overridden by other roles.

// Define roles and permissions
$roles = [
    'user' => ['read'],
    'admin' => ['read', 'write'],
    'super_admin' => ['read', 'write', 'delete'],
];

// Check if user has super admin privileges
function hasSuperAdminPrivileges($userRole) {
    return $userRole === 'super_admin';
}

// Example usage
$userRole = 'admin';

if (hasSuperAdminPrivileges($userRole)) {
    // Allow super admin actions
    echo "Super admin actions allowed";
} else {
    // Check for regular role permissions
    if (in_array('write', $roles[$userRole])) {
        // Allow write actions
        echo "Write actions allowed";
    } else {
        // Deny access
        echo "Access denied";
    }
}