|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
5 | | -from typing import List, overload |
| 5 | +from typing import TYPE_CHECKING, List, overload |
6 | 6 |
|
7 | 7 | from requests.sessions import Session as Session |
8 | 8 |
|
9 | 9 | from .resources import Resource, ResourceParameters, Resources |
10 | 10 |
|
| 11 | +if TYPE_CHECKING: |
| 12 | + from .groups import Group |
| 13 | + from .users import User |
| 14 | + |
11 | 15 |
|
12 | 16 | class Permission(Resource): |
13 | | - def delete(self) -> None: |
14 | | - """Delete the permission.""" |
| 17 | + def destroy(self) -> None: |
| 18 | + """Destroy the permission.""" |
15 | 19 | path = f"v1/content/{self['content_guid']}/permissions/{self['id']}" |
16 | 20 | url = self.params.url + path |
17 | 21 | self.params.session.delete(url) |
@@ -137,3 +141,85 @@ def get(self, uid: str) -> Permission: |
137 | 141 | url = self.params.url + path |
138 | 142 | response = self.params.session.get(url) |
139 | 143 | return Permission(self.params, **response.json()) |
| 144 | + |
| 145 | + def destroy(self, *permissions: str | Group | User | Permission) -> list[Permission]: |
| 146 | + """Remove supplied content item permissions. |
| 147 | +
|
| 148 | + Removes all provided permissions from the content item's permissions. If a permission isn't |
| 149 | + found, it is silently ignored. |
| 150 | +
|
| 151 | + Parameters |
| 152 | + ---------- |
| 153 | + *permissions : str | Group | User | Permission |
| 154 | + The content item permissions to remove. If a `str` is received, it is compared against |
| 155 | + the `Permissions`'s `principal_guid`. If a `Group` or `User` is received, the associated |
| 156 | + `Permission` will be removed. |
| 157 | +
|
| 158 | + Returns |
| 159 | + ------- |
| 160 | + list[Permission] |
| 161 | + The removed permissions. If a permission is not found, there is nothing to remove and |
| 162 | + it is not included in the returned list. |
| 163 | +
|
| 164 | + Examples |
| 165 | + -------- |
| 166 | + ```python |
| 167 | + from posit import connect |
| 168 | +
|
| 169 | + #### User-defined inputs #### |
| 170 | + # 1. specify the guid for the content item |
| 171 | + content_guid = "CONTENT_GUID_HERE" |
| 172 | + # 2. specify either the principal_guid or group name prefix |
| 173 | + principal_guid = "USER_OR_GROUP_GUID_HERE" |
| 174 | + group_name_prefix = "GROUP_NAME_PREFIX_HERE" |
| 175 | + ############################ |
| 176 | +
|
| 177 | + client = connect.Client() |
| 178 | +
|
| 179 | + # Remove a single permission by principal_guid |
| 180 | + client.content.get(content_guid).permissions.destroy(principal_guid) |
| 181 | +
|
| 182 | + # Remove by user (if principal_guid is a user) |
| 183 | + user = client.users.get(principal_guid) |
| 184 | + client.content.get(content_guid).permissions.destroy(user) |
| 185 | +
|
| 186 | + # Remove by group (if principal_guid is a group) |
| 187 | + group = client.groups.get(principal_guid) |
| 188 | + client.content.get(content_guid).permissions.destroy(group) |
| 189 | +
|
| 190 | + # Remove all groups with a matching prefix name |
| 191 | + groups = client.groups.find(prefix=group_name_prefix) |
| 192 | + client.content.get(content_guid).permissions.destroy(*groups) |
| 193 | +
|
| 194 | + # Confirm new permissions |
| 195 | + client.content.get(content_guid).permissions.find() |
| 196 | + ``` |
| 197 | + """ |
| 198 | + from .groups import Group |
| 199 | + from .users import User |
| 200 | + |
| 201 | + if len(permissions) == 0: |
| 202 | + raise ValueError("Expected at least one `permission` to remove") |
| 203 | + |
| 204 | + principal_guids: set[str] = set() |
| 205 | + |
| 206 | + for arg in permissions: |
| 207 | + if isinstance(arg, str): |
| 208 | + principal_guid = arg |
| 209 | + elif isinstance(arg, (Group, User)): |
| 210 | + principal_guid: str = arg["guid"] |
| 211 | + elif isinstance(arg, Permission): |
| 212 | + principal_guid: str = arg["principal_guid"] |
| 213 | + else: |
| 214 | + raise TypeError( |
| 215 | + f"destroy() expected argument type 'str', 'User', 'Group', or 'Permission' but got '{type(arg).__name__}'", |
| 216 | + ) |
| 217 | + principal_guids.add(principal_guid) |
| 218 | + |
| 219 | + destroyed_permissions: list[Permission] = [] |
| 220 | + for permission in self.find(): |
| 221 | + if permission["principal_guid"] in principal_guids: |
| 222 | + permission.destroy() |
| 223 | + destroyed_permissions.append(permission) |
| 224 | + |
| 225 | + return destroyed_permissions |
0 commit comments