"use strict"; /**  * Licensed Materials - Property of IBM  * IBM Cognos Products: Cognos Analytics  * Copyright IBM Corp. 2015, 2018  * US Government Users Restricted Rights - * Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.  */ define(['q', 'jquery', 'doT', 'underscore', 'bi/commons/utils/BidiUtil', 'bi/admin/common/slideout/BasePane', 'bi/admin/nls/StringResource', 'bi/admin/common/utils/AJAXUtils', 'ba-react-admin/ba-react-admin.min'], function (Q, $, dot, _, BidiUtil, BasePane, StringResource, AJAXUtils, AdminReact) { //NOSONAR: needed for amd 'use strict'; //NOSONAR: meant to be strict var CreateNewPane = BasePane.extend({ /** * @param options.$el - JQuery node to append the table HTML * @param options.items {object} - An array of tab metadata. Must provide name, module, and objectInfo (containing all the basic properties of the object) * * @constructor */ init: function init(options) { CreateNewPane.inherited('init', this, arguments); _.extend(this, options); this.autoValidate = true; this._stringResource = StringResource; this.title = StringResource.get('addNewUser'); }, _validate: function _validate() { if (!this.validate()) { this.glassContext.appController.showErrorMessage(StringResource.get('validateErrorMsg'), StringResource.get('error')); return false; } var passwordString = this.$el.find("input[type=password]").val(); var userNameString = this.$el.find("input[cog-data=userName]").val(); var givenNameString = this.$el.find("input[id=givenName]").val(); var givenSurnameString = this.$el.find("input[id=surname]").val(); var passwordRegex = /^(?=.*?[a-z])(?=.*?[A-Z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$/; var isPasswordValidate = passwordRegex.test(passwordString); var isGivenNameValidate = givenNameString !== ""; var isGivenSurnameValidate = givenSurnameString !== ""; if (userNameString.length < 3) { this.glassContext.appController.showErrorMessage(StringResource.get('userNameInvalidErrorMsg'), StringResource.get('error')); return false; } if (!isPasswordValidate) { this.glassContext.appController.showErrorMessage(StringResource.get('passwordInvalidErrorMsg'), StringResource.get('error')); return false; } if (!isGivenNameValidate) { this.glassContext.appController.showErrorMessage(StringResource.get('givenNameInvalidErrorMsg'), StringResource.get('error')); return false; } if (!isGivenSurnameValidate) { this.glassContext.appController.showErrorMessage(StringResource.get('givenSurnameInvalidErrorMsg'), StringResource.get('error')); return false; } return true; }, _createAccountObject: function _createAccountObject(objectInfo, pid) { var objURL = AJAXUtils.getPath('createUser', pid); var options = { dataType: 'json', type: 'POST', contentType: 'application/json; charset=utf-8', data: JSON.stringify(objectInfo), url: objURL, cache: false }; return this.glassContext.services.ajax.ajax(options); }, renderBody: function renderBody($body) { var self = this; var deferred = Q.defer(); var sHtml = dot.template(this.template)(this.objectInfo); $body.append(sHtml); this._setTextDirection(this.$el.find("input[type=text]")); this._setTextDirection(this.$el.find("textarea")); this.$el.find('#btnCreate').on('primaryaction', function () { if (!self._validate(self.objectInfo)) { return; } self._assemblyObject(self.objectInfo); self._createObject(self.objectInfo, self.pid).done(function () { var deferred = Q.defer(); deferred.resolve(self.objectInfo); self._refreshAccountListPane(); var sText = StringResource.get('userToastCreateMsg', { 'name': self.objectInfo.defaultName }); self.glassContext.appController.showToast(sText, { type: 'success' }); }, function (err) { this.glassContext.appController.showErrorMessage(err.responseJSON.messages.join('\n'), StringResource.get('error')); }.bind(this)); }.bind(this)); this.$el.find('#btnCancel').on('primaryaction', function () { this.close(); }.bind(this)); deferred.resolve(); return deferred.promise; }, _refreshAccountListPane: function _refreshAccountListPane() { if (this.parentView.pagingEnabled) { AdminReact.PaginationHelper.reloadListViewinPagingContext(this.parentView.glassContext, this.parentView._accountExplorer, this.parentView.listView, this.parentView.listAdaptor, true); } else { this.parentView.listAdaptor.sortChanged = true; this.parentView.listView.reload(false); } this.close(); }, _assemblyObject: function _assemblyObject(objectInfo) { this.$el.find("[cog-data]").each(function () { var propName = $(this).attr("cog-data").trim(); if ($(this).attr("type") !== "checkbox") { objectInfo[propName] = $(this).val(); } }); objectInfo.defaultName = objectInfo.givenName + ' ' + objectInfo.surname; objectInfo.password = btoa(objectInfo.password); }, _createObject: function _createObject(objectInfo, pid) { var deferred = Q.defer(); this._createAccountObject(objectInfo, pid).done(function (data) { deferred.resolve(data); }).fail(function (jqXHR, err) { deferred.reject(err); }); return deferred.promise; }, setFocus: function setFocus() { this.$el.find('input[type=text]').filter(':visible:first').focus(); }, _onBlur: function _onBlur() {} }); return CreateNewPane; });