Skip to content

Commit 1c068bb

Browse files
committed
Merge branch 'version-4.38' of https://github.com/bedatadriven/activityinfo-R into version-4.38
2 parents d9e5c76 + d957395 commit 1c068bb

File tree

9 files changed

+869
-28
lines changed

9 files changed

+869
-28
lines changed

DESCRIPTION

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,11 @@ Suggests:
4747
markdown,
4848
withr,
4949
assertthat,
50+
tidyverse,
5051
tidyr,
5152
purrr,
53+
readr,
54+
readxl,
5255
tinytex
5356
VignetteBuilder: knitr
5457
Config/testthat/edition: 3

R/databases.R

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ checkUserRole <- function(databaseId, newUser, roleId, roleParameters, roleResou
334334
#' @param roleId the id of the role to assign to the user.
335335
#' @param roleParameters a named list containing the role parameter values
336336
#' @param roleResources an optional list of optional grant-based resources assigned to the user
337+
#' @param assignment optionally create and pass a \code{\link[activityinfo]{roleAssignment}} like in updateUserRole()
337338
#'
338339
#' @details
339340
#'
@@ -381,9 +382,17 @@ checkUserRole <- function(databaseId, newUser, roleId, roleParameters, roleResou
381382
#' @export
382383
addDatabaseUser <- function(databaseId, email, name, locale = NA_character_, roleId,
383384
roleParameters = list(),
384-
roleResources = c(databaseId)) {
385+
roleResources = c(databaseId), assignment) {
385386

386387
url <- paste(activityInfoRootUrl(), "resources", "databases", databaseId, "users", sep = "/")
388+
389+
if (!missing(assignment)) {
390+
stopifnot("An assignment must be created with roleAssignment()" = ("activityInfoRoleAssignment" %in% class(assignment)))
391+
stopifnot("Either an assignment must be provided or roleId to addDatabaseUser(), but not both." = missing(roleId))
392+
roleId = assignment$id
393+
roleParameters = assignment$parameters
394+
roleResources = assignment$resources
395+
}
387396

388397
request <- list(
389398
email = email,
@@ -631,7 +640,7 @@ updateUserRole <- function(databaseId, userId, assignment) {
631640
roleAssignment <- function(roleId, roleParameters = list(), roleResources) {
632641
stopifnot(is.list(roleParameters))
633642
if (any(is.na(names(roleParameters)))) {
634-
stop("roleParameters must be named with each parameter name.")
643+
stop("In the `roleParameters` list, each item must be named")
635644
}
636645

637646
if (length(roleParameters) == 0) {
@@ -981,7 +990,8 @@ deleteRoles <- function(databaseId, roleIds) {
981990
#' See \link{role} for the creation of roles.
982991
#'
983992
#' @param id the id of the parameter, for example "partner", which can
984-
#' be used in a formula as "@user.partner"
993+
#' be used in a formula as "@user.partner". The id starts with a letter and may
994+
#' contain letters, numbers and underscores _ under 32 characters.
985995
#' @param label the label of the partner, for example, "Reporting partner"
986996
#' @param range the id of a reference table, for example the list of partners,
987997
#' or a formula
@@ -995,7 +1005,7 @@ deleteRoles <- function(databaseId, roleIds) {
9951005
#' }
9961006
parameter <- function(id, label, range) {
9971007
stopifnot("The id must be a character string" = is.null(id)||(is.character(id)&&length(id)==1&&nchar(id)>0))
998-
stopifnot("The id must start with a letter, must be made of letters and underscores _ and cannot be longer than 32 characters" = is.null(id)||grepl("^[A-Za-z][A-Za-z0-9_]{0,31}$", id))
1008+
stopifnot("The id must start with a letter, must be made of letters, numbers and underscores _ and cannot be longer than 32 characters" = is.null(id)||grepl("^[A-Za-z][A-Za-z0-9_]{0,31}$", id))
9991009
stopifnot("The label is required to be a character string" = (is.character(label)&&length(label)==1&&nchar(label)>0))
10001010
stopifnot("The range is required and must be a character string" = !is.null(range)&&(is.character(range)&&length(range)==1&&nchar(range)>0))
10011011

@@ -1109,7 +1119,8 @@ roleFilter <- function(id, label, filter) {
11091119
#' Some administrative permissions are defined at the level of the role rather
11101120
#' than within grants. See \link{databasePermissions}.
11111121
#'
1112-
#' @param id the id of the role
1122+
#' @param id the id of the role, must start with a lower case letter and may
1123+
#' contain up to 32 lower case letters and numbers
11131124
#' @param label the label or name of the role, e.g. "Viewer" or "Administrator"
11141125
#' @param parameters a list of \link{parameter} items defining role parameters
11151126
#' @param grants a list of \link{grant} items for each resource and their
@@ -1149,7 +1160,7 @@ roleFilter <- function(id, label, filter) {
11491160
#' }
11501161
role <- function(id, label, parameters = list(), grants, permissions = databasePermissions()) {
11511162
stopifnot("The id must be a character string" = is.null(id)||(is.character(id)&&length(id)==1&&nchar(id)>0))
1152-
stopifnot("The id must start with a letter, must be made of lowercase letters and underscores _ and cannot be longer than 32 characters" = is.null(id)||grepl("^[a-z][a-z0-9_]{0,31}$", id))
1163+
stopifnot("The id must start with a letter, must be made of lowercase letters and numbers and cannot be longer than 32 characters" = is.null(id)||grepl("^[a-z][a-z0-9]{0,31}$", id))
11531164

11541165
stopifnot("The label is required to be a character string" = (is.character(label)&&length(label)==1&&nchar(label)>0))
11551166

man/addDatabaseUser.Rd

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

man/parameter.Rd

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

man/role.Rd

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

tests/testthat/test-databases.R

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,32 @@ addTestUsers <- function(database, tree, nUsers = 1, roleId, roleParameters = li
8686
})
8787
}
8888

89+
90+
addTestUsersWithAssignment <- function(database, tree, nUsers = 1, assignment) {
91+
lapply(1:nUsers, function(x) {
92+
newUserEmail <- sprintf("test%[email protected]", cuid())
93+
newDatabaseUser <- addDatabaseUser(
94+
databaseId = database$databaseId,
95+
email = newUserEmail,
96+
name = "Test database user",
97+
locale = "en",
98+
assignment = assignment
99+
)
100+
101+
if (newDatabaseUser$added) {
102+
testthat::expect_identical(newDatabaseUser$user$email, newUserEmail)
103+
testthat::expect_identical(newDatabaseUser$user$role$id, assignment$id)
104+
testthat::expect_true(newDatabaseUser$user$role$resources[[1]] %in% assignment$resources)
105+
newDatabaseUser
106+
} else {
107+
warning("Could not add user with assignment.")
108+
NULL
109+
}
110+
})
111+
}
112+
113+
114+
89115
deleteTestUsers <- function(database, returnedUsers) {
90116
lapply(returnedUsers, function(newDatabaseUser) {
91117
if (newDatabaseUser$added) {
@@ -340,7 +366,7 @@ testthat::test_that("addRole() and deleteRoles() work", {
340366

341367
testthat::expect_length(addedTree$roles, length(originalTree$roles)+2)
342368

343-
testthat::test_that("deleteRoles", {
369+
testthat::test_that("deleteRoles()", {
344370
deleteRoles(database$databaseId, roleIds = c(roleId1, roleId2))
345371

346372
deletedTree <- getDatabaseTree(database$databaseId)
@@ -356,16 +382,11 @@ testthat::test_that("addRole() and deleteRoles() work", {
356382

357383
})
358384

359-
testthat::test_that("deleteRole() works", {
360-
361-
})
362-
363-
364385
testthat::test_that("updateRole() works for both legacy and new roles", {
365386
roleId <- "rp"
366387
roleLabel <- "Reporting partner"
367388

368-
# create a partner reference form
389+
# create a partner reference form with label "Reporting Partners". Label is reused to find the form later on.
369390
partnerForm <- formSchema(
370391
databaseId = database$databaseId,
371392
label = "Reporting Partners") |>
@@ -579,6 +600,26 @@ testthat::test_that("roleAssignment() works", {
579600
))
580601
})
581602

603+
604+
testthat::test_that("addDatabaseUser() accepts a role assignment with parameters and optional grants", {
605+
rpRole <- getDatabaseRole(database$databaseId, roleId = "rp")
606+
607+
optionalGrants <- as.list(unlist(lapply(rpRole$grants, function(x) {if (x$optional) return(x$resourceId)})))
608+
609+
partnerFormId = optionalGrants[[1]] # could also use the label "Reporting Partners" if multiple grants are given
610+
611+
userRoleParam <- list(
612+
partner = reference(formId = partnerFormId, recordId = "partner1")
613+
)
614+
615+
addTestUsersWithAssignment(database, tree, nUsers = 1, assignment = roleAssignment(
616+
roleId = "rp",
617+
roleParameters = userRoleParam,
618+
roleResources = optionalGrants
619+
))
620+
})
621+
622+
582623
testthat::test_that("updateGrant() works", {
583624
#old method - not tested#
584625
})

0 commit comments

Comments
 (0)