Guest User

Untitled

a guest
May 24th, 2018
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.53 KB | None | 0 0
  1. shinyUI(
  2. uiOutput("ui")
  3. )
  4.  
  5. shinyServer(function(input, output, session) {
  6. #### UI code --------------------------------------------------------------
  7. output$ui <- dashboardPage(dashboardHeader(title = "My Page"),
  8. dashboardSidebar(
  9. if (user_input$authenticated == FALSE) {
  10. NULL
  11. } else {
  12. sidebarMenuOutput("sideBar_menu_UI")
  13. }
  14. ),
  15. dashboardBody(
  16. if (user_input$authenticated == FALSE) {
  17. ##### UI code for login page
  18. uiOutput("uiLogin")
  19. uiOutput("pass")
  20. } else {
  21. #### Your app's UI code goes here!
  22. uiOutput("obs")
  23. plotOutput("distPlot")
  24. }
  25. ))
  26.  
  27. #### YOUR APP'S SERVER CODE GOES HERE ----------------------------------------
  28. # slider input widget
  29. output$obs <- renderUI({
  30. sliderInput("obs", "Number of observations:",
  31. min = 1, max = 1000, value = 500)
  32. })
  33.  
  34. # render histogram once slider input value exists
  35. output$distPlot <- renderPlot({
  36. req(input$obs)
  37. hist(rnorm(input$obs), main = "")
  38. })
  39.  
  40. output$sideBar_menu_UI <- renderMenu({
  41. sidebarMenu(id = "sideBar_Menu",
  42. menuItem("Menu 1", tabName="menu1_tab", icon = icon("calendar")),
  43. menuItem("Menu 2", tabName="menu2_tab", icon = icon("database"))
  44. )
  45. })
  46.  
  47. #### PASSWORD server code ----------------------------------------------------
  48. # reactive value containing user's authentication status
  49.  
  50. # user_input <- reactiveValues(authenticated = FALSE, valid_credentials = FALSE,
  51. # user_locked_out = FALSE, status = "")
  52.  
  53. # authenticate user by:
  54. # 1. checking whether their user name and password are in the credentials
  55. # data frame and on the same row (credentials are valid)
  56. # 2. if credentials are valid, retrieve their lockout status from the data frame
  57. # 3. if user has failed login too many times and is not currently locked out,
  58. # change locked out status to TRUE in credentials DF and save DF to file
  59. # 4. if user is not authenticated, determine whether the user name or the password
  60. # is bad (username precedent over pw) or he is locked out. set status value for
  61. # error message code below
  62.  
  63. observeEvent(input$login_button, {
  64. credentials <- readRDS("credentials/credentials.rds")
  65.  
  66. row_username <- which(credentials$user == input$user_name)
  67. row_password <- which(credentials$pw == digest(input$password)) # digest() makes md5 hash of password
  68.  
  69. # if user name row and password name row are same, credentials are valid
  70. # and retrieve locked out status
  71. if (length(row_username) == 1 &&
  72. length(row_password) >= 1 && # more than one user may have same pw
  73. (row_username %in% row_password)) {
  74. user_input$valid_credentials <- TRUE
  75. user_input$user_locked_out <- credentials$locked_out[row_username]
  76. }
  77.  
  78. # if user is not currently locked out but has now failed login too many times:
  79. # 1. set current lockout status to TRUE
  80. # 2. if username is present in credentials DF, set locked out status in
  81. # credentials DF to TRUE and save DF
  82. if (input$login_button == num_fails_to_lockout &
  83. user_input$user_locked_out == FALSE) {
  84.  
  85. user_input$user_locked_out <- TRUE
  86.  
  87. if (length(row_username) == 1) {
  88. credentials$locked_out[row_username] <- TRUE
  89.  
  90. saveRDS(credentials, "credentials/credentials.rds")
  91. }
  92. }
  93.  
  94. # if a user has valid credentials and is not locked out, he is authenticated
  95. if (user_input$valid_credentials == TRUE & user_input$user_locked_out == FALSE) {
  96. user_input$authenticated <- TRUE
  97. } else {
  98. user_input$authenticated <- FALSE
  99. }
  100.  
  101. # if user is not authenticated, set login status variable for error messages below
  102. if (user_input$authenticated == FALSE) {
  103. if (user_input$user_locked_out == TRUE) {
  104. user_input$status <- "locked_out"
  105. } else if (length(row_username) > 1) {
  106. user_input$status <- "credentials_data_error"
  107. } else if (input$user_name == "" || length(row_username) == 0) {
  108. user_input$status <- "bad_user"
  109. } else if (input$password == "" || length(row_password) == 0) {
  110. user_input$status <- "bad_password"
  111. }
  112. }
  113. })
  114.  
  115. # password entry UI componenets:
  116. # username and password text fields, login button
  117. output$uiLogin <- renderUI({
  118. wellPanel(
  119. textInput("user_name", "User Name:"),
  120.  
  121. passwordInput("password", "Password:"),
  122.  
  123. actionButton("login_button", "Log in")
  124. )
  125. })
  126.  
  127. # red error message if bad credentials
  128. output$pass <- renderUI({
  129. if (user_input$status == "locked_out") {
  130. h5(strong(paste0("Your account is locked because of too manyn",
  131. "failed login attempts. Contact administrator."), style = "color:red"), align = "center")
  132. } else if (user_input$status == "credentials_data_error") {
  133. h5(strong("Credentials data error - contact administrator!", style = "color:red"), align = "center")
  134. } else if (user_input$status == "bad_user") {
  135. h5(strong("User name not found!", style = "color:red"), align = "center")
  136. } else if (user_input$status == "bad_password") {
  137. h5(strong("Incorrect password!", style = "color:red"), align = "center")
  138. } else {
  139. ""
  140. }
  141. })
  142. })
Add Comment
Please, Sign In to add comment