CreateNewPane.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. "use strict";
  2. /**
  3.  * Licensed Materials - Property of IBM
  4.  * IBM Cognos Products: Cognos Analytics
  5.  * Copyright IBM Corp. 2015, 2018
  6.  * US Government Users Restricted Rights -
  7. * Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  8.  */
  9. 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) {
  10. //NOSONAR: needed for amd
  11. 'use strict'; //NOSONAR: meant to be strict
  12. var CreateNewPane = BasePane.extend({
  13. /**
  14. * @param options.$el - JQuery node to append the table HTML
  15. * @param options.items {object} - An array of tab metadata. Must provide name, module, and objectInfo (containing all the basic properties of the object)
  16. *
  17. * @constructor
  18. */
  19. init: function init(options) {
  20. CreateNewPane.inherited('init', this, arguments);
  21. _.extend(this, options);
  22. this.autoValidate = true;
  23. this._stringResource = StringResource;
  24. this.title = StringResource.get('addNewUser');
  25. },
  26. _validate: function _validate() {
  27. if (!this.validate()) {
  28. this.glassContext.appController.showErrorMessage(StringResource.get('validateErrorMsg'), StringResource.get('error'));
  29. return false;
  30. }
  31. var passwordString = this.$el.find("input[type=password]").val();
  32. var userNameString = this.$el.find("input[cog-data=userName]").val();
  33. var givenNameString = this.$el.find("input[id=givenName]").val();
  34. var givenSurnameString = this.$el.find("input[id=surname]").val();
  35. var passwordRegex = /^(?=.*?[a-z])(?=.*?[A-Z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$/;
  36. var isPasswordValidate = passwordRegex.test(passwordString);
  37. var isGivenNameValidate = givenNameString !== "";
  38. var isGivenSurnameValidate = givenSurnameString !== "";
  39. if (userNameString.length < 3) {
  40. this.glassContext.appController.showErrorMessage(StringResource.get('userNameInvalidErrorMsg'), StringResource.get('error'));
  41. return false;
  42. }
  43. if (!isPasswordValidate) {
  44. this.glassContext.appController.showErrorMessage(StringResource.get('passwordInvalidErrorMsg'), StringResource.get('error'));
  45. return false;
  46. }
  47. if (!isGivenNameValidate) {
  48. this.glassContext.appController.showErrorMessage(StringResource.get('givenNameInvalidErrorMsg'), StringResource.get('error'));
  49. return false;
  50. }
  51. if (!isGivenSurnameValidate) {
  52. this.glassContext.appController.showErrorMessage(StringResource.get('givenSurnameInvalidErrorMsg'), StringResource.get('error'));
  53. return false;
  54. }
  55. return true;
  56. },
  57. _createAccountObject: function _createAccountObject(objectInfo, pid) {
  58. var objURL = AJAXUtils.getPath('createUser', pid);
  59. var options = {
  60. dataType: 'json',
  61. type: 'POST',
  62. contentType: 'application/json; charset=utf-8',
  63. data: JSON.stringify(objectInfo),
  64. url: objURL,
  65. cache: false
  66. };
  67. return this.glassContext.services.ajax.ajax(options);
  68. },
  69. renderBody: function renderBody($body) {
  70. var self = this;
  71. var deferred = Q.defer();
  72. var sHtml = dot.template(this.template)(this.objectInfo);
  73. $body.append(sHtml);
  74. this._setTextDirection(this.$el.find("input[type=text]"));
  75. this._setTextDirection(this.$el.find("textarea"));
  76. this.$el.find('#btnCreate').on('primaryaction', function () {
  77. if (!self._validate(self.objectInfo)) {
  78. return;
  79. }
  80. self._assemblyObject(self.objectInfo);
  81. self._createObject(self.objectInfo, self.pid).done(function () {
  82. var deferred = Q.defer();
  83. deferred.resolve(self.objectInfo);
  84. self._refreshAccountListPane();
  85. var sText = StringResource.get('userToastCreateMsg', {
  86. 'name': self.objectInfo.defaultName
  87. });
  88. self.glassContext.appController.showToast(sText, {
  89. type: 'success'
  90. });
  91. }, function (err) {
  92. this.glassContext.appController.showErrorMessage(err.responseJSON.messages.join('\n'), StringResource.get('error'));
  93. }.bind(this));
  94. }.bind(this));
  95. this.$el.find('#btnCancel').on('primaryaction', function () {
  96. this.close();
  97. }.bind(this));
  98. deferred.resolve();
  99. return deferred.promise;
  100. },
  101. _refreshAccountListPane: function _refreshAccountListPane() {
  102. if (this.parentView.pagingEnabled) {
  103. AdminReact.PaginationHelper.reloadListViewinPagingContext(this.parentView.glassContext, this.parentView._accountExplorer, this.parentView.listView, this.parentView.listAdaptor, true);
  104. } else {
  105. this.parentView.listAdaptor.sortChanged = true;
  106. this.parentView.listView.reload(false);
  107. }
  108. this.close();
  109. },
  110. _assemblyObject: function _assemblyObject(objectInfo) {
  111. this.$el.find("[cog-data]").each(function () {
  112. var propName = $(this).attr("cog-data").trim();
  113. if ($(this).attr("type") !== "checkbox") {
  114. objectInfo[propName] = $(this).val();
  115. }
  116. });
  117. objectInfo.defaultName = objectInfo.givenName + ' ' + objectInfo.surname;
  118. objectInfo.password = btoa(objectInfo.password);
  119. },
  120. _createObject: function _createObject(objectInfo, pid) {
  121. var deferred = Q.defer();
  122. this._createAccountObject(objectInfo, pid).done(function (data) {
  123. deferred.resolve(data);
  124. }).fail(function (jqXHR, err) {
  125. deferred.reject(err);
  126. });
  127. return deferred.promise;
  128. },
  129. setFocus: function setFocus() {
  130. this.$el.find('input[type=text]').filter(':visible:first').focus();
  131. },
  132. _onBlur: function _onBlur() {}
  133. });
  134. return CreateNewPane;
  135. });