Skip to content

Commit 1db2333

Browse files
authored
users unable to search files (#1076)
* files were not indexed in filesMultiple endpoint in datasets * index files when a new user is added * index files after removing user role * new method - indexes all files in a dataset this should save some time as often every file needs to be indexed after the dataset * indexing files when we change group permissions * fix formatting * found bugs when adding a user to a group, elasticsearch is not updated when adding a group, if there were no read only users and empty list was inserted into the index * indexing files * temorarily removing pre commit hooks fixing indexing for files * using local shellcheck does not work * removing unused import * pre-commit * ran pre-commit resolved conflicts * ran pre commit
1 parent 001c686 commit 1db2333

File tree

3 files changed

+75
-22
lines changed

3 files changed

+75
-22
lines changed

backend/app/routers/authorization.py

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@
2121
GroupAndRole,
2222
UserAndRole,
2323
)
24+
from app.models.files import FileDB, FileOut
2425
from app.models.groups import GroupDB
2526
from app.models.users import UserDB
2627
from app.routers.authentication import get_admin, get_admin_mode
27-
from app.search.index import index_dataset
28+
from app.search.index import index_dataset, index_dataset_files
2829
from beanie import PydanticObjectId
2930
from beanie.operators import In, Or
3031
from bson import ObjectId
@@ -39,6 +40,7 @@ async def save_authorization(
3940
dataset_id: str,
4041
authorization_in: AuthorizationBase,
4142
user=Depends(get_current_username),
43+
es=Depends(get_elasticsearchclient),
4244
allow: bool = Depends(Authorization("editor")),
4345
):
4446
"""Save authorization info in Mongo. This is a triple of dataset_id/user_id/role/group_id."""
@@ -61,6 +63,7 @@ async def save_authorization(
6163
**authorization_in.dict(), creator=user, user_ids=user_ids
6264
)
6365
await authorization.insert()
66+
await index_dataset_files(es, dataset_id, update=True)
6467
return authorization.dict()
6568

6669

