Managing a User’s Groups
Problem
You want to view or adjust a user’s group memberships.
Solution
from posit.connect import Client
= Client()
client
= "GROUP_GUID_HERE"
group_guid = "USER_GUID_HERE"
user_guid
# Get user and group objects
= client.groups.get(group_guid)
group = client.users.get(user_guid)
user
# View groups that a user belongs to
= user.groups.find()
user_groups
# Add user to a group
user.groups.add(group)
# Remove user from a group
user.groups.delete(group)
library(connectapi)
library(dplyr)
library(purrr)
<- connect()
client
= "GROUP_GUID_HERE"
group_guid = "USER_GUID_HERE"
user_guid
# View groups that a user belongs to
<-
user_groups get_groups(client) %>%
mutate(
members = map(guid, ~ get_group_members(client, .x))
%>%
) filter(map_lgl(members, ~ nrow(.x) > 0)) %>%
filter(map_lgl(members, ~ user_guid %in% .x$guid)) %>%
mutate(members = NULL)
# Add a user to a group
$PUT(paste0("groups/", group_guid, "/members"), body=list(user_guid=user_guid))
client
# Remove a user from a group
$DELETE(paste0("groups/", group_guid, "/members/", user_guid)) client