"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(['doT', 'q', 'jquery', 'underscore', 'bi/commons/utils/BidiUtil', 'bi/admin/nls/StringResource', 'bi/glass/app/ContentView', 'bacontentnav/utils/WidgetNavigator', 'bi/commons/ui/dialogs/ConfirmationDialog', 'text!bi/admin/account/templates/MembersTabTemplate.html', 'bi/admin/common/utils/AJAXUtils', 'bi/commons/utils/ContentFormatter'], function (dot, Q, $, _, BidiUtil, StringResource, ContentView, WidgetNavigator, ConfirmationDialog, template, AJAXUtils, ContentFormatter) { var MembersTab = ContentView.extend({ init: function init(options) { MembersTab.inherited('init', this, arguments); this.unavailableMembersExist = false; $.extend(this, options); }, render: function render() { var strings = { 'addMember': StringResource.get('addMember'), 'removeMember': StringResource.get('removeMember'), 'removeMemberButton': StringResource.get('removeMemberButton'), 'tableCaption': StringResource.get('membersTableCaption'), 'tableheader': { 'type': StringResource.get('type'), 'name': StringResource.get('name'), 'action': StringResource.get('removeActionColumn') }, 'group': StringResource.get('groupIconName'), 'account': StringResource.get('accountIconName'), 'import': StringResource.get('import'), 'select': StringResource.get('select'), 'addBulkMembers': StringResource.get('import'), 'unavailableMembersExist': StringResource.get('unavailableMembersExist') }; // check write permission var writable = this.objectInfo.permissions && this.objectInfo.permissions.indexOf('write') > -1 ? true : false; if (!this.glassContext.isDevInstall()) { //external namespace has no write permission var currentNamespace = this.accountExplorer.getCurrentNamespace(); if (currentNamespace.defaultName !== "Cognos") { writable = false; } } return this._getMembers().then(function (members) { var htmlStr; if (members === 'everyone' || members === 'allAuthUsers') { htmlStr = this._getBlankHtml(members); } else { members = _.map(members, function (member) { return this._patchItem(member); }.bind(this)); var sText = StringResource.get('noMember', { 'type': this.objectInfo.type }); var tempObj = { 'members': members, 'writable': writable, 'strings': strings, 'unavailableMembersExist': this.unavailableMembersExist, noMemberText: sText }; htmlStr = dot.template(template)(tempObj); } this.$el.append(htmlStr); $.each(this.$el.find('.groupListFocusable .text-title'), function (index, value) { ContentFormatter.middleShortenString(value); }); this.widgetKeyController = new WidgetNavigator({ $el: this.$el.find(".groupList-table"), focusClass: "groupListFocusable" }); this._bindEvents(); this._enforceTextDir(); }.bind(this)); }, refresh: function refresh() { this.$el.empty(); this.render(); }, _enforceTextDir: function _enforceTextDir() { var $spans = this.$el.find(".text-title"); $spans.attr('dir', BidiUtil.resolveBaseTextDir(this.innerHTML)); }, _removeMembers: function _removeMembers(members) { var objId = this.objectInfo.id; var objType = this.objectInfo.type; var membersURL; switch (objType) { case 'role': membersURL = AJAXUtils.getPath('getRoleMembers', objId); break; case 'group': membersURL = AJAXUtils.getPath('getGroupMembers', objId); break; default: membersURL = ""; break; } var options = { dataType: 'json', type: 'PUT', data: JSON.stringify(members), url: membersURL, cache: false }; return this.glassContext.services.ajax.ajax(options); }, _getMemberList: function _getMemberList() { var objId = this.objectInfo.id; var objType = this.objectInfo.type; if (objType !== 'group' && objType !== 'role') { this.logger.error("Can't get member list for object type: " + this.objectInfo.type); var resp = $.Deferred(); resp.reject(resp, "Can't get member list for object type: " + this.objectInfo.type); return resp.promise(); } var membersURL = 'v1/' + objType + "s/" + objId + '/members'; var options = { dataType: 'json', type: 'GET', data: {}, url: membersURL, cache: false }; return this.glassContext.services.ajax.ajax(options); }, _getMembers: function _getMembers() { var deferred = Q.defer(); switch (this.objectInfo.type) { case 'group': var isEveryoneGroup = this.objectInfo.searchPath.indexOf("CAMID(\"::Everyone") >= 0; var isAllAuthUserGroup = this.objectInfo.searchPath.indexOf("CAMID(\"::All Authenticated Users") >= 0; if (isEveryoneGroup) { deferred.resolve('everyone'); } else if (isAllAuthUserGroup) { deferred.resolve('allAuthUsers'); } else { this._getMemberList().then(function (data) { this._checkifUnavailableMembersExist(data); var members = [].concat(data.groups, data.users); deferred.resolve(members); }.bind(this), function (dfd, err) { deferred.reject(err); }); } break; case 'role': this._getMemberList().then(function (data) { this._checkifUnavailableMembersExist(data); var members = [].concat(data.groups, data.users, data.roles); deferred.resolve(members); }.bind(this), function (dfd, err) { deferred.reject(err); }); break; default: deferred.resolve(); break; } return deferred.promise; }, _checkifUnavailableMembersExist: function _checkifUnavailableMembersExist(data) { if (data.unavailableMembersExist && !this.unavailableMembersExist) { this.unavailableMembersExist = data.unavailableMembersExist; } }, _getBlankHtml: function _getBlankHtml(members) { return '