-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserAuthWithAdmin.R
More file actions
338 lines (282 loc) · 15.4 KB
/
Copy pathuserAuthWithAdmin.R
File metadata and controls
338 lines (282 loc) · 15.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
if (!require(shiny))
install.packages(shiny)
# For Verifying Credentials via API
#if (!require(RCurl))
# install.packages(RCurl)
#if (!require(XML))
# install.packages(XML)
# Application Loading Message
loading_message <- "Loading..."
# Authentication Message
login_message <- "Please log in."
# Welcome Message
welcome_message <- "Welcome!"
# Access Denied Message
no_access_message <- "Sorry, you don't have access to this app."
server <- function(input,output,session){
# Load Permissions File (or query DB, etc.):
permissions <- read.csv("permissions.csv",stringsAsFactors=FALSE)
##### User Authentication:
user <- reactiveValues(logged = FALSE)
output$status <- reactive({user$logged})
outputOptions(output, "status", suspendWhenHidden=FALSE)
# Login Fields
output$ui_login <- renderUI({
wellPanel(
textInput("username", "Username:"),
passwordInput("password", "Password:"),
br(),
actionButton("login", "Log in"),
a(href = "http://shiny.rstudio.com/", target="_blank", "Forgot your password?")
)
})
# Authenticate User
profile <- reactive({
if (input$login > 0) {
username <- isolate(input$username)
password <- isolate(input$password)
# For simple demonstration, username = user1 and password = pass1:
if (username == "user1" & password == "pass1") {
status <- 1
} else {
status <- 0
}
list(status = status
,username = username)
# Optionally, Post Credentials to API for Verification
# url <- "www.example.com"
# postContent <- URLencode(paste0("id=",username,"&password=",password))
# myHeader <- c('Content-Type' = "application/x-www-form-urlencoded")
# resp <- getURL(url = url
# ,postfields = postContent
# ,httpheader = myHeader
# ,verbose = TRUE)
#
# # Parse Response and Extract Valuable Info
# resp_parsed <- xmlToList(resp)
#
# list(status = resp_parsed$status
# ,id = resp_parsed$id
# ,email = resp_parsed$email)
}
})
# Check Username and Password
output$pass <- renderText({
if (input$login > 0) {
if (profile()$status == 1) {
user$logged <- TRUE
# Load Permissions After User is Identified and Check against Table
perm_vals$admin_access <- ifelse(subset(permissions,id==tolower(profile()$username))[,"admin_access"] == 1,TRUE,FALSE)
perm_vals$app_1_access <- ifelse(subset(permissions,id==tolower(profile()$username))[,"app_1_access"] == 1,TRUE,FALSE)
perm_vals$app_2_access <- ifelse(subset(permissions,id==tolower(profile()$username))[,"app_2_access"] == 1,TRUE,FALSE)
perm_vals$app_3_access <- ifelse(subset(permissions,id==tolower(profile()$username))[,"app_3_access"] == 1,TRUE,FALSE)
} else {
"Username or password failed!"
}
}
})
#####
##### Admin / Set Permissions:
# Pass Permissions to ui.R as output values for conditional tabs
perm_vals <- reactiveValues(admin_access = FALSE
,app_1_access = FALSE
,app_2_access = FALSE
,app_3_access = FALSE)
output$admin_access <- reactive({perm_vals$admin_access})
output$app_1_access <- reactive({perm_vals$app_1_access})
output$app_2_access <- reactive({perm_vals$app_2_access})
output$app_3_access <- reactive({perm_vals$app_3_access})
outputOptions(output, "admin_access", suspendWhenHidden=FALSE)
outputOptions(output, "app_1_access", suspendWhenHidden=FALSE)
outputOptions(output, "app_2_access", suspendWhenHidden=FALSE)
outputOptions(output, "app_3_access", suspendWhenHidden=FALSE)
input_perm <- reactive({
if(input$save_perm > 0){
isolate({
id <- input$id_perm
admin_access <- input$admin_perm
app_1_access <- input$app_1_perm
app_2_access <- input$app_2_perm
app_3_access <- input$app_3_perm
})
# Create new set of permissions for ID
perms_new <- data.frame(id = tolower(id)
,admin_access = admin_access
,app_1_access = app_1_access
,app_2_access = app_2_access
,app_3_access = app_3_access
,stringsAsFactors = FALSE)
# Load old permissions
perms_tmp <- read.csv("permissions.csv",stringsAsFactors=FALSE)
# Look up ID in old permission table. If exists, update old record. Else, create new record.
if (perms_new$id %in% perms_tmp[,"id"]){
perms_tmp[which(perms_new$id == perms_tmp[,"id"]),"admin_access"] <- perms_new$admin_access
perms_tmp[which(perms_new$id == perms_tmp[,"id"]),"app_1_access"] <- perms_new$app_1_access
perms_tmp[which(perms_new$id == perms_tmp[,"id"]),"app_2_access"] <- perms_new$app_2_access
perms_tmp[which(perms_new$id == perms_tmp[,"id"]),"app_3_access"] <- perms_new$app_3_access
} else {
row_count <- nrow(perms_tmp)
perms_tmp[row_count+1,"id"] <- perms_new$id
perms_tmp[row_count+1,"admin_access"] <- perms_new$admin_access
perms_tmp[row_count+1,"app_1_access"] <- perms_new$app_1_access
perms_tmp[row_count+1,"app_2_access"] <- perms_new$app_2_access
perms_tmp[row_count+1,"app_3_access"] <- perms_new$app_3_access
}
# Write new permissions to file
write.csv(perms_tmp,"permissions.csv",row.names=FALSE)
return(perms_tmp)
} else {
# If nothing has changed, return the original permissions for reference
return(permissions)
}
})
output$perm_table <- renderTable({
input_perm()
})
}
ui <- navbarPage("App with User Auth and Admin"
# Login Tab
,tabPanel("Login"
,conditionalPanel("output.status == null"
,h3(loading_message)
)
,conditionalPanel("output.status == 0"
,sidebarPanel(p("Username = user1; Password = pass1")
,htmlOutput("ui_login")
,htmlOutput("pass")
)
)
,conditionalPanel("output.status == 1"
,br()
,h1(welcome_message)
)
)
# Admin Tab
,tabPanel("Admin"
,conditionalPanel("output.status == null"
,h3(loading_message)
)
,conditionalPanel("output.status == 0"
,h3(login_message)
)
,conditionalPanel("output.status == 1"
,conditionalPanel("output.admin_access == 0"
,h3(no_access_message)
)
,conditionalPanel("output.admin_access == 1"
# Application title
,titlePanel("Admin")
# Sidebar Layout
,sidebarLayout(
sidebarPanel("Set Permissions"
,textInput("id_perm","User ID")
,selectInput("admin_perm"
,label = "Admin"
,choices = list("No" = 0
,"Yes" = 1))
,selectInput("app_1_perm"
,label = "Application 1"
,choices = list("No" = 0
,"Yes" = 1))
,selectInput("app_2_perm"
,label = "Application 2"
,choices = list("No" = 0
,"Yes" = 1))
,selectInput("app_3_perm"
,label = "Application 3"
,choices = list("No" = 0
,"Yes" = 1))
,actionButton("save_perm", label = h6("Save"))
)
,mainPanel(
tabPanel("View Permissions"
,tableOutput("perm_table")
)
)
)
)
)
)
# App 1
,tabPanel("App 1"
,conditionalPanel("output.status == null"
,h3(loading_message)
)
,conditionalPanel("output.status == 0"
,h3(login_message)
)
,conditionalPanel("output.status == 1"
,conditionalPanel("output.app_1_access == 0"
,h3(no_access_message)
)
,conditionalPanel("output.app_1_access == 1"
# Application title
,titlePanel("App 1")
# Sidebar Layout
,sidebarLayout(
sidebarPanel(
# Input Stuff
)
,mainPanel(
# Output Stuff
)
)
)
)
)
# App 2
,tabPanel("App 2"
,conditionalPanel("output.status == null"
,h3(loading_message)
)
,conditionalPanel("output.status == 0"
,h3(login_message)
)
,conditionalPanel("output.status == 1"
,conditionalPanel("output.app_2_access == 0"
,h3(no_access_message)
)
,conditionalPanel("output.app_2_access == 1"
# Application title
,titlePanel("App 2")
# Sidebar Layout
,sidebarLayout(
sidebarPanel(
# Input Stuff
)
,mainPanel(
# Output Stuff
)
)
)
)
)
# App 3
,tabPanel("App 3"
,conditionalPanel("output.status == null"
,h3(loading_message)
)
,conditionalPanel("output.status == 0"
,h3(login_message)
)
,conditionalPanel("output.status == 1"
,conditionalPanel("output.app_3_access == 0"
,h3(no_access_message)
)
,conditionalPanel("output.app_3_access == 1"
# Application title
,titlePanel("App 3")
# Sidebar Layout
,sidebarLayout(
sidebarPanel(
# Input Stuff
)
,mainPanel(
# Output Stuff
)
)
)
)
)
)
shinyApp(server=server,ui=ui)