datastorm-open / shinymanager

Simple and secure authentification mechanism for single shiny applications.
https://datastorm-open.github.io/shinymanager/
386 stars 79 forks source link

Reload Credentials data without restarting the app #67

Closed JoaquinTavella closed 3 years ago

JoaquinTavella commented 3 years ago

Hi! I made a simple user managment function that allows me to create a user and upload a credential dataframe.

observeEvent(input$btnCreateUser,{
     load(file = "./RDA/credentials.rda")

         if ((input$DniID %in% credentials$dni) == F & (input$user %in% credentials$user) == F) {
           u1 <- data.frame(
               user = input$user, # mandatory
               password = input$password, 
               level = 0,
               nombre = input$NombreId,
               apellido = input$ApellidoId,
               dni = input$DniID
             )

          credentials <- rbind(credentials,u1)
          save(credentials,file = "./RDA/credentials.rda")
          showNotification("Usuario Creado correctamente",duration = 5,closeButton = T,type = "message")
          load(file = "./RDA/credentials.rda", envir = .GlobalEnv)
          output$tablaUsuarios <-
            DT::renderDataTable(DT::datatable(credentials))

          updateTextInput(session, "NombreId", value = " ")     
          updateTextInput(session, "ApellidoId", value = " ")
          updateTextInput(session, "user", value = " ")
          updateNumericInput(session, "DniID", value = 0)

         }
        else{
         showNotification("No se puede crear el usuario: DNI o USUARIO ya existente",duration = 5,closeButton = T,type = "error")
                 }

    },ignoreInit = T)

The problem with this is that when I logout and try to use this new user the credentials dataframe does not load, even when I call it again after I saving it.

` load(file = "./RDA/credentials.rda", envir = .GlobalEnv)

I tried to load the data frame in this line again, but it only accept data frames.

res_auth <- secure_server(
    check_credentials = check_credentials(credentials)
  )

I tried this but does not work.


observeEvent(input$go_auth,{
  load(file = "./RDA/credentials.rda")
  res_auth <- secure_server(
    check_credentials = check_credentials(credentials)
  )
  # Create reactive values including all credentials
  creds_reactive <- reactive({
    reactiveValuesToList(res_auth)
  })

  # Hide extraOutput only when condition is TRUE
  observe({
    if (!is.null(creds_reactive()$level) && creds_reactive()$level > 0) shinyjs::hide(selector = 'a[data-value = "GastoMensual"')
  })

  output$auth_output <- renderPrint({
    reactiveValuesToList(res_auth)
  })

})
pvictor commented 3 years ago

Hello,

Why not use an encrypted SQLite database and the admin mode ? Adding a new user is a functionnality implemented.

Victor