Created
April 11, 2022 01:19
-
-
Save efrenfuentes/880740603bdea0486ac9ab2bfff55ec9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Authorization: | |
def __init__(self, permissions, users): | |
self.permissions = permissions | |
self.users = users | |
def get_user(self, user_id): | |
for user in self.users: | |
if user_id == user["id"]: | |
return user | |
return None | |
def get_role_permissions(self, role): | |
role_permissions = [] | |
for permission in self.permissions: | |
if permission["active"]: | |
if permission["role"] == role: | |
role_permissions.append(permission["name"]) | |
return role_permissions | |
def list_permissions(self, user_id): | |
user = self.get_user(user_id) | |
user_permissions = [] | |
for role in user["roles"]: | |
role_permissions = self.get_role_permissions(role) | |
user_permissions = list(set(user_permissions + role_permissions)) | |
return user_permissions | |
def check_permitted(self, permission_name, user_id): | |
user_permissions = self.list_permissions(user_id) | |
if permission_name in user_permissions: | |
return True | |
return False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment