Advertisement
Guest User

Untitled

a guest
Jul 10th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var CiscoGuestUser = Class.create();
  2. CiscoGuestUser.prototype = {
  3.     _passwordChars: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
  4.    
  5.     _ciscoAuthProfile: (function() {
  6.         var profileRecord = new GlideRecord('sys_auth_profile_basic');
  7.         var profileId = profileRecord.get('name', 'Cisco') ? profileId.sys_id + '' : 'b7171752db3a93c0197bb14ffe9619f3';
  8.        
  9.         return {
  10.             type: 'basic',
  11.             profileId: profileId
  12.         };
  13.     })(),
  14.    
  15.     initialize: function(/*GlideRecord*/ guestRecord) {
  16.         if(guestRecord) {
  17.             this.setFromRecord(guestRecord);
  18.         }
  19.     },
  20.    
  21.     setFromRecord: function(/*GlideRecord*/ guestRecord) {
  22.         this._guestName = guestRecord.u_guest + '';
  23.         this._guestCompanyName = guestRecord.u_company + '';
  24.         this._ciscoUserName = this._generateUserName();
  25.         this._ciscoUserPassword = this._generateRandomPassword();
  26.     },
  27.    
  28.     _generateRandomPassword: function() {
  29.         var randomPass = '';
  30.        
  31.         for(var i = 0; i < 8; i++) {
  32.             randomPass += this._passwordChars.charAt(Math.floor(Math.random() * this._passwordChars.length));
  33.         }
  34.        
  35.         return randomPass;
  36.     },
  37.    
  38.     _generateUserName: function() {
  39.         return ('HOM.' + this._guestName.trim().split(/\s+/).map(function(el, index) {
  40.             if(index == 0)
  41.                 return el.split('')[0].toUpperCase();
  42.             else if(index == 1)
  43.                 return '.' + el;
  44.             else return '';
  45.         }).join('') + '-' + this._guestCompanyName).split('').slice(0, 27).join('');
  46.     },
  47.    
  48.     getGuestName: function() {
  49.         return this._guestName;
  50.     },
  51.    
  52.     getGuestCompany: function() {
  53.         return this._guestCompanyName;
  54.     },
  55.    
  56.     getCiscoUserName: function() {
  57.         return this._ciscoUserName;
  58.     },
  59.    
  60.     getCiscoUserPassword: function() {
  61.         return this._ciscoUserPassword;
  62.     },
  63.    
  64.     createUser: function() {
  65.         var headers = {
  66.             'Content-Type': 'application/json',
  67.             'Accept': 'application/json'
  68.         },
  69.         createEndpoint = gs.getProperty('hoffmann.cisco.guest.create.endpoint') || 'https://demucsrnmpi01.corp.hoffmann-group.com/webacs/api/v3/op/guestUser',
  70.         midServer = 'demucsrapmid01-test',
  71.         httpMethod = 'post',
  72.         httpTimeout = 60,
  73.         async = true,
  74.         reqBody = JSON.stringify({
  75.             manageGuestUsersDTO: {
  76.                 applyGuestUserTo: 'CONTROLLER_LIST',
  77.                 controllerIds: {
  78.                     controllerId: [607674067]
  79.                 },
  80.                 description: 'test',
  81.                 disclaimer: 'test',
  82.                 //to-do change to take date from departure field and format it
  83.                 endTime: "2018-10-06T16:39:52.637Z",
  84.                 password: this.getCiscoUserPassword(),
  85.                 profile: 'HOG_GUEST',
  86.                 rebootController: false,
  87.                 saveConfigToFlash: true,
  88.                 userRole: 'default',
  89.                 username: this.getCiscoUserName()
  90.             }
  91.         });
  92.        
  93.         var req = new sn_ws.RESTMessageV2();
  94.         req.setEndpoint(createEndpoint);
  95.         req.setAuthenticationProfile(this._ciscoAuthProfile.type, this._ciscoAuthProfile.profileId);
  96.         req.setHttpMethod(httpMethod);
  97.         req.setHttpTimeout(httpTimeout);
  98.         req.setMIDServer(midServer);
  99.         for(var header in headers) {
  100.             req.setRequestHeader(header, headers[header]);
  101.         }
  102.         req.setRequestBody(reqBody);
  103.        
  104.         var res = async == true ? req.executeAsync() : req.execute();
  105.        
  106.         RESTLogger.logOutbound(req, res, {
  107.             midServer: midServer,
  108.             httpMethod: httpMethod,
  109.             httpTimeout: httpTimeout,
  110.             async: async,
  111.             authType: this._ciscoAuthProfile.type,
  112.             profileId: this._ciscoAuthProfile.profileId
  113.         });
  114.        
  115.         return !res.haveError();
  116.     },
  117.    
  118.     type: 'CiscoGuestUser'
  119. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement