Skip to content

Commit 3e961e9

Browse files
authored
Add helper method to determine if user has admin rights (#291)
1 parent f77c4a8 commit 3e961e9

5 files changed

Lines changed: 14 additions & 12 deletions

File tree

src/core/access.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from sqlalchemy.engine import Row
44

5-
from database.users import User, UserGroup
5+
from database.users import User
66
from schemas.datasets.openml import Visibility
77

88

@@ -17,5 +17,4 @@ async def _user_has_access(
1717
return False
1818
if user.user_id == dataset.uploader:
1919
return True
20-
user_groups = await user.get_groups()
21-
return UserGroup.ADMIN in user_groups
20+
return await user.is_admin()

src/database/users.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,6 @@ async def get_groups(self) -> list[UserGroup]:
7373
group_ids = await get_user_groups_for(user_id=self.user_id, connection=self._database)
7474
self._groups = [UserGroup(group_id) for group_id in group_ids]
7575
return self._groups
76+
77+
async def is_admin(self) -> bool:
78+
return UserGroup.ADMIN in await self.get_groups()

src/routers/openml/datasets.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
_format_dataset_url,
3333
_format_parquet_url,
3434
)
35-
from database.users import User, UserGroup
35+
from database.users import User
3636
from routers.dependencies import (
3737
Pagination,
3838
expdb_connection,
@@ -144,7 +144,7 @@ async def list_datasets( # noqa: PLR0913, C901
144144

145145
if user is None:
146146
clauses.append("AND `visibility`='public'")
147-
elif UserGroup.ADMIN not in await user.get_groups():
147+
elif not await user.is_admin():
148148
clauses.append("AND (`visibility`='public' OR `uploader`=:user_id)")
149149
parameters["user_id"] = user.user_id
150150

@@ -347,12 +347,12 @@ async def update_dataset_status(
347347

348348
dataset = await _get_dataset_raise_otherwise(dataset_id, user, expdb)
349349

350-
can_deactivate = dataset.uploader == user.user_id or UserGroup.ADMIN in await user.get_groups()
350+
can_deactivate = dataset.uploader == user.user_id or await user.is_admin()
351351
if status == DatasetStatus.DEACTIVATED and not can_deactivate:
352352
msg = f"Dataset {dataset_id} is not owned by you."
353353
raise DatasetNotOwnedError(msg)
354354

355-
if status == DatasetStatus.ACTIVE and UserGroup.ADMIN not in await user.get_groups():
355+
if status == DatasetStatus.ACTIVE and not await user.is_admin():
356356
msg = "Only administrators can activate datasets."
357357
raise DatasetAdminOnlyError(msg)
358358

src/routers/openml/setups.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
TagNotFoundError,
1414
TagNotOwnedError,
1515
)
16-
from database.users import User, UserGroup
16+
from database.users import User
1717
from routers.dependencies import expdb_connection, fetch_user_or_raise
1818
from routers.types import SystemString64
1919

@@ -67,7 +67,7 @@ async def untag_setup(
6767
msg = f"Setup {setup_id} does not have tag {tag!r}."
6868
raise TagNotFoundError(msg)
6969

70-
if matched_tag_row.uploader != user.user_id and UserGroup.ADMIN not in await user.get_groups():
70+
if matched_tag_row.uploader != user.user_id and not await user.is_admin():
7171
msg = (
7272
f"You may not remove tag {tag!r} of setup {setup_id} because it was not created by you."
7373
)

src/routers/openml/study.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
StudyPrivateError,
1818
)
1919
from core.formatting import _str_to_bool
20-
from database.users import User, UserGroup
20+
from database.users import User
2121
from routers.dependencies import expdb_connection, fetch_user
2222
from schemas.core import Visibility
2323
from schemas.study import CreateStudy, Study, StudyStatus, StudyType
@@ -44,7 +44,7 @@ async def _get_study_raise_otherwise(
4444
if user is None:
4545
msg = "Must authenticate for private study."
4646
raise AuthenticationRequiredError(msg)
47-
if study.creator != user.user_id and UserGroup.ADMIN not in await user.get_groups():
47+
if study.creator != user.user_id and not await user.is_admin():
4848
msg = "Study is private."
4949
raise StudyPrivateError(msg)
5050
if _str_to_bool(study.legacy):
@@ -71,7 +71,7 @@ async def attach_to_study(
7171
raise AuthenticationRequiredError(msg)
7272
study = await _get_study_raise_otherwise(study_id, user, expdb)
7373
# PHP lets *anyone* edit *any* study. We're not going to do that.
74-
if study.creator != user.user_id and UserGroup.ADMIN not in await user.get_groups():
74+
if study.creator != user.user_id and not await user.is_admin():
7575
msg = f"Study {study_id} can only be edited by its creator."
7676
raise StudyNotEditableError(msg)
7777
if study.status != StudyStatus.IN_PREPARATION:

0 commit comments

Comments
 (0)