-
-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathusers.py
More file actions
155 lines (135 loc) · 4.32 KB
/
users.py
File metadata and controls
155 lines (135 loc) · 4.32 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
152
153
154
155
import dataclasses
from enum import IntEnum
from typing import Annotated, Self
from pydantic import StringConstraints
from sqlalchemy import Connection, text
from config import load_configuration
# Enforces str is 32 hexadecimal characters, does not check validity.
# If `allow_test_api_keys` is set, the key may also be one of `normaluser`,
# `normaluser2`, or `abc` (admin).
api_key_pattern = r"^[0-9a-fA-F]{32}$"
if load_configuration()["development"].get("allow_test_api_keys"):
api_key_pattern = r"^([0-9a-fA-F]{32}|normaluser|normaluser2|abc)$"
APIKey = Annotated[
str,
StringConstraints(pattern=api_key_pattern),
]
class UserGroup(IntEnum):
ADMIN = (1,)
READ_WRITE = (2,)
READ_ONLY = (3,)
def get_user_id_for(*, api_key: APIKey, connection: Connection) -> int | None:
user = connection.execute(
text(
"""
SELECT *
FROM users
WHERE session_hash = :api_key
""",
),
parameters={"api_key": api_key},
).one_or_none()
return user.id if user else None
def get_user_groups_for(*, user_id: int, connection: Connection) -> list[UserGroup]:
row = connection.execute(
text(
"""
SELECT group_id
FROM users_groups
WHERE user_id = :user_id
""",
),
parameters={"user_id": user_id},
)
return [UserGroup(group) for (group,) in row]
@dataclasses.dataclass
class User:
user_id: int
_database: Connection
_groups: list[UserGroup] | None = None
@classmethod
def fetch(cls, api_key: APIKey, user_db: Connection) -> Self | None:
if user_id := get_user_id_for(api_key=api_key, connection=user_db):
return cls(user_id, _database=user_db)
return None
@property
def groups(self) -> list[UserGroup]:
if self._groups is None:
groups = get_user_groups_for(user_id=self.user_id, connection=self._database)
self._groups = [UserGroup(group_id) for group_id in groups]
return self._groups
def get_user_resource_count(*, user_id: int, expdb: Connection) -> int:
"""Return the total number of datasets, flows, and runs owned by the user."""
dataset_count = (
expdb.execute(
text("SELECT COUNT(*) FROM dataset WHERE uploader = :user_id"),
parameters={"user_id": user_id},
).scalar()
or 0
)
flow_count = (
expdb.execute(
text("SELECT COUNT(*) FROM implementation WHERE uploader = :user_id"),
parameters={"user_id": user_id},
).scalar()
or 0
)
run_count = (
expdb.execute(
text("SELECT COUNT(*) FROM run WHERE uploader = :user_id"),
parameters={"user_id": user_id},
).scalar()
or 0
)
study_count = (
expdb.execute(
text("SELECT COUNT(*) FROM study WHERE creator = :user_id"),
parameters={"user_id": user_id},
).scalar()
or 0
)
task_study_count = (
expdb.execute(
text("SELECT COUNT(*) FROM task_study WHERE uploader = :user_id"),
parameters={"user_id": user_id},
).scalar()
or 0
)
run_study_count = (
expdb.execute(
text("SELECT COUNT(*) FROM run_study WHERE uploader = :user_id"),
parameters={"user_id": user_id},
).scalar()
or 0
)
dataset_tag_count = (
expdb.execute(
text("SELECT COUNT(*) FROM dataset_tag WHERE uploader = :user_id"),
parameters={"user_id": user_id},
).scalar()
or 0
)
return int(
dataset_count
+ flow_count
+ run_count
+ study_count
+ task_study_count
+ run_study_count
+ dataset_tag_count,
)
def delete_user(*, user_id: int, connection: Connection) -> None:
"""Remove the user and their group memberships from the user database."""
with connection.begin_nested() as transaction:
try:
connection.execute(
text("DELETE FROM users_groups WHERE user_id = :user_id"),
parameters={"user_id": user_id},
)
connection.execute(
text("DELETE FROM users WHERE id = :user_id"),
parameters={"user_id": user_id},
)
except Exception:
transaction.rollback()
raise