@@ -205,6 +208,7 @@ async def set_dataset_group_role(
205208
await index_dataset(
206209
es, DatasetOut(**dataset.dict()), auth_db.user_ids
207210
)
211+
await index_dataset_files(es, str(dataset_id))
208212
if len(readonly_user_ids) > 0:
209213
readonly_auth_db = AuthorizationDB(
210214
creator=user_id,
@@ -217,6 +221,7 @@ async def set_dataset_group_role(
217221
await index_dataset(
218222
es, DatasetOut(**dataset.dict()), readonly_auth_db.user_ids
219223
)
224+
await index_dataset_files(es, str(dataset_id), update=True)
220225
return auth_db.dict()
221226
else:
222227
# Create new role entry for this dataset
@@ -228,27 +233,33 @@ async def set_dataset_group_role(
228233
else:
229234
user_ids.append(u.user.email)
230235
# add the users who get the role
231-
auth_db = AuthorizationDB(
232-
creator=user_id,
233-
dataset_id=PydanticObjectId(dataset_id),
234-
role=role,
235-
group_ids=[PydanticObjectId(group_id)],
236-
user_ids=user_ids,
237-
)
238-
readonly_auth_db = AuthorizationDB(
239-
creator=user_id,
240-
dataset_id=PydanticObjectId(dataset_id),
241-
role=RoleType.VIEWER,
242-
group_ids=[PydanticObjectId(group_id)],
243-
user_ids=readonly_user_ids,
244-
)
245-
# if there are read only users add them with the role of viewer
246-
await auth_db.insert()
247-
await index_dataset(es, DatasetOut(**dataset.dict()), auth_db.user_ids)
248-
await readonly_auth_db.insert()
249-
await index_dataset(
250-
es, DatasetOut(**dataset.dict()), readonly_auth_db.user_ids
251-
)
236+
if len(readonly_user_ids) > 0:
237+
readonly_auth_db = AuthorizationDB(
238+
creator=user_id,
239+
dataset_id=PydanticObjectId(dataset_id),
240+
role=RoleType.VIEWER,
241+
group_ids=[PydanticObjectId(group_id)],
242+
user_ids=readonly_user_ids,
243+
)
244+
await readonly_auth_db.insert()
245+
await index_dataset(
246+
es, DatasetOut(**dataset.dict()), readonly_auth_db.user_ids
247+
)
248+
await index_dataset_files(es, str(dataset_id))
249+
if len(user_ids) > 0:
250+
auth_db = AuthorizationDB(
251+
creator=user_id,
252+
dataset_id=PydanticObjectId(dataset_id),
253+
role=role,
254+
group_ids=[PydanticObjectId(group_id)],
255+
user_ids=user_ids,
256+
)
257+
# if there are read only users add them with the role of viewer
258+
await auth_db.insert()
259+
await index_dataset(
260+
es, DatasetOut(**dataset.dict()), auth_db.user_ids
261+
)
262+
await index_dataset_files(es, str(dataset_id))
252263
return auth_db.dict()
253264
else:
254265
raise HTTPException(status_code=404, detail=f"Group {group_id} not found")
@@ -303,6 +314,7 @@ async def set_dataset_user_role(
303314
auth_db.user_ids.append(username)
304315
await auth_db.save()
305316
await index_dataset(es, DatasetOut(**dataset.dict()), auth_db.user_ids)
317+
await index_dataset_files(es, dataset_id, update=True)
306318
return auth_db.dict()
307319
else:
308320
# Create a new entry
@@ -314,6 +326,7 @@ async def set_dataset_user_role(
314326
)
315327
await auth_db.insert()
316328
await index_dataset(es, DatasetOut(**dataset.dict()), [username])
329+
await index_dataset_files(es, dataset_id)
317330
return auth_db.dict()
318331
else:
319332
raise HTTPException(status_code=404, detail=f"User {username} not found")
@@ -351,6 +364,7 @@ async def remove_dataset_group_role(
351364
await auth_db.save()
352365
# Update elasticsearch index with new users
353366
await index_dataset(es, DatasetOut(**dataset.dict()), auth_db.user_ids)
367+
await index_dataset_files(es, str(dataset_id), update=True)
354368
return auth_db.dict()
355369
else:
356370
raise HTTPException(status_code=404, detail=f"Group {group_id} not found")
@@ -387,6 +401,7 @@ async def remove_dataset_user_role(
387401
await auth_db.save()
388402
# Update elasticsearch index with updated users
389403
await index_dataset(es, DatasetOut(**dataset.dict()), auth_db.user_ids)
404+
await index_dataset_files(es, dataset_id, update=True)
390405
return auth_db.dict()
391406
else:
392407
raise HTTPException(status_code=404, detail=f"User {username} not found")

backend/app/routers/groups.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
from datetime import datetime
22
from typing import Optional
33

4+
from app import dependencies
45
from app.deps.authorization_deps import AuthorizationDB, GroupAuthorization
56
from app.keycloak_auth import get_current_user, get_user
67
from app.models.authorization import RoleType
8+
from app.models.datasets import DatasetDB, DatasetOut
79
from app.models.groups import GroupBase, GroupDB, GroupIn, GroupOut, Member
810
from app.models.pages import Paged, _construct_page_metadata, _get_page_query
911
from app.models.users import UserDB, UserOut
1012
from app.routers.authentication import get_admin, get_admin_mode
13+
from app.search.index import index_dataset, index_dataset_files
1114
from beanie import PydanticObjectId
1215
from beanie.operators import Or, Push, RegEx
1316
from bson.objectid import ObjectId
@@ -218,6 +221,7 @@ async def add_member(
218221
group_id: str,
219222
username: str,
220223
role: Optional[str] = None,
224+
es=Depends(dependencies.get_elasticsearchclient),
221225
allow: bool = Depends(GroupAuthorization("editor")),
222226
):
223227
"""Add a new user to a group."""
@@ -245,6 +249,20 @@ async def add_member(
245249
).update(
246250
Push({AuthorizationDB.user_ids: username}),
247251
)
252+
# index the datasets in the group
253+
group_authorizations = await AuthorizationDB.find(
254+
AuthorizationDB.group_ids == ObjectId(group_id)
255+
).to_list()
256+
for auth in group_authorizations:
257+
if (
258+
dataset := await DatasetDB.get(
259+
PydanticObjectId(auth.dataset_id)
260+
)
261+
) is not None:
262+
await index_dataset(
263+
es, DatasetOut(**dataset.dict()), auth.user_ids
264+
)
265+
await index_dataset_files(es, str(auth.dataset_id), update=True)
248266
return group.dict()
249267
raise HTTPException(status_code=404, detail=f"Group {group_id} not found")
250268
raise HTTPException(status_code=404, detail=f"User {username} not found")
@@ -254,6 +272,7 @@ async def add_member(
254272
async def remove_member(
255273
group_id: str,
256274
username: str,
275+
es=Depends(dependencies.get_elasticsearchclient),
257276
allow: bool = Depends(GroupAuthorization("editor")),
258277
):
259278
"""Remove a user from a group."""
@@ -278,6 +297,16 @@ async def remove_member(
278297
# Update group itself
279298
group.users.remove(found_user)
280299
await group.replace()
300+
# index the datasets in the group
301+
group_authorizations = await AuthorizationDB.find(
302+
AuthorizationDB.group_ids == ObjectId(group_id)
303+
).to_list()
304+
for auth in group_authorizations:
305+
if (
306+
dataset := await DatasetDB.get(PydanticObjectId(auth.dataset_id))
307+
) is not None:
308+
await index_dataset(es, DatasetOut(**dataset.dict()), auth.user_ids)
309+
await index_dataset_files(es, str(auth.dataset_id), update=True)
281310

282311
return group.dict()
283312
raise HTTPException(status_code=404, detail=f"Group {group_id} not found")

backend/app/search/index.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,15 @@ async def index_file(
114114
insert_record(es, settings.elasticsearch_index, doc, file.id)
115115

116116

117+
async def index_dataset_files(es: Elasticsearch, dataset_id: str, update: bool = False):
118+
query = [
119+
FileDB.dataset_id == ObjectId(dataset_id),
120+
]
121+
files = await FileDB.find(*query).to_list()
122+
for file in files:
123+
await index_file(es, FileOut(**file.dict()), update=update)
124+
125+
117126
async def index_folder(
118127
es: Elasticsearch,
119128
folder: FolderOut,

0 commit comments

Comments
 (0)