R/gtm_permissions_methods.R

Defines functions gtm_permissions_update gtm_permissions_delete gtm_permissions_get gtm_permissions_create gtm_permissions_list

Documented in gtm_permissions_create gtm_permissions_delete gtm_permissions_get gtm_permissions_list gtm_permissions_update

#' gtm_permissions_list
#'
#' Lists all of the user permissions in a GTM account.
#' @param account_id The ID of the account
#' @param token An OAuth token object - needs to be set to 'manage users'
#' @param verbose If you want a message letting you know that your results have been successfully retrieved. Defaults to FALSE
#' @export

gtm_permissions_list <- function(account_id,token,verbose=FALSE) {

  require(tidyverse)
  require(httr)

  token_status(token)

  get_url <- paste('https://www.googleapis.com/tagmanager/v2/accounts',account_id,'user_permissions',sep='/')

  call <- easy_call('get',get_url,token)

  call_content <- content(call,'parsed')

  permissions_tbl <- tibble()

  for (i in 1:length(call_content$userPermission)) {

    permission <- call_content$userPermission[[i]]

    index_nr <- i

    containers <- tibble()

    for (i in 1:length(permission$containerAccess)) {

      x <- tibble(container_id = permission$containerAccess[[i]]$containerId,
                  container_permission = permission$containerAccess[[i]]$permission,
                  index = index_nr)

      containers <- bind_rows(containers,x)

    }

    permission$containerAccess <- NULL

    single_permission <- tibble(account_id = permission$accountId,
                                user_permission_id = as.numeric(str_replace(permission$path,".*permissions/",'')),
                                email_address = permission$emailAddress,
                                account_access = permission$accountAccess$permission,
                                index = index_nr) %>%
      full_join(containers,by='index') %>%
      select(-index)

    permissions_tbl <- bind_rows(permissions_tbl,single_permission)

  }

  permissions_tbl <- permissions_tbl %>%
    arrange(email_address)

  verbose_calls(call,call_content,verbose)

  return(permissions_tbl)

}

#' gtm_permissions_create
#'
#' Lists all of the user permissions in a GTM account.
#' @param account_id The ID of the account
#' @param email The email to which you want to grant permissions
#' @param account_permission The account level permissions for the user; possible values are "accountPermissionUnspecified", "admin", "noAccess", and "user"
#' @param container_permissions A data frame of the permissions per container, with the container ID in the first column and the permission in the second column. Possible permissions are "approve",  "containerPermissionUnspecified", "edit", "noAccess", "publish", and "read"
#' @param token An OAuth token object - needs to be set to 'manage users'
#' @param verbose If you want a message letting you know that your results have been successfully retrieved. Defaults to FALSE
#' @export

gtm_permissions_create <- function(account_id,email,account_permission,container_permissions,token,verbose=FALSE) {

  require(tidyverse)
  require(httr)
  require(rlist)

  container_permissions <- container_permissions %>%
    rename(container_id = 1,
           permission = 2)

  token_status(token)

  post_url <- paste('https://www.googleapis.com/tagmanager/v2/accounts',account_id,'user_permissions',sep='/')

  call_body <- list(emailAddress = email,
                  accountAccess = list(
                    permission = account_permission
                  ))

  container_access <- list()

  for (i in 1:nrow(container_permissions)) {

    element <- list(containerId = container_permissions$container_id[i],
    permission = container_permissions$permission[i])

  container_access <- list.append(container_access,element)
  }

  call_body$containerAccess <- container_access

  call <- easy_call('post',post_url,token,call_body)

  call_content <- content(call,'parsed')

  verbose_calls(call,call_content,verbose)

}


#' gtm_permissions_get
#'
#' Get the a user's permissions in a GTM account.
#' @param account_id The ID of the account
#' @param user_permission_id The ID of the user permission
#' @param token An OAuth token object - needs to be set to 'manage users'
#' @param email_address If you want to access the data via email address instead of the ID, you can set user_permission_id to NULL and then add the address here (warning: it's slower!)
#' @param verbose If you want a message letting you know that your results have been successfully retrieved. Defaults to FALSE
#' @export

gtm_permissions_get <- function(account_id,permission_id,token,email=NULL,verbose=FALSE) {

  require(tidyverse)
  require(httr)
  require(purrr)

  token_status(token)

  if (is_empty(permission_id) == TRUE) {

    permission <- gtm_permissions_list(account_id,token) %>%
      filter(email_address == email)

  } else {

    permission <- gtm_permissions_list(account_id,token) %>%
      filter(user_permission_id == permission_id)
  }

  return(permission)

}

#' gtm_permissions_delete
#'
#' Delete a user's permissions in a GTM account.
#' @param account_id The ID of the account
#' @param user_permission_id The ID of the user permission
#' @param token An OAuth token object - needs to be set to 'manage users'
#' @param email_address If you want to access the data via email address instead of the ID, you can set user_permission_id to NULL and then add the address here (warning: it's slower!)
#' @param verbose If you want a message letting you know that your results have been successfully retrieved. Defaults to FALSE
#' @export

gtm_permissions_delete <- function(account_id,permission_id,token,email=NULL,verbose=FALSE) {

  require(tidyverse)
  require(httr)
  require(purrr)

  #in theory this should work, but it doesn't in practice

  token_status(token)

  if (is_empty(permission_id) == TRUE) {

    permission_id <- gtm_permissions_list(account_id,token) %>%
      filter(email_address == email) %>%
      distinct(user_permission_id) %>%
      pull(user_permission_id)

  }

  delete_url <- paste('https://www.googleapis.com/tagmanager/v2/accounts',account_id,'user_permissions',permission_id,sep='/')

call <- easy_call('delete',delete_url,user_token)

call_content <- content(call,'parsed')

verbose_calls(call,call_content,verbose)

}

#' gtm_permissions_update
#'
#' Update the user permissions in a GTM account.
#' @param account_id The ID of the account
#' @param email The email of the user
#' @param user_permission_id The ID of the user permission. Note: if you leave this NULL then the call will use the email address to find the permission ID.
#' @param account_permission The account level permissions for the user; possible values are "accountPermissionUnspecified", "admin", "noAccess", and "user"
#' @param container_permissions A data frame of the permissions per container, with the container ID in the first column and the permission in the second column. Possible permissions are "approve",  "containerPermissionUnspecified", "edit", "noAccess", "publish", and "read"
#' @param token An OAuth token object - needs to be set to 'manage users'
#' @param verbose If you want a message letting you know that your results have been successfully retrieved. Defaults to FALSE
#' @export


gtm_permissions_update <- function(account_id,email,permission_id,account_permission,container_permissions,token,verbose=FALSE) {


#note: seems to be an issue on the GTM API front

  require(tidyverse)
  require(httr)
  require(rlist)
  require(purrr)

  container_permissions <- container_permissions %>%
    rename(container_id = 1,
           permission = 2)

  token_status(token)
  
  if (is_empty(permission_id) == TRUE) {

    permission_id <- gtm_permissions_list(account_id,token) %>%
      filter(email_address == email) %>%
      distinct(user_permission_id) %>%
      pull(user_permission_id)

  }

  post_url <- paste('https://www.googleapis.com/tagmanager/v2/accounts',account_id,'user_permissions',permission_id,sep='/')

  call_body <- list(emailAddress = email,
                  accountAccess = list(
                    permission = account_permission
                  ))

  container_access <- list()

  for (i in 1:nrow(container_permissions)) {

    element <- list(containerId = container_permissions$container_id[i],
    permission = container_permissions$permission[i])

  container_access <- list.append(container_access,element)
  }

  call_body$containerAccess <- container_access

  call <- easy_call('post',post_url,token,call_body)

  call_content <- content(call,'parsed')

  verbose_calls(call,call_content,verbose)

}
neugelb/gtmr documentation built on June 25, 2020, 10:06 a.m.