Revoking Access from a User
Problem
You need to revoke a user’s access to a content item.
Solution
Use the Python or R SDKs to revoke access. You need to specify the following information:
- The
content_guid
for the content item from which you want to remove permissions. - Either the
user_guid
orusername
for the user whose access is being revoked. Theusername
is used ifuser_guid
is blank.
from posit import connect
# 1. specify the guid for the content item
= "CONTENT_GUID_HERE"
content_guid # 2. specify either the guid or name for the user being removed (username will be used if user_guid is blank)
= ""
user_guid = "USERNAME_HERE"
username
= connect.Client()
client
# search by username to find the user_guid if blank
if not user_guid and username:
= client.users.find(prefix=username)
user_match if not user_match:
raise Exception("Invalid username")
elif len(user_match) != 1:
raise Exception("More than one username found, ensure you enter a unique name")
else:
= user_match[0]["guid"]
user_guid elif not username:
raise Exception("Either user_guid or username 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 == user_guid:
perm.delete()
# Confirm new permissions
client.content.get(content_guid).permissions.find()
library(connectapi)
#### User-defined inputs ####
# 1. specify the guid for the content item
= "CONTENT_GUID_HERE"
content_guid # 2. specify either the guid or name for the user being removed (username will be used if user_guid is blank)
= ""
user_guid = "USERNAME_HERE"
username ############################
<- connect()
client
# search by username to find the user_guid if blank
if (user_guid == "" && username != "") {
<- get_users(client, prefix = username)
user_match if (nrow(user_match) == 0) {
stop("Invalid username")
else if (length(unique(user_match$username)) != 1) {
} stop("More than one username found, ensure you enter a unique name")
else {
} <- unique(user_match$guid)
user_guid
}else if (username == "") {
} stop("Either user_guid or username must be specified")
}
# For the specified content item remove the desired user
<- content_item(client, content_guid)
content content_delete_user(content, user_guid)
# Confirm new permissions
get_content_permissions(content)