datastorm-open / shinymanager

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

Possibility to save logs #125

Closed Jorge-hercas closed 1 year ago

Jorge-hercas commented 2 years ago

Hi and thanks for reading me

Thank you very much for creating the application, as it has been very useful for me. I would like to know if there is any way to save user logs without deleting them after the server stops, in order to keep a more accurate record of the app. Maybe overwriting a RDS file or something like that?

Thanks for the help

Jorge-hercas commented 2 years ago

In update to the previous thread, I found an unofficial way to do that, creating a conditional that saves an RDS file (if it exists) and from it create a table with the logs. Something like:

library(shiny)
library(shinymanager)
library(shinyjs)
library(dplyr)

# define some credentials
credentials <- data.frame(
  user = c("shiny", "shiny2"), # mandatory
  password = c("111", "111"), # mandatory
  level = c(2, 0)
)

ui <- fluidPage(
  shinyjs::useShinyjs(),
  tags$h2("My secure application"),
  tableOutput("ejemplo")
)

# Wrap your UI with secure_app
ui <- secure_app(ui)

server <- function(input, output, session) {
  # call the server part
  # check_credentials returns a function to authenticate users
  res_auth <- secure_server(
    check_credentials = check_credentials(credentials)
  )
  # Create reactive values including all credentials
  creds_reactive <- reactive({
    reactiveValuesToList(res_auth) |> 
      as.data.frame()  |> 
      mutate(hora =as.character( Sys.time()))
  })

  output$ejemplo <- renderTable({

    if (file.exists("ejemplo.RDS")){
      var <- readRDS("ejemplo.RDS") |> 
        bind_rows(as.data.frame(creds_reactive()) )

      saveRDS(var,"ejemplo.RDS")
    }else{
      saveRDS(as.data.frame(creds_reactive()),"ejemplo.RDS")
    }

    readRDS("ejemplo.RDS") |> 
      as.data.frame()

  })

}

shinyApp(ui, server)
pvictor commented 2 years ago

Hello,

If you use the SQLite backend for credentials, logs are automatically saved and available in the admin panel. See https://datastorm-open.github.io/shinymanager/articles/SQLite_Admin.html

Otherwise you can use a package like {shinylogs}.

Victor