Skip to content

Commit bce44c3

Browse files
committed
drafted sharing
1 parent 1ef4437 commit bce44c3

File tree

3 files changed

+62
-56
lines changed

3 files changed

+62
-56
lines changed

packages/postgres-database/src/simcore_postgres_database/utils_tags.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
from .utils_tags_sql import (
1111
count_groups_with_given_access_rights_stmt,
1212
create_tag_stmt,
13-
delete_tag_sharing_stmt,
13+
delete_tag_access_rights_stmt,
1414
delete_tag_stmt,
1515
get_tag_stmt,
1616
has_access_rights_stmt,
1717
list_tag_group_access_stmt,
1818
list_tags_stmt,
19-
share_tag_stmt,
2019
update_tag_stmt,
20+
upsert_tags_access_rights_stmt,
2121
)
2222

2323

@@ -112,7 +112,7 @@ async def create(
112112
assert tag # nosec
113113

114114
# take tag ownership
115-
access_stmt = share_tag_stmt(
115+
access_stmt = upsert_tags_access_rights_stmt(
116116
tag_id=tag.id,
117117
user_id=user_id,
118118
read=read,
@@ -255,7 +255,7 @@ async def _has_access_rights(
255255
)
256256
return result.fetchone() is not None
257257

258-
async def list_tag_group_access(
258+
async def list_access_rights(
259259
self,
260260
connection: AsyncConnection | None = None,
261261
*,
@@ -299,7 +299,7 @@ async def create_or_update_access_rights(
299299
)
300300

301301
result = await conn.execute(
302-
share_tag_stmt(
302+
upsert_tags_access_rights_stmt(
303303
tag_id=tag_id,
304304
group_id=group_id,
305305
read=read,
@@ -328,6 +328,6 @@ async def delete_access_rights(
328328
)
329329

330330
deleted: bool = await conn.scalar(
331-
delete_tag_sharing_stmt(tag_id=tag_id, group_id=group_id)
331+
delete_tag_access_rights_stmt(tag_id=tag_id, group_id=group_id)
332332
)
333333
return deleted

packages/postgres-database/src/simcore_postgres_database/utils_tags_sql.py

Lines changed: 54 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -164,17 +164,60 @@ def delete_tag_stmt(*, user_id: int, tag_id: int):
164164

165165

166166
#
167-
# ACCESS RIGHTS AND SHARING: GROUP<--> TAGS
167+
# ACCESS RIGHTS
168168
#
169169

170170

171-
def list_tag_group_access_stmt(*, tag_id: int):
172-
return sa.select(tags_access_rights.c.group_id, *_ACCESS_RIGHTS_COLUMNS).where(
173-
tags_access_rights.c.tag_id == tag_id
171+
def has_access_rights_stmt(
172+
*,
173+
tag_id: int,
174+
caller_user_id: int | None = None,
175+
caller_group_id: int | None = None,
176+
read: bool = False,
177+
write: bool = False,
178+
delete: bool = False,
179+
):
180+
conditions = []
181+
182+
# caller
183+
if caller_user_id is not None:
184+
group_condition = (
185+
tags_access_rights.c.group_id
186+
== sa.select(users.c.primary_gid)
187+
.where(users.c.id == caller_user_id)
188+
.scalar_subquery()
189+
)
190+
elif caller_group_id is not None:
191+
group_condition = tags_access_rights.c.group_id == caller_group_id
192+
else:
193+
msg = "Either caller_user_id or caller_group_id must be provided."
194+
raise ValueError(msg)
195+
196+
conditions.append(group_condition)
197+
if read:
198+
conditions.append(tags_access_rights.c.read.is_(True))
199+
if write:
200+
conditions.append(tags_access_rights.c.write.is_(True))
201+
if delete:
202+
conditions.append(tags_access_rights.c.delete.is_(True))
203+
204+
return sa.select(tags_access_rights).where(
205+
sa.and_(
206+
tags_access_rights.c.tag_id == tag_id,
207+
*conditions,
208+
)
174209
)
175210

176211

177-
def share_tag_stmt(
212+
def list_tag_group_access_stmt(*, tag_id: int):
213+
return sa.select(
214+
tags_access_rights.c.tag_id,
215+
tags_access_rights.c.group_id,
216+
*_ACCESS_RIGHTS_COLUMNS,
217+
).where(tags_access_rights.c.tag_id == tag_id)
218+
219+
220+
def upsert_tags_access_rights_stmt(
178221
*,
179222
tag_id: int,
180223
group_id: int | None = None,
@@ -209,11 +252,15 @@ def share_tag_stmt(
209252
index_elements=["tag_id", "group_id"],
210253
set_={"read": read, "write": write, "delete": delete},
211254
)
212-
.returning(tags_access_rights.c.group_id, *_ACCESS_RIGHTS_COLUMNS)
255+
.returning(
256+
tags_access_rights.c.tag_id,
257+
tags_access_rights.c.group_id,
258+
*_ACCESS_RIGHTS_COLUMNS,
259+
)
213260
)
214261

215262

216-
def delete_tag_sharing_stmt(*, tag_id: int, group_id: int):
263+
def delete_tag_access_rights_stmt(*, tag_id: int, group_id: int):
217264
return (
218265
sa.delete(tags_access_rights)
219266
.where(
@@ -224,47 +271,6 @@ def delete_tag_sharing_stmt(*, tag_id: int, group_id: int):
224271
)
225272

226273

227-
def has_access_rights_stmt(
228-
*,
229-
tag_id: int,
230-
caller_user_id: int | None = None,
231-
caller_group_id: int | None = None,
232-
read: bool = False,
233-
write: bool = False,
234-
delete: bool = False,
235-
):
236-
conditions = []
237-
238-
# caller
239-
if caller_user_id is not None:
240-
group_condition = (
241-
tags_access_rights.c.group_id
242-
== sa.select(users.c.primary_gid)
243-
.where(users.c.id == caller_user_id)
244-
.scalar_subquery()
245-
)
246-
elif caller_group_id is not None:
247-
group_condition = tags_access_rights.c.group_id == caller_group_id
248-
else:
249-
msg = "Either caller_user_id or caller_group_id must be provided."
250-
raise ValueError(msg)
251-
252-
conditions.append(group_condition)
253-
if read:
254-
conditions.append(tags_access_rights.c.read.is_(True))
255-
if write:
256-
conditions.append(tags_access_rights.c.write.is_(True))
257-
if delete:
258-
conditions.append(tags_access_rights.c.delete.is_(True))
259-
260-
return sa.select(tags_access_rights).where(
261-
sa.and_(
262-
tags_access_rights.c.tag_id == tag_id,
263-
*conditions,
264-
)
265-
)
266-
267-
268274
#
269275
# PROJECT TAGS
270276
#

packages/postgres-database/tests/test_utils_tags.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
get_tags_for_project_stmt,
2929
get_tags_for_services_stmt,
3030
list_tags_stmt,
31-
share_tag_stmt,
3231
update_tag_stmt,
32+
upsert_tags_access_rights_stmt,
3333
)
3434
from sqlalchemy.ext.asyncio import AsyncEngine
3535

@@ -690,7 +690,7 @@ def _check(func_smt, **kwargs):
690690
)
691691

692692
_check(
693-
share_tag_stmt,
693+
upsert_tags_access_rights_stmt,
694694
tag_id=tag_id,
695695
user_id=user_id,
696696
read=True,

0 commit comments

Comments
 (0)