|
14 | 14 |
|
15 | 15 |
|
16 | 16 | class Permission(Resource): |
17 | | - def delete(self) -> None: |
18 | | - """Delete the permission.""" |
| 17 | + def destroy(self) -> None: |
| 18 | + """Destroy the permission.""" |
19 | 19 | path = f"v1/content/{self['content_guid']}/permissions/{self['id']}" |
20 | 20 | url = self.params.url + path |
21 | 21 | self.params.session.delete(url) |
@@ -215,3 +215,85 @@ def get(self, uid: str) -> Permission: |
215 | 215 | url = self.params.url + path |
216 | 216 | response = self.params.session.get(url) |
217 | 217 | return Permission(self.params, **response.json()) |
| 218 | + |
| 219 | + def destroy(self, *permissions: str | Group | User | Permission) -> list[Permission]: |
| 220 | + """Remove supplied content item permissions. |
| 221 | +
|
| 222 | + Removes all provided permissions from the content item's permissions. If a permission isn't |
| 223 | + found, it is silently ignored. |
| 224 | +
|
| 225 | + Parameters |
| 226 | + ---------- |
| 227 | + *permissions : str | Group | User | Permission |
| 228 | + The content item permissions to remove. If a `str` is received, it is compared against |
| 229 | + the `Permissions`'s `principal_guid`. If a `Group` or `User` is received, the associated |
| 230 | + `Permission` will be removed. |
| 231 | +
|
| 232 | + Returns |
| 233 | + ------- |
| 234 | + list[Permission] |
| 235 | + The removed permissions. If a permission is not found, there is nothing to remove and |
| 236 | + it is not included in the returned list. |
| 237 | +
|
| 238 | + Examples |
| 239 | + -------- |
| 240 | + ```python |
| 241 | + from posit import connect |
| 242 | +
|
| 243 | + #### User-defined inputs #### |
| 244 | + # 1. specify the guid for the content item |
| 245 | + content_guid = "CONTENT_GUID_HERE" |
| 246 | + # 2. specify either the principal_guid or group name prefix |
| 247 | + principal_guid = "USER_OR_GROUP_GUID_HERE" |
| 248 | + group_name_prefix = "GROUP_NAME_PREFIX_HERE" |
| 249 | + ############################ |
| 250 | +
|
| 251 | + client = connect.Client() |
| 252 | +
|
| 253 | + # Remove a single permission by principal_guid |
| 254 | + client.content.get(content_guid).permissions.destroy(principal_guid) |
| 255 | +
|
| 256 | + # Remove by user (if principal_guid is a user) |
| 257 | + user = client.users.get(principal_guid) |
| 258 | + client.content.get(content_guid).permissions.destroy(user) |
| 259 | +
|
| 260 | + # Remove by group (if principal_guid is a group) |
| 261 | + group = client.groups.get(principal_guid) |
| 262 | + client.content.get(content_guid).permissions.destroy(group) |
| 263 | +
|
| 264 | + # Remove all groups with a matching prefix name |
| 265 | + groups = client.groups.find(prefix=group_name_prefix) |
| 266 | + client.content.get(content_guid).permissions.destroy(*groups) |
| 267 | +
|
| 268 | + # Confirm new permissions |
| 269 | + client.content.get(content_guid).permissions.find() |
| 270 | + ``` |
| 271 | + """ |
| 272 | + from .groups import Group |
| 273 | + from .users import User |
| 274 | + |
| 275 | + if len(permissions) == 0: |
| 276 | + raise ValueError("Expected at least one `permission` to remove") |
| 277 | + |
| 278 | + principal_guids: set[str] = set() |
| 279 | + |
| 280 | + for arg in permissions: |
| 281 | + if isinstance(arg, str): |
| 282 | + principal_guid = arg |
| 283 | + elif isinstance(arg, (Group, User)): |
| 284 | + principal_guid: str = arg["guid"] |
| 285 | + elif isinstance(arg, Permission): |
| 286 | + principal_guid: str = arg["principal_guid"] |
| 287 | + else: |
| 288 | + raise TypeError( |
| 289 | + f"destroy() expected argument type 'str', 'User', 'Group', or 'Permission' but got '{type(arg).__name__}'", |
| 290 | + ) |
| 291 | + principal_guids.add(principal_guid) |
| 292 | + |
| 293 | + destroyed_permissions: list[Permission] = [] |
| 294 | + for permission in self.find(): |
| 295 | + if permission["principal_guid"] in principal_guids: |
| 296 | + permission.destroy() |
| 297 | + destroyed_permissions.append(permission) |
| 298 | + |
| 299 | + return destroyed_permissions |
0 commit comments