Repository: ranger Updated Branches: refs/heads/master 987abe806 -> 32d73afd6
RANGER-2119: Ranger UI: resource field issue when we set lookupSupported attribute to false in ranger-service-def. Signed-off-by: Mehul Parikh <[email protected]> Project: http://git-wip-us.apache.org/repos/asf/ranger/repo Commit: http://git-wip-us.apache.org/repos/asf/ranger/commit/32d73afd Tree: http://git-wip-us.apache.org/repos/asf/ranger/tree/32d73afd Diff: http://git-wip-us.apache.org/repos/asf/ranger/diff/32d73afd Branch: refs/heads/master Commit: 32d73afd6bc5ac5d9b894f3d14f3eaf9b1b4f408 Parents: 987abe8 Author: ni3galave <[email protected]> Authored: Mon Jun 4 18:20:25 2018 +0530 Committer: Mehul Parikh <[email protected]> Committed: Mon Jun 4 19:40:29 2018 +0530 ---------------------------------------------------------------------- .../scripts/models/BackboneFormDataType.js | 37 +++-- .../main/webapp/scripts/modules/XAOverrides.js | 29 +++- .../src/main/webapp/scripts/utils/XAUtils.js | 41 ++++++ .../scripts/views/policies/RangerPolicyForm.js | 138 +++++++++---------- 4 files changed, 157 insertions(+), 88 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ranger/blob/32d73afd/security-admin/src/main/webapp/scripts/models/BackboneFormDataType.js ---------------------------------------------------------------------- diff --git a/security-admin/src/main/webapp/scripts/models/BackboneFormDataType.js b/security-admin/src/main/webapp/scripts/models/BackboneFormDataType.js index a928470..1329eb2 100644 --- a/security-admin/src/main/webapp/scripts/models/BackboneFormDataType.js +++ b/security-admin/src/main/webapp/scripts/models/BackboneFormDataType.js @@ -103,20 +103,25 @@ define(function(require) { //checkParentHideShow field formObj.fieldAttrs = { 'data-name' : 'field-'+v.name, 'parent' : v.parent }; formObj['resourceOpts'] = {'data-placeholder': v.label }; - - if(!_.isUndefined(v.lookupSupported) && v.lookupSupported ){ - var opts = { - 'type' : v.name, - 'lookupURL' : "service/plugins/services/lookupResource/"+form.rangerService.get('name') - }; + if(!_.isUndefined(v.lookupSupported)){ + var opts = {}; if(_.has(v, 'validationRegEx') && !_.isEmpty(v.validationRegEx)){ - opts['regExpValidation'] = {'type': 'regexp', 'regexp':new RegExp(v.validationRegEx), 'message' : v.validationMessage}; + opts['regExpValidation'] = {'type': 'regexp', 'regexp':new RegExp(v.validationRegEx), 'message' : v.validationMessage}; + } + //To support single value input + if( XAUtils.isSinglevValueInput(v) ){ + opts['singleValueInput'] = true; + } + opts['type'] = v.name; + if(v.lookupSupported){ + opts['lookupURL'] = "service/plugins/services/lookupResource/"+form.rangerService.get('name'); + resourceOpts['select2Opts'] = form.getPlugginAttr(true, opts); + }else{ + resourceOpts['select2Opts'] = XAUtils.select2OptionForUserCreateChoice(); + if(!_.isUndefined(opts.singleValueInput) && opts.singleValueInput){ + resourceOpts['select2Opts']['maximumSelectionSize'] = 1; + } } - //To support single value input - if( XAUtils.isSinglevValueInput(v) ){ - opts['singleValueInput'] = true; - } - resourceOpts['select2Opts'] = form.getPlugginAttr(true, opts); formObj['resourceOpts'] = resourceOpts; } //same level resources check @@ -187,11 +192,13 @@ define(function(require) { formObj['name'] = v.name; formObj['editorAttrs'] = {'data-placeholder': v.label }; formObj.fieldAttrs = { 'data-name' : 'field-'+v.name, 'parent' : v.parent }; - if(!_.isUndefined(v.lookupSupported) && v.lookupSupported ){ + if(!_.isUndefined(v.lookupSupported)){ var options = { 'containerCssClass' : v.name, - 'lookupURL' : "service/plugins/services/lookupResource/"+form.rangerService.get('name') - }; + }; + if(v.lookupSupported){ + options['lookupURL'] = "service/plugins/services/lookupResource/"+form.rangerService.get('name'); + } //to support regexp level validation if(_.has(v, 'validationRegEx') && !_.isEmpty(v.validationRegEx)){ options['regExpValidation'] = {'type': 'regexp', 'regexp':new RegExp(v.validationRegEx), 'message' : v.validationMessage}; http://git-wip-us.apache.org/repos/asf/ranger/blob/32d73afd/security-admin/src/main/webapp/scripts/modules/XAOverrides.js ---------------------------------------------------------------------- diff --git a/security-admin/src/main/webapp/scripts/modules/XAOverrides.js b/security-admin/src/main/webapp/scripts/modules/XAOverrides.js index ecce373..3832916 100644 --- a/security-admin/src/main/webapp/scripts/modules/XAOverrides.js +++ b/security-admin/src/main/webapp/scripts/modules/XAOverrides.js @@ -520,7 +520,7 @@ this.renderSameLevelResource(); return this; }, - renderResource : function() { + renderResource : function(def) { var that = this; var Vent = require('modules/Vent'); if(!_.isNull(this.value) && !_.isEmpty(this.value)){ @@ -539,6 +539,28 @@ // that.checkDirtyFieldForSelect2($(e.currentTarget), that, this.value); } }); + //Handle resource at same level option for lookupSupport true/false condition + if(def){ + if(def.lookupSupported){ + var opts = {}; + var singleValueInput = XAUtil.isSinglevValueInput(def); + opts['singleValueInput'] = singleValueInput; + 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'); + opts['type'] = def.name; + this.resourceOpts['select2Opts'] = that.form.getPlugginAttr(true, opts); + }else{ + this.resourceOpts.select2Opts['containerCssClass'] = def.name; + delete this.resourceOpts.select2Opts['ajax']; + delete this.resourceOpts.select2Opts['tags']; + this.resourceOpts.select2Opts['data'] = []; + if(singleValueInput){ + this.resourceOpts['select2Opts']['maximumSelectionSize'] = 1; + } + } + } //create select2 if select2Opts is specified if(!_.isUndefined(this.resourceOpts.select2Opts)){ this.$resource.select2(this.resourceOpts.select2Opts).on('change',function(e){ @@ -607,7 +629,8 @@ var val = _.isEmpty(that.preserveResourceValues[e.currentTarget.value]) ? '' : that.preserveResourceValues[e.currentTarget.value].split(','); that.$resource.select2('val', val) }else{ - that.$resource.select2('val', '') + that.$resource.select2('val', ""); + that.value=[]; } //reset values that.value.isExcludes = false; @@ -639,6 +662,8 @@ if(that.recursiveSupport) that.value.isRecursive = true; that.excludeSupport = def.excludesSupported; that.renderToggles(); + //Handle resource at same level option for lookupSupport true/false condition + that.renderResource(def); } //trigger resource event for showing respective access permissions Vent.trigger('resourceType:change', changeType = 'resourceType', e.currentTarget.value, e.currentTarget.value, e); http://git-wip-us.apache.org/repos/asf/ranger/blob/32d73afd/security-admin/src/main/webapp/scripts/utils/XAUtils.js ---------------------------------------------------------------------- diff --git a/security-admin/src/main/webapp/scripts/utils/XAUtils.js b/security-admin/src/main/webapp/scripts/utils/XAUtils.js index 5bec224..d9366a1 100644 --- a/security-admin/src/main/webapp/scripts/utils/XAUtils.js +++ b/security-admin/src/main/webapp/scripts/utils/XAUtils.js @@ -1425,5 +1425,46 @@ define(function(require) { $('.popover').remove(); $('.datetimepicker').remove(); }; + //select2 option + XAUtils.select2OptionForUserCreateChoice = function(){ + var opts = { + multiple: true, + data:[], + closeOnSelect : true, + width :'220px', + allowClear: true, + tokenSeparators: ["," , " "], + minimumInputLength: 1, + initSelection : function (element, callback) { + var data = []; + //to set single select value + if(!_.isUndefined(opts.singleValueInput) && opts.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}); + }); + callback(data); + }, + createSearchChoice: function(term, data) { + term = _.escape(term); + if ($(data).filter(function() { + return this.text.localeCompare(term) === 0; + }).length === 0) { + if($.inArray(term, this.val()) >= 0){ + return null; + }else{ + return { + id : term, + text: term + }; + } + } + }, + } + return opts; + } return XAUtils; }); \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ranger/blob/32d73afd/security-admin/src/main/webapp/scripts/views/policies/RangerPolicyForm.js ---------------------------------------------------------------------- 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 e431f0a..e3d9635 100644 --- a/security-admin/src/main/webapp/scripts/views/policies/RangerPolicyForm.js +++ b/security-admin/src/main/webapp/scripts/views/policies/RangerPolicyForm.js @@ -560,77 +560,73 @@ define(function(require){ function extractLast( term ) { return split( term ).pop(); } - - this.fields[that.pathFieldName].editor.$el.find('[data-js="resource"]').tagit({ - autocomplete : { - cache: false, - source: function( request, response ) { - var url = "service/plugins/services/lookupResource/"+that.rangerService.get('name'); - var context ={ - 'userInput' : extractLast( 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 p = $.ajax({ - url : url, - type : "POST", - data : JSON.stringify(context), - dataType : 'json', - contentType: "application/json; charset=utf-8", - }).done(function(data){ - if(data){ - response(data); - } else { - response(); - } - - }).fail(function(){ - response(); - - }); - setTimeout(function(){ - p.abort(); - console.log('connection timeout for resource path request...!!'); - }, 10000); - }, - open : function(){ - $(this).removeClass('working'); - }, - search: function() { - if(!_.isUndefined(this.value) && _.contains(this.value,',')){ - _.each(this.value.split(',') , function(tag){ - that.fields[that.pathFieldName].editor.$el.tagit("createTag", tag); - }); - return false; - } - var term = extractLast( this.value ); - $(this).addClass('working'); - if ( term.length < 1 ) { - return false; - } - }, - - }, - 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; - } - } - }).on('change',function(e){ - //check dirty field for tagit input type : `path` - XAUtil.checkDirtyField($(e.currentTarget).val(), defaultValue.toString(), $(e.currentTarget)) - }); - - + 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 ), + 'resourceName' : that.pathFieldName, + 'resources' : {} + }; + var val = that.fields[that.pathFieldName].editor.getValue(); + context.resources[that.pathFieldName] = _.isNull(val) || _.isEmpty(val) ? [] : val.resource.split(","); + var p = $.ajax({ + url : url, + type : "POST", + data : JSON.stringify(context), + dataType : 'json', + contentType: "application/json; charset=utf-8", + }).done(function(data){ + if(data){ + response(data); + } else { + response(); + } + }).fail(function(){ + response(); + }); + setTimeout(function(){ + p.abort(); + console.log('connection timeout for resource path request...!!'); + }, 10000); + }, + open : function(){ + $(this).removeClass('working'); + }, + search: function() { + if(!_.isUndefined(this.value) && _.contains(this.value,',')){ + _.each(this.value.split(',') , function(tag){ + that.fields[that.pathFieldName].editor.$el.tagit("createTag", tag); + }); + return false; + } + var term = extractLast( this.value ); + $(this).addClass('working'); + if ( term.length < 1 ) { + return false; + } + }, + } + } + 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){ + //check dirty field for tagit input type : `path` + XAUtil.checkDirtyField($(e.currentTarget).val(), defaultValue.toString(), $(e.currentTarget)); + }); }, getPlugginAttr :function(autocomplete, options){ var that =this, type = options.containerCssClass, validRegExpString = true, select2Opts=[];
