Thursday, June 7, 2012

Let Jenkins visitors know their access is restricted

We use Jenkins in the QA department and expose it to other teams and departments. For new users it is easy to be confused as to why they cannot do certain things, such as start a build, because Jenkins simply hides icons that a visitor does not have access to. In this article I explain how to get a header on every page Jenkins serves that lets them know their access is restricted:



Follow these simple steps:
  1. Install the Page Markup Plugin
  2. Go to Manage Jenkins > Configure System
  3. For Header HTML in the Additional Page HTML Markup section, put:
     <div id="my-banner" align="center" style="display:none;">  
     <p /><img src="/userContent/error.png" /><b>You are not logged in. To start a job or make changes you must log in. If you do not have an account, you can <a href="/signup">sign up</a> for one.</b>  
     </div>  
    
    Note that the image referenced here does not come with Jenkins and needs to be put in the userContent folder.
  4. For Footer HTML in the Additional Page HTML Markup section, put:
     <script type="text/javascript">  
     function loggedIn() {  
         var x;  
         x = document.getElementById('login-field')  
         if(x.innerText) {  
             x = x.innerText;  
         }else{  
             x = x.textContent;  
         }  
         return (x.indexOf('log out') > 0);  
     }  
     if (!loggedIn()) {  
         document.getElementById('my-banner').style.display = 'block';   
     }  
     </script>  
    
  5. Hit Save
The header will only display if a visitor is not signed in and Javascript is enabled.