-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathuser.R
More file actions
59 lines (57 loc) · 1.58 KB
/
user.R
File metadata and controls
59 lines (57 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#' User
#'
#' Get user details
#'
#' `user_guid_from_username()` is a helper to retrieve a user GUID, given the
#' user's username. It is useful in Shiny applications for using `session$user`
#'
#' @param client A Connect R6 object
#' @param username The user's username
#'
#' @rdname user
#'
#' @export
user_guid_from_username <- function(client, username) {
validate_R6_class(client, "Connect")
user <- client$users(prefix = username)
res <- user$results
if (length(res) == 0) {
stop("ERROR: user not found")
} else if (length(res) > 1) {
filt <- purrr::keep(res, ~ .x$username == username)
if (length(filt) == 1) {
return(filt[[1]]$guid)
} else {
warning(
"WARNING: multiple users found, but a unique exact match could not be found. Returning the first"
)
return(res[[1]]$guid)
}
} else {
return(res[[1]]$guid)
}
}
#' Extract User GUID
#'
#' Helper function to extract a user GUID from either a character string or a
#' `connect_user` object.
#'
#' @param user Either a character string containing a user GUID or a
#' `connect_user` object (as returned by `Connect$user()` or `Connect$users()`)
#'
#' @return A character string containing the user GUID
#'
#' @keywords internal
get_user_guid <- function(user) {
if (is.character(user)) {
return(user)
} else if (inherits(user, "connect_user")) {
if (!is.null(user$guid)) {
return(user$guid)
} else {
stop("connect_user object does not contain a guid field")
}
} else {
stop("user must be either a character string (GUID) or a connect_user object")
}
}