Skip to content

Commit 24476d5

Browse files
committed
Add users to Collection view model
Only collection admins can see all collection contributors. Collection viewers and editors can only see a list of collection admins. The rationale is that collection viewers and editors may want to contact a collection admin, but do not need to know who other editors or viewers of the collection are.
1 parent 97c7699 commit 24476d5

File tree

3 files changed

+111
-7
lines changed

3 files changed

+111
-7
lines changed

src/mavedb/routers/collections.py

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,27 @@ def list_my_collections(
6060
.scalars()
6161
.all()
6262
)
63-
# filter score sets and experiments based on user permissions
63+
6464
for item in collection_bundle[role.value]:
65+
# filter score sets and experiments based on user permissions
6566
item.score_sets = [
6667
score_set for score_set in item.score_sets if has_permission(user_data, score_set, Action.READ)
6768
]
6869
item.experiments = [
6970
experiment for experiment in item.experiments if has_permission(user_data, experiment, Action.READ)
7071
]
72+
# unless user is admin of this collection, filter users to only admins
73+
# the rationale is that all collection contributors should be able to see admins
74+
# to know who to contact, but only collection admins should be able to see viewers and editors
75+
if role in (ContributionRole.viewer, ContributionRole.editor):
76+
admins = []
77+
for user_assoc in item.user_associations:
78+
if user_assoc.contribution_role == ContributionRole.admin:
79+
admin = user_assoc.user
80+
# role must be set in order to assign users to collection
81+
setattr(admin, "role", ContributionRole.admin)
82+
admins.append(admin)
83+
item.users = admins
7184

7285
return collection_bundle
7386

