Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

with_* methods to temporarily change user or role to test stuff? #5

Open
sckott opened this issue Oct 24, 2024 · 3 comments
Open

with_* methods to temporarily change user or role to test stuff? #5

sckott opened this issue Oct 24, 2024 · 3 comments

Comments

@sckott
Copy link
Member

sckott commented Oct 24, 2024

Idea is to:

with_db_role(
  role = "analysts", {
  tbl(con, "passwd")
})
# OR
with_db_user(
  user = "jane", {
  tbl(con, "passwd")
})

Where the current user before the with block is called is reset as the user when the with block exits so that the current user can enter user or role contexts quickly and test things out

There is a method in withr for databases, https://withr.r-lib.org/reference/with_db_connection.html with_db_connection, so that may be part of the solution

@seankross ?

@seankross
Copy link

This sounds great!

@sckott
Copy link
Member Author

sckott commented Oct 29, 2024

great, i'll spec something out

@sckott
Copy link
Member Author

sckott commented Oct 29, 2024

@seankross check branch with

library(rls)
library(DBI)
library(RPostgres)
library(dplyr)

con <- dbConnect(Postgres())

rls_current_user(con)

dbCreateTable(con, "cars", mtcars)
rows_append(
  tbl(con, "cars"),
  copy_inline(con, mtcars),
  in_place = TRUE
)

dbExecute(con, "CREATE ROLE analysts")

# should fail because role analysts doesn't have access to cars yet
with_db_user(
 con = con,
 user = "analysts",
 code = dbGetQuery(con, "select * from cars")
)

# back to current user
rls_current_user(con)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants