From 882da4331e08d8f3435a197cca987db5186c4325 Mon Sep 17 00:00:00 2001 From: KOSASIH Date: Wed, 18 Sep 2024 10:46:30 +0700 Subject: [PATCH] Create permission_matrix.py --- app/permission_matrix.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 app/permission_matrix.py diff --git a/app/permission_matrix.py b/app/permission_matrix.py new file mode 100644 index 000000000..76a781aba --- /dev/null +++ b/app/permission_matrix.py @@ -0,0 +1,29 @@ +import json + +class PermissionMatrix: + def __init__(self): + self.permissions = { + 'admin': ['create_user', 'delete_user', 'edit_user'], + 'moderator': ['edit_user'], + 'user': ['view_profile'] + } + + def has_permission(self, user_role, permission): + return permission in self.permissions.get(user_role, []) + + def add_permission(self, user_role, permission): + if user_role not in self.permissions: + self.permissions[user_role] = [] + self.permissions[user_role].append(permission) + + def remove_permission(self, user_role, permission): + if user_role in self.permissions and permission in self.permissions[user_role]: + self.permissions[user_role].remove(permission) + + def save_permissions(self, file_path): + with open(file_path, 'w') as file: + json.dump(self.permissions, file) + + def load_permissions(self, file_path): + with open(file_path, 'r') as file: + self.permissions = json.load(file)