Skip to content

Instantly share code, notes, and snippets.

@akbertram
Created August 13, 2024 08:49
Show Gist options
  • Save akbertram/435d36fae04230af9cbf721b32691e89 to your computer and use it in GitHub Desktop.
Save akbertram/435d36fae04230af9cbf721b32691e89 to your computer and use it in GitHub Desktop.
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