cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
518
Views
0
Helpful
3
Replies

User / Customer Role Membership Check in ISF / Javascript

lim.park01
Level 1
Level 1

Did not see this in the documentation...

Does anyone know if checking for the User or Customer Role membership inside ISF / Javascript is supported? -- Like similar to how we can check for "Moment".

If so, what is the variable name and the values?

Thanks.

3 Replies 3

Paul Jeffrey
Level 1
Level 1

Based on what I've seen there doesn't appear to be a JavaScript variable for this, if you view the source you will see only the following exist:

  var Moment = 'ordering';
  var EditRequisitionBeforeOrdering = false;
  var Context = 'MyServices';
  var UserID = '1';
  var ReqCustomerID = '1';
  var ReqInitiatorID = '1';
  var ReqID = '0';
  var ReqEntryID = '0';
  var PricePerUnit  = 1425.75;
  var Quantity  = 1;
  var Bundled = false;
  var TaskID = '0';
  var TaskName  = '';
  var ServiceID  = '553';
  var ServiceName  = 'New Employee On Board';

 

Our approach (and have also seen this used in IAC in a function called checkRole in customcso.js) is to use the NS API to call /RequestCenter/nsapi/directory/currentuser and extract the role attributes from the DOM object

Thanks for the reply.

That sounds like a good option, but unfortunately, that will take me little more time/effort than I thought... I'll have to figure out some other alternative for now.

As a follow up question, in the integration guide, there are examples of using nsAPI from JavaScript Portlets. Do you know if the same libraries etc. will work and supported from the plain ISF / Javascript functions?

 

Thanks.

I think some of the libraries such as ExtJS are loaded and usable on the order forms, but for ones that are not, you can point to the existing library files under RequestCenter.war and add them as a JavaScript library in service designer.

Below is a quick example of looking up the role through the NS API, it should be enough to get you started on what you need.

 

function doSomething(){
    
    //Performs NS API XHR Request and returns the responseXML document
    function nsapiRequest(url) {
        var xhr = new XMLHttpRequest();
        xhr.open("GET", url, false);
        xhr.send();
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                return xhr.responseXML;
            } else {
                console.error(xhr.statusText);
            }
        }
    }

    //Function to check if user belongs to a role
    function hasRole(roleName) {
        var doc = nsapiRequest("/RequestCenter/nsapi/directory/people/currentuser");
        //Get all associated role elements
        var roles = doc.getElementsByTagName("associatedRole");
            
        //Loop through roles to check for a specific one, could also build an array of all user roles
        for (var i = 0; i < roles.length; i++) {
            //Extract role value
            var role = roles[i].childNodes[1].firstChild.nodeValue;
            if (role === roleName)
                return true;
        }
        return false;
    }

    var siteAdmin = hasRole("Site Administrator");
    if (siteAdmin){
        //do something
    }
}

Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community: