Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Permission.create(principal:User | Group) support #343

Merged
merged 8 commits into from
Dec 5, 2024
103 changes: 86 additions & 17 deletions src/posit/connect/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,36 +67,105 @@ def count(self) -> int:
return len(self.find())

@overload
def create(self, *, principal_guid: str, principal_type: str, role: str) -> Permission:
def create(self, *, principal_guid: str, principal_type: str, role: str) -> Permission: ...

@overload
def create(self, *args: User | Group, role: str) -> list[Permission]: ...

def create(self, *args: User | Group, **kwargs) -> Permission | list[Permission]:
schloerke marked this conversation as resolved.
Show resolved Hide resolved
"""Create a permission.

Parameters
----------
*args : User | Group
The principal users or groups to add.
role : str
The principal role. Currently only `"viewer"` and `"owner"` are supported.
principal_guid : str
User guid or Group guid.
principal_type : str
The principal type. Either `"user"` or `"group"`.
role : str
The principal role. Currently only `"viewer"` and `"owner"` are supported

Returns
-------
Permission
"""

@overload
def create(self, **kwargs) -> Permission:
"""Create a permission.
Permission | List[Permission]
Returns a `Permission` when the kwargs: `principal_guid`, `principal_type`, and `role`
are used. Returns a `list[Permission]` when `*args` are used.
schloerke marked this conversation as resolved.
Show resolved Hide resolved
schloerke marked this conversation as resolved.
Show resolved Hide resolved

Returns
-------
Permission
"""
Examples
--------
```python
from posit import connect

def create(self, **kwargs) -> Permission:
"""Create a permission.
client = connect.Client()
content_item = client.content.get(content_guid)

# New permission role
role = "viewer" # Or "owner"

# Example groups and users
groups = client.groups.find(prefix="GROUP_NAME_PREFIX_HERE")
user = client.users.get("USER_GUID_HERE")
users = [user]

# Add many group and user permissions with the same role
content_item.permissions.create(*groups, *users, role=role)
schloerke marked this conversation as resolved.
Show resolved Hide resolved

# Add a group permission
group = groups[0]
content_item.permissions.create(group, role=role)
# Add a user permission
content_item.permissions.create(user, role=role)

# Add a group permission manually
content_item.permissions.create(
principal_guid=group["guid"],
principal_type="group",
role=role,
)
# Add a user permission manually
content_item.permissions.create(
principal_guid=user["guid"],
principal_type="user",
role=role,
)

Returns
-------
Permission
# Confirm new permissions
content_item.permissions.find()
```
"""
if len(args) > 0:
# Avoid circular imports
from .groups import Group
from .users import User

for arg in args:
if not isinstance(arg, (User, Group)):
raise TypeError(f"Invalid argument type: {type(arg)}")
if "principal_guid" in kwargs:
raise ValueError("'principal_guid' can not be defined with `*args` present.")
schloerke marked this conversation as resolved.
Show resolved Hide resolved
if "principal_type" in kwargs:
raise ValueError("'principal_guid' can not be defined with `*args` present.")

perms: list[Permission] = []
for arg in args:
if isinstance(arg, User):
principal_type = "user"
elif isinstance(arg, Group):
principal_type = "group"
else:
raise TypeError(f"Invalid argument type: {type(arg)}")

perm = self.create(
principal_guid=arg["guid"],
principal_type=principal_type,
role=kwargs["role"],
)
perms.append(perm)
return perms

path = f"v1/content/{self.content_guid}/permissions"
url = self.params.url + path
response = self.params.session.post(url, json=kwargs)
Expand Down Expand Up @@ -158,7 +227,7 @@ def destroy(self, *permissions: str | Group | User | Permission) -> list[Permiss
Returns
-------
list[Permission]
The removed permissions. If a permission is not found, there is nothing to remove and
The removed permissions. If a permission is not found, there is nothing to remove and
it is not included in the returned list.

Examples
Expand Down
15 changes: 15 additions & 0 deletions tests/posit/connect/test_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,21 @@ def test(self):
role=role,
)

with pytest.raises(TypeError):
permissions.create( # pyright: ignore[reportCallIssue]
"not a user or group",
)
with pytest.raises(ValueError):
permissions.create( # pyright: ignore[reportCallIssue]
User(params, guid=principal_guid),
principal_guid=principal_guid,
)
with pytest.raises(ValueError):
permissions.create( # pyright: ignore[reportCallIssue]
User(params, guid=principal_guid),
principal_type=principal_type,
)

# assert
assert permission == fake_permission

Expand Down
Loading