@@ -101,6 +114,20 @@ def fetch_collection(
101114
item.experiments = [
102115
experiment for experiment in item.experiments if has_permission(user_data, experiment, Action.READ)
103116
]
117+
# unless user is admin of this collection, filter users to only admins
118+
# the rationale is that all collection contributors should be able to see admins
119+
# to know who to contact, but only collection admins should be able to see viewers and editors
120+
# TODO either create permissions action for this or look up user's role outside of the permissions module
121+
# for now, just assume that if user has permission to add role, they are a collection admin
122+
if not has_permission(user_data, item, Action.ADD_ROLE):
123+
admins = []
124+
for user_assoc in item.user_associations:
125+
if user_assoc.contribution_role == ContributionRole.admin:
126+
admin = user_assoc.user
127+
# role must be set in order to assign users to collection
128+
setattr(admin, "role", ContributionRole.admin)
129+
admins.append(admin)
130+
item.users = admins
104131

105132
return item
106133

@@ -270,6 +297,20 @@ async def update_collection(
270297
item.experiments = [
271298
experiment for experiment in item.experiments if has_permission(user_data, experiment, Action.READ)
272299
]
300+
# unless user is admin of this collection, filter users to only admins
301+
# the rationale is that all collection contributors should be able to see admins
302+
# to know who to contact, but only collection admins should be able to see viewers and editors
303+
# TODO either create permissions action for this or look up user's role outside of the permissions module
304+
# for now, just assume that if user has permission to add role, they are a collection admin
305+
if not has_permission(user_data, item, Action.ADD_ROLE):
306+
admins = []
307+
for user_assoc in item.user_associations:
308+
if user_assoc.contribution_role == ContributionRole.admin:
309+
admin = user_assoc.user
310+
# role must be set in order to assign users to collection
311+
setattr(admin, "role", ContributionRole.admin)
312+
admins.append(admin)
313+
item.users = admins
273314

274315
return item
275316

@@ -325,6 +366,20 @@ async def add_score_set_to_collection(
325366
item.experiments = [
326367
experiment for experiment in item.experiments if has_permission(user_data, experiment, Action.READ)
327368
]
369+
# unless user is admin of this collection, filter users to only admins
370+
# the rationale is that all collection contributors should be able to see admins
371+
# to know who to contact, but only collection admins should be able to see viewers and editors
372+
# TODO either create permissions action for this or look up user's role outside of the permissions module
373+
# for now, just assume that if user has permission to add role, they are a collection admin
374+
if not has_permission(user_data, item, Action.ADD_ROLE):
375+
admins = []
376+
for user_assoc in item.user_associations:
377+
if user_assoc.contribution_role == ContributionRole.admin:
378+
admin = user_assoc.user
379+
# role must be set in order to assign users to collection
380+
setattr(admin, "role", ContributionRole.admin)
381+
admins.append(admin)
382+
item.users = admins
328383

329384
return item
330385

@@ -390,6 +445,20 @@ async def delete_score_set_from_collection(
390445
item.experiments = [
391446
experiment for experiment in item.experiments if has_permission(user_data, experiment, Action.READ)
392447
]
448+
# unless user is admin of this collection, filter users to only admins
449+
# the rationale is that all collection contributors should be able to see admins
450+
# to know who to contact, but only collection admins should be able to see viewers and editors
451+
# TODO either create permissions action for this or look up user's role outside of the permissions module
452+
# for now, just assume that if user has permission to add role, they are a collection admin
453+
if not has_permission(user_data, item, Action.ADD_ROLE):
454+
admins = []
455+
for user_assoc in item.user_associations:
456+
if user_assoc.contribution_role == ContributionRole.admin:
457+
admin = user_assoc.user
458+
# role must be set in order to assign users to collection
459+
setattr(admin, "role", ContributionRole.admin)
460+
admins.append(admin)
461+
item.users = admins
393462

394463
return item
395464

@@ -445,6 +514,20 @@ async def add_experiment_to_collection(
445514
item.experiments = [
446515
experiment for experiment in item.experiments if has_permission(user_data, experiment, Action.READ)
447516
]
517+
# unless user is admin of this collection, filter users to only admins
518+
# the rationale is that all collection contributors should be able to see admins
519+
# to know who to contact, but only collection admins should be able to see viewers and editors
520+
# TODO either create permissions action for this or look up user's role outside of the permissions module
521+
# for now, just assume that if user has permission to add role, they are a collection admin
522+
if not has_permission(user_data, item, Action.ADD_ROLE):
523+
admins = []
524+
for user_assoc in item.user_associations:
525+
if user_assoc.contribution_role == ContributionRole.admin:
526+
admin = user_assoc.user
527+
# role must be set in order to assign users to collection
528+
setattr(admin, "role", ContributionRole.admin)
529+
admins.append(admin)
530+
item.users = admins
448531

449532
return item
450533

@@ -508,6 +591,20 @@ async def delete_experiment_from_collection(
508591
item.experiments = [
509592
experiment for experiment in item.experiments if has_permission(user_data, experiment, Action.READ)
510593
]
594+
# unless user is admin of this collection, filter users to only admins
595+
# the rationale is that all collection contributors should be able to see admins
596+
# to know who to contact, but only collection admins should be able to see viewers and editors
597+
# TODO either create permissions action for this or look up user's role outside of the permissions module
598+
# for now, just assume that if user has permission to add role, they are a collection admin
599+
if not has_permission(user_data, item, Action.ADD_ROLE):
600+
admins = []
601+
for user_assoc in item.user_associations:
602+
if user_assoc.contribution_role == ContributionRole.admin:
603+
admin = user_assoc.user
604+
# role must be set in order to assign users to collection
605+
setattr(admin, "role", ContributionRole.admin)
606+
admins.append(admin)
607+
item.users = admins
511608

512609
return item
513610

@@ -594,6 +691,8 @@ async def add_user_to_collection_role(
594691
item.experiments = [
595692
experiment for experiment in item.experiments if has_permission(user_data, experiment, Action.READ)
596693
]
694+
# TODO only collection admins can get to this point in the function, so shouldn't need to filter out
695+
# viewers and editors before returning item, but should check with others
597696

598697
return item
599698

@@ -669,6 +768,8 @@ async def remove_user_from_collection_role(
669768
item.experiments = [
670769
experiment for experiment in item.experiments if has_permission(user_data, experiment, Action.READ)
671770
]
771+
# TODO only collection admins can get to this point in the function, so shouldn't need to filter out
772+
# viewers and editors before returning item, but should check with others
672773

673774
return item
674775

src/mavedb/view_models/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,6 @@ def get(self, key: Any, default: Any = ...) -> Any:
4343
# The standard is to name properties as the plural of the enum value
4444
if key[:-1] in ContributionRole._member_map_:
4545
user_assc = getattr(self._obj, "user_associations")
46-
return [user.user for user in user_assc if key[:-1] == user_assc.role.name]
46+
return [user.user for user in user_assc if key[:-1] == user.contribution_role.name]
4747
else:
4848
return super().get(key, default)

src/mavedb/view_models/collection.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def get(self, key: Any, default: Any = ...) -> Any:
1717
return sorted([score_set.urn for score_set in score_sets if score_set.superseding_score_set is None])
1818
elif key == "experiment_urns":
1919
experiments = getattr(self._obj, "experiments") or []
20-
return [experiment.urn for experiment in experiments]
20+
return sorted([experiment.urn for experiment in experiments])
2121
else:
2222
return super().get(key, default)
2323

@@ -69,14 +69,17 @@ class Config:
6969

7070

7171
# Properties to return to non-admin clients
72+
# NOTE: Coupled to ContributionRole enum
7273
class Collection(SavedCollection):
7374
experiment_urns: list[str]
7475
score_set_urns: list[str]
76+
admins: list[User]
77+
viewers: list[User]
78+
editors: list[User]
7579

7680

77-
# Properties to return to admin clients
81+
# Properties to return to admin clients or non-admin clients who are admins of the returned collection
7882
# NOTE: Coupled to ContributionRole enum
83+
# TODO should MaveDB admins get AdminUsers instead of Users?
7984
class AdminCollection(Collection):
80-
viewers: list[User]
81-
editors: list[User]
82-
admins: list[User]
85+
pass

0 commit comments

Comments
 (0)