Last active
August 29, 2015 14:18
-
-
Save colegatron/017884a34182c45e4768 to your computer and use it in GitHub Desktop.
CartoDB SetLinkPermission Table
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################################################## | |
# Script : SetCartoDBMapLinkPermissions | |
# Author : Ivan Gonzalez / es.linkedin.com/in/colegatron | |
# Date : Apr 07, 2015 | |
# Description: Set Link Permissions to a table | |
############################################################## | |
# Purpose: | |
# | |
# Automate setting the permission of a table to 'LINK' | |
# I need to set it up as 'PRIVATE' previously because a bug | |
# in CartoDB side during the nightly table upload process | |
# lefts the table in an unknown state. | |
# | |
# The scripts automates the login, sets then the table as | |
# PRIVATE, then as LINK, then logout and then closes the browser. | |
# Caution: It also changes IE security settings!! | |
# | |
# Notes: | |
# - not happy with execution speed retrieving the links | |
# - feel free to use, modify and improve it | |
############################################################## | |
$userName = "myusername" | |
$password = "mysecretpass" | |
$tableName = "thetablename" | |
$baseUrl = "https://$userName.cartodb.com/tables/" | |
$cdbUrl = "https://$userName.cartodb.com/maps" | |
$cdbLogin = "http://cartodb.com/login" | |
$cdbLogout = "http://cartodb.com/logout" | |
$IEVisible = $true | |
write-host "Setting LINK permission to $tableName table" | |
function waitIEReady( $browser ) { | |
while ( $browser.busy ) { sleep 2 } | |
} | |
# Returns the current IE Security Admin status and sets security upon $status | |
function Set-IEESC( $status ) | |
{ | |
if ( $status -eq $false ) { $status = 0; $cTxt = "[DISABLED]" } else { $status = 1; $cTxt = "[ENABLED]" } | |
$AdminKey = “HKLM:\SOFTWARE\Microsoft\Active Setup\Installed Components\{A509B1A7-37EF-4b3f-8CFC-4F3A74704073}” | |
$UserKey = “HKLM:\SOFTWARE\Microsoft\Active Setup\Installed Components\{A509B1A8-37EF-4b3f-8CFC-4F3A74704073}” | |
$cRet = if ((Get-ItemProperty -Path $AdminKey).IsInstalled -eq 1) { $true } else { $false } | |
Set-ItemProperty -Path $AdminKey -Name “IsInstalled” -Value $status | |
Set-ItemProperty -Path $UserKey -Name “IsInstalled” -Value $status | |
Stop-Process -Name Explorer | |
Write-Host “IE Enhanced Security Configuration (ESC) has been $cTxt.” -ForegroundColor Green | |
return $cRet | |
} | |
$currentStatus = Set-IEESC $false | |
try { | |
$ie = new-object -com "InternetExplorer.Application" | |
$ie.visible = $IEVisible | |
$ie.Navigate( $cdbUrl ) | |
waitIEReady( $ie ) | |
$doc = $ie.document | |
write-host "Checking if we are logged or not..." | |
$loginLink = $doc.getElementsByTagName("a") | where-object -property href -EQ $cdbLogin | |
if ( $loginLink ) { | |
write-host "We are not logged, need to log in..." | |
$loginLink.click() | |
waitIEReady( $ie ) | |
$ie.document.getElementById("session_email").Value = $userName | |
$ie.document.getElementById("session_password").Value = $password | |
$ie.document.getElementById("new_session").submit() | |
waitIEReady( $ie ) | |
$needToLogout = $true | |
} | |
# Once logged finally open the correct page | |
$ie.Navigate( $($baseUrl + $tableName) ) | |
waitIEReady( $ie ) | |
$doc = $ie.document | |
write-host -NoNewline "Opening privacy settings window..." | |
$linkW = $doc.getElementsByTagName("a") | where-object -property href -EQ ( $baseUrl + $tableName + "#/visualize" ) | |
write-host "clicking..." | |
$linkW.item(0).click(); | |
waitIEReady( $ie ) | |
write-host -NoNewline "Getting 'private' option link..." | |
$link = $doc.getElementsByTagName("a") | where-object -property href -EQ ( $baseUrl + $tableName + "#/private" ) | |
write-host "clicking..." | |
$link.click(); | |
waitIEReady( $ie ) | |
write-host -NoNewline "Getting saving settings link..." | |
$linkS = $doc.getElementsByTagName("a") | where-object -property href -EQ ( $baseUrl + $tableName + "#/save-settings" ) | |
write-host "clicking..." | |
$linkS.click(); | |
waitIEReady( $ie ) | |
write-host "Opening privacy settings window...clicking..." | |
$linkW = $doc.getElementsByTagName("a") | where-object -property href -EQ ( $baseUrl + $tableName + "#/visualize" ) | |
$linkW.item(0).click(); | |
waitIEReady( $ie ) | |
write-host -NoNewline "Getting 'link' option link..." | |
$link = $doc.getElementsByTagName("a") | where-object -property href -EQ ( $baseUrl + $tableName + "#/link" ) | |
write-host "clicking..." | |
$link.click(); | |
waitIEReady( $ie ) | |
write-host "Clicking saving settings link..." | |
$linkS = $doc.getElementsByTagName("a") | where-object -property href -EQ ( $baseUrl + $tableName + "#/save-settings" ) | |
$linkS.click(); | |
waitIEReady( $ie ) | |
if ( $needToLogout ) { | |
write-host "Logout (CartoDB session expiration is too long)..." | |
$ie.Navigate( $cdbLogout ) | |
waitIEReady( $ie ) | |
} | |
} | |
finally { | |
write-host "Closing window ..." | |
$ie.quit(); | |
write-host "Reseting to previous security settings..." | |
Set-IEESC $currentStatus | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment