Skip to content

Commit

Permalink
small tweaks part 1 of a few
Browse files Browse the repository at this point in the history
  • Loading branch information
seankross committed Dec 6, 2024
1 parent b4cfcdc commit c2c1b5f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 34 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ vignettes/figure
notes.R
^CODE_OF_CONDUCT\.md$
endtoend-*
^\.Rproj\.user$
17 changes: 17 additions & 0 deletions rls.Rproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Version: 1.0

RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: Sweave
LaTeX: pdfLaTeX

BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
57 changes: 23 additions & 34 deletions vignettes/rls.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -23,35 +23,16 @@ library(dbplyr)
library(rls)
```

## Start PostgreSQL locally

Start Postgres however you do that.

## Create a connection

## Create a connection to a Postgres database

``` r
con <- dbConnect(Postgres())
```



## Create roles


``` r
# Administrator
dbExecute(con, "CREATE ROLE admin")
# Normal user
dbExecute(con, "CREATE ROLE bob")
# Another normal user
dbExecute(con, "CREATE ROLE alice")
con <- dbConnect(
Postgres(),
# fill your credentials for dbname, host, port, and user here
)
```

## Create a table

Create the table (with no data)

## Create an example table schema and roles for your database

``` r
invisible(dbExecute(con, "
Expand All @@ -66,27 +47,29 @@ invisible(dbExecute(con, "
shell text NOT NULL
);
"))

# Administrator
dbExecute(con, "CREATE ROLE admin")
# Normal user
dbExecute(con, "CREATE ROLE bob")
# Another normal user
dbExecute(con, "CREATE ROLE alice")
```

Some sample data
## Add data to the table


``` r
sample_data <- tribble(
example_data <- tribble(
~user_name, ~pwhash, ~uid, ~gid, ~real_name, ~home_phone, ~home_dir, ~shell,
'admin','xxx',0,0,'Admin','111-222-3333','/root','/bin/dash',
'bob','xxx',1,1,'Bob','123-456-7890','/home/bob','/bin/zsh',
'alice','xxx',2,1,'Alice','098-765-4321','/home/alice','/bin/zsh'
)
```

Append rows to the `passwd` table


``` r
rows_append(
tbl(con, "passwd"),
copy_inline(con, sample_data),
copy_inline(con, example_data),
in_place = TRUE
)
```
Expand All @@ -107,7 +90,7 @@ tbl(con, "passwd")

## Enable row level security

Enable row level security with `rls_enable`
Enable row level security on the `passwd` table with with `rls_enable`


``` r
Expand All @@ -127,6 +110,9 @@ rls_check_status(con, "passwd")

## Create row level security policies

1. Get the reference to the table
2. Create a new policy called "admin_all", which will

`admin_all` = Administrator can see all rows and add any rows


Expand All @@ -136,6 +122,7 @@ policy1 <- rls_tbl(con, "passwd") %>%
rows_existing(TRUE) %>%
rows_new(TRUE) %>%
to(admin)

policy1
#> <row_policy> admin_all
#> user: admin
Expand All @@ -148,8 +135,10 @@ policy1
#> 1 0 /root 111-222-3333 xxx Admin /bin/dash 0 admin
#> 2 1 /home/bob 123-456-7890 xxx Bob /bin/zsh 1 bob
#> 3 1 /home/alice 098-765-4321 xxx Alice /bin/zsh 2 alice

rls_run(policy1)
#> [1] 0

rls_policies(con)
#> # A tibble: 1 × 8
#> schemaname tablename policyname permissive roles cmd qual with_check
Expand Down

0 comments on commit c2c1b5f

Please sign in to comment.