"use strict"; /** * Licensed Materials - Property of IBM * IBM Cognos Products: Cognos Analytics * Copyright IBM Corp. 2015, 2020 * US Government Users Restricted Rights - Use, duplication or disclosure * restricted by GSA ADP Schedule Contract with IBM Corp. */ define(['q', 'underscore', 'doT', 'bi/commons/utils/Utils', 'bi/admin/account/AccountExplorer', 'bi/admin/common/ui/MagicWand', 'bacontentnav/common/ui/Breadcrumbs', 'bi/commons/ui/AccessibleView', 'bi/admin/common/ui/listview/ListView', 'bi/admin/nls/StringResource', 'bacontentnav/common/ui/ContentGenericDialog', 'text!bi/admin/account/templates/SecurityObjectExplorerTemplate.html', 'text!bi/admin/account/templates/CreateNewGroupTemplate.html', 'text!bi/admin/account/templates/CreateNewUserTemplate.html', 'bi/admin/common/utils/AJAXUtils', 'bi/commons/utils/BidiUtil', 'bi/commons/utils/ContentFormatter', 'bi/commons/ui/properties/PropertyUIControl', 'bacontentnav/common/ui/contentbar_components/Button', 'react-dom', 'react', 'ba-react-admin/ba-react-admin.min', 'bi/admin/common/Uploader'], function (Q, _, dot, Utils, AccountExplorer, MagicWand, Breadcrumbs, View, ListView, StringResource, ContentGenericDialog, SecurityObjectExplorerTemplate, CreateNewGroupTemplate, CreateNewUserTemplate, AJAXUtils, BidiUtil, ContentFormatter, PropertyUIControl, Button, ReactDOM, React, AdminReact, Uploader) { 'use strict'; //NOSONAR: meant to be strict var SecurityObjectExplorerView = View.extend({ showBreadcrumbs: true, defaultTypes: ['namespaceFolder', 'group', 'role', 'account'], _breadCrumbs: null, _accountExplorer: null, listAdaptor: null, init: function init(options) { SecurityObjectExplorerView.inherited('init', this, arguments); this.glassContext = options.glassContext; if (!this._accountExplorer && options.accountExplorer) { this._accountExplorer = options.accountExplorer; } else { this._accountExplorer = this._getNewAccountExplorer({ glassContext: options.glassContext }); } this.listAdaptor = options.listAdaptor; this.parent = options.parent; this.includeToolbar = options.includeToolbar ? options.includeToolbar : false; this.hideSearch = options.hideSearch; this.filterPushed = false; this.hidePaging = options.hidePaging; this.pagingEnabled = false; this.filterStringValue = { 'group': true, 'account': true, 'role': true }; }, _getNewAccountExplorer: function _getNewAccountExplorer(options) { return new AccountExplorer(options); }, _getLdapStatus: function _getLdapStatus() { var options = { dataType: 'json', type: 'GET', data: {}, url: AJAXUtils.getAJAXURL("ldapStatus"), cache: false }; return this.glassContext.services.ajax.ajax(options); }, _showPaging: function _showPaging() { // this checks if we want to show paging in a component. For example we want to hide it in accounts copy/move view. return _.isUndefined(this.hidePaging) || !this.hidePaging; }, _populatePagingStatusAndValue: function _populatePagingStatusAndValue() { var pagingEnabled = this._checkIfPagingIsEnabled(); var pagingValue = this._getPagingValue(); return Promise.all([pagingEnabled, pagingValue]).then(function (responses) { this.pagingEnabled = responses[1] === 'true'; this.pagingValueSet = parseInt(responses[0]); }.bind(this)); }, _renderPagingWidget: function _renderPagingWidget() { if (this.pagingEnabled && this._showPaging()) { this._accountExplorer.pagingEnabled = this.pagingEnabled; this._accountExplorer.pagingValueSet = this.pagingValueSet; this._accountExplorer.searchPageSize = this.pagingValueSet; var pagingBar = React.createElement(AdminReact.Pagination, { 'glassContext': this.glassContext, 'StringResource': StringResource, 'accountExplorer': this._accountExplorer, 'listView': this.listView, 'defaultPagingValue': this.pagingValueSet, 'listAdaptor': this.listAdaptor }); this.pagerControl = ReactDOM.render(pagingBar, this.$el.find('.bi-admin-account-pagingBar')[0]); this._accountExplorer.pagerControl = this.pagerControl; this.$el.find('.bi-admin-account-pagingBar').hide(); $(this.listAdaptor).on("ldapBrowsingEvent", function (event, type) { this._updatePagingBar(type); }.bind(this)); } }, _checkIfPagingIsEnabled: function _checkIfPagingIsEnabled() { var endpoint = "v1/configuration/keys/Admin.namespaceChildrenPerPage"; var pagingSettingsPromise = this.glassContext.services.fetch.get(endpoint); return pagingSettingsPromise.then(function (response) { return response.data['Admin.namespaceChildrenPerPage']; }); }, _getPagingValue: function _getPagingValue() { var endpoint = "v1/configuration/keys/Admin.enableNamespaceChildrenPaging"; var pagingValuePromise = this.glassContext.services.fetch.get(endpoint); return pagingValuePromise.then(function (response) { return response.data['Admin.enableNamespaceChildrenPaging']; }); }, _renderTemplate: function _renderTemplate() { var paneInfo = { 'strings': { 'type': StringResource.get('type'), 'addNewUser': StringResource.get('addNewUser'), 'addNewGroup': StringResource.get('addNewGroup'), 'addNewRole': StringResource.get('addNewRole'), 'addNewFolder': StringResource.get('addNewFolder'), 'import': StringResource.get('import') }, showFilter: this.listAdaptor.shouldShowFilter() }; var sHtml = dot.template(SecurityObjectExplorerTemplate)(paneInfo); this.$el.html(sHtml); BidiUtil.initElementForBidi(this.$el.find(".bi-admin-input.bi-admin-input-search")[0]); this.myAccount = React.createElement(AdminReact.UserWidget, { 'parent': this, 'glassContext': this.glassContent, 'StringResource': StringResource, 'clickFunc': this._createObj.bind(this), 'importOpenIdFunc': this.importOpenId.bind(this), 'importFunc': this.importFunc.bind(this), 'label': 'First Label' }); ReactDOM.render(this.myAccount, this.$el.find('.bi-admin-account-createNewBar')[0]); //init to hide create bar before get ldap status and checking permissions this.$el.find('.bi-admin-account-createNewBar').hide(); this.$el.find('.multiUploadText').text(StringResource.get('importText')); }, importFunc: function importFunc(selectedOption) { if (selectedOption === 'importUsers') { //import users this.importOpenId('users'); } else if (selectedOption === 'importGroups') { //import groups this.importOpenId('groups'); } }, _updateOrUploadUsers: function _updateOrUploadUsers(options) { var pid = this._accountExplorer.getCurrentPath().id; var requestOptions = { 'headers': { 'Content-Type': 'text/csv' }, 'url': 'v1/users?pid=' + pid, 'type': 'POST', 'data': options.data, 'Content-Length': options.byteLength, 'processData': false }; return this.glassContext.services.ajax.ajax(requestOptions); }, _updateOrUploadGroups: function _updateOrUploadGroups(options) { var pid = this._accountExplorer.getCurrentPath().id; var requestOptions = { 'headers': { 'Content-Type': 'text/csv' }, 'url': 'v1/groups?pid=' + pid, 'type': 'POST', 'data': options.data, 'Content-Length': options.byteLength, 'processData': false }; return this.glassContext.services.ajax.ajax(requestOptions); }, _createUploader: function _createUploader(type) { if (type === 'users') { return new Uploader({ 'glassContext': this.glassContext, '$el': this.$el, 'fileType': 'csv', 'ajax': this._updateOrUploadUsers.bind(this), 'ajaxOptions': {} }); } else if (type === 'groups') { return new Uploader({ 'glassContext': this.glassContext, '$el': this.$el, 'fileType': 'csv', 'ajax': this._updateOrUploadGroups.bind(this), 'ajaxOptions': {} }); } }, _refreshAccountListPane: function _refreshAccountListPane(isCreatingObject) { if (this.pagingEnabled) { this.listAdaptor.sortChanged = false; AdminReact.PaginationHelper.reloadListViewinPagingContext(this.glassContext, this._accountExplorer, this.listView, this.listAdaptor, isCreatingObject); } else { this.listAdaptor.sortChanged = true; this.listView.reload(false); } }, importOpenId: function importOpenId(type) { var fileUploader = this._createUploader(type); return fileUploader.doUpload().then(function () { this._refreshAccountListPane(true); }.bind(this), function (err) { this.glassContext.appController.showErrorMessage(StringResource.get('OIDCErrorSuggestion', { serverError: err.responseJSON.messages }), StringResource.get('error')); }.bind(this)); }, changeBrowseSearch: function changeBrowseSearch(isBrowsing) { var elList = this.$el.find('.bi-admin-account-list'); if (isBrowsing) { this._accountExplorer.newSearch(); this.listView.setPaging(true); elList.addClass("security-browsing"); } else { this.listView.setPaging(true); elList.removeClass("security-browsing"); } }, _renderSearch: function _renderSearch() { var rSearch = this.$el.find(".bi-admin-filterbar-leading"); var searchComp = React.createElement(AdminReact.AdminSearchInput, { 'parent': this, StringResource: StringResource, placeholder: StringResource.get('filterTips'), onSearch: function (evt, newValue, searchType) { var isBrowsing = newValue === ""; this.listView.setPaging(false); this.listAdaptor.searchText = encodeURIComponent(newValue.trim()); this.listAdaptor.searchType = searchType; this.listAdaptor.isSearching = true; this._accountExplorer.clearGroupedContent(); this.listView._clearTable(); this.listView.showWorking(); if (!isBrowsing) { this.listView.reload(false).then(function () { this.changeBrowseSearch(isBrowsing); if (this.pagingEnabled && this.pagerControl) { this.pagerControl.store.updatePager(this._accountExplorer, this.listView, this.listAdaptor); } }.bind(this)); } else { this._accountExplorer.selectCurrentPath().then(function () { this.listView.reload(false).then(function () { this.changeBrowseSearch(isBrowsing); this._clearSearch(); }.bind(this)); }.bind(this)); } }.bind(this), ref: function (searchInput) { this._searchInput = searchInput; }.bind(this), onClear: function (e) { var isBrowsing = true; this.listView.setPaging(false); this.listAdaptor.searchText = ""; this._accountExplorer.clearGroupedContent(); this._accountExplorer.selectCurrentPath().then(function () { this.listView.reload(false).then(function () { this.changeBrowseSearch(isBrowsing); }.bind(this)); if (this.pagingEnabled && this.pagerControl) { this._clearSearch(); } }.bind(this)); }.bind(this) }); ReactDOM.render(searchComp, rSearch[0]); $(this.listAdaptor).on("ldapBrowsingEvent", function (event, type) { this._updateFilterBar(type); }.bind(this)); }, _clearSearch: function _clearSearch() { this.pagerControl.store.updatePager(this._accountExplorer, this.listView, this.listAdaptor); this.listAdaptor.isSearching = undefined; }, _initializeAccountExplorer: function _initializeAccountExplorer() { var deferred = Q.defer(); this._accountExplorer._defaultTypes = this.defaultTypes; this._accountExplorer.startUp().done(function () { if (this.listAdaptor.cognosNamespaceOnly) { this._accountExplorer.selectNamespace("Cognos").done(function () { this._updateAccountExplorerInit().done(function () { this.filterStringValue.account = false; this.filterStringValue.role = true; if (this.listAdaptor.allowedSelectionTypes === "role") { this.filterStringValue.group = false; } else { this.filterStringValue.group = true; } deferred.resolve(); }.bind(this)); }.bind(this)); } else { this._updateAccountExplorerInit().done(function () { this.filterStringValue.account = true; this.filterStringValue.role = true; this.filterStringValue.group = true; deferred.resolve(); }.bind(this)); } }.bind(this)); return deferred.promise; }, _updateAccountExplorerInit: function _updateAccountExplorerInit() { var deferred = Q.defer(); this._getLdapStatus().done(function (result) { this._ldapStatus = !!result['ads-state']; this._configToolBar(); deferred.resolve(); }.bind(this)); this.listAdaptor._accountExplorer = this._accountExplorer; this.listAdaptor.on('pathChanged', function (path) { this._updateBreadcrumb(path); this.listAdaptor.off('locationChanged'); this.listAdaptor.on("locationChanged", function (obj) { this._navigate(obj.data); }.bind(this)); }.bind(this)); return deferred.promise; }, _renderListView: function _renderListView() { var deferred = Q.defer(); var elList = this.$el.find('.bi-admin-account-list'); elList.addClass("security-browsing"); elList.empty(); this.listView = this._getNewListView({ el: elList, dataAdaptor: this.listAdaptor, glassContext: this.glassContext, accessibleLabel: this.title, activeInputForm: null, multiSelect: true, onSelectionChange: this.onSelectionChange }); this.listView.singleSelectCallback = this._rowClickHandler.bind(this); if (this.listAdaptor.contextMenuHandler) { this.listView.contextMenuCallback = this.listAdaptor.contextMenuHandler.bind(this.listAdaptor, this); } this.listView.render().then(function () { if (!this.pagingEnabled) { this._bindListViewEvents(); } }.bind(this)).then(deferred.resolve, deferred.reject); return deferred.promise; }, refresh: function refresh() { this._refreshAccountListPane(); }, _getNewListView: function _getNewListView(options) { return new ListView(options); }, _bindListViewEvents: function _bindListViewEvents() { $(this.listView).on("previousPage", function () { if (!this._accountExplorer.isTopOfPage()) { this.listView.setPaging(false); this._accountExplorer.cacheOldContent(); this._accountExplorer.clearGroupedContent(); this._accountExplorer.selectCurrentPath().then(function () { this.listAdaptor.isGetLessData = true; this.listView.reload(false).then(function () { this.changeBrowseSearch(false); this.listAdaptor.isGetLessData = false; this.listView.setPaging(true); }.bind(this)); }.bind(this)); } }.bind(this)); $(this.listView).on("nextPage", function () { if (!this.scrollCausedByRowClick && !this._accountExplorer.noMoreData) { this.listView.setPaging(false); this._accountExplorer.cacheOldContent(); this._accountExplorer.clearGroupedContent(); this._accountExplorer.selectCurrentPath().then(function () { this.listAdaptor.isGetMoreData = true; this.listView.reload(false).then(function () { this.changeBrowseSearch(false); this.listAdaptor.isGetMoreData = false; this.listView.adjustScrollForPageDown(); this.listView.setPaging(true); }.bind(this)); }.bind(this)); } else { delete this.scrollCausedByRowClick; } }.bind(this)); }, clearShowWorking: function clearShowWorking() { this.$el.find(".adminAccountsShowWorking").remove(); }, showWorking: function showWorking() { var loadingAnimation = Utils.getLoadingAnimation(1); var $container = $('
'); $container.append(loadingAnimation); this.$el.append($container); }, render: function render() { var deferred = Q.defer(); this._renderTemplate(); this.showWorking(); var accountsPagingStatus = this._populatePagingStatusAndValue().catch(function (err) { this.pagingEnabled = false; this.logger.error(err); }.bind(this)); this._renderSearch(); this._initializeAccountExplorer().then(function () { return accountsPagingStatus; }).then(this._renderListView.bind(this)).then(function () { this._bindEvents(); this._renderPagingWidget(); this.clearShowWorking(); }.bind(this)).then(deferred.resolve, deferred.reject); return deferred.promise; }, _bindFilter: function _bindFilter() { if (!this.filterPushed) { this.filterPushed = true; this.glassContext.appController.showSlideOut({ 'parent': this.parent.slideout, 'position': 'left', 'width': '200px', 'content': { 'parentView': this, 'glassContext': this.glassCtxt, 'showSwitchButton': true, 'module': 'bi/admin/account/slideout/UserGroupSelectorPane', 'view': this, 'callback': this } }).on('hide', function () { this.filterPushed = false; }.bind(this)); } }, getSelectedObjects: function getSelectedObjects() { return this.listView.getSelectedObjects(); }, _getFilterBar: function _getFilterBar() { return this.$el.find(".bi-admin-filterbar.folderpane-filterbar"); }, _getPaginBar: function _getPaginBar() { return this.$el.find(".bi-admin-account-pagingBar"); }, _updateFilterBar: function _updateFilterBar(type) { var filterBar = this._getFilterBar(); this._hideBar(filterBar, type); }, _updatePagingBar: function _updatePagingBar(type) { var pagingBar = this._getPaginBar(); this._hideBar(pagingBar, type); }, _hideBar: function _hideBar(bar, type) { if (type === "directory") { bar.css("display", "none"); } else { bar.css("display", "block"); } }, _bindEvents: function _bindEvents() { this.$el.find('.multiUploadBtn').on('primaryaction', function () {}.bind(this)); var $filterButton = this.$el.find('#filterDef').on('primaryaction', function () { this._bindFilter(); }.bind(this)); var $label = $('