|
| 1 | +import functools |
| 2 | + |
| 3 | +import sqlalchemy as sa |
| 4 | +from simcore_postgres_database.models.groups import user_to_groups |
| 5 | +from simcore_postgres_database.models.projects_tags import projects_tags |
| 6 | +from simcore_postgres_database.models.services_tags import services_tags |
| 7 | +from simcore_postgres_database.models.tags import tags |
| 8 | +from simcore_postgres_database.models.tags_to_groups import tags_to_groups |
| 9 | +from simcore_postgres_database.models.users import users |
| 10 | +from sqlalchemy.dialects.postgresql import insert as pg_insert |
| 11 | + |
| 12 | +_TAG_COLUMNS = [ |
| 13 | + tags.c.id, |
| 14 | + tags.c.name, |
| 15 | + tags.c.description, |
| 16 | + tags.c.color, |
| 17 | +] |
| 18 | + |
| 19 | +_ACCESS_COLUMNS = [ |
| 20 | + tags_to_groups.c.read, |
| 21 | + tags_to_groups.c.write, |
| 22 | + tags_to_groups.c.delete, |
| 23 | +] |
| 24 | + |
| 25 | + |
| 26 | +_COLUMNS = _TAG_COLUMNS + _ACCESS_COLUMNS |
| 27 | + |
| 28 | + |
| 29 | +def _join_user_groups_tag(*, access_condition, tag_id: int, user_id: int): |
| 30 | + return user_to_groups.join( |
| 31 | + tags_to_groups, |
| 32 | + (user_to_groups.c.uid == user_id) |
| 33 | + & (user_to_groups.c.gid == tags_to_groups.c.group_id) |
| 34 | + & (access_condition) |
| 35 | + & (tags_to_groups.c.tag_id == tag_id), |
| 36 | + ) |
| 37 | + |
| 38 | + |
| 39 | +def _join_user_to_given_tag(*, access_condition, tag_id: int, user_id: int): |
| 40 | + return _join_user_groups_tag( |
| 41 | + access_condition=access_condition, |
| 42 | + tag_id=tag_id, |
| 43 | + user_id=user_id, |
| 44 | + ).join(tags) |
| 45 | + |
| 46 | + |
| 47 | +def _join_user_to_tags(*, access_condition, user_id: int): |
| 48 | + return user_to_groups.join( |
| 49 | + tags_to_groups, |
| 50 | + (user_to_groups.c.uid == user_id) |
| 51 | + & (user_to_groups.c.gid == tags_to_groups.c.group_id) |
| 52 | + & (access_condition), |
| 53 | + ).join(tags) |
| 54 | + |
| 55 | + |
| 56 | +def get_tag_stmt( |
| 57 | + user_id: int, |
| 58 | + tag_id: int, |
| 59 | +): |
| 60 | + return sa.select(*_COLUMNS).select_from( |
| 61 | + _join_user_to_given_tag( |
| 62 | + access_condition=tags_to_groups.c.read.is_(True), |
| 63 | + tag_id=tag_id, |
| 64 | + user_id=user_id, |
| 65 | + ) |
| 66 | + ) |
| 67 | + |
| 68 | + |
| 69 | +def list_tags_stmt(*, user_id: int): |
| 70 | + return ( |
| 71 | + sa.select(*_COLUMNS) |
| 72 | + .select_from( |
| 73 | + _join_user_to_tags( |
| 74 | + access_condition=tags_to_groups.c.read.is_(True), |
| 75 | + user_id=user_id, |
| 76 | + ) |
| 77 | + ) |
| 78 | + .order_by(tags.c.id) |
| 79 | + ) |
| 80 | + |
| 81 | + |
| 82 | +def create_tag_stmt(**values): |
| 83 | + return tags.insert().values(**values).returning(*_TAG_COLUMNS) |
| 84 | + |
| 85 | + |
| 86 | +def count_users_with_access_rights_stmt( |
| 87 | + *, |
| 88 | + user_id: int, |
| 89 | + tag_id: int, |
| 90 | + read: bool | None, |
| 91 | + write: bool | None, |
| 92 | + delete: bool | None |
| 93 | +): |
| 94 | + """ |
| 95 | + How many users are given these access permissions |
| 96 | + """ |
| 97 | + access = [] |
| 98 | + if read is not None: |
| 99 | + access.append(tags_to_groups.c.read == read) |
| 100 | + if write is not None: |
| 101 | + access.append(tags_to_groups.c.write == write) |
| 102 | + if delete is not None: |
| 103 | + access.append(tags_to_groups.c.delete == delete) |
| 104 | + |
| 105 | + if not access: |
| 106 | + msg = "Undefined access" |
| 107 | + raise ValueError(msg) |
| 108 | + |
| 109 | + j = _join_user_groups_tag( |
| 110 | + access_condition=functools.reduce(sa.and_, access), |
| 111 | + user_id=user_id, |
| 112 | + tag_id=tag_id, |
| 113 | + ) |
| 114 | + return sa.select(sa.func.count(user_to_groups.c.uid)).select_from(j) |
| 115 | + |
| 116 | + |
| 117 | +def set_tag_access_rights_stmt( |
| 118 | + *, tag_id: int, user_id: int, read: bool, write: bool, delete: bool |
| 119 | +): |
| 120 | + scalar_subq = ( |
| 121 | + sa.select(users.c.primary_gid).where(users.c.id == user_id).scalar_subquery() |
| 122 | + ) |
| 123 | + return ( |
| 124 | + tags_to_groups.insert() |
| 125 | + .values( |
| 126 | + tag_id=tag_id, |
| 127 | + group_id=scalar_subq, |
| 128 | + read=read, |
| 129 | + write=write, |
| 130 | + delete=delete, |
| 131 | + ) |
| 132 | + .returning(*_ACCESS_COLUMNS) |
| 133 | + ) |
| 134 | + |
| 135 | + |
| 136 | +def update_tag_stmt(*, user_id: int, tag_id: int, **updates): |
| 137 | + return ( |
| 138 | + tags.update() |
| 139 | + .where(tags.c.id == tag_id) |
| 140 | + .where( |
| 141 | + (tags.c.id == tags_to_groups.c.tag_id) & (tags_to_groups.c.write.is_(True)) |
| 142 | + ) |
| 143 | + .where( |
| 144 | + (tags_to_groups.c.group_id == user_to_groups.c.gid) |
| 145 | + & (user_to_groups.c.uid == user_id) |
| 146 | + ) |
| 147 | + .values(**updates) |
| 148 | + .returning(*_COLUMNS) |
| 149 | + ) |
| 150 | + |
| 151 | + |
| 152 | +def delete_tag_stmt(*, user_id: int, tag_id: int): |
| 153 | + return ( |
| 154 | + tags.delete() |
| 155 | + .where(tags.c.id == tag_id) |
| 156 | + .where( |
| 157 | + (tags_to_groups.c.tag_id == tag_id) & (tags_to_groups.c.delete.is_(True)) |
| 158 | + ) |
| 159 | + .where( |
| 160 | + (tags_to_groups.c.group_id == user_to_groups.c.gid) |
| 161 | + & (user_to_groups.c.uid == user_id) |
| 162 | + ) |
| 163 | + .returning(tags_to_groups.c.delete) |
| 164 | + ) |
| 165 | + |
| 166 | + |
| 167 | +def get_tags_for_project_stmt(*, project_index: int): |
| 168 | + return sa.select(projects_tags.c.tag_id).where( |
| 169 | + projects_tags.c.study_id == project_index |
| 170 | + ) |
| 171 | + |
| 172 | + |
| 173 | +def add_tag_to_project_stmt(*, project_index: int, tag_id: int) -> None: |
| 174 | + return ( |
| 175 | + pg_insert(projects_tags) |
| 176 | + .values( |
| 177 | + study_id=project_index, |
| 178 | + tag_id=tag_id, |
| 179 | + ) |
| 180 | + .on_conflict_do_nothing() |
| 181 | + ) |
| 182 | + |
| 183 | + |
| 184 | +def get_tags_for_services_stmt(*, key: str, version: str): |
| 185 | + return sa.select(services_tags.c.tag_id).where( |
| 186 | + (services_tags.c.service_key == key) |
| 187 | + & (services_tags.c.service_version == version) |
| 188 | + ) |
| 189 | + |
| 190 | + |
| 191 | +def add_tag_to_services_stmt(*, key: str, version: str, tag_id: int) -> None: |
| 192 | + return ( |
| 193 | + pg_insert(services_tags) |
| 194 | + .values( |
| 195 | + service_key=key, |
| 196 | + service_version=version, |
| 197 | + tag_id=tag_id, |
| 198 | + ) |
| 199 | + .on_conflict_do_nothing() |
| 200 | + ) |
0 commit comments