R/access_management_helpers.R

Defines functions initialize_s3_users_db connect_to_s3_board add_new_report delete_users_group create_users_group add_users_to_group

Documented in add_new_report add_users_to_group connect_to_s3_board create_users_group delete_users_group

#' add_users_to_group
#' @param users_db database of all users
#' @param new_users list of users
#' @param target_group group to which to add users to
#' @export

add_users_to_group <- function(users_db = NULL,new_users = NULL, target_group = NULL){
  group_already_exists <- users_db$groups%>%dplyr::filter(group_name == !!target_group)%>%nrow() == 1
  if(group_already_exists == FALSE)print("group not found")
  new_users <- new_users[!new_users%in%users_db$users_groups$user_name]
  if(length(new_users)> 0 & group_already_exists == TRUE){
    new_user_group <- data.frame(user_name  = new_users, users_group = target_group)
    users_db$users_groups <- users_db$users_groups %>%dplyr::bind_rows(new_user_group)
  }
  return(users_db)
}

#' create_users_group
#' @param users_db database of all users
#' @param group_name name of the group
#' @param group_description description of the group
#' @export

create_users_group <- function(users_db = NULL,group_name = NULL, group_description = ""){
  group_already_exists <- users_db$groups%>%dplyr::filter(group_name == !!group_name)%>%nrow() == 1
  if(group_already_exists == TRUE)shinyalert::shinyalert(title = "Group Creation", text ="Already Exist", type = "warning")
  if(length(group_name) == 1 & group_already_exists == FALSE){
    new_group <- data.frame(group_name = group_name, group_description = group_description)
    users_db$groups <-users_db$groups%>%dplyr::bind_rows(new_user_group)
    shinyalert::shinyalert(title = "Group Creation", text ="Successful", type = "success")
  }
  return(users_db)
}


#' delete_users_group
#' @param users_db database of all users
#' @param group_name name of the group
#' @export

delete_users_group <- function(users_db = NULL,target_group = NULL){
  group_already_exists <- users_db$groups%>%dplyr::filter(group_name == !!target_group)%>%nrow() == 1
  if(group_already_exists == FALSE)shinyalert::shinyalert(title = "Group Deletion", text ="Not Available", type = "warning")
  if(group_already_exists == TRUE){
    users_db$groups <- users_db$groups%>%dplyr::filter(group_name != !! target_group)
    shinyalert::shinyalert(title = "Group Deletion", text ="Successful", type = "success")
  }
  return(users_db)
}


#' add_new_report
#' @param users_db database of all users
#' @param user_names list of new users
#' @param as_admin group to which to add users to
#' @export

add_new_report <- function(users_db = NULL,report_name = NULL,report_creator = NULL, co_creators = NULL,access_actors = NULL , expiration_date = NULL){
  if(is.null(report_name))return(users_db)
  reports_exists <- users_db$reports_access%>%dplyr::filter(report_name ==!!report_name)%>%nrow() > 0
  if(reports_exists == TRUE){
    print("Report (name) Exists already: please change it")
    return(users_db)
  }
  if(!is.null(report_name)){
    if(is.null(report_creator))report_creator <- Sys.getenv("SHINYPROXY_USERNAME")
    if(is.null(access_actors))access_actors <- report_creator
    access_actors <- c(access_actors, report_creator)%>%unique()
    new_report <- data.frame(report_name = report_name,report_creator = report_creator, co_creators = co_creators,access_actors = access_actors,expiration_date=expiration_date )
    users_db$groups <-users_db$reports_access%>%dplyr::bind_rows(new_report)
  }
  return(users_db)
}



#' connect_to_s3_board
#' @param s3_bucket s3 storage bucker
#' @param s3_prefix folder where to store the files, always should finish by /
#' @export
connect_to_s3_board <- function(s3_bucket = "fair-public-content", s3_prefix = "user_manager_db/"){
  report_s3_board <- pins::board_s3(
    bucket = s3_bucket,
    prefix = s3_prefix,
    versioned = TRUE,
    access_key = Sys.getenv("S3_ACCESS_KEY"),
    secret_access_key = Sys.getenv("S3_ACCESS_KEY_SECRET"),
    session_token = NULL,
    credential_expiration = NULL,
    profile = NULL,
    region = "eu-west-3",
    endpoint = NULL,
    cache = NULL
  )
  return(report_s3_board)
}

initialize_s3_users_db <- function(report_s3_board = NULL, users_db_name = "users_db", overwrite = FALSE){
  users <- data.frame(user_name = "test_user", is_admin = FALSE, user_description = "")
  users_groups <- data.frame(user_name = "test_user",users_group = "test_group")

  groups <- data.frame(group_name = "test_group" , users_group = "test_group", is_admin = FALSE, group_description = "")

  reports_access <- data.frame(report_name = "test_report")

  users_db <- list(users = users, users_groups = users_groups,groups = groups, reports_access = reports_access )

  s3_files <- pins::pin_list(board = report_s3_board)
  if( users_db_name %in% s3_files & overwrite == FALSE)return("file already exists and cannot be overwritten")
  pins::pin_write(board = report_s3_board,x = users_db, name = users_db_name,type = "rds")
}

# report_s3_board <- connect_to_s3_board(s3_bucket = "fair-public-content", s3_prefix = "user_manager_db/")
# report_s3_board%>%initialize_s3_users_db(overwrite = TRUE)
# users_db  <- pins::pin_read(board = report_s3_board,name = "users_db")
Aqvayli06/SaldaeModulesUI documentation built on Feb. 4, 2024, 6:25 a.m.