Advertisement
Guest User

Untitled

a guest
Jul 11th, 2018
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var CiscoGuestUser = Class.create();
  2.  
  3. CiscoGuestUser.CISCO_ADDRESS = gs.getProperty('hoffmann.cisco.endpoint') || 'https://demucsrnmpi01.corp.hoffmann-group.com';
  4.  
  5. CiscoGuestUser.MID_SERVER = gs.getProperty('hoffmann.cisco.midserver') || '';
  6.  
  7. CiscoGuestUser.CONTROLLER = gs.getProperty('cisco.guest.users.controller') || 607674067;
  8.  
  9. CiscoGuestUser.CISCO_AUTH_DATA = (function() {
  10.     var profileName = gs.getProperty('hoffmann.cisco.basic.profile.name') || 'Cisco';
  11.     var profileRecord = new GlideRecord('sys_auth_profile_basic');
  12.     var profileId = profileRecord.get('name', profileName) ? profileRecord.sys_id + '' : 'b7171752db3a93c0197bb14ffe9619f3';
  13.        
  14.     return {
  15.         type: 'basic',
  16.         profileId: profileId
  17.     };
  18. })();
  19.  
  20. CiscoGuestUser.getGuestUserNames = function(/*integer (optional)*/ controllerId) {
  21.     var controller = controllerId || CiscoGuestUser.CONTROLLER;
  22.     var endpoint = CiscoGuestUser.CISCO_ADDRESS + '/webacs/api/v3/op/guestUser/guestUsersOnController?controllerId=' + controller,
  23.         midServer = CiscoGuestUser.MID_SERVER,
  24.         httpTimeout = 60,
  25.         httpMethod = 'get',
  26.         async = false;
  27.    
  28.     var req = new sn_ws.RESTMessageV2();
  29.     req.setEndpoint(endpoint);
  30.     req.setRequestHeader('Accept', 'application/json');
  31.     req.setMIDServer(midServer);
  32.     req.setAuthenticationProfile(CiscoGuestUser.CISCO_AUTH_DATA.type, CiscoGuestUser.CISCO_AUTH_DATA.profileId);
  33.     req.setHttpTimeout(httpTimeout);
  34.     req.setHttpMethod(httpMethod);
  35.  
  36.     var res = async == true ? req.executeAsync() : req.execute();
  37.    
  38.     RESTLogger.logOutbound(req, res, {
  39.         httpMethod: httpMethod,
  40.         httpTimeout: httpTimeout,
  41.         authType: CiscoGuestUser.CISCO_AUTH_DATA.type,
  42.         profileId: CiscoGuestUser.CISCO_AUTH_DATA.profileId,
  43.         async: async,
  44.         midServer: midServer
  45.     });
  46.    
  47.     if(res.haveError()) {
  48.         return null;
  49.     }
  50.    
  51.     var usersArr = JSON.parse(res.getBody()).mgmtResponse.guestUserNamesDTO[0].usernames.username;
  52.     //convert the array to a map so we can check whether user exists without iterating the elements
  53.     var result = {};
  54.     usersArr.forEach(function(el) {
  55.         result[el] = el;
  56.     });
  57.    
  58.     return result;
  59. };
  60.  
  61. CiscoGuestUser.prototype = {
  62.    
  63.     initialize: function(/*GlideRecord*/ guestRecord) {
  64.         if(guestRecord) {
  65.             this.setFromRecord(guestRecord);
  66.         }
  67.     },
  68.    
  69.     setFromRecord: function(/*GlideRecord*/ guestRecord) {
  70.         this._guestName = guestRecord.u_guest + '';
  71.         this._guestCompanyName = guestRecord.u_company + '';
  72.         this._ciscoUserName = this._generateUserName();
  73.         this._ciscoUserPassword = this._generateRandomPassword();
  74.         this._departureDate = this._formatDateTime(guestRecord.u_date_of_departure.getDisplayValue());
  75.         this._sysId = guestRecord.sys_id + '';
  76.     },
  77.    
  78.     _formatDateTime: function(dateTime) {
  79.         //converts dd.mm.yyyy hh:mm:ss to yyyy-mm-ddThh:mm:ss+00:00
  80.         return dateTime ? dateTime.split(' ').map(function(el, index) {
  81.             if(index == 0)
  82.                return (el + '').split('.').reverse().join('-') + 'T';
  83.             else
  84.                return el + '.637Z';
  85.         }).join('') :
  86.         '';
  87.     },
  88.    
  89.     _generateRandomPassword: function() {
  90.         var passwordChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
  91.             randomPass = '';
  92.        
  93.         for(var i = 0; i < 8; i++) {
  94.             randomPass += passwordChars.charAt(Math.floor(Math.random() * passwordChars.length));
  95.         }
  96.        
  97.         return randomPass;
  98.     },
  99.    
  100.     _generateUserName: function() {
  101.         //builds a username in format  HOM-<Capital first letter of first name>.<second name>-<company name>   if username is above 28 chars - cuts it off (cisco allows 28 max length for usernames)
  102.         return ('HOM-' + this.getGuestName().trim().split(/\s+/).map(function(el, index) {
  103.             if(index == 0)
  104.                 return el.split('')[0].toUpperCase();
  105.             else if(index == 1)
  106.                 return '.' + el;
  107.             else return '';
  108.         }).join('') + '-' + this.getCompanyName()).split('').slice(0, 27).join('');
  109.     },
  110.    
  111.     getGuestName: function() {
  112.         return this._guestName;
  113.     },
  114.    
  115.     getCompanyName: function() {
  116.         return this._guestCompanyName;
  117.     },
  118.    
  119.     getCiscoUserName: function() {
  120.         return this._ciscoUserName;
  121.     },
  122.    
  123.     getRecordSysId: function() {
  124.         return this._sysId;
  125.     },
  126.    
  127.     setCiscoUserName: function(/*string*/ userName) {
  128.         this._ciscoUserName = userName.length <= 28 ? userName : userName.split('').slice(0, 27).join('');
  129.     },
  130.    
  131.     getCiscoUserPassword: function() {
  132.         return this._ciscoUserPassword;
  133.     },
  134.    
  135.     setCiscoUserPassword: function(/*string*/ password) {
  136.         this._ciscoUserPassword = password;
  137.     },
  138.    
  139.     userExistsIn: function(/*object (map)*/ usernames) {
  140.         return usernames ? usernames[this.getCiscoUserName()] != undefined : CiscoGuestUser.getGuestUserNames()[this.getCiscoUserName()] != undefined;
  141.     },
  142.    
  143.     getDepartureDate: function() {
  144.         return this._departureDate;
  145.     },
  146.    
  147.     getGuestInfo: function() {
  148.         return {
  149.             guestName: this.getGuestName(),
  150.             companyName: this.getCompanyName(),
  151.             ciscoUserName: this.getCiscoUserName(),
  152.             sys_id: this.getRecordSysId()
  153.         };
  154.     },
  155.    
  156.     getGuestInfoAsString: function() {
  157.         return 'Guest name: ' + this.getGuestName() + '\n' +
  158.             'Guest company name: ' + this.getCompanyName() + '\n' +
  159.             'Cisco user name: ' + this.getCiscoUserName() + '\n' +
  160.             'Guest registration history record sys_id: ' + this.getRecordSysId();
  161.     },
  162.    
  163.     _sendUserData: function(httpMethod) {
  164.         //the API for creating/updating users is the same endpoint and accepts the same data, difference is only the httpMethod used
  165.         var headers = {
  166.             'Content-Type': 'application/json',
  167.             'Accept': 'application/json'
  168.         },
  169.         endpoint = CiscoGuestUser.CISCO_ADDRESS + '/webacs/api/v3/op/guestUser',
  170.         midServer = CiscoGuestUser.MID_SERVER,
  171.         httpTimeout = 60,
  172.         async = false,
  173.         controller = CiscoGuestUser.CONTROLLER,
  174.         reqBody = JSON.stringify({
  175.             manageGuestUsersDTO: {
  176.                 applyGuestUserTo: 'CONTROLLER_LIST',
  177.                 controllerIds: {
  178.                     controllerId: [controller]
  179.                 },
  180.                 description: 'Guest User Arriving Needing WLAN access',
  181.                 disclaimer: 'Guest User WLAN access',
  182.                 endTime: this.getDepartureDate(),
  183.                 password: this.getCiscoUserPassword(),
  184.                 profile: 'HOG_GUEST',
  185.                 rebootController: false,
  186.                 saveConfigToFlash: true,
  187.                 userRole: 'default',
  188.                 username: this.getCiscoUserName()
  189.             }
  190.         });
  191.        
  192.         var req = new sn_ws.RESTMessageV2();
  193.         req.setEndpoint(endpoint);
  194.         req.setAuthenticationProfile(CiscoGuestUser.CISCO_AUTH_DATA.type, CiscoGuestUser.CISCO_AUTH_DATA.profileId);
  195.         req.setHttpMethod(httpMethod);
  196.         req.setHttpTimeout(httpTimeout);
  197.         req.setMIDServer(midServer);
  198.         for(var header in headers) {
  199.             req.setRequestHeader(header, headers[header]);
  200.         }
  201.         req.setRequestBody(reqBody);
  202.        
  203.         var res = async == true ? req.executeAsync() : req.execute();
  204.        
  205.         RESTLogger.logOutbound(req, res, {
  206.             midServer: midServer,
  207.             httpMethod: httpMethod,
  208.             httpTimeout: httpTimeout,
  209.             async: async,
  210.             authType: CiscoGuestUser.CISCO_AUTH_DATA.type,
  211.             profileId: CiscoGuestUser.CISCO_AUTH_DATA.profileId
  212.         });
  213.        
  214.         return !res.haveError();
  215.     },
  216.    
  217.     createUser: function() {
  218.         return this._sendUserData('post');
  219.     },
  220.    
  221.     updateUser: function() {
  222.         return this._sendUserData('put');
  223.     },
  224.    
  225.     type: 'CiscoGuestUser'
  226. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement