You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
6.0KB

  1. #' Add an user
  2. #'
  3. #' Add an i2b2 user to the instance
  4. #'
  5. #' @param admin The username to connect with
  6. #' @param pass The password for the user
  7. #' @param id The id (no spaces, unique) of the new user
  8. #' @param name The full name of the new user
  9. #' @param email The email of the new user
  10. #' @param password The password for the new user
  11. #' @param url The URL of the i2b2 cell to communicate with
  12. #' @param domain The domain to act on
  13. #' @return The XML return message as an httr::content() object
  14. #' @export
  15. add_user <- function(admin, pass, id, name, email, password, url = "http://127.0.0.1:9090/i2b2/services/PMService/getServices", domain = "")
  16. {
  17. base_msg() %>%
  18. add_header(admin, pass, domain) %>%
  19. add_body("pm:set_user", user_name = id, full_name = name, email = email, password = password, is_admin = 0) %>%
  20. send_msg(url)
  21. }
  22. #' Add user roles
  23. #'
  24. #' Add roles to an i2b2 user
  25. #'
  26. #' @param admin The username to connect with
  27. #' @param pass The password for the user
  28. #' @param id The id (no spaces, unique) of the new user
  29. #' @param project The project to add the user role to
  30. #' @param roles A character vector of roles to add
  31. #' @param url The URL of the i2b2 cell to communicate with
  32. #' @param domain The domain to act on
  33. #' @return The XML return message as an httr::content() object
  34. #' @export
  35. add_user_roles <- function(admin, pass, id, project, roles, url = "http://127.0.0.1:9090/i2b2/services/PMService/getServices", domain = "")
  36. {
  37. roles %>%
  38. purrr::walk(function(role)
  39. {
  40. base_msg() %>%
  41. add_header(admin, pass, domain) %>%
  42. add_body("pm:set_role", user_name = id, role = role, project_id = project) %>%
  43. send_msg(url)
  44. })
  45. }
  46. #' Add multiple users
  47. #'
  48. #' Add users in bulk from a dataframe
  49. #'
  50. #' Add users in bulk from a dataframe containing the following columns:
  51. #' * id: The unique user id to add for each user
  52. #' * name: The name for each user
  53. #' * email: The email address for each user
  54. #' * password: The password for each user
  55. #' * role: The role to give the user
  56. #'
  57. #' Any role can be given from the ones defined
  58. #' (ADMIN, MANAGER, USER, DATA_PROT, DATA_DEID, DATA_LDS, DATA_AGG, DATA_OBFSC)
  59. #' ADMIN gives all roles for the project, and ADMIN role to project @
  60. #' MANAGER gives all roles down from DATA_DEID for the project
  61. #' USER gives USER and DATA_OBFSC roles for the project
  62. #' Any DATA_* role gives USER and roles down from the DATA_* role given to the project
  63. #'
  64. #' @param admin The username to connect with
  65. #' @param pass The password for the user
  66. #' @param users The dataframe containing the users to add
  67. #' @param url The URL of the i2b2 cell to communicate with
  68. #' @param domain The domain to act on
  69. #' @return The XML return message as an httr::content() object
  70. #' @export
  71. add_users <- function(admin, pass, users, url = "http://127.0.0.1:9090/i2b2/services/PMService/getServices", domain = "")
  72. {
  73. apply(users, 1, function(user)
  74. {
  75. add_user(admin, pass, user["id"], user["name"], user["email"], user["password"], url, domain)
  76. if (user["role"] == "ADMIN")
  77. {
  78. roles <- c("MANAGER", "USER", "DATA_PROT", "DATA_DEID", "DATA_LDS", "DATA_AGG", "DATA_OBFSC")
  79. add_user_roles(admin, pass, user["id"], "@", "ADMIN", url, domain)
  80. }
  81. else if (user["role"] == "MANAGER")
  82. roles <- c("MANAGER", "USER", "DATA_DEID", "DATA_LDS", "DATA_AGG", "DATA_OBFSC")
  83. else if (user["role"] == "USER")
  84. roles <- c("USER", "DATA_OBFSC")
  85. else if (user["role"] == "DATA_PROT")
  86. roles <- c("USER", "DATA_PROT", "DATA_DEID", "DATA_LDS", "DATA_AGG", "DATA_OBFSC")
  87. else if (user["role"] == "DATA_DEID")
  88. roles <- c("USER", "DATA_DEID", "DATA_LDS", "DATA_AGG", "DATA_OBFSC")
  89. else if (user["role"] == "DATA_LDS")
  90. roles <- c("USER", "DATA_LDS", "DATA_AGG", "DATA_OBFSC")
  91. else if (user["role"] == "DATA_AGG")
  92. roles <- c("USER", "DATA_AGG", "DATA_OBFSC")
  93. else if (user["role"] == "DATA_OBFSC")
  94. roles <- c("USER", "DATA_OBFSC")
  95. add_user_roles(admin, pass, user["id"], user["project"], roles, url, domain)
  96. })
  97. }
  98. #' Delete users
  99. #'
  100. #' Delete i2b2 users from the instance
  101. #'
  102. #' @param host The host to connect to
  103. #' @param admin The admin account for the PostgreSQL database
  104. #' @param pass The password for the database admin
  105. #' @param users A character vector of user ids
  106. #' @export
  107. delete_users <- function(users, host = "", admin = "", pass = "")
  108. {
  109. pm <- RPostgreSQL::dbConnect(RPostgreSQL::PostgreSQL(), host = host, dbname = "i2b2pm", user = admin, password = pass)
  110. user <- users %>% stringr::str_c(collapse = "','") %>% stringr::str_c("'", . ,"'")
  111. RPostgreSQL::dbGetQuery(pm, stringr::str_c("DELETE FROM pm_user_data WHERE (user_id IN (", user, "));"))
  112. RPostgreSQL::dbGetQuery(pm, stringr::str_c("DELETE FROM pm_project_user_roles WHERE (user_id IN (", user, "));"))
  113. RPostgreSQL::dbDisconnect(pm)
  114. }
  115. #' List users
  116. #'
  117. #' Delete i2b2 users from the instance
  118. #'
  119. #' @param host The host to connect to
  120. #' @param admin The admin account for the PostgreSQL database
  121. #' @param pass The password for the database admin
  122. #' @export
  123. list_users <- function(host = "", admin = "", pass = "")
  124. {
  125. dplyr::src_postgres("i2b2pm", host = host, user = admin, pass = pass) %>%
  126. dplyr::tbl("pm_user_data") %>%
  127. dplyr::select(user_id, full_name, email, project_path) %>%
  128. dplyr::collect()
  129. }
  130. #' List user roles
  131. #'
  132. #' Delete i2b2 users from the instance
  133. #'
  134. #' @param user An user id
  135. #' @param host The host to connect to
  136. #' @param admin The admin account for the PostgreSQL database
  137. #' @param pass The password for the database admin
  138. #' @export
  139. list_user_roles <- function(user, host = "", admin = "", pass = "")
  140. {
  141. dplyr::src_postgres("i2b2pm", host = host, user = admin, pass = pass) %>%
  142. dplyr::tbl("pm_project_user_roles") %>%
  143. dplyr::filter(user_id == user) %>%
  144. dplyr::select(project_id, user_id, user_role_cd) %>%
  145. dplyr::collect()
  146. }