Nested Group Permissions

Start Timer

0:00:00

Upvote
0
Downvote
Save question
View comments

Let’s say database access is managed with groups. A group lists its users and the groups nested inside it, and nesting can go any number of levels deep. A permission group grants (operation, database) pairs to a list of groups, and every user inside those groups, directly or through nesting, gets those permissions.

Write a function called has_permission that takes a user, an operation, a database, the groups and the permission_groups, and returns whether the user can run that operation on that database.

Note: Nesting can be circular, and a group can list a group that isn’t defined.

Example:

Input:

groups = {
    "engineering": {"users": ["erin"], "groups": ["data"]},
    "data": {"users": ["dev"], "groups": ["interns"]},
    "interns": {"users": ["ivan"], "groups": []},
}
permission_groups = [
    {"groups": ["engineering"], "permissions": [("read", "logs")]},
    {"groups": ["data"], "permissions": [("read", "warehouse"), ("write", "warehouse")]},
]

Output:

has_permission("ivan", "read", "logs", groups, permission_groups) -> True
has_permission("erin", "read", "warehouse", groups, permission_groups) -> False
.
.
.
.
.


Comments

Loading comments