Revoking Access from a Group

Problem

You need to revoke a group’s access to a content item.

Solution

Use the Python or R SDKs to revoke access. You need to specify the following information:

  1. The content_guid for the content item from which you want to remove permissions.
  2. Either the group_guid or group_name for the group whose access is being revoked. The group_name is used if group_guid is blank.
from posit import connect

#### User-defined inputs ####
# 1. specify the guid for the content item
content_guid = "CONTENT_GUID_HERE"
# 2. specify either the guid or name for the group to be removed (group_name will be used if group_guid is blank)
group_guid = ""
group_name = "GROUP_NAME_HERE"
############################

client = connect.Client()

# search by group_name to find the group_guid if blank
if not group_guid and group_name:
    group_match = client.get("/v1/groups", params={"prefix": group_name}).json()
    if not group_match["results"]:
        raise Exception("Invalid group name")
    elif len(group_match["results"]) != 1:
        raise Exception("More than one group name found, ensure you enter a unique name")
    else:
        group_guid = group_match["results"][0]["guid"]
elif not group_name:
    raise Exception("Either group_guid or group_name must be specified")

# For the specified content item remove the desired user
for perm in client.content.get(content_guid).permissions.find():
    if perm.principal_guid == group_guid:
        perm.delete()

# Confirm new permissions
client.content.get(content_guid).permissions.find()
library(connectapi)

# 1. specify the guid for the content item
content_guid = "CONTENT_GUID_HERE"
# 2. specify either the guid or name for the group to be removed (group_name will be used if group_guid is blank)
group_guid = ""
group_name = "GROUP_NAME_HERE"

client <- connect()

# search by group_name to find the group_guid if blank
if (group_guid == "" && group_name != "") {
  group_match <- get_groups(client, prefix = group_name)
  if (nrow(group_match) == 0) {
    stop("Invalid group name")
  } else if (length(unique(group_match$name)) != 1) {
    stop("More than one group name found, ensure you enter a unique name")
  } else {
    group_guid <- unique(group_match$guid)
  }
} else if (group_name == "") {
  stop("Either group_guid or group_name must be specified")
}

# For the specified content item remove the desired group
content <- content_item(client, content_guid)
content_delete_group(content, group_guid)

# Confirm new permissions
get_content_permissions(content)