Thursday, February 21, 2013

SharePoint, cookies + masterpages

Today I find my self tasked to take care of a requirement on our network to alert users that they are accessing sensitive information and that in order to access our portal we need to make sure the message pops up no matter if they have book marked their sites or have shortcuts to other locations on the portal that they use to get into their site.

Normally I would create a Visual Studio solution for a custom master page, but our portal was migrated from 2007 to 2010 and some site collections had already been using a customized master page that in most cases had been modified using SharePoint Designer and those changes were not going to be changed.

The other challenge I had was I needed a way to find out what sites had a different master page than the custom one that was in use at the top level site,  luckily we only had 3 different master pages in our farm.

Approach to resolving the problem.

Session cookie
In order to get our pop up to display on our intranet we decided to use a cookie that would be called from the master page, this script was uploaded into our main portal site and put in the document library, Site Assets, and we made sure that ad\domain users had read access to the library. Since SharePoint 2010 supports javascript out of the box this should be a quick fix.

Once this was done we can use the same line of code added to all of the custom master pages to reference our script, so if the message ever needed to be updated a user could go to the library and edit the javascript file instead of waiting for a developer and a trip through our change management process just to update some text.

The last requirement was that the pop up should needed to be based on the session for the current user, once the user closed their browser the cookie was destroyed, which means that the next time the user opened their browser and went to our site they would be prompted again to acknowledge that they were accessing sensitive information, this needed to be seamless, meaning that when the users navigated from site collection to site collection they should never see the prompt, until they closed and reopened their browser.

var bannerText = "No physical clone trooper costume was produced for their first onscreen appearance. \n\n As revealed in the audio commentary of Star Wars Episode II: Attack of the Clones, every single clone trooper in the film is a computer generated image. In fact, the only thing present on the set was the actor, whose head was used during scenes requiring a clone's face. In Star Wars Episode III: Revenge of the Sith, the armor is redesigned, but remains computer generated. Within the fictional history of the Star Wars universe, this is the basis of the Imperial Stormtrooper armor and shows a gradual evolution in design toward the armor worn in episodes III-VI, known as "Phase II" armor.\n\n The actual armor never changes, but the helmet progresses.";

function setCookie(name,value,days) {
       if (days) {
          var date = new Date();
          date.setTime(date.getTime()+(days*24*60*60*1000));
          var expires = ""; expires="+date.toGMTString();
       }
       else var expires = "";
       document.cookie = name + "=" + value + expires + "; path=/; domain=.company.com";
    }

function getCookie(name) {
       var nameEQ = name + "=";
       var ca = document.cookie.split(';');
       for(var i=0;i < ca.length;i++) {
          var c = ca[i];
          while (c.charAt(0)==' ') c = c.substring(1,c.length);
          if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
       }
       return null;
    }

function CheckAuth() 
{
/* Check to see if the AuthConsent cookie exists and put it into a variable so we can check to see if it found what we were looking for*/
 var userConsents = getCookie('AuthConsent');
/* The cookie was found, lets see if the value of true has been added to the file */
    if (userConsents != 'true') {
/* The cookie file was found but the value of true was not in it, so show the warning banner to the user */
        var haveConsent = confirm(bannerText);
/* The user clicked the OK button so now add the cookie */
        if (haveConsent) {
/* Here we set the cookie name in the first parameter, then the value inside the cookie file which we used the word true for, it could be any unique value, and last we set the number of days we want the cookie to set for, we say 0 as we want the cookie to be destroyed when the user closes their browser. */
            setCookie('AuthConsent', 'true', 0);
        }
        else {
            window.navigate("denied.html");
        }
    }
}
CheckAuth()
Updating the master page file
First make sure you know which master page your site is using, either by using Powershell or going to the portal and going to Site Actions > Site Settings > under Look and Feel > Master page. (You really should use Powershell to find out if any of your sites have a different master page.) Also please note that if you do not see the Master page link highlighted below you will need to go to Site Actions> Manage Site Features and enable SharePoint Publishing, IF YOU GO THROUGH THE UI, otherwise use Powershell :)
Open SharePoint Designer and check out the current master page and then Right click on the master page and Edit in Advanced Mode so we can add our little one liner in between the head tags, normally I find the closing tag and add my script reference just above it.
Once this has been added then we need to save, check in, and approve the master page so all of our users can start using it and getting that wonderful prompt. Once you check it in and then accept the dialog window to go to SharePoint and Approve the page, you should start seeing the prompt as the page loads up. In this instance only people with access to approve and higher should be able to see this change, once you Approve it all users will see the change.

Powershell script
Since the master page was customized and set differently in various sites and site collections, I needed a way to update all the sites in a site collection where they were not using the same master page, for this we will use Powershell to loop through all of our sites and set the master page.

if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) 
{
    Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}
CLS
## Set variables
$url = "https://www.company.com/"
$masterpage = "$url/_catalogs/masterpage/Intranet.IT.master"
## Script
Start-SPAssignment -Global
$site = Get-SPWeb $url
if($site)
{
    foreach($web in $webs.Webs)
    {
        $web.custommasterurl = $masterpage
        $web.masterUrl = $masterpage 
        $web.Update()
        $web.dispose()
    }
}
else
{
    write-host "Site not found, please check the URL of the site"
}

Stop-SPAssignment -Global

No comments: