Skip to content

Commit 53e63d4

Browse files
authored
fix #155 - codebook permissions (#156)
* fix #155 - codebook permissions * improved legibility of permission checks --------- Co-authored-by: hlageek <[email protected]>
1 parent 027ff98 commit 53e63d4

File tree

1 file changed

+89
-36
lines changed

1 file changed

+89
-36
lines changed

R/mod_categories.R

Lines changed: 89 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -193,49 +193,102 @@ mod_categories_server <- function(id, glob) {
193193

194194
# Create edge -----------------------------------------
195195
observeEvent(input$edges_category, {
196-
if (!is.null(glob$user) && glob$user$data$codebook_modify == 1) {
197-
# Can only modify own codes and categories
198-
owns_code <- glob$codebook %>%
199-
dplyr::filter(code_id == input$edges_category$code_id) %>%
200-
dplyr::pull(user_id) == glob$user$user_id
201-
202-
owns_category <- dplyr::tbl(glob$pool, "categories") %>%
203-
dplyr::filter(category_id == !!input$edges_category$category_id) %>%
204-
dplyr::collect() %>%
205-
dplyr::pull(user_id) == glob$user$user_id
206-
207-
if (all(c(owns_code, owns_category))) {
208-
add_category_code_record(
209-
pool = glob$pool,
210-
active_project = glob$active_project,
211-
user_id = glob$user$user_id,
212-
edge = input$edges_category
213-
)
214-
} else {
215-
warn_user("You don't have permissions for modifying codes and categories created by others.")
216-
# TODO: delete code from category
217-
}
218-
} else if (!is.null(glob$user) && glob$user$data$codebook_other_modify == 1) {
219-
# Can modify all codes and categories
220-
add_category_code_record(
221-
pool = glob$pool,
222-
active_project = glob$active_project,
223-
user_id = glob$user$user_id,
224-
edge = input$edges_category
225-
)
226-
}
227-
})
196+
# check ownership
197+
owns_code <- glob$codebook %>%
198+
dplyr::filter(code_id == !!input$edges_category$code_id) %>%
199+
dplyr::pull(user_id) == glob$user$user_id
228200

229-
# Delete edge
230-
observeEvent(input$edges_category_delete, {
231-
delete_category_code_record(
201+
owns_category <- dplyr::tbl(glob$pool, "categories") %>%
202+
dplyr::filter(category_id == !!input$edges_category$category_id) %>%
203+
dplyr::collect() %>%
204+
dplyr::pull(user_id) == glob$user$user_id
205+
206+
# initialize as negative permission
207+
permission_check <- FALSE
208+
209+
if (!is.null(glob$user) && glob$user$data$codebook_modify != 1) {
210+
# User has no permissions
211+
warn_user("You don't have permissions for modifying codes and categories.")
212+
} else if (all(c(owns_code, owns_category))) {
213+
# Edge belongs to user
214+
permission_check <- TRUE
215+
} else if (!is.null(glob$user) && glob$user$data$codebook_other_modify == 1) {
216+
# Edge belongs to others but user can modify all codes and categories
217+
permission_check <- TRUE
218+
} else {
219+
# User can edit own edges but this edge belongs to others
220+
warn_user("You don't have permissions for modifying codes and categories created by others.")
221+
}
222+
223+
if (permission_check) {
224+
add_category_code_record(
232225
pool = glob$pool,
233226
active_project = glob$active_project,
234227
user_id = glob$user$user_id,
235-
edge = input$edges_category_delete
228+
edge = input$edges_category
236229
)
230+
} else {
231+
# re-render categories
232+
output$categories_ui <- renderUI({
233+
render_categories(
234+
id = id,
235+
active_project = glob$active_project,
236+
pool = glob$pool,
237+
user = glob$user
238+
)
239+
})
240+
}
237241
})
238242

243+
# Delete edge ----
244+
observeEvent(input$edges_category_delete, {
245+
# check ownership
246+
owns_code <- glob$codebook %>%
247+
dplyr::filter(code_id == !!input$edges_category_delete$code_id) %>%
248+
dplyr::pull(user_id) == glob$user$user_id
249+
250+
owns_category <- dplyr::tbl(glob$pool, "categories") %>%
251+
dplyr::filter(category_id == !!input$edges_category_delete$category_id) %>%
252+
dplyr::collect() %>%
253+
dplyr::pull(user_id) == glob$user$user_id
254+
255+
# initialize as negative permission
256+
permission_check <- FALSE
257+
258+
if (!is.null(glob$user) && glob$user$data$codebook_modify != 1) {
259+
# User has no permissions
260+
warn_user("You don't have permissions for modifying codes and categories.")
261+
} else if (all(c(owns_code, owns_category))) {
262+
# Edge belongs to user
263+
permission_check <- TRUE
264+
} else if (!is.null(glob$user) && glob$user$data$codebook_other_modify == 1) {
265+
# Edge belongs to others but user can modify all codes and categories
266+
permission_check <- TRUE
267+
} else {
268+
# User can edit own edges but this edge belongs to others
269+
warn_user("You don't have permissions for modifying codes and categories created by others.")
270+
}
271+
272+
if (permission_check) {
273+
delete_category_code_record(
274+
pool = glob$pool,
275+
active_project = glob$active_project,
276+
user_id = glob$user$user_id,
277+
edge = input$edges_category_delete
278+
)
279+
} else {
280+
# re-render categories
281+
output$categories_ui <- renderUI({
282+
render_categories(
283+
id = id,
284+
active_project = glob$active_project,
285+
pool = glob$pool,
286+
user = glob$user
287+
)
288+
})
289+
}
290+
})
291+
239292
# return active categories details in glob$category ----
240293
})
241294
}

0 commit comments

Comments
 (0)