Granting Access to a User
Problem
You need to grant a user access to a content item.
Solution
Use the Python or R SDKs to grant access. You need to specify the following information:
- The
content_guid
for the content item from which you want to add permissions. - Either the
user_guid
orusername
for the user being granted access. Theusername
is used ifuser_guid
is blank. - The
access_type
which determines the type of permissions the user receives. Valid values are “viewer” and “owner” (collaborator).
from posit import connect
# 1. specify the guid for the content item
= "CONTENT_GUID_HERE"
content_guid # 2. specify either the guid or username for the user being added (username will be used if user_guid is blank)
= ""
user_guid = "USERNAME_HERE"
username # 3. specify if the user should be added as a "viewer" or "owner" (collaborator)
= "viewer"
access_type
= 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 add the desired user
client.content.get(content_guid).permissions.create(=user_guid,
principal_guid="user",
principal_type=access_type,
role
)
# Confirm new permissions
client.content.get(content_guid).permissions.find()
library(connectapi)
# 1. specify the guid for the content item
= "CONTENT_GUID_HERE"
content_guid # 2. specify either the guid or name for the user being added (username will be used if user_guid is blank)
= ""
user_guid = "USERNAME_HERE"
username # 3. specify if the user should be added as a "viewer" or "owner" (collaborator)
= "viewer"
access_type
<- 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 add the desired user
<- content_item(client, content_guid)
content content_add_user(content, user_guid, role = access_type)
# Confirm new permissions
get_content_permissions(content)