Skip to content

Commit 717a7b5

Browse files
committed
support passing connect_user object to downstream functions
we still support GUID string for now
1 parent d22fbf6 commit 717a7b5

File tree

6 files changed

+81
-20
lines changed

6 files changed

+81
-20
lines changed

R/connect.R

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -495,8 +495,9 @@ Connect <- R6::R6Class(
495495
# users -----------------------------------------------
496496

497497
#' @description Get user details.
498-
#' @param guid The user GUID.
498+
#' @param guid The user GUID or a `connect_user` object.
499499
user = function(guid) {
500+
guid <- get_user_guid(guid)
500501
prepend_class(self$GET(v1_url("users", guid)), "connect_user")
501502
},
502503

@@ -589,8 +590,9 @@ Connect <- R6::R6Class(
589590
},
590591

591592
#' @description Lock a user.
592-
#' @param user_guid User GUID.
593+
#' @param user_guid User GUID or a `connect_user` object.
593594
users_lock = function(user_guid) {
595+
user_guid <- get_user_guid(user_guid)
594596
path <- v1_url("users", user_guid, "lock")
595597
message(path)
596598
self$POST(
@@ -600,8 +602,9 @@ Connect <- R6::R6Class(
600602
},
601603

602604
#' @description Unlock a user.
603-
#' @param user_guid User GUID.
605+
#' @param user_guid User GUID or a `connect_user` object.
604606
users_unlock = function(user_guid) {
607+
user_guid <- get_user_guid(user_guid)
605608
path <- v1_url("users", user_guid, "lock")
606609
self$POST(
607610
path = path,
@@ -610,9 +613,10 @@ Connect <- R6::R6Class(
610613
},
611614

612615
#' @description Update a user.
613-
#' @param user_guid User GUID.
616+
#' @param user_guid User GUID or a `connect_user` object.
614617
#' @param ... User fields.
615618
users_update = function(user_guid, ...) {
619+
user_guid <- get_user_guid(user_guid)
616620
path <- v1_url("users", user_guid)
617621
self$PUT(
618622
path = path,
@@ -645,16 +649,18 @@ Connect <- R6::R6Class(
645649

646650
#' @description Add a group member.
647651
#' @param group_guid The group GUID.
648-
#' @param user_guid The user GUID.
652+
#' @param user_guid The user GUID or a `connect_user` object.
649653
group_member_add = function(group_guid, user_guid) {
654+
user_guid <- get_user_guid(user_guid)
650655
path <- v1_url("groups", group_guid, "members")
651656
self$POST(path, body = list(user_guid = user_guid))
652657
},
653658

654659
#' @description Remove a group member.
655660
#' @param group_guid The group GUID.
656-
#' @param user_guid The user GUID.
661+
#' @param user_guid The user GUID or a `connect_user` object.
657662
group_member_remove = function(group_guid, user_guid) {
663+
user_guid <- get_user_guid(user_guid)
658664
path <- v1_url("groups", group_guid, "members", user_guid)
659665
self$DELETE(path)
660666
},

R/content.R

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,14 @@ Content <- R6::R6Class(
219219
self$connect$GET(url)
220220
},
221221
#' @description Add a principal to the ACL for this content.
222-
#' @param principal_guid GUID for the target user or group.
222+
#' @param principal_guid GUID for the target user or group. When
223+
#' `principal_type = "user"`, can also be a `connect_user` object.
223224
#' @param principal_type Acting on user or group.
224225
#' @param role The kind of content access.
225226
permissions_add = function(principal_guid, principal_type, role) {
227+
if (principal_type == "user") {
228+
principal_guid <- get_user_guid(principal_guid)
229+
}
226230
url <- v1_url("content", self$content$guid, "permissions")
227231
self$connect$POST(
228232
url,
@@ -235,10 +239,14 @@ Content <- R6::R6Class(
235239
},
236240
#' @description Alter a principal in the ACL for this content.
237241
#' @param id The target identifier.
238-
#' @param principal_guid GUID for the target user or group.
242+
#' @param principal_guid GUID for the target user or group. When
243+
#' `principal_type = "user"`, can also be a `connect_user` object.
239244
#' @param principal_type Acting on user or group.
240245
#' @param role The kind of content access.
241246
permissions_update = function(id, principal_guid, principal_type, role) {
247+
if (principal_type == "user") {
248+
principal_guid <- get_user_guid(principal_guid)
249+
}
242250
url <- v1_url("content", self$content$guid, "permissions", id)
243251
self$connect$PUT(
244252
url,
@@ -954,7 +962,6 @@ set_run_as <- function(content, run_as, run_as_current_user = FALSE) {
954962
return(content)
955963
}
956964

957-
958965
#' Delete Content
959966
#'
960967
#' Delete a content item. WARNING: This action deletes all history, configuration,
@@ -1041,6 +1048,7 @@ content_update_access_type <- function(
10411048
#' @rdname content_update
10421049
#' @export
10431050
content_update_owner <- function(content, owner_guid) {
1051+
owner_guid <- get_user_guid(owner_guid)
10441052
content_update(content = content, owner_guid = owner_guid)
10451053
}
10461054

@@ -1103,7 +1111,6 @@ unlock_content <- function(content) {
11031111
return(content)
11041112
}
11051113

1106-
11071114
#' Verify Content Name
11081115
#'
11091116
#' Ensures that a content name fits the specifications / requirements of Posit
@@ -1178,7 +1185,6 @@ delete_bundle <- function(content, bundle_id) {
11781185
return(content)
11791186
}
11801187

1181-
11821188
#' Content permissions
11831189
#'
11841190
#' Get or set content permissions for a content item
@@ -1213,6 +1219,7 @@ content_add_user <- function(content, guid, role = c("viewer", "owner")) {
12131219
validate_R6_class(content, "Content")
12141220
role <- .define_role(role)
12151221

1222+
guid <- purrr::map_chr(guid, get_user_guid)
12161223
purrr::map(guid, ~ .content_add_permission_impl(content, "user", .x, role))
12171224

12181225
return(content)
@@ -1280,6 +1287,7 @@ content_add_group <- function(content, guid, role = c("viewer", "owner")) {
12801287
#' @export
12811288
content_delete_user <- function(content, guid) {
12821289
validate_R6_class(content, "Content")
1290+
guid <- purrr::map_chr(guid, get_user_guid)
12831291
purrr::map(
12841292
guid,
12851293
~ .content_delete_permission_impl(
@@ -1333,6 +1341,7 @@ content_delete_group <- function(content, guid) {
13331341
#' @export
13341342
get_user_permission <- function(content, guid, add_owner = TRUE) {
13351343
validate_R6_class(content, "Content")
1344+
guid <- get_user_guid(guid)
13361345
res <- .get_permission(content, "user", guid, add_owner = add_owner)
13371346
if (length(res) > 0) {
13381347
return(res[[1]])
@@ -1361,7 +1370,6 @@ get_group_permission <- function(content, guid) {
13611370
}
13621371
}
13631372

1364-
13651373
#' @rdname permissions
13661374
#' @export
13671375
get_content_permissions <- function(content, add_owner = TRUE) {

R/user.R

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,28 @@ user_guid_from_username <- function(client, username) {
3232
return(res[[1]]$guid)
3333
}
3434
}
35+
36+
#' Extract User GUID
37+
#'
38+
#' Helper function to extract a user GUID from either a character string or a
39+
#' `connect_user` object.
40+
#'
41+
#' @param user Either a character string containing a user GUID or a
42+
#' `connect_user` object (as returned by `Connect$user()` or `Connect$users()`)
43+
#'
44+
#' @return A character string containing the user GUID
45+
#'
46+
#' @keywords internal
47+
get_user_guid <- function(user) {
48+
if (is.character(user)) {
49+
return(user)
50+
} else if (inherits(user, "connect_user")) {
51+
if (!is.null(user$guid)) {
52+
return(user$guid)
53+
} else {
54+
stop("connect_user object does not contain a guid field")
55+
}
56+
} else {
57+
stop("user must be either a character string (GUID) or a connect_user object")
58+
}
59+
}

man/Content.Rd

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/PositConnect.Rd

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/get_user_guid.Rd

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)