Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var CiscoGuestUser = Class.create();
- CiscoGuestUser.CISCO_ADDRESS = gs.getProperty('hoffmann.cisco.endpoint') || 'https://demucsrnmpi01.corp.hoffmann-group.com';
- CiscoGuestUser.MID_SERVER = gs.getProperty('hoffmann.cisco.midserver') || '';
- CiscoGuestUser.CONTROLLER = gs.getProperty('cisco.guest.users.controller') || 607674067;
- CiscoGuestUser.CISCO_AUTH_DATA = (function() {
- var profileName = gs.getProperty('hoffmann.cisco.basic.profile.name') || 'Cisco';
- var profileRecord = new GlideRecord('sys_auth_profile_basic');
- var profileId = profileRecord.get('name', profileName) ? profileRecord.sys_id + '' : 'b7171752db3a93c0197bb14ffe9619f3';
- return {
- type: 'basic',
- profileId: profileId
- };
- })();
- CiscoGuestUser.getGuestUserNames = function(/*integer (optional)*/ controllerId) {
- var controller = controllerId || CiscoGuestUser.CONTROLLER;
- var endpoint = CiscoGuestUser.CISCO_ADDRESS + '/webacs/api/v3/op/guestUser/guestUsersOnController?controllerId=' + controller,
- midServer = CiscoGuestUser.MID_SERVER,
- httpTimeout = 60,
- httpMethod = 'get',
- async = false;
- var req = new sn_ws.RESTMessageV2();
- req.setEndpoint(endpoint);
- req.setRequestHeader('Accept', 'application/json');
- req.setMIDServer(midServer);
- req.setAuthenticationProfile(CiscoGuestUser.CISCO_AUTH_DATA.type, CiscoGuestUser.CISCO_AUTH_DATA.profileId);
- req.setHttpTimeout(httpTimeout);
- req.setHttpMethod(httpMethod);
- var res = async == true ? req.executeAsync() : req.execute();
- RESTLogger.logOutbound(req, res, {
- httpMethod: httpMethod,
- httpTimeout: httpTimeout,
- authType: CiscoGuestUser.CISCO_AUTH_DATA.type,
- profileId: CiscoGuestUser.CISCO_AUTH_DATA.profileId,
- async: async,
- midServer: midServer
- });
- if(res.haveError()) {
- return null;
- }
- var usersArr = JSON.parse(res.getBody()).mgmtResponse.guestUserNamesDTO[0].usernames.username;
- //convert the array to a map so we can check whether user exists without iterating the elements
- var result = {};
- usersArr.forEach(function(el) {
- result[el] = el;
- });
- return result;
- };
- CiscoGuestUser.prototype = {
- initialize: function(/*GlideRecord*/ guestRecord) {
- if(guestRecord) {
- this.setFromRecord(guestRecord);
- }
- },
- setFromRecord: function(/*GlideRecord*/ guestRecord) {
- this._guestName = guestRecord.u_guest + '';
- this._guestCompanyName = guestRecord.u_company + '';
- this._ciscoUserName = this._generateUserName();
- this._ciscoUserPassword = this._generateRandomPassword();
- this._departureDate = this._formatDateTime(guestRecord.u_date_of_departure.getDisplayValue());
- this._sysId = guestRecord.sys_id + '';
- },
- _formatDateTime: function(dateTime) {
- //converts dd.mm.yyyy hh:mm:ss to yyyy-mm-ddThh:mm:ss+00:00
- return dateTime ? dateTime.split(' ').map(function(el, index) {
- if(index == 0)
- return (el + '').split('.').reverse().join('-') + 'T';
- else
- return el + '.637Z';
- }).join('') :
- '';
- },
- _generateRandomPassword: function() {
- var passwordChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
- randomPass = '';
- for(var i = 0; i < 8; i++) {
- randomPass += passwordChars.charAt(Math.floor(Math.random() * passwordChars.length));
- }
- return randomPass;
- },
- _generateUserName: function() {
- //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)
- return ('HOM-' + this.getGuestName().trim().split(/\s+/).map(function(el, index) {
- if(index == 0)
- return el.split('')[0].toUpperCase();
- else if(index == 1)
- return '.' + el;
- else return '';
- }).join('') + '-' + this.getCompanyName()).split('').slice(0, 27).join('');
- },
- getGuestName: function() {
- return this._guestName;
- },
- getCompanyName: function() {
- return this._guestCompanyName;
- },
- getCiscoUserName: function() {
- return this._ciscoUserName;
- },
- getRecordSysId: function() {
- return this._sysId;
- },
- setCiscoUserName: function(/*string*/ userName) {
- this._ciscoUserName = userName.length <= 28 ? userName : userName.split('').slice(0, 27).join('');
- },
- getCiscoUserPassword: function() {
- return this._ciscoUserPassword;
- },
- setCiscoUserPassword: function(/*string*/ password) {
- this._ciscoUserPassword = password;
- },
- userExistsIn: function(/*object (map)*/ usernames) {
- return usernames ? usernames[this.getCiscoUserName()] != undefined : CiscoGuestUser.getGuestUserNames()[this.getCiscoUserName()] != undefined;
- },
- getDepartureDate: function() {
- return this._departureDate;
- },
- getGuestInfo: function() {
- return {
- guestName: this.getGuestName(),
- companyName: this.getCompanyName(),
- ciscoUserName: this.getCiscoUserName(),
- sys_id: this.getRecordSysId()
- };
- },
- getGuestInfoAsString: function() {
- return 'Guest name: ' + this.getGuestName() + '\n' +
- 'Guest company name: ' + this.getCompanyName() + '\n' +
- 'Cisco user name: ' + this.getCiscoUserName() + '\n' +
- 'Guest registration history record sys_id: ' + this.getRecordSysId();
- },
- _sendUserData: function(httpMethod) {
- //the API for creating/updating users is the same endpoint and accepts the same data, difference is only the httpMethod used
- var headers = {
- 'Content-Type': 'application/json',
- 'Accept': 'application/json'
- },
- endpoint = CiscoGuestUser.CISCO_ADDRESS + '/webacs/api/v3/op/guestUser',
- midServer = CiscoGuestUser.MID_SERVER,
- httpTimeout = 60,
- async = false,
- controller = CiscoGuestUser.CONTROLLER,
- reqBody = JSON.stringify({
- manageGuestUsersDTO: {
- applyGuestUserTo: 'CONTROLLER_LIST',
- controllerIds: {
- controllerId: [controller]
- },
- description: 'Guest User Arriving Needing WLAN access',
- disclaimer: 'Guest User WLAN access',
- endTime: this.getDepartureDate(),
- password: this.getCiscoUserPassword(),
- profile: 'HOG_GUEST',
- rebootController: false,
- saveConfigToFlash: true,
- userRole: 'default',
- username: this.getCiscoUserName()
- }
- });
- var req = new sn_ws.RESTMessageV2();
- req.setEndpoint(endpoint);
- req.setAuthenticationProfile(CiscoGuestUser.CISCO_AUTH_DATA.type, CiscoGuestUser.CISCO_AUTH_DATA.profileId);
- req.setHttpMethod(httpMethod);
- req.setHttpTimeout(httpTimeout);
- req.setMIDServer(midServer);
- for(var header in headers) {
- req.setRequestHeader(header, headers[header]);
- }
- req.setRequestBody(reqBody);
- var res = async == true ? req.executeAsync() : req.execute();
- RESTLogger.logOutbound(req, res, {
- midServer: midServer,
- httpMethod: httpMethod,
- httpTimeout: httpTimeout,
- async: async,
- authType: CiscoGuestUser.CISCO_AUTH_DATA.type,
- profileId: CiscoGuestUser.CISCO_AUTH_DATA.profileId
- });
- return !res.haveError();
- },
- createUser: function() {
- return this._sendUserData('post');
- },
- updateUser: function() {
- return this._sendUserData('put');
- },
- type: 'CiscoGuestUser'
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement