Skip to content

Commit 0cfd68d

Browse files
committed
Update safe_data.opal to include user permissions for the datasets, if user is provided
1 parent fbda551 commit 0cfd68d

File tree

1 file changed

+52
-1
lines changed

1 file changed

+52
-1
lines changed

R/safe_data.R

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ safe_data.opal <- function(
6060
user = NULL
6161
) {
6262
# declare local bindings
63-
created <- lastUpdate <- name <- new_dataset_entity <- NULL
63+
created <- lastUpdate <- name <- new_dataset_entity <- subject <- NULL
6464

6565
# x is a valid opal connection object
6666
validate_opal_con(x)
@@ -123,10 +123,61 @@ safe_data.opal <- function(
123123
return(new_dataset_entity)
124124
})
125125

126+
# initialise empty list with entities for user level permissions
127+
user_perm_entity_lst <- NULL
128+
129+
# extract user permissions, if `user` is not NULL
130+
if (!is.null(user)) {
131+
## get permissions for each table in the project
132+
## get table permissions
133+
project_table_permissions_tbl <- seq_along(project_tables) |>
134+
lapply(\(i) get_table_permissions(x, project, project_tables[i])) |>
135+
dplyr::bind_rows() |>
136+
dplyr::filter(subject == !!user)
137+
138+
## create a safe data entities data frame
139+
safe_data_entities_tbl <- tibble::tibble(
140+
table_id = paste0(project, "_", project_tables),
141+
name = project_tables
142+
) |>
143+
dplyr::mutate(
144+
table_id = paste0(dataset_id_suffix, sapply(table_id, digest::digest))
145+
)
146+
147+
safe_people_entities_tbl <- x |>
148+
safe_people(user = user) |>
149+
flatten_safe_people() |>
150+
dplyr::rename("user_id" = "id")
151+
152+
## combine the table permissions with Dataset & People entities' @ids
153+
project_table_permissions_tbl_v2 <- project_table_permissions_tbl |>
154+
dplyr::left_join(safe_data_entities_tbl, by = c("table" = "name")) |>
155+
dplyr::left_join(safe_people_entities_tbl, by = c("subject" = "name")) |>
156+
dplyr::rename(user = subject)
157+
158+
## generate user permission entities and add to the RO-Crate
159+
user_perm_entity_lst <- project_table_permissions_tbl_v2 |>
160+
purrr::pmap(user_perm_entity) |>
161+
purrr::list_c()
162+
}
163+
126164
# add table entities to the `rocrate` object
127165
for (i in seq_along(project_dataset_entities)) {
128166
rocrate <- rocrate |>
129167
rocrateR::add_entity(project_dataset_entities[[i]], overwrite = TRUE)
168+
169+
# add user permissions associated to the current table (if any)
170+
table_id <- getElement(project_dataset_entities[[i]], "@id")
171+
usr_pr_idx <- sapply(user_perm_entity_lst, getElement, "object") == table_id
172+
173+
# add entity (if any) to the RO-Crate
174+
if (any(usr_pr_idx)) {
175+
rocrate <- rocrate |>
176+
rocrateR::add_entity(
177+
user_perm_entity_lst[usr_pr_idx][[1]],
178+
overwrite = TRUE
179+
)
180+
}
130181
}
131182

132183
# attach input arguments as attributes

0 commit comments

Comments
 (0)