Skip to content

Commit

Permalink
feat: update groups and users
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomeriko96 committed Jan 6, 2024
1 parent 2e1680a commit 66a68fa
Show file tree
Hide file tree
Showing 37 changed files with 391 additions and 52 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,6 @@ export(get_workbook_tabs)
export(make_rootnodes)
export(query_user_on_site)
export(remove_user_from_group)
export(update_group)
export(update_user)
importFrom(magrittr,"%>%")
62 changes: 62 additions & 0 deletions R/update_group.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#' Update Group
#'
#' Updates a group on a Tableau Server or Tableau Cloud site.
#'
#' @param tableau A list containing the Tableau authentication variables: `base_url`, `token`, and `site_id`.
#' @param group_id The ID of the group to update.
#' @param group_name The new name for the group (optional).
#' @param AD_group_name The name of the Active Directory group to synchronize with (optional).
#' @param AD_domain The domain for the Active Directory group (optional).
#' @param minimum_site_role Required if an import element or grantLicenseMode attribute are present in the request. The site role assigned to users who are imported from Active Directory or granted a license automatically using the grant license on-sync or on-login mode.
#' @param license_mode Optional. The mode for automatically applying licenses for group members. When the mode is onLogin, a license is granted for each group member when they log in to a site.
#' @param on_demand_access Optional. A boolean value that is used to enable on-demand access for embedded Tableau content when the Tableau Cloud site is licensed with Embedded Analytics usage-based model.
#' @param asJob A Boolean value indicating whether to synchronize with Active Directory as a background task (true) or synchronize immediately (false). Default is false.
#' @param api_version The API version to use (default: 3.19).
#'
#' @return TRUE if the operation was successful, FALSE otherwise.
#' @export
#'
#' @family Tableau REST API
update_group <- function(tableau, group_id, group_name = NULL, AD_group_name = NULL, AD_domain = NULL, minimum_site_role = NULL, license_mode = NULL, on_demand_access = NULL, asJob = FALSE, api_version = 3.19) {
base_url <- tableau$base_url
token <- tableau$token
site_id <- tableau$site_id

url <- paste0(
base_url,
"/api/",
api_version,
"/sites/",
site_id,
"/groups/",
group_id,
if (asJob) "?asJob=true" else ""
)

# Construct the request body
request_body <- paste0(
"<tsRequest>",
"<group ",
if (!is.null(group_name)) paste0("name=\"", group_name, "\" "),
if (!is.null(AD_group_name)) paste0("AD-group-name=\"", AD_group_name, "\" "),
if (!is.null(AD_domain)) paste0("AD-domain=\"", AD_domain, "\" "),
if (!is.null(minimum_site_role)) paste0("minimum-site-role=\"", minimum_site_role, "\" "),
if (!is.null(license_mode)) paste0("license-mode=\"", license_mode, "\" "),
if (!is.null(on_demand_access)) paste0("on-demand-access=\"", on_demand_access, "\" "),
"/>",
"</tsRequest>"
)

api_response <- httr::PUT(
url,
httr::add_headers("X-Tableau-Auth" = token),
body = request_body
)

# Check the response status code
if (httr::status_code(api_response) != 200) {
stop("Group update failed. Please check your API key and base URL.")
}

return(TRUE)
}
58 changes: 58 additions & 0 deletions R/update_user.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#' Update User
#'
#' Modifies information about the specified user.
#'
#' @param tableau A list containing the Tableau authentication variables: `base_url`, `token`, and `site_id`.
#' @param user_id The ID of the user to update.
#' @param fullName The new name for the user (optional).
#' @param email The new email address for the user (optional).
#' @param password The new password for the user (optional).
#' @param siteRole The new site role to assign to the user.
#' @param authSetting The authentication type for the user (optional).
#' @param api_version The API version to use (default: 3.19).
#'
#' @return TRUE if the operation was successful, FALSE otherwise.
#' @export
#'
#' @family Tableau REST API
update_user <- function(tableau, user_id, fullName = NULL, email = NULL, password = NULL, siteRole, authSetting = NULL, api_version = 3.19) {
base_url <- tableau$base_url
token <- tableau$token
site_id <- tableau$site_id

url <- paste0(
base_url,
"/api/",
api_version,
"/sites/",
site_id,
"/users/",
user_id
)

# Construct the request body
request_body <- paste0(
"<tsRequest>",
"<user ",
if (!is.null(fullName)) paste0("fullName=\"", fullName, "\" "),
if (!is.null(email)) paste0("email=\"", email, "\" "),
if (!is.null(password)) paste0("password=\"", password, "\" "),
"siteRole=\"", siteRole, "\" ",
if (!is.null(authSetting)) paste0("authSetting=\"", authSetting, "\" "),
"/>",
"</tsRequest>"
)

api_response <- httr::PUT(
url,
httr::add_headers("X-Tableau-Auth" = token),
body = request_body
)

# Check the response status code
if (httr::status_code(api_response) != 200) {
stop("User update failed. Please check your API key and base URL.")
}

return(TRUE)
}
4 changes: 2 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,8 @@
- [x] Query User On Site
- [x] Remove User from Group
- [x] Remove User from Site
- [ ] Update Group
- [ ] Update User
- [x] Update Group
- [x] Update User
- [ ] List Virtual Connection Database Connections - Available only with a Data Management license.
- [ ] List Virtual Connections - Available only with a Data Management license.
- [ ] Update Virtual Connection Database Connections - Available only with a Data Management license.
Expand Down
41 changes: 21 additions & 20 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,49 @@ reference:
# desc: Analytics Extensions Settings methods for Tableau REST API.
# contents:
# - None
#

# - title: Ask Data Lens Methods - Retired in API 3.22
# desc: Ask Data Lens Settings methods for Tableau REST API.
# contents:
# - None
#

- title: Authentication Methods
desc: Authentication methods for Tableau REST API.
contents:
- authenticate_PAT
- authenticate_server
#

- title: Connected App Methods
desc: Connected app methods for Tableau REST API.
contents:
- get_server_connected_apps
#

# - title: Content Exploration Methods
# desc: Content exploration methods for Tableau REST API.
# contents:
# - None
#

# - title: Dashboard Extensions Settings Methods - Retired in API 3.21
# desc: Dashboard Extensions methods for Tableau REST API.
# contents:
# - None
#

- title: Data Sources Methods
desc: Data sources methods for Tableau REST API.
contents:
- download_tableau_data
- get_server_datasources
#

- title: Extract and Encryption Methods
desc: Extract and encryption methods for Tableau REST API.
contents:
- get_server_refresh_tasks
#

- title: Favorites Methods
desc: Favorites methods for Tableau REST API.
contents:
- get_server_user_favorites
#

# - title: Flow Methods
# desc: Flow methods for Tableau REST API.
# contents:
Expand All @@ -59,14 +59,14 @@ reference:
# desc: Identity pools methods for Tableau REST API.
# contents:
# - None
#

- title: Jobs, Tasks, and Schedules Methods
desc: Jobs, tasks, and schedules methods for Tableau REST API.
contents:
- get_server_jobs
- get_server_schedules

#

- title: Metadata Methods
desc: Metadata methods for Tableau REST API.
contents:
Expand All @@ -76,27 +76,27 @@ reference:
# desc: Metrics settings methods for Tableau REST API.
# contents:
# - None
#

# - title: Mobile Settings Methods
# desc: Mobile settings methods for Tableau REST API.
# contents:
# - None
#

# - title: Notifications Methods
# desc: Notifications methods for Tableau REST API.
# contents:
# - None
#

# - title: Permissions Methods
# desc: Permissions methods for Tableau REST API.
# contents:
# - None
#

- title: Projects Methods
desc: Projects methods for Tableau REST API.
contents:
- get_server_projects
#

# - title: Publishing Methods
# desc: Publishing methods for Tableau REST API.
# contents:
Expand All @@ -111,7 +111,7 @@ reference:
# desc: Revisions methods for Tableau REST API.
# contents:
# - None
#

- title: Server Methods
desc: Server methods for Tableau REST API.
contents:
Expand All @@ -137,7 +137,7 @@ reference:
# desc: Tableau extensions settings methods for Tableau REST API.
# contents:
# - None
#

- title: Users and Groups Methods
desc: Users and groups methods for Tableau REST API.
contents:
Expand All @@ -152,13 +152,14 @@ reference:
- query_user_on_site
- remove_user_from_group
- remove_user_from_site
- update_group
- update_user
#
# - title: Virtual Connections Methods
# desc: Virtual Connections methods for Tableau REST API.
# contents:
# - None
#
#

- title: Workbooks and Views Methods
desc: Workbooks and Views Methods methods for Tableau REST API.
contents:
Expand Down
4 changes: 3 additions & 1 deletion man/add_tags_to_view.Rd

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

4 changes: 3 additions & 1 deletion man/add_tags_to_workbook.Rd

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

4 changes: 3 additions & 1 deletion man/add_user_to_group.Rd

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

4 changes: 3 additions & 1 deletion man/add_user_to_site.Rd

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

4 changes: 3 additions & 1 deletion man/authenticate_server.Rd

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

4 changes: 3 additions & 1 deletion man/create_group.Rd

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

4 changes: 3 additions & 1 deletion man/download_filtered_tableau_image.Rd

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

4 changes: 3 additions & 1 deletion man/download_tableau_crosstab_excel.Rd

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

4 changes: 3 additions & 1 deletion man/download_tableau_data.Rd

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

4 changes: 3 additions & 1 deletion man/download_workbooks_server.Rd

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

4 changes: 3 additions & 1 deletion man/download_workbooks_server_pdf.Rd

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

Loading

0 comments on commit 66a68fa

Please sign in to comment.