Skip to content

Commit de3622e

Browse files
committed
refactor: Refactor permissions into module for improved readability and testability
- Refactored the `permissions.py` file into a permissions module - Entities now have their own has_permission function, and the core `has_permission` function acts as a dispatcher. This structure significantly improves readability and testability of the permissions boundary. It also greatly improves its extensibility for future permissions updates. - Added comprehensive tests for all implemented permissions. Tests are modular and can be easily added to and changed.
1 parent c177c42 commit de3622e

26 files changed

+5169
-3691
lines changed

src/mavedb/lib/permissions.py

Lines changed: 0 additions & 524 deletions
This file was deleted.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""
2+
Permission system for MaveDB entities.
3+
4+
This module provides a comprehensive permission system for checking user access
5+
to various entity types including ScoreSets, Experiments, Collections, etc.
6+
7+
Main Functions:
8+
has_permission: Check if a user has permission for an action on an entity
9+
assert_permission: Assert permission or raise exception
10+
11+
Usage:
12+
>>> from mavedb.lib.permissions import Action, has_permission, assert_permission
13+
>>>
14+
>>> # Check permission and handle response
15+
>>> result = has_permission(user_data, score_set, Action.READ)
16+
>>> if result.permitted:
17+
... # User has access
18+
... pass
19+
>>>
20+
>>> # Assert permission (raises exception if denied)
21+
>>> assert_permission(user_data, score_set, Action.UPDATE)
22+
"""
23+
24+
from .actions import Action
25+
from .core import assert_permission, has_permission
26+
27+
__all__ = ["has_permission", "assert_permission", "Action"]
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from enum import Enum
2+
3+
4+
class Action(Enum):
5+
LOOKUP = "lookup"
6+
READ = "read"
7+
UPDATE = "update"
8+
DELETE = "delete"
9+
ADD_EXPERIMENT = "add_experiment"
10+
ADD_SCORE_SET = "add_score_set"
11+
SET_SCORES = "set_scores"
12+
ADD_ROLE = "add_role"
13+
PUBLISH = "publish"
14+
ADD_BADGE = "add_badge"
15+
CHANGE_RANK = "change_rank"

0 commit comments

Comments
 (0)