Skip to content

Commit 3a6afd0

Browse files
committed
support passing connect_user object to downstream functions
we still support GUID string for now
1 parent 4f829eb commit 3a6afd0

File tree

6 files changed

+81
-16
lines changed

6 files changed

+81
-16
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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,14 @@ Content <- R6::R6Class(
226226
self$get_connect()$GET(url)
227227
},
228228
#' @description Add a principal to the ACL for this content.
229-
#' @param principal_guid GUID for the target user or group.
229+
#' @param principal_guid GUID for the target user or group. When
230+
#' `principal_type = "user"`, can also be a `connect_user` object.
230231
#' @param principal_type Acting on user or group.
231232
#' @param role The kind of content access.
232233
permissions_add = function(principal_guid, principal_type, role) {
234+
if (principal_type == "user") {
235+
principal_guid <- get_user_guid(principal_guid)
236+
}
233237
url <- v1_url("content", self$get_content()$guid, "permissions")
234238
self$get_connect()$POST(
235239
url,
@@ -242,10 +246,14 @@ Content <- R6::R6Class(
242246
},
243247
#' @description Alter a principal in the ACL for this content.
244248
#' @param id The target identifier.
245-
#' @param principal_guid GUID for the target user or group.
249+
#' @param principal_guid GUID for the target user or group. When
250+
#' `principal_type = "user"`, can also be a `connect_user` object.
246251
#' @param principal_type Acting on user or group.
247252
#' @param role The kind of content access.
248253
permissions_update = function(id, principal_guid, principal_type, role) {
254+
if (principal_type == "user") {
255+
principal_guid <- get_user_guid(principal_guid)
256+
}
249257
url <- v1_url("content", self$get_content()$guid, "permissions", id)
250258
self$get_connect()$PUT(
251259
url,
@@ -1048,6 +1056,7 @@ content_update_access_type <- function(
10481056
#' @rdname content_update
10491057
#' @export
10501058
content_update_owner <- function(content, owner_guid) {
1059+
owner_guid <- get_user_guid(owner_guid)
10511060
content_update(content = content, owner_guid = owner_guid)
10521061
}
10531062

@@ -1161,6 +1170,7 @@ content_add_user <- function(content, guid, role = c("viewer", "owner")) {
11611170
validate_R6_class(content, "Content")
11621171
role <- .define_role(role)
11631172

1173+
guid <- purrr::map_chr(guid, get_user_guid)
11641174
purrr::map(guid, ~ .content_add_permission_impl(content, "user", .x, role))
11651175

11661176
return(content)
@@ -1228,6 +1238,7 @@ content_add_group <- function(content, guid, role = c("viewer", "owner")) {
12281238
#' @export
12291239
content_delete_user <- function(content, guid) {
12301240
validate_R6_class(content, "Content")
1241+
guid <- purrr::map_chr(guid, get_user_guid)
12311242
purrr::map(
12321243
guid,
12331244
~ .content_delete_permission_impl(
@@ -1281,6 +1292,7 @@ content_delete_group <- function(content, guid) {
12811292
#' @export
12821293
get_user_permission <- function(content, guid, add_owner = TRUE) {
12831294
validate_R6_class(content, "Content")
1295+
guid <- get_user_guid(guid)
12841296
res <- .get_permission(content, "user", guid, add_owner = add_owner)
12851297
if (length(res) > 0) {
12861298
return(res[[1]])

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)