From 4e5e7755b5705fd49b6a7f70bed216bde119450e Mon Sep 17 00:00:00 2001 From: Harshal Dhumal Date: Fri, 15 Jan 2016 19:10:29 +0530 Subject: [PATCH] Make the privilege control to work as expected. Using the NodeListByNameCell for selecting the grantee. This patch includes: - Change the RolePrivilegesModel schema to allow to use the NodeListByNameCell for showing grantee, and granter information. - When setting some value - do not use the silent flag in Select2Cell, which will allow the session manager to take the value in account, which is used by the NodeListByNameCell. - Set the top object in the children of the UniqueControl collections. [Ashesh Vashi] - Pass on the node information to the actual data model, so that - we can use node information by the control itself. [Ashesh Vashi] - While adding the new privilege, set the granter to the current user. [Ashesh Vashi] --- .../browser/server_groups/servers/__init__.py | 3 +- .../servers/static/js/privilege.js | 54 +++++++++++++++---- .../browser/templates/browser/js/node.js | 7 ++- web/pgadmin/static/js/backform.pgadmin.js | 13 +++-- .../static/js/backgrid/backgrid.pgadmin.js | 2 +- 5 files changed, 60 insertions(+), 19 deletions(-) diff --git a/web/pgadmin/browser/server_groups/servers/__init__.py b/web/pgadmin/browser/server_groups/servers/__init__.py index c38265dce..04156ee2b 100644 --- a/web/pgadmin/browser/server_groups/servers/__init__.py +++ b/web/pgadmin/browser/server_groups/servers/__init__.py @@ -115,7 +115,8 @@ class ServerModule(sg.ServerGroupPluginModule): { 'name': 'pgadmin.browser.server.privilege', 'path': url_for('browser.index') + 'server/static/js/privilege', - 'when': self.node_type + 'when': self.node_type, + 'deps': ['pgadmin.browser.node.ui'] }, { 'name': 'pgadmin.browser.server.variable', diff --git a/web/pgadmin/browser/server_groups/servers/static/js/privilege.js b/web/pgadmin/browser/server_groups/servers/static/js/privilege.js index 7d2ccc4c0..74d09f407 100644 --- a/web/pgadmin/browser/server_groups/servers/static/js/privilege.js +++ b/web/pgadmin/browser/server_groups/servers/static/js/privilege.js @@ -5,7 +5,7 @@ function(_, $, Backbone, Backform, Backgrid, Alertify, pgNode) { // Export global even in AMD case in case this script is loaded with // others that may still expect a global Backform. - return factory(root, _, $, Backbone, Backform, Alertify, pgNode); + return factory(root, _, $, Backbone, Backform, Backgrid, Alertify, pgNode); }); // Next for Node.js or CommonJS. jQuery may not be needed as a module. @@ -14,15 +14,16 @@ $ = root.jQuery || root.$ || root.Zepto || root.ender, Backbone = require('backbone') || root.Backbone, Backform = require('backform') || root.Backform; + Backgrid = require('backgrid') || root.Backgrid; Alertify = require('alertify') || root.Alertify; pgAdmin = require('pgadmin.browser.node') || root.pgAdmin.Browser.Node; factory(root, _, $, Backbone, Backform, Alertify, pgNode); // Finally, as a browser global. } else { - factory(root, root._, (root.jQuery || root.Zepto || root.ender || root.$), root.Backbone, root.Backform, root.pgAdmin.Browser.Node); + factory(root, root._, (root.jQuery || root.Zepto || root.ender || root.$), root.Backbone, root.Backform, root.Backgrid, root.alertify, root.pgAdmin.Browser.Node); } -} (this, function(root, _, $, Backbone, Backform, Alertify, pgNode) { +} (this, function(root, _, $, Backbone, Backform, Backgrid, Alertify, pgNode) { /** * Each Privilege, supporeted by an database object, will be represented @@ -65,17 +66,40 @@ * provide the type of privileges (it supports). */ privileges:[], - schema: [{ - id: 'grantee', label:'Grantee', type:'text', group: null, cell: 'string', - disabled: false, cellHeaderClasses: 'width_percent_40' - }, { + id: 'grantee', label:'Grantee', type:'text', group: null, + editable: true, cellHeaderClasses: 'width_percent_40', + cell: 'node-list-by-name', node: 'role', + disabled : function(column, collection) { + if (column instanceof Backbone.Collection) { + // This has been called during generating the header cell + return false; + } + return !(this.node_info && this.node_info.server.user.name == column.get('grantor')); + }, + transform: function(data) { + var res = + Backgrid.Extension.NodeListByNameCell.prototype.defaults.transform.apply( + this, arguments + ); + res.unshift({label: 'public', value: 'public'}); + return res; + } + },{ id: 'privileges', label:'Privileges', type: 'collection', model: PrivilegeModel, group: null, - disabled: false, cell: 'privilege', control: 'text', - cellHeaderClasses: 'width_percent_40' + cell: 'privilege', control: 'text', cellHeaderClasses: 'width_percent_40', + disabled : function(column, collection) { + if (column instanceof Backbone.Collection) { + // This has been called during generating the header cell + return false; + } + return !(this.node_info && this.node_info.server.user.name == column.get('grantor') || + this.attributes.node_info.server.user.name == column.get('grantor')); + } },{ - id: 'grantor', label: 'Granter', type: 'text', disabled: true + id: 'grantor', label: 'Granter', type: 'text', disabled: true, + cell: 'node-list-by-name', node: 'role' }], /* @@ -86,6 +110,14 @@ pgNode.Model.prototype.initialize.apply(this, arguments); + if (_.isNull(attrs)) { + this.set( + 'grantor', + opts && opts.top && opts.top.node_info && opts.top.node_info.server.user.name, + {silent: true} + ); + } + /* * Define the collection of the privilege supported by this model */ @@ -437,7 +469,7 @@ if (rawData instanceof Backbone.Collection) { rawData.each(function(m) { if (m.get('privilege')) { - res += m.get('privilege_type'); + res += self.notation[m.get('privilege_type')]; if (m.get('with_grant')) { res += '*'; } diff --git a/web/pgadmin/browser/templates/browser/js/node.js b/web/pgadmin/browser/templates/browser/js/node.js index d2aa94d4e..afce2884b 100644 --- a/web/pgadmin/browser/templates/browser/js/node.js +++ b/web/pgadmin/browser/templates/browser/js/node.js @@ -132,8 +132,10 @@ function($, _, S, pgAdmin, Menu, Backbone, Alertify, Backform) { } // We know - which data model to be used for this object. - var newModel = new (this.model.extend({urlRoot: urlBase}))(attrs, {}), - info = this.getTreeNodeHierarchy.apply(this, [item]), + var info = this.getTreeNodeHierarchy.apply(this, [item]), + newModel = new (this.model.extend({urlRoot: urlBase})) ( + attrs, {node_info: info} + ), groups = Backform.generateViewSchema( info, newModel, type, this, node ); @@ -1551,6 +1553,7 @@ function($, _, S, pgAdmin, Menu, Backbone, Alertify, Backform) { (self.collection && self.collection.handler); self.trackChanges = false; self.errorModel = new Backbone.Model(); + self.node_info = options.node_info; if (self.schema && _.isArray(self.schema)) { _.each(self.schema, function(s) { diff --git a/web/pgadmin/static/js/backform.pgadmin.js b/web/pgadmin/static/js/backform.pgadmin.js index fb8010e5c..bd03b235a 100644 --- a/web/pgadmin/static/js/backform.pgadmin.js +++ b/web/pgadmin/static/js/backform.pgadmin.js @@ -75,7 +75,7 @@ 'collection': ['sub-node-collection', 'sub-node-collection', 'string'], 'uniqueColCollection': ['unique-col-collection', 'unique-col-collection', 'string'], 'switch' : 'switch', - 'select2': 'select2', + 'select2': 'select2' }; var getMappedControl = Backform.getMappedControl = function(type, mode) { @@ -651,6 +651,7 @@ model: self.field.get('model'), silent: true, handler: self.model.handler || self.model, + top: self.model.top || self.model, attrName: self.field.get('name') }); self.model.set(self.field.get('name'), collection, {silent: true}); @@ -725,7 +726,7 @@ * conflicting with another model value. */ - m.set(uniqueChangedAttr[0], m.previous(uniqueChangedAttr[0]), {silent: true}); + m.set(uniqueChangedAttr[0], m.previous(uniqueChangedAttr[0])); } if (oldModel) { var idx = collection.indexOf(oldModel); @@ -859,7 +860,11 @@ } $(grid.body.$el.find($("tr.new"))).removeClass("new") - var m = new (data.model)(null, {silent: true}); + var m = new (data.model) (null, { + silent: true, + handler: self.model.handler || self.model, + top: self.model.top || self.model + }); collection.add(m); var idx = collection.indexOf(m), @@ -1205,7 +1210,7 @@ ); } - // After validation we need to set that value into model (only if all falgs are true) + // After validation we need to set that value into model (only if all flags are true) if (isValid) { this.stopListening(this.model, "change:" + name, this.render); this.model.set(name, value); diff --git a/web/pgadmin/static/js/backgrid/backgrid.pgadmin.js b/web/pgadmin/static/js/backgrid/backgrid.pgadmin.js index 85f99a6dd..b9a3f8114 100644 --- a/web/pgadmin/static/js/backgrid/backgrid.pgadmin.js +++ b/web/pgadmin/static/js/backgrid/backgrid.pgadmin.js @@ -367,7 +367,7 @@ onSave: function (e) { var model = this.model; var column = this.column; - model.set(column.get("name"), this.$select.val(),{silent:true}); + model.set(column.get("name"), this.$select.val()); } }); return Backgrid;