-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdata.py
More file actions
151 lines (106 loc) · 3.95 KB
/
data.py
File metadata and controls
151 lines (106 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
"""Data classes and enums for representing roles, permissions, and policies."""
from enum import Enum
from typing import Literal
from attrs import define
class GroupingPolicyIndex(Enum):
"""Index of fields in a grouping policy."""
SUBJECT = 0
ROLE = 1
SCOPE = 2
# The rest of the fields are optional and can be ignored for now
class PolicyIndex(Enum):
"""Index of fields in a policy."""
ROLE = 0
ACT = 1
SCOPE = 2
EFFECT = 3
# The rest of the fields are optional and can be ignored for now
@define
class UserData:
"""A user is a subject that can be assigned roles and permissions.
Attributes:
username: The username. Automatically prefixed with 'user:' if not present.
"""
username: str
def __attrs_post_init__(self):
"""Ensure username has 'user:' namespace prefix."""
if not self.username.startswith("user:"):
object.__setattr__(self, "username", f"user:{self.username}")
@define
class ScopeData:
"""A scope is a context in which roles and permissions are assigned.
Attributes:
scope_id: The scope identifier (e.g., 'course-v1:edX+DemoX+2021_T1').
This class assumes that the scope is already namespaced appropriately
before being passed in, as scopes can vary widely (e.g., courses, organizations).
"""
scope_id: str
@define
class SubjectData:
"""A subject is an entity that can be assigned roles and permissions.
Attributes:
subject_id: The subject identifier namespaced (e.g., 'user:john_doe').
This class assumes that the subject was already namespaced by their own
type (e.g., 'user:', 'group:') before being passed in since subjects can be
users, groups, or other entities.
"""
subject_id: str
@define
class ActionData:
"""An action is an operation that can be performed in a specific scope.
Attributes:
action: The action name. Automatically prefixed with 'act:' if not present.
"""
action_id: str
def __attrs_post_init__(self):
"""Ensure action name has 'act:' namespace prefix."""
if not self.action_id.startswith("act:"):
object.__setattr__(self, "action_id", f"act:{self.action_id}")
@define
class PermissionData: # TODO: change to policy?
"""A permission is an action that can be performed under certain conditions.
Attributes:
name: The name of the permission.
"""
action: ActionData
effect: Literal["allow", "deny"] = "allow"
@define
class RoleMetadataData:
"""Metadata for a role.
Attributes:
description: A description of the role.
created_at: The date and time the role was created.
created_by: The ID of the subject who created the role.
"""
description: str = None
created_at: str = None
created_by: str = None
@define
class RoleData:
"""A role is a named group of permissions.
Attributes:
name: The name of the role. Must have 'role:' namespace prefix.
permissions: A list of permissions assigned to the role.
scopes: A list of scopes assigned to the role.
metadata: A dictionary of metadata assigned to the role. This can include
information such as the description of the role, creation date, etc.
"""
name: str
permissions: list[PermissionData] = None
metadata: RoleMetadataData = None
def __attrs_post_init__(self):
"""Ensure role name has 'role:' namespace prefix."""
if not self.name.startswith("role:"):
object.__setattr__(self, "name", f"role:{self.name}")
@define
class RoleAssignmentData:
"""A role assignment is the assignment of a role to a subject in a specific scope.
Attributes:
subject: The ID of the user namespaced (e.g., 'user:john_doe').
email: The email of the user.
role_name: The name of the role.
scope: The scope in which the role is assigned.
"""
subject: UserData
role: RoleData
scope: ScopeData