|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
5 | | -from typing import TYPE_CHECKING, List, overload |
| 5 | +from typing import TYPE_CHECKING, List, Optional, overload |
6 | 6 |
|
7 | 7 | from requests.sessions import Session as Session |
8 | 8 |
|
@@ -67,36 +67,101 @@ def count(self) -> int: |
67 | 67 | return len(self.find()) |
68 | 68 |
|
69 | 69 | @overload |
70 | | - def create(self, *, principal_guid: str, principal_type: str, role: str) -> Permission: |
| 70 | + def create(self, /, *, principal_guid: str, principal_type: str, role: str) -> Permission: ... |
| 71 | + |
| 72 | + @overload |
| 73 | + def create(self, principal: User | Group, /, *, role: str) -> Permission: ... |
| 74 | + |
| 75 | + def create( |
| 76 | + self, |
| 77 | + principal: Optional[User | Group] = None, |
| 78 | + /, |
| 79 | + **kwargs, |
| 80 | + ) -> Permission: |
71 | 81 | """Create a permission. |
72 | 82 |
|
73 | 83 | Parameters |
74 | 84 | ---------- |
| 85 | + principal : User | Group |
| 86 | + The principal user or group to add. |
| 87 | + role : str |
| 88 | + The principal role. Currently only `"viewer"` and `"owner"` are supported. |
75 | 89 | principal_guid : str |
| 90 | + User guid or Group guid. |
76 | 91 | principal_type : str |
| 92 | + The principal type. Either `"user"` or `"group"`. |
77 | 93 | role : str |
| 94 | + The principal role. Currently only `"viewer"` and `"owner"` are supported |
78 | 95 |
|
79 | 96 | Returns |
80 | 97 | ------- |
81 | 98 | Permission |
82 | | - """ |
| 99 | + The created permission. |
83 | 100 |
|
84 | | - @overload |
85 | | - def create(self, **kwargs) -> Permission: |
86 | | - """Create a permission. |
| 101 | + Examples |
| 102 | + -------- |
| 103 | + ```python |
| 104 | + from posit import connect |
87 | 105 |
|
88 | | - Returns |
89 | | - ------- |
90 | | - Permission |
91 | | - """ |
| 106 | + client = connect.Client() |
| 107 | + content_item = client.content.get(content_guid) |
92 | 108 |
|
93 | | - def create(self, **kwargs) -> Permission: |
94 | | - """Create a permission. |
| 109 | + # New permission role |
| 110 | + role = "viewer" # Or "owner" |
| 111 | +
|
| 112 | + # Example groups and users |
| 113 | + groups = client.groups.find(prefix="GROUP_NAME_PREFIX_HERE") |
| 114 | + group = groups[0] |
| 115 | + user = client.users.get("USER_GUID_HERE") |
| 116 | + users_and_groups = [user, *groups] |
| 117 | +
|
| 118 | + # Add a group permission |
| 119 | + content_item.permissions.create(group, role=role) |
| 120 | + # Add a user permission |
| 121 | + content_item.permissions.create(user, role=role) |
| 122 | +
|
| 123 | + # Add many group and user permissions with the same role |
| 124 | + for principal in users_and_groups: |
| 125 | + content_item.permissions.create(principal, role=role) |
| 126 | +
|
| 127 | + # Add a group permission manually |
| 128 | + content_item.permissions.create( |
| 129 | + principal_guid=group["guid"], |
| 130 | + principal_type="group", |
| 131 | + role=role, |
| 132 | + ) |
| 133 | + # Add a user permission manually |
| 134 | + content_item.permissions.create( |
| 135 | + principal_guid=user["guid"], |
| 136 | + principal_type="user", |
| 137 | + role=role, |
| 138 | + ) |
95 | 139 |
|
96 | | - Returns |
97 | | - ------- |
98 | | - Permission |
| 140 | + # Confirm new permissions |
| 141 | + content_item.permissions.find() |
| 142 | + ``` |
99 | 143 | """ |
| 144 | + if principal is not None: |
| 145 | + # Avoid circular imports |
| 146 | + from .groups import Group |
| 147 | + from .users import User |
| 148 | + |
| 149 | + if isinstance(principal, User): |
| 150 | + principal_type = "user" |
| 151 | + elif isinstance(principal, Group): |
| 152 | + principal_type = "group" |
| 153 | + else: |
| 154 | + raise TypeError(f"Invalid argument type: {type(principal).__name__}") |
| 155 | + |
| 156 | + if "principal_guid" in kwargs: |
| 157 | + raise ValueError("'principal_guid' can not be defined with `principal` present.") |
| 158 | + if "principal_type" in kwargs: |
| 159 | + raise ValueError("'principal_guid' can not be defined with `principal` present.") |
| 160 | + |
| 161 | + # Set the corresponding kwargs |
| 162 | + kwargs["principal_guid"] = principal["guid"] |
| 163 | + kwargs["principal_type"] = principal_type |
| 164 | + |
100 | 165 | path = f"v1/content/{self.content_guid}/permissions" |
101 | 166 | url = self.params.url + path |
102 | 167 | response = self.params.session.post(url, json=kwargs) |
|
0 commit comments