0
0
Microservicessystem_design~10 mins

Role-based access control in Microservices - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if a user has the required role before accessing a service.

Microservices
if user.roles.__contains__([1]):
    allow_access()
Drag options to blanks, or click blank then click option'
A"admin"
Baccess
Croles
Duser.roles
Attempts:
3 left
💡 Hint
Common Mistakes
Using the variable name 'roles' instead of the role string.
Checking the entire roles list instead of a specific role.
2fill in blank
medium

Complete the code to assign roles to a new user in the microservice.

Microservices
new_user.roles = [1]
Drag options to blanks, or click blank then click option'
A["guest"]
B"admin"
Croles
Duser.roles
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning a single string instead of a list.
Using undefined variables like 'roles' without declaration.
3fill in blank
hard

Fix the error in the code that checks if a user has the 'editor' role.

Microservices
if [1] in user.roles:
    grant_edit_permission()
Drag options to blanks, or click blank then click option'
Auser
Broles
Cuser.roles
D"editor"
Attempts:
3 left
💡 Hint
Common Mistakes
Checking if the entire roles list is in itself.
Using variable names instead of the role string.
4fill in blank
hard

Fill both blanks to create a dictionary mapping roles to permissions.

Microservices
role_permissions = { [1]: [2] for role in roles_list }
Drag options to blanks, or click blank then click option'
Arole
B["read"]
Crole.upper()
D["write"]
Attempts:
3 left
💡 Hint
Common Mistakes
Using role.upper() as key which changes the role name.
Assigning a string instead of a list for permissions.
5fill in blank
hard

Fill all three blanks to filter users who have the 'admin' role and create a list of their IDs.

Microservices
admin_ids = [user.[1] for user in users if '[2]' in user.[3]]
Drag options to blanks, or click blank then click option'
Aid
Badmin
Croles
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'name' instead of 'id' for user identifier.
Checking for 'admin' in wrong attribute.
Using variable names instead of string for role.