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

Add "Find groups from user" recipe #340

Closed
schloerke opened this issue Nov 15, 2024 · 1 comment · Fixed by #341
Closed

Add "Find groups from user" recipe #340

schloerke opened this issue Nov 15, 2024 · 1 comment · Fixed by #341
Assignees
Labels
enhancement New feature or request sdk Used for automation

Comments

@schloerke
Copy link
Collaborator

schloerke commented Nov 15, 2024

Docs issue: https://github.com/rstudio/connect/issues/28176

Proposal that can be done today:

user = client.users.get("USER_GUID_HERE")
user_guid = user["guid"]

user_groups = user.groups.find()
# For every group
for group in client.groups.find():
    # Get group members
    group_guid = group["guid"]
    response = client.get("v1/groups/{group_guid}/members")
    members_raw = response.json()
    # For each member in the group
    for member_raw in members_raw:
        if member_raw["guid"] == user_guid:
            user_groups.append(group)
            break
user_groups

Proposal if Group.members is added:

## By implementing `Group.members` property...
for group in client.groups.find():
    group_members: list[User] = group.members.find()
    for group_member in group_members:
        if group_member['guid'] == user_guid:
            user_groups.append(group)
            break

Proposal if User.groups property and Group.members property are added

## By implementing `User.groups` property...
user_groups = user.groups.find()

# ...where
@property
def user_groups(self) -> UserGroups:
    ...

class UserGroups(Resource):

    def find() -> list[User]:
        user_groups: list[Group] = []
    
        for group in self.params.client.groups.find():
            group_members = group.members.find()
            for group_member in group_members:
                if group_member['guid'] == user_guid:
                    user_groups.append(group)
                    break

        return user_groups
@schloerke schloerke added enhancement New feature or request sdk Used for automation labels Nov 15, 2024
@schloerke schloerke self-assigned this Nov 15, 2024
@schloerke
Copy link
Collaborator Author

Final recipe:

Retrieve the groups to which the user belongs:

user = client.users.get("USER_GUID_HERE")
groups = user.groups.find()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request sdk Used for automation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant