/* Helper function: saves specified value to persistent storage: into cookies and
html 5 session storage which may not be available in old browsers. There
may be problems wiht using cookies in IE inside iframe (it requires p3p header),
that's why we need some another storage*/
function saveToStorage(key, val) {
$.cookie(key, val);
if (sessionStorage) {
sessionStorage.setItem(key, val);
}
}
/* Helper function: gets value by specified key from cookie. If cookie is not available
(see comment above), then it uses html 5 session storage */
function getFromStorage(key) {
var result = $.cookie(key);
if (!result && sessionStorage) {
result = sessionStorage.getItem(key);
}
return result;
}
/* Helper function: cleanups previos results and errors */
function cleanup() {
$(".error").empty();
$("#results").empty();
}
/* Helper function: reads query string parameters */
function getQueryVariable(variable) {
var query = window.location.search.substring(1);
if (query == "") {
var hashIndex = document.location.href.indexOf("#");
if (hashIndex > 0) {
query = document.location.href.substring(hashIndex + 1);
}
}
var vars = query.split("&");
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split("=");
if (pair[0] == variable) {
return pair[1];
}
}
return false;
}
/* Gets roles from Taloyhtio API using CORS with jquery.ajax() */
function getRoles() {
// read token from query string
var accessToken = getQueryVariable("access_token");
if (!accessToken) {
$(".error").html("Access token not obtained. Before to call API, obtain access token first.");
return;
}
// retrieve previously stored site url from storage
var siteUrl = getFromStorage("pmcSiteUrl");
if (!siteUrl) {
$(".error").html("PMC site url not specified. Specify PMC site url in siteUrl query string parameter.");
return;
}
// make HTTP GET ajax call to Taloyhtio REST API
$.ajax({
type: 'GET',
url: 'https://api.generalsso.com/dataapi.svc/getroles?siteUrl=' + siteUrl,
dataType: 'json',
contentType: 'application/json; charset=utf-8',
// access token is specified in Authorization HTTP header by the following way:
// Authorization: Bearer {token}
headers: { 'Authorization': 'Bearer ' + accessToken },
success: function(result) {
// display roles in user friendly form
cleanup();
if (result && result.d && result.d.Roles) {
var content = "User email: " + result.d.Email + "
";
content = content.concat("
| Condo short name | "); content = content.concat("Condo or PMC? | "); content = content.concat("Role | "); content = content.concat("Web id | "); content = content.concat("Web url | "); content = content.concat("
|---|---|---|---|---|
| " + r.CondoShortName + " | "); content = content.concat("" + (r.IsCondo ? "Condo" : "PMC") + " | "); content = content.concat("" + r.Name + " | "); content = content.concat("" + r.WebId + " | "); content = content.concat("" + r.WebUrl + " | "); content = content.concat("