Skip to content

Commit 2478ce8

Browse files
committed
Enable setting/getting of asset-level permissions in the latest gobbler.
1 parent 79d8878 commit 2478ce8

File tree

7 files changed

+102
-29
lines changed

7 files changed

+102
-29
lines changed

R/fetchPermissions.R

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
#' This will call the REST API if the caller is not on the same filesystem as the registry.
55
#'
66
#' @param project String containing the project name.
7+
#' @param asset String containing the asset name.
8+
#' If specified, permissions are retrieved for the asset rather than the entire project.
79
#' @inheritParams listProjects
810
#'
9-
#' @return List containing the permissions for this project.
10-
#' This has the following elements:
11+
#' @return List containing the permissions for this project/asset.
12+
#' For project-level permissions, the list has the following elements:
1113
#' \itemize{
1214
#' \item \code{owners}, a character vector containing the user IDs of owners of this project.
1315
#' \item \code{uploaders}, a list of lists specifying the users or organizations who are authorzied to upload to this project.
@@ -27,6 +29,7 @@
2729
#' In this mode, any user can create any number of new assets in this project.
2830
#' Each user can also upload new versions of any asset that they created in this mode.
2931
#' }
32+
#' For asset-level permissions, the list has \code{owners} and \code{uploaders} to describe the owners and uploaders, respectively, for the specified \code{asset}.
3033
#'
3134
#' @author Aaron Lun
3235
#'
@@ -50,17 +53,36 @@
5053
#' @export
5154
#' @importFrom jsonlite fromJSON
5255
#' @import httr2
53-
fetchPermissions <- function(project, registry, url, forceRemote=FALSE) {
54-
if (file.exists(registry) && !forceRemote) {
55-
content <- file.path(registry, project, "..permissions")
56+
fetchPermissions <- function(project, registry, url, asset=NULL, forceRemote=FALSE) {
57+
use.registry <- (file.exists(registry) && !forceRemote)
58+
59+
if (is.null(asset)) {
60+
if (use.registry) {
61+
content <- file.path(registry, project, "..permissions")
62+
} else {
63+
req <- request(paste0(url, "/fetch/", paste(project, "..permissions", sep="/")))
64+
resp <- req_perform(req)
65+
content <- resp_body_string(resp)
66+
}
67+
perms <- fromJSON(content, simplifyVector=FALSE)
68+
5669
} else {
57-
req <- request(paste0(url, "/fetch/", paste(project, "..permissions", sep="/")))
58-
resp <- req_perform(req)
59-
content <- resp_body_string(resp)
70+
perms <- list(owners=list(), uploaders=list())
71+
if (use.registry) {
72+
content <- file.path(registry, project, asset, "..permissions")
73+
if (file.exists(content)) {
74+
perms <- fromJSON(content, simplifyVector=FALSE)
75+
}
76+
} else {
77+
perms <- tryCatch({
78+
req <- request(paste0(url, "/fetch/", paste(project, asset, "..permissions", sep="/")))
79+
resp <- req_perform(req)
80+
content <- resp_body_string(resp)
81+
fromJSON(content, simplifyVector=FALSE)
82+
}, httr2_http_404 = function(cnd) perms)
83+
}
6084
}
6185

62-
perms <- fromJSON(content, simplifyVector=FALSE)
63-
6486
# Converting everything to POSIX dates.
6587
for (i in seq_along(perms$uploaders)) {
6688
current <- perms$uploaders[[i]]

R/setPermissions.R

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33
#' Set the owner and uploader permissions for a project.
44
#'
55
#' @param project String containing the project name.
6-
#' @param owners Character vector containing the user IDs for owners of this project.
6+
#' @param asset String containing the asset name.
7+
#' If specified, permissions are set on the asset rather than the entire project.
8+
#' @param owners Character vector containing the user IDs for owners of this project/asset.
79
#' If \code{NULL}, no change is made to the existing owners of the project.
8-
#' @param uploaders List specifying the authorized uploaders for this project.
10+
#' @param uploaders List specifying the authorized uploaders for this project/asset.
911
#' See the \code{uploaders} field in the \code{\link{fetchPermissions}} return value for the expected format.
10-
#' If \code{NULL}, no change is made to the existing uploaders of the project.
12+
#' If \code{NULL}, no change is made to the existing uploaders of the project/asset.
1113
#' @param globalWrite Logical scalar indicating whether global writes should be enabled (see \code{\link{fetchPermissions}} for details).
1214
#' If \code{NULL}, no change is made to the global write status of the project.
13-
#' @param append Logical scalar indicating whether \code{owners} and \code{uploaders} should be appended to the existing owners and uploaders, respectively, of the project.
15+
#' Ignored if \code{asset} is specified.
16+
#' @param append Logical scalar indicating whether \code{owners} and \code{uploaders} should be appended to the existing owners and uploaders, respectively, of the project/asset.
1417
#' If \code{FALSE}, the \code{owners} and \code{uploaders} are used to replace the existing values.
1518
#' @param registry String containing a path to the registry.
1619
#' @inheritParams createProject
@@ -49,10 +52,12 @@
4952
#' fetchPermissions("test", registry=info$registry)
5053
#'
5154
#' @export
52-
setPermissions <- function(project, registry, staging, url, owners=NULL, uploaders=NULL, globalWrite=NULL, append=TRUE) {
55+
setPermissions <- function(project, registry, staging, url, asset=NULL, owners=NULL, uploaders=NULL, globalWrite=NULL, append=TRUE) {
5356
perms <- list()
57+
names(perms) <- character(0)
58+
5459
if (append) {
55-
old.perms <- fetchPermissions(project, registry=registry)
60+
old.perms <- fetchPermissions(project, asset=asset, registry=registry, url=url)
5661
if (!is.null(owners)) {
5762
perms$owners <- as.list(union(unlist(old.perms$owners), owners))
5863
}
@@ -72,10 +77,14 @@ setPermissions <- function(project, registry, staging, url, owners=NULL, uploade
7277
perms$uploaders <- sanitize_uploaders(perms$uploaders)
7378
}
7479

75-
if (!is.null(globalWrite)) {
80+
payload <- list(project=project)
81+
if (!is.null(asset)) {
82+
payload$asset <- asset
83+
} else if (!is.null(globalWrite)) {
7684
perms$global_write <- globalWrite
7785
}
7886

79-
dump_request(staging, url, "set_permissions", list(project=project, permissions=perms))
87+
payload$permissions <- perms
88+
dump_request(staging, url, "set_permissions", payload)
8089
invisible(NULL)
8190
}

R/startGobbler.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
#'
3737
#' @export
3838
#' @importFrom utils download.file
39-
startGobbler <- function(staging=tempfile(), registry=tempfile(), port = NULL, wait = 1, version = "0.3.8", overwrite = FALSE) {
39+
startGobbler <- function(staging=tempfile(), registry=tempfile(), port = NULL, wait = 1, version = "0.3.9", overwrite = FALSE) {
4040
if (!is.null(running$active)) {
4141
return(list(new=FALSE, staging=running$staging, registry=running$registry, port=running$port, url=assemble_url(running$port)))
4242
}

man/fetchPermissions.Rd

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

man/setPermissions.Rd

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

man/startGobbler.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-permissions.R

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ info <- startGobbler()
55
removeProject("test-perms", staging=info$staging, url=info$url)
66
createProject("test-perms", staging=info$staging, url=info$url, owners="LTLA")
77

8-
test_that("permission setting works as expected", {
8+
test_that("project-level permission setting works as expected", {
99
until <- round(Sys.time() + 1000000)
1010
setPermissions("test-perms",
1111
owners="jkanche",
@@ -59,3 +59,36 @@ test_that("permission setting works as expected", {
5959
perms <- fetchPermissions("test-perms", registry=info$registry)
6060
expect_false(perms$global_write)
6161
})
62+
63+
test_that("asset-level permission setting works as expected", {
64+
until <- round(Sys.time() + 1000000)
65+
setPermissions("test-perms",
66+
asset="foobar",
67+
owners="jkanche",
68+
uploaders=list(
69+
list(id="lawremi", until=until)
70+
),
71+
staging=info$staging,
72+
url=info$url,
73+
registry=info$registry
74+
)
75+
76+
perms <- fetchPermissions("test-perms", asset="foobar", registry=info$registry)
77+
expect_identical(perms$owners, list("jkanche"))
78+
expect_identical(length(perms$uploaders), 1L)
79+
expect_identical(perms$uploaders[[1]]$id, "lawremi")
80+
expect_equal(perms$uploaders[[1]]$until, until)
81+
expect_null(perms$global_write)
82+
83+
# Works with remote.
84+
rperms <- fetchPermissions("test-perms", asset="foobar", forceRemote=TRUE, registry=info$registry, url=info$url)
85+
expect_identical(perms, rperms)
86+
87+
# Works correctly when there are no permissions.
88+
perms <- fetchPermissions("test-perms", asset="stuff", registry=info$registry)
89+
expect_identical(perms$owners, list())
90+
expect_identical(perms$uploaders, list())
91+
92+
rperms <- fetchPermissions("test-perms", asset="stuff", forceRemote=TRUE, registry=info$registry, url=info$url)
93+
expect_identical(perms, rperms)
94+
})

0 commit comments

Comments
 (0)