Skip to content

Commit

Permalink
fix #10 all examples i think are self contained and clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
sckott committed Nov 15, 2024
1 parent c5a9a4a commit b4cfcdc
Show file tree
Hide file tree
Showing 12 changed files with 123 additions and 26 deletions.
38 changes: 30 additions & 8 deletions R/privileges.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#' @param .data an s3 object of class `privilege`
#' @param ... one of all, select, update, insert, delete
#' @param cols (character) vector of column names
#' @examplesIf interactive() && has_postgres()
#' @examplesIf has_postgres()
#' library(DBI)
#' library(RPostgres)
#' con <- dbConnect(Postgres())
Expand All @@ -23,6 +23,9 @@
#' rls_tbl(con, "passwd") %>%
#' grant(update, select, cols = c("real_name", "home_phone")) %>%
#' to(jane)
#'
#' # cleanup
#' dbDisconnect(con)
grant <- function(.data, ..., cols = NULL) {
pipe_autoexec(toggle = rls_env$auto_pipe)
.data <- as_priv(.data)
Expand All @@ -40,7 +43,7 @@ grant <- function(.data, ..., cols = NULL) {
#'
#' @export
#' @inheritParams grant
#' @examplesIf interactive() && has_postgres()
#' @examplesIf has_postgres()
#' library(DBI)
#' library(RPostgres)
#' con <- dbConnect(Postgres())
Expand All @@ -55,6 +58,9 @@ grant <- function(.data, ..., cols = NULL) {
#' rls_tbl(con, "passwd") %>%
#' revoke(update, cols = c("real_name", "home_phone")) %>%
#' from(jane)
#'
#' # cleanup
#' dbDisconnect(con)
revoke <- function(.data, ..., cols = NULL) {
pipe_autoexec(toggle = rls_env$auto_pipe)
.data <- as_priv(.data)
Expand All @@ -81,13 +87,23 @@ revoke <- function(.data, ..., cols = NULL) {
#' setup_example_table(con, "passwd")
#' }
#'
#' rls_tbl(con, "passwd") %>% to(jane)
#' rls_tbl(con, "passwd") %>% to(jane, bob, alice)
#' rls_tbl(con, "passwd") %>% grant(select) %>% to(jane)
#' rls_tbl(con, "passwd") %>% grant(select) %>% from(jane)
#' rls_tbl(con, "passwd") %>% grant(select) %>% to(jane, bob, alice)
#'
#' # Errors: doesn't make sense to pass rls_tbl output directly to to/from
#' # rls_tbl(con, "passwd") %>% from(jane)
#' # #> ! must pass privilege or row_policy to to/from
#'
#' # cleanup
#' dbDisconnect(con)
to <- function(.data, ...) {
pipe_autoexec(toggle = rls_env$auto_pipe)
.data <- switch(class(.data),
assert_is(.data, c("privilege", "row_policy", "tbl_sql"))
.data <- switch_multiclass(class(.data),
privilege = as_priv(.data),
row_policy = as_row_policy(.data)
row_policy = as_row_policy(.data),
tbl_sql = rls_abort("must pass privilege or row_policy to to/from")
)
.data$user <- dot_names(...)
.data
Expand All @@ -103,7 +119,7 @@ from <- to
#' @keywords internal
#' @param priv an S3 object of class `privilege`, required
#' @param con DBI connection object, required
#' @examplesIf interactive()
#' @examplesIf interactive() && has_postgres()
#' library(tibble)
#' library(RPostgres)
#' library(DBI)
Expand All @@ -121,7 +137,7 @@ from <- to
#' # ON fruits
#' # TO jane
#' priv <-
#' rls_tbl(con, "flights") %>%
#' rls_tbl(con, "fruits") %>%
#' grant(select) %>%
#' to(jane)
#' priv
Expand All @@ -147,7 +163,13 @@ from <- to
#' to(jane)
#' priv
#' sql <- translate_privilege(priv, con)
#' sql
#' dbExecute(con, sql)
#'
#' # cleanup
#' dbRemoveTable(con, "fruits")
#' dbExecute(con, "DROP ROLE jane")
#' dbDisconnect(con)
translate_privilege <- function(priv, con) {
assert_is(priv, "privilege")
is_conn(con)
Expand Down
34 changes: 28 additions & 6 deletions R/row_policy.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#' @export
#' @inheritParams grant
#' @param name (character) scalar name for the policy. required
#' @examplesIf interactive() && has_postgres()
#' @examplesIf has_postgres()
#' library(DBI)
#' library(RPostgres)
#' con <- dbConnect(Postgres())
Expand All @@ -13,6 +13,11 @@
#' rls_tbl(con, "passwd") %>%
#' row_policy("my_policy") %>%
#' rls_run()
#' rls_policies(con)
#'
#' # cleanup
#' rls_drop_policies(con)
#' dbDisconnect(con)
row_policy <- function(.data, name) {
pipe_autoexec(toggle = rls_env$auto_pipe)
assert_is(name, "character")
Expand All @@ -26,7 +31,7 @@ row_policy <- function(.data, name) {
#'
#' @export
#' @inheritParams grant
#' @examplesIf interactive() && has_postgres()
#' @examplesIf has_postgres()
#' library(DBI)
#' library(RPostgres)
#' con <- dbConnect(Postgres())
Expand All @@ -36,6 +41,9 @@ row_policy <- function(.data, name) {
#' rls_tbl(con, "passwd") %>%
#' row_policy("their_policy") %>%
#' commands(update)
#'
#' # cleanup
#' dbDisconnect(con)
commands <- function(.data, ...) {
pipe_autoexec(toggle = rls_env$auto_pipe)
.data <- as_row_policy(.data)
Expand All @@ -50,7 +58,7 @@ commands <- function(.data, ...) {
#' @param using an expression to use to check against existing rows
#' @param sql (character) sql syntax to use for existing rows
#' @details Use either `using` or `sql`, not both
#' @examplesIf interactive() && has_postgres()
#' @examplesIf has_postgres()
#' library(DBI)
#' library(RPostgres)
#' con <- dbConnect(Postgres())
Expand All @@ -61,6 +69,9 @@ commands <- function(.data, ...) {
#' row_policy("a_good_policy") %>%
#' commands(update) %>%
#' rows_existing(sql = 'current_user = "user_name"')
#'
#' # cleanup
#' dbDisconnect(con)
rows_existing <- function(.data, using = NULL, sql = NULL) {
pipe_autoexec(toggle = rls_env$auto_pipe)
using_quo <- enquo(using)
Expand All @@ -84,7 +95,7 @@ rows_existing <- function(.data, using = NULL, sql = NULL) {
#' new rows or editing of existing rows
#' @param sql (character) sql syntax to use for new rows
#' @details Use either `check` or `sql`, not both
#' @examplesIf interactive() && has_postgres()
#' @examplesIf has_postgres()
#' library(DBI)
#' library(RPostgres)
#' con <- dbConnect(Postgres())
Expand All @@ -105,6 +116,9 @@ rows_existing <- function(.data, using = NULL, sql = NULL) {
#' rows_existing(sql = 'current_user = "user_name"') %>%
#' rows_new(home_phone == "098-765-4321") %>%
#' to(jane)
#'
#' # cleanup
#' dbDisconnect(con)
rows_new <- function(.data, check = NULL, sql = NULL) {
pipe_autoexec(toggle = rls_env$auto_pipe)
check_quo <- enquo(check)
Expand Down Expand Up @@ -137,11 +151,14 @@ express <- function(x) {
#' @param policy an S3 object of class `row_policy`, required
#' @param con DBI connection object, required
#' @references <https://www.postgresql.org/docs/current/sql-createpolicy.html>
#' @examplesIf interactive()
#' library(RPostgres)
#' @examplesIf has_postgres()
#' library(DBI)
#' library(RPostgres)
#' con <- dbConnect(Postgres())
#' setup_example_table(con)
#'
#' # create role
#' dbExecute(con, "CREATE ROLE jane")
#'
#' if (rls_policy_exists(con, "blue_policy")) {
#' rls_drop_policy(con, name = "blue_policy", table = "passwd")
Expand All @@ -157,6 +174,11 @@ express <- function(x) {
#' sql <- translate_row_policy(policy, con)
#' sql
#' dbExecute(con, sql)
#'
#' # cleanup
#' rls_drop_policies(con)
#' dbExecute(con, "DROP ROLE jane")
#' dbDisconnect(con)
translate_row_policy <- function(policy, con) {
is_conn(con)
create_statement <- switch(class(con),
Expand Down
11 changes: 11 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,14 @@ dot_names <- function(...) {
collapse <- function(x) {
paste(x, collapse = ", ")
}

#' switch that handles more than 1 class in the EXPR variable
#' @noRd
switch_multiclass <- function(EXPR, ...) {
classes <- c("tbl_sql", "privilege", "row_policy")
check_in <- classes %in% EXPR
if (!any(check_in)) {
rls_abort(format_error(".data must be one of {clz_col(classes)}"))
}
switch(classes[check_in], ...)
}
5 changes: 4 additions & 1 deletion man/commands.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion man/grant.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion man/revoke.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion man/row_policy.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion man/rows_existing.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion man/rows_new.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 10 additions & 2 deletions man/to.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions man/translate_privilege.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b4cfcdc

Please sign in to comment.