Bird
0
0

In this RBAC microservice code:

medium📝 Analysis Q7 of 15
Microservices - Authentication and Authorization
In this RBAC microservice code:
def has_permission(user_roles, permission):
    for role in user_roles:
        if permission in role_permissions.get(role, []):
            return True
    return False

role_permissions = {"admin": ["read", "write"], "user": ["read"]}
print(has_permission([], "read"))

What is the output and why?
ATrue because 'read' is a common permission
BFalse because user_roles is empty
CError due to empty list
DNone
Step-by-Step Solution
Solution:
  1. Step 1: Analyze input user_roles

    User roles list is empty, so loop does not run.
  2. Step 2: Determine function return

    Since no roles checked, function returns False.
  3. Final Answer:

    False because user_roles is empty -> Option B
  4. Quick Check:

    Empty roles list = False access [OK]
Quick Trick: Empty roles mean no permissions granted [OK]
Common Mistakes:
MISTAKES
  • Assuming default True for empty roles
  • Expecting error on empty list
  • Ignoring loop behavior

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Microservices Quizzes