Skip to content
Permalink
master
Switch branches/tags
Go to file
 
 
Cannot retrieve contributors at this time
---
title: "File permissions"
---
```{r setup, include = FALSE}
auth_success <- tryCatch(
googledrive:::drive_auth_docs(),
googledrive_auth_internal_error = function(e) e
)
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
error = TRUE,
purl = googledrive::drive_has_token(),
eval = googledrive::drive_has_token()
)
options(tidyverse.quiet = TRUE)
```
```{r eval = !googledrive::drive_has_token(), echo = FALSE, comment = NA}
googledrive:::drive_bullets(c(
"Code chunks will not be evaluated, because:",
strsplit(auth_success$message, split = "\n")[[1]]
))
googledrive::drive_deauth()
```
```{r, include = FALSE}
# clean up if any previous runs left this lying around
trash_me <- googledrive::drive_find("chicken-perm-article")
if (googledrive::some_files(trash_me)) {
drive_trash(trash_me)
}
```
You can use googledrive to manage permissions on your Drive files, i.e. grant different people or groups of people various levels of access (read, comment, edit, etc.).
Let's upload a file and view its permissions.
```{r}
library(googledrive)
file <- drive_example("chicken.txt") %>%
drive_upload(name = "chicken-perm-article.txt") %>%
drive_reveal("permissions")
file
```
`shared = FALSE` indicates that this file is not yet shared with anyone and, for those so inclined, detailed information on permissions can be found in the `permissions_resource` list-column. Note that the `drive_resource`, which is always present in a dribble, typically also contains information on permissions. So if you just want to *know* about permissions, as opposed to modifying them, you can probably consult `drive_resource`.
Let's give a specific person permission to edit this file and a customized message, using the `emailAddress` and `emailMessage` parameters.
```{r, eval = FALSE}
file <- file %>%
drive_share(
role = "writer",
type = "user",
emailAddress = "serena@example.com",
emailMessage = "Would appreciate your feedback on this!"
)
```
Let's say we also want "anyone with a link" to be able to read the file.
```{r eval = FALSE}
file <- file %>%
drive_share(role = "reader", type = "anyone")
```
This comes up often enough that we've made a convenience wrapper:
```{r}
file <- file %>%
drive_share_anyone()
file
```
We see that the file is now `shared = TRUE`.
Now that we've made a few updates to our permissions, the `permissions_resource` list-column has become more interesting. Here's how to pull important information out of this and put into a tibble with one row per permission. (*Permission handling will become more formalized in future versions of googledrive. See [the issue](https://github.com/tidyverse/googledrive/issues/180)*). We use other packages in the tidyverse now for this data wrangling.
```{r eval = FALSE}
library(tidyverse)
perm <- pluck(file, "permissions_resource", 1, "permissions")
permissions <- tibble(
id = map_chr(perm, "id", .default = NA_character_),
name = map_chr(perm, "displayName", .default = NA_character_),
type = map_chr(perm, "type", .default = NA_character_),
role = map_chr(perm, "role", .default = NA_character_),
email = map_chr(perm, "emailAddress", .default = NA_character_)
)
permissions
```
We've suppressed execution of the above chunk but here's some static, indicative output:
```{r eval = FALSE}
#> # A tibble: 3 x 5
#> id name type role email
#> <chr> <chr> <chr> <chr> <chr>
#> 1 12345678901… Serena Somebody user writ… serena@example.com
#> 2 anyoneWithL… NA anyone read… NA
#> 3 98765432109… Orville Owner user owner orville@example.com
```
## Clean up
```{r}
drive_rm(file)
```