Created
August 13, 2024 08:49
-
-
Save akbertram/435d36fae04230af9cbf721b32691e89 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(activityinfo) | |
## Change configuration here | |
databaseId <- "cjw8lkpkfqhhcl92" | |
fromRoleLabel <- "Reporting partner" | |
toRoleLabel <- "Reporting partner (2)" | |
## End configuration | |
### Find the from and to role definitions | |
roles <- getDatabaseTree(databaseId)$roles | |
roleLabels <- sapply(roles, function(r) r$label) | |
## Check that labels are correct | |
if(!(fromRoleLabel %in% roleLabels)) { | |
stop(sprintf("Role '%s' not found in database", fromRoleLabel)) | |
} | |
if(!(toRoleLabel %in% roleLabels)) { | |
stop(sprintf("Role '%s' not found in database", toRoleLabel)) | |
} | |
fromRole <- roles[[ which(roleLabels == fromRoleLabel) ]] | |
toRole <- roles[[ which(roleLabels == toRoleLabel) ]] | |
## Map old parameters to new parameters | |
parameterMap <- character(0) | |
fromParameterLabels <- sapply(fromRole$parameters, function(p) p$label) | |
for(toParameter in toRole$parameters) { | |
if(!(toParameter$label %in% fromParameterLabels)) { | |
stop(sprintf("Role '%s' does not have a parameter named '%s', cannot migrate", fromRole$label, toParameter$label)) | |
} | |
fromParameter <- fromRole$parameters[[ which(fromParameterLabels == toParameter$label )]] | |
parameterMap[toParameter$parameterId] <- fromParameter$parameterId | |
} | |
## Now loop through all users and reassign them to the new role | |
users <- getDatabaseUsers(databaseId) | |
for(i in seq_along(users$userId)) { | |
userId <- users[i, "userId"] | |
fromAssignment <- users[i, "role"][[1]] | |
if(identical(fromAssignment$id, fromRole$id)) { | |
newAssignment <- list(id = toRole$id, parameters = list()) | |
for(parameterId in names(parameterMap)) { | |
newAssignment$parameters[[ parameterId ]] <- fromAssignment$parameters[[ parameterMap[parameterId ]]] | |
} | |
str(newAssignment) | |
updateUserRole(databaseId, userId, newAssignment) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment