This is an automated email from the ASF dual-hosted git repository. pradeep pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/ranger.git
commit 617d8dd74d1479829b7af9754b5b714059788d8e Author: Nitin Galave <[email protected]> AuthorDate: Mon Jun 7 18:47:51 2021 +0530 RANGER-3306: Allow comma in policy resource text field. Signed-off-by: pradeep <[email protected]> --- .../src/main/webapp/libs/bower/tag-it/js/tag-it.js | 4 +- security-admin/src/main/webapp/scripts/Init.js | 2 +- .../src/main/webapp/scripts/modules/XAOverrides.js | 48 ++++++++++----- .../src/main/webapp/scripts/utils/XAUtils.js | 10 ++-- .../scripts/views/policies/RangerPolicyForm.js | 69 ++++++++++------------ .../views/security_zone/ZoneResourceForm.js | 41 +++++-------- .../scripts/views/security_zone/zoneResource.js | 8 ++- .../scripts/views/service/AuditFilterConfig.js | 9 ++- .../views/service/ServiceAuditFilterResources.js | 49 +++++---------- security-admin/src/main/webapp/styles/xa.css | 4 ++ 10 files changed, 116 insertions(+), 128 deletions(-) diff --git a/security-admin/src/main/webapp/libs/bower/tag-it/js/tag-it.js b/security-admin/src/main/webapp/libs/bower/tag-it/js/tag-it.js index 04e36b1..ab36e1a 100644 --- a/security-admin/src/main/webapp/libs/bower/tag-it/js/tag-it.js +++ b/security-admin/src/main/webapp/libs/bower/tag-it/js/tag-it.js @@ -241,7 +241,6 @@ // Tab will also create a tag, unless the tag input is empty, // in which case it isn't caught. if ( - (event.which === $.ui.keyCode.COMMA && event.shiftKey === false) || event.which === $.ui.keyCode.ENTER || ( event.which == $.ui.keyCode.TAB && @@ -297,7 +296,7 @@ this.tagInput.autocomplete(autocompleteOptions).bind('autocompleteopen.tagit', function(event, ui) { that.tagInput.data('autocomplete-open', true); }).bind('autocompleteclose.tagit', function(event, ui) { - that.tagInput.data('autocomplete-open', false) + that.tagInput.data('autocomplete-open', false); }); this.tagInput.autocomplete('widget').addClass('tagit-autocomplete'); @@ -588,4 +587,3 @@ }); })(jQuery); - diff --git a/security-admin/src/main/webapp/scripts/Init.js b/security-admin/src/main/webapp/scripts/Init.js index aa970be..e155fa3 100644 --- a/security-admin/src/main/webapp/scripts/Init.js +++ b/security-admin/src/main/webapp/scripts/Init.js @@ -132,7 +132,7 @@ require.config({ 'bootstrap-notify': '../libs/bower/bootstrap-notify/js/bootstrap-notify', 'jquery.cookie': '../libs/other/jquery-cookie/js/jquery.cookie', 'jquery-toggles': '../libs/bower/jquery-toggles/js/toggles.min', - 'tag-it': '../libs/bower/tag-it/js/tag-it.min', + 'tag-it': '../libs/bower/tag-it/js/tag-it', 'select2': '../libs/bower/select2/select2', 'bootbox': '../libs/bower/bootbox/js/bootbox', 'visualsearch': '../libs/other/visualsearch/js/visualsearch', diff --git a/security-admin/src/main/webapp/scripts/modules/XAOverrides.js b/security-admin/src/main/webapp/scripts/modules/XAOverrides.js index 1c4e457..9237abd 100644 --- a/security-admin/src/main/webapp/scripts/modules/XAOverrides.js +++ b/security-admin/src/main/webapp/scripts/modules/XAOverrides.js @@ -528,25 +528,33 @@ this.renderSameLevelResource(); return this; }, - renderResource : function(def) { - var that = this; - var Vent = require('modules/Vent'); - if(!_.isNull(this.value) && !_.isEmpty(this.value)){ - this.value.values = _.map(this.value.values, function(val){ return _.escape(val); }); - // Set initial value for resources. - if (_.isUndefined(def)) { - this.$resource.val(this.value.values.toString()); + renderResource : function(def) { + var that = this; + var Vent = require('modules/Vent'); + if(!_.isNull(this.value) && !_.isEmpty(this.value)){ + if (!_.isUndefined(this.key) && (this.key == "path" || this.key == "relativepath")) { + this.value.values = this.value.values.join('||'); + // Set initial value for resources. + if (_.isUndefined(def)) { + this.$resource.val((this.value.values)); + } + } else { + this.value.values = _.map(this.value.values, function(val){ return _.escape(val)}); + // Set initial value for resources. + if (_.isUndefined(def)) { + this.$resource.val(JSON.stringify(this.value.values)); + } } //to preserve resources values to text field if(!_.isUndefined(this.value.resourceType)){ - this.preserveResourceValues[this.value.resourceType] = this.value.values.toString(); + this.preserveResourceValues[this.value.resourceType] = this.value.values; }else{ - this.preserveResourceValues[this.name] = this.value.values.toString(); + this.preserveResourceValues[this.name] = this.value.values; } } //check dirtyField for input this.$resource.on('change', function(e) { - if(_.isUndefined(that.resourceOpts.select2Opts)){ + if(_.isUndefined(that.resourceOpts.select2Opts)){ // that.checkDirtyFieldForSelect2($(e.currentTarget), that, this.value); } }); @@ -559,7 +567,11 @@ if(_.has(def, 'validationRegEx') && !_.isEmpty(def.validationRegEx)){ opts['regExpValidation'] = {'type': 'regexp', 'regexp':new RegExp(def.validationRegEx), 'message' : def.validationMessage}; } - opts['lookupURL'] = "service/plugins/services/lookupResource/"+this.form.rangerService.get('name'); + if(!_.isUndefined(this.form.serviceName)) { + opts['lookupURL'] = "service/plugins/services/lookupResource/"+this.form.serviceName; + } else { + opts['lookupURL'] = "service/plugins/services/lookupResource/"+this.form.rangerService.get('name'); + } opts['type'] = def.name; this.resourceOpts['select2Opts'] = that.form.getPlugginAttr(true, opts); }else{ @@ -636,8 +648,8 @@ } this.$resourceType.on('change', function(e,onChangeResources) { if(!_.isUndefined(that.preserveResourceValues[e.currentTarget.value])){ - var val = _.isEmpty(that.preserveResourceValues[e.currentTarget.value]) ? '' : that.preserveResourceValues[e.currentTarget.value].split(','); - that.$resource.select2('val', val) + var val = _.isEmpty(that.preserveResourceValues[e.currentTarget.value]) ? '' : that.preserveResourceValues[e.currentTarget.value]; + that.$resource.val(JSON.stringify(val)) }else{ that.$resource.select2('val', ""); } @@ -683,13 +695,17 @@ }); } }, - getValue: function() { + getValue: function(fieldName) { var that = this; //checkParent if(this.$el.parents('.form-group').hasClass('hideResource')){ return null; } - this.value['resource'] = this.$resource.val(); + if (that.key == "path" || that.key == "relativepath") { + this.value['resource'] = this.$resource.tagit("assignedTags"); + } else { + this.value['resource'] = this.$resource.select2('data'); + } //validation diff --git a/security-admin/src/main/webapp/scripts/utils/XAUtils.js b/security-admin/src/main/webapp/scripts/utils/XAUtils.js index b90f885..4266876 100644 --- a/security-admin/src/main/webapp/scripts/utils/XAUtils.js +++ b/security-admin/src/main/webapp/scripts/utils/XAUtils.js @@ -1515,7 +1515,7 @@ define(function(require) { closeOnSelect : true, width :'220px', allowClear: true, - tokenSeparators: ["," , " "], + tokenSeparators: [" "], minimumInputLength: 1, initSelection : function (element, callback) { var data = []; @@ -1525,9 +1525,11 @@ define(function(require) { return; } //this is form multi-select value - $(element.val().split(",")).each(function () { - data.push({id: this, text: this}); - }); + if(_.isArray(JSON.parse(element.val()))) { + $(JSON.parse(element.val())).each(function () { + data.push({id: this, text: this}); + }) + } callback(data); }, createSearchChoice: function(term, data) { diff --git a/security-admin/src/main/webapp/scripts/views/policies/RangerPolicyForm.js b/security-admin/src/main/webapp/scripts/views/policies/RangerPolicyForm.js index b52579a..4f08c75 100644 --- a/security-admin/src/main/webapp/scripts/views/policies/RangerPolicyForm.js +++ b/security-admin/src/main/webapp/scripts/views/policies/RangerPolicyForm.js @@ -571,7 +571,13 @@ define(function(require){ //single value support // if(! XAUtil.isSinglevValueInput(obj) ){ if(!_.isUndefined(tmpObj) && _.isObject(tmpObj) && !_.isEmpty(tmpObj.resource)){ - rPolicyResource.set('values',tmpObj.resource.split(',')); + rPolicyResource.set('values',_.map(tmpObj.resource, function(m) { + if(obj.type == "path") { + return m + } else { + return m.text + } + })); if(!_.isUndefined(tmpObj.isRecursive)){ rPolicyResource.set('isRecursive', tmpObj.isRecursive) } @@ -681,25 +687,19 @@ define(function(require){ if(!this.model.isNew() && !_.isUndefined(this.model.get('path'))){ defaultValue = this.model.get('path').values; } - function split( val ) { - return val.split( /,\s*/ ); - } - function extractLast( term ) { - return split( term ).pop(); - } - var tagitOpts = {} - if(!_.isUndefined(options.lookupURL) && options.lookupURL){ - tagitOpts["autocomplete"] = { - cache: false, + var tagitOpts = {} + if(!_.isUndefined(options.lookupURL) && options.lookupURL){ + tagitOpts["autocomplete"] = { + cache: false, source: function( request, response ) { var url = "service/plugins/services/lookupResource/"+that.rangerService.get('name'); var context ={ - 'userInput' : extractLast( request.term ), + 'userInput' : request.term, 'resourceName' : that.pathFieldName, 'resources' : {} }; - var val = that.fields[that.pathFieldName].editor.getValue(); - context.resources[that.pathFieldName] = _.isNull(val) || _.isEmpty(val) ? [] : val.resource.split(","); + var val = that.fields[that.pathFieldName].editor.getValue('pathField'); + context.resources[that.pathFieldName] = _.isNull(val) || _.isEmpty(val) ? [] : val.resource; var p = $.ajax({ url : url, type : "POST", @@ -724,9 +724,9 @@ define(function(require){ $(this).removeClass('working'); }, search: function() { - if(!_.isUndefined(this.value) && (/[ ,]+/).test(this.value)){ - var values = this.value.trim().split(/[ ,]+/); - if (values.length > 1) { + if(!_.isUndefined(this.value) && this.value.includes('||')) { + var values = this.value.trim().split('||'); + if (values.length > 1) { for (var i = 0; i < values.length; i++) { that.fields[that.pathFieldName].editor.$el.find('[data-js="resource"]').tagit("createTag", values[i]); } @@ -734,28 +734,24 @@ define(function(require){ } else { return val } - } - var term = extractLast( this.value ); - $(this).addClass('working'); - if ( term.length < 1 ) { - return false; - } - }, - } - } - tagitOpts['beforeTagAdded'] = function(event, ui) { + } + } + } + } + tagitOpts['beforeTagAdded'] = function(event, ui) { // do something special that.fields[that.pathFieldName].$el.removeClass('error'); that.fields[that.pathFieldName].$el.find('.help-inline').html(''); var tags = []; - console.log(ui.tag); if(!_.isUndefined(options.regExpValidation) && !options.regExpValidation.regexp.test(ui.tagLabel)){ that.fields[that.pathFieldName].$el.addClass('error'); that.fields[that.pathFieldName].$el.find('.help-inline').html(options.regExpValidation.message); return false; } } - this.fields[that.pathFieldName].editor.$el.find('[data-js="resource"]').tagit(tagitOpts).on('change', function(e){ + tagitOpts['singleFieldDelimiter'] = '||'; + tagitOpts['singleField'] = false; + this.fields[that.pathFieldName].editor.$el.find('input[data-js="resource"]').tagit(tagitOpts).on('change', function(e){ //check dirty field for tagit input type : `path` XAUtil.checkDirtyField($(e.currentTarget).val(), defaultValue.toString(), $(e.currentTarget)); }); @@ -772,18 +768,15 @@ define(function(require){ multiple: true, minimumInputLength: 1, width :'220px', - tokenSeparators: [",", " "], + tokenSeparators: [' '], initSelection : function (element, callback) { var data = []; //to set single select value - if(!_.isUndefined(options.singleValueInput) && options.singleValueInput){ - callback({ id : element.val(), text : element.val() }); - return; + if(_.isArray(JSON.parse(element.val()))) { + $(JSON.parse(element.val())).each(function () { + data.push({id: this, text: this}); + }) } - //this is form multi-select value - $(element.val().split(",")).each(function () { - data.push({id: this, text: this}); - }); callback(data); }, createSearchChoice: function(term, data) { @@ -879,7 +872,7 @@ define(function(require){ }else{ val = this.fields[name].getValue(); } - resources[name] = _.isNull(val) ? [] : val.resource.split(','); + resources[name] = _.isNull(val) ? [] : val.resource; if(!_.isEmpty(currentResource.parent)){ name = currentResource.parent; }else{ diff --git a/security-admin/src/main/webapp/scripts/views/security_zone/ZoneResourceForm.js b/security-admin/src/main/webapp/scripts/views/security_zone/ZoneResourceForm.js index f87e7a0..1725670 100644 --- a/security-admin/src/main/webapp/scripts/views/security_zone/ZoneResourceForm.js +++ b/security-admin/src/main/webapp/scripts/views/security_zone/ZoneResourceForm.js @@ -240,14 +240,6 @@ define(function(require) { if (!this.model.isNew() && !_.isUndefined(this.model.get('path'))) { defaultValue = this.model.get('path').values; } - - function split(val) { - return val.split(/,\s*/); - } - - function extractLast(term) { - return split(term).pop(); - } var tagitOpts = {} if (!_.isUndefined(options.lookupURL) && options.lookupURL) { tagitOpts["autocomplete"] = { @@ -255,12 +247,12 @@ define(function(require) { source: function(request, response) { var url = "service/plugins/services/lookupResource/" + that.rangerService.get('name'); var context = { - 'userInput': extractLast(request.term), + 'userInput': request.term, 'resourceName': that.pathFieldName, 'resources': {} }; - var val = that.fields[that.pathFieldName].editor.getValue(); - context.resources[that.pathFieldName] = _.isNull(val) || _.isEmpty(val) ? [] : val.resource.split(","); + var val = that.fields[that.pathFieldName].editor.getValue('pathField'); + context.resources[that.pathFieldName] = _.isNull(val) || _.isEmpty(val) ? [] : val.resource; var p = $.ajax({ url: url, type: "POST", @@ -286,8 +278,8 @@ define(function(require) { }, search: function() { $('.tagit-autocomplete').addClass('tagit-position'); - if (!_.isUndefined(this.value) && (/[ ,]+/).test(this.value)) { - var values = this.value.trim().split(/[ ,]+/); + if(!_.isUndefined(this.value) && this.value.includes('||')) { + var values = this.value.trim().split('||'); if (values.length > 1) { for (var i = 0; i < values.length; i++) { that.fields[that.pathFieldName].editor.$el.find('[data-js="resource"]').tagit("createTag", values[i]); @@ -297,11 +289,6 @@ define(function(require) { return val } } - var term = extractLast(_.escape(this.value)); - $(this).addClass('working'); - if (term.length < 1) { - return false; - } }, } } @@ -310,13 +297,14 @@ define(function(require) { that.fields[that.pathFieldName].$el.removeClass('error'); that.fields[that.pathFieldName].$el.find('.help-inline').html(''); var tags = []; - console.log(ui.tag); if (!_.isUndefined(options.regExpValidation) && !options.regExpValidation.regexp.test(ui.tagLabel)) { that.fields[that.pathFieldName].$el.addClass('error'); that.fields[that.pathFieldName].$el.find('.help-inline').html(options.regExpValidation.message); return false; } } + tagitOpts['singleFieldDelimiter'] = '||'; + tagitOpts['singleField'] = false; this.fields[that.pathFieldName].editor.$el.find('[data-js="resource"]').tagit(tagitOpts).on('change', function(e) { //check dirty field for tagit input type : `path` XAUtil.checkDirtyField($(e.currentTarget).val(), defaultValue.toString(), $(e.currentTarget)); @@ -344,7 +332,7 @@ define(function(require) { multiple: true, minimumInputLength: 1, width: '220px', - tokenSeparators: [",", " "], + tokenSeparators: [" "], initSelection: function(element, callback) { var data = []; //to set single select value @@ -356,12 +344,11 @@ define(function(require) { return; } //this is form multi-select value - $(element.val().split(",")).each(function() { - data.push({ - id: this, - text: this - }); - }); + if(_.isArray(JSON.parse(element.val()))) { + $(JSON.parse(element.val())).each(function () { + data.push({id: this, text: this}); + }) + } callback(data); }, createSearchChoice: function(term, data) { @@ -474,7 +461,7 @@ define(function(require) { } else { val = this.fields[name].getValue(); } - resources[name] = _.isNull(val) ? [] : val.resource.split(','); + resources[name] = _.isNull(val) ? [] : val.resource; if (!_.isEmpty(currentResource.parent)) { name = currentResource.parent; } else { diff --git a/security-admin/src/main/webapp/scripts/views/security_zone/zoneResource.js b/security-admin/src/main/webapp/scripts/views/security_zone/zoneResource.js index 7634eab..8c19c0a 100644 --- a/security-admin/src/main/webapp/scripts/views/security_zone/zoneResource.js +++ b/security-admin/src/main/webapp/scripts/views/security_zone/zoneResource.js @@ -40,7 +40,7 @@ define(function(require) { var resources = _.map(this.model.get('resources'), function(val, key) { return { 'key': key, - 'value': val + 'value': val.join(", ") } }) return { @@ -197,7 +197,11 @@ define(function(require) { _.each(that.rangerServiceDefModel.get('resources'), function(obj) { var tmpObj = that.form.model.get(obj.name); if (!_.isUndefined(tmpObj) && _.isObject(tmpObj) && !_.isEmpty(tmpObj.resource)) { - tmpResource[obj.name] = tmpObj.resource.split(','); + if (obj.type == "path") { + tmpResource[obj.name] = tmpObj.resource; + } else { + tmpResource[obj.name] = _.map(tmpObj.resource, function(val){return val.text}); + } that.form.model.unset(obj.name); } }); diff --git a/security-admin/src/main/webapp/scripts/views/service/AuditFilterConfig.js b/security-admin/src/main/webapp/scripts/views/service/AuditFilterConfig.js index 2032da1..27e2aae 100644 --- a/security-admin/src/main/webapp/scripts/views/service/AuditFilterConfig.js +++ b/security-admin/src/main/webapp/scripts/views/service/AuditFilterConfig.js @@ -358,8 +358,13 @@ define(function(require) { var tmpObj = that.form.model.get(obj.name); if (!_.isUndefined(tmpObj) && _.isObject(tmpObj) && !_.isEmpty(tmpObj.resource)) { tmpResource[obj.name] ={}, resource[obj.name] ={}; - tmpResource[obj.name].values = tmpObj.resource.split(','); - resource[obj.name].values = tmpObj.resource.split(','); + if (obj.type == "path") { + tmpResource[obj.name].values = tmpObj.resource; + resource[obj.name].values = tmpObj.resource; + } else { + tmpResource[obj.name].values = _.map(tmpObj.resource, function(val){return val.text}); + resource[obj.name].values = _.map(tmpObj.resource, function(val){return val.text}); + } if (obj.excludesSupported) { resource[obj.name]['isExcludes'] = tmpObj.isExcludes; } diff --git a/security-admin/src/main/webapp/scripts/views/service/ServiceAuditFilterResources.js b/security-admin/src/main/webapp/scripts/views/service/ServiceAuditFilterResources.js index c08f745..318cdcb 100644 --- a/security-admin/src/main/webapp/scripts/views/service/ServiceAuditFilterResources.js +++ b/security-admin/src/main/webapp/scripts/views/service/ServiceAuditFilterResources.js @@ -234,14 +234,6 @@ define(function(require) { if (!this.model.isNew() && !_.isUndefined(this.model.get('path'))) { defaultValue = this.model.get('path').values; } - - function split(val) { - return val.split(/,\s*/); - } - - function extractLast(term) { - return split(term).pop(); - } var tagitOpts = {} if (!_.isUndefined(options.lookupURL) && options.lookupURL) { tagitOpts["autocomplete"] = { @@ -249,12 +241,12 @@ define(function(require) { source: function(request, response) { var url = "service/plugins/services/lookupResource/" + that.serviceName; var context = { - 'userInput': extractLast(request.term), + 'userInput': request.term, 'resourceName': that.pathFieldName, 'resources': {} }; - var val = that.fields[that.pathFieldName].editor.getValue(); - context.resources[that.pathFieldName] = _.isNull(val) || _.isEmpty(val) ? [] : val.resource.split(","); + var val = that.fields[that.pathFieldName].editor.getValue('pathField'); + context.resources[that.pathFieldName] = _.isNull(val) || _.isEmpty(val) ? [] : val.resource; var p = $.ajax({ url: url, type: "POST", @@ -280,8 +272,8 @@ define(function(require) { }, search: function() { $('.tagit-autocomplete').addClass('tagit-position'); - if (!_.isUndefined(this.value) && (/[ ,]+/).test(this.value)) { - var values = this.value.trim().split(/[ ,]+/); + if(!_.isUndefined(this.value) && this.value.includes('||')) { + var values = this.value.trim().split('||'); if (values.length > 1) { for (var i = 0; i < values.length; i++) { that.fields[that.pathFieldName].editor.$el.find('[data-js="resource"]').tagit("createTag", values[i]); @@ -291,11 +283,6 @@ define(function(require) { return val } } - var term = extractLast(_.escape(this.value)); - $(this).addClass('working'); - if (term.length < 1) { - return false; - } }, } } @@ -304,13 +291,14 @@ define(function(require) { that.fields[that.pathFieldName].$el.removeClass('error'); that.fields[that.pathFieldName].$el.find('.help-inline').html(''); var tags = []; - console.log(ui.tag); if (!_.isUndefined(options.regExpValidation) && !options.regExpValidation.regexp.test(ui.tagLabel)) { that.fields[that.pathFieldName].$el.addClass('error'); that.fields[that.pathFieldName].$el.find('.help-inline').html(options.regExpValidation.message); return false; } } + tagitOpts['singleFieldDelimiter'] = '||'; + tagitOpts['singleField'] = false; this.fields[that.pathFieldName].editor.$el.find('[data-js="resource"]').tagit(tagitOpts).on('change', function(e) { //check dirty field for tagit input type : `path` XAUtil.checkDirtyField($(e.currentTarget).val(), defaultValue.toString(), $(e.currentTarget)); @@ -338,24 +326,15 @@ define(function(require) { multiple: true, minimumInputLength: 1, width: '220px', - tokenSeparators: [",", " "], + tokenSeparators: [" "], initSelection: function(element, callback) { var data = []; - //to set single select value - if (!_.isUndefined(options.singleValueInput) && options.singleValueInput) { - callback({ - id: element.val(), - text: element.val() - }); - return; - } //this is form multi-select value - $(element.val().split(",")).each(function() { - data.push({ - id: this, - text: this - }); - }); + if(_.isArray(JSON.parse(element.val()))) { + $(JSON.parse(element.val())).each(function () { + data.push({id: this, text: this}); + }) + } callback(data); }, createSearchChoice: function(term, data) { @@ -468,7 +447,7 @@ define(function(require) { } else { val = this.fields[name].getValue(); } - resources[name] = _.isNull(val) ? [] : val.resource.split(','); + resources[name] = _.isNull(val) ? [] : val.resource; if (!_.isEmpty(currentResource.parent)) { name = currentResource.parent; } else { diff --git a/security-admin/src/main/webapp/styles/xa.css b/security-admin/src/main/webapp/styles/xa.css index 0435174..f170d47 100644 --- a/security-admin/src/main/webapp/styles/xa.css +++ b/security-admin/src/main/webapp/styles/xa.css @@ -979,6 +979,10 @@ ul.tagit li.tagit-choice .tagit-close { border-radius: 0 !important; } +.ui-menu.ui-widget.ui-widget-content.ui-autocomplete.ui-front.tagit-autocomplete { + width: 50% !important; +} + table.backgrid tr > td > .label { float: left; margin: 0 3px 2px 0;
