diff options
author | Garren Smith <garren.smith@gmail.com> | 2013-09-24 13:31:01 +0200 |
---|---|---|
committer | Garren Smith <garren.smith@gmail.com> | 2013-09-25 17:41:35 +0200 |
commit | f0fbb006688d811f3081e5fe6c978d0779d7edb3 (patch) | |
tree | 127e3ceb332a8711c17ab71160a0bddfec835399 | |
parent | c7249d0287c749e0b0c86941f29e62ed331bdf5f (diff) | |
download | couchdb-f0fbb006688d811f3081e5fe6c978d0779d7edb3.tar.gz |
initial implementation
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | src/fauxton/app/addons/permissions/base.js | 25 | ||||
-rw-r--r-- | src/fauxton/app/addons/permissions/resources.js | 57 | ||||
-rw-r--r-- | src/fauxton/app/addons/permissions/routes.js | 64 | ||||
-rw-r--r-- | src/fauxton/app/addons/permissions/templates/item.html | 17 | ||||
-rw-r--r-- | src/fauxton/app/addons/permissions/templates/permissions.html | 15 | ||||
-rw-r--r-- | src/fauxton/app/addons/permissions/templates/section.html | 37 | ||||
-rw-r--r-- | src/fauxton/app/addons/permissions/views.js | 189 | ||||
-rw-r--r-- | src/fauxton/settings.json.default | 1 |
9 files changed, 406 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore index a4798c913..58f992ebc 100644 --- a/.gitignore +++ b/.gitignore @@ -95,6 +95,7 @@ src/fauxton/app/addons/* !src/fauxton/app/addons/contribute !src/fauxton/app/addons/auth !src/fauxton/app/addons/exampleAuth +!src/fauxton/app/addons/permissions src/fauxton/settings.json* !src/fauxton/settings.json.default share/www/fauxton diff --git a/src/fauxton/app/addons/permissions/base.js b/src/fauxton/app/addons/permissions/base.js new file mode 100644 index 000000000..016ba1c02 --- /dev/null +++ b/src/fauxton/app/addons/permissions/base.js @@ -0,0 +1,25 @@ +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy of +// the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations under +// the License. + +define([ + "app", + "api", + "addons/permissions/routes" +], + +function(app, FauxtonAPI, Permissions) { + + Permissions.initialize = function() {}; + + return Permissions; +}); + diff --git a/src/fauxton/app/addons/permissions/resources.js b/src/fauxton/app/addons/permissions/resources.js new file mode 100644 index 000000000..6f4cb7d61 --- /dev/null +++ b/src/fauxton/app/addons/permissions/resources.js @@ -0,0 +1,57 @@ +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy of +// the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations under +// the License. + +define([ + "app", + "api" +], +function (app, FauxtonAPI ) { + var Permissions = FauxtonAPI.addon(); + + Permissions.Security = Backbone.Model.extend({ + defaults: { + admins: { + names: [], + roles: [] + }, + + members: { + names: [], + roles: [] + } + + }, + + isNew: function () { + return false; + }, + + initialize: function (attrs, options) { + this.database = options.database; + }, + + url: function () { + return this.database.id + '/_security'; + }, + + addItem: function (value, type, section) { + var sectionValues = this.get(section); + + sectionValues[type].push(value); + return this.set(section, sectionValues); + } + + }); + + return Permissions; +}); + diff --git a/src/fauxton/app/addons/permissions/routes.js b/src/fauxton/app/addons/permissions/routes.js new file mode 100644 index 000000000..f6c658777 --- /dev/null +++ b/src/fauxton/app/addons/permissions/routes.js @@ -0,0 +1,64 @@ +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy of +// the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations under +// the License. + +define([ + "app", + "api", + "modules/databases/base", + "addons/permissions/views" +], +function (app, FauxtonAPI, Databases, Permissions) { + + var PermissionsRouteObject = FauxtonAPI.RouteObject.extend({ + layout: 'one_pane', + selectedHeader: 'Databases', + + routes: { + 'database/:database/permissions': 'permissions' + }, + + initialize: function (route, masterLayout, options) { + var docOptions = app.getParams(); + docOptions.include_docs = true; + + this.databaseName = options[0]; + this.database = new Databases.Model({id:this.databaseName}); + this.security = new Permissions.Security(null, { + database: this.database + }); + }, + + establish: function () { + return [this.database.fetch(), this.security.fetch()]; + }, + + permissions: function () { + this.setView('#dashboard-content', new Permissions.Permissions({ + database: this.database, + model: this.security + })); + + }, + + crumbs: function () { + return [ + {"name": "Databases", "link": "/_all_dbs"}, + {"name": this.database.id, "link": Databases.databaseUrl(this.database)}, + {"name": "Permissions", "link": "/permissions"} + ]; + }, + + }); + + Permissions.RouteObjects = [PermissionsRouteObject]; + return Permissions; +}); diff --git a/src/fauxton/app/addons/permissions/templates/item.html b/src/fauxton/app/addons/permissions/templates/item.html new file mode 100644 index 000000000..aa01d0b6e --- /dev/null +++ b/src/fauxton/app/addons/permissions/templates/item.html @@ -0,0 +1,17 @@ +<!-- +Licensed under the Apache License, Version 2.0 (the "License"); you may not +use this file except in compliance with the License. You may obtain a copy of +the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +License for the specific language governing permissions and limitations under +the License. +--> +<li> +<span> <%= item %> </span> +<button type="button" style="float:none; margin-bottom:6px" class="close">×</button> +</li> diff --git a/src/fauxton/app/addons/permissions/templates/permissions.html b/src/fauxton/app/addons/permissions/templates/permissions.html new file mode 100644 index 000000000..d2474251e --- /dev/null +++ b/src/fauxton/app/addons/permissions/templates/permissions.html @@ -0,0 +1,15 @@ +<!-- +Licensed under the Apache License, Version 2.0 (the "License"); you may not +use this file except in compliance with the License. You may obtain a copy of +the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +License for the specific language governing permissions and limitations under +the License. +--> +<p>Each database contains lists of admins and members. Admins and members are each defined by names and roles, which are lists of strings.</p> +<div id="sections"> </div> diff --git a/src/fauxton/app/addons/permissions/templates/section.html b/src/fauxton/app/addons/permissions/templates/section.html new file mode 100644 index 000000000..102d857f1 --- /dev/null +++ b/src/fauxton/app/addons/permissions/templates/section.html @@ -0,0 +1,37 @@ +<!-- +Licensed under the Apache License, Version 2.0 (the "License"); you may not +use this file except in compliance with the License. You may obtain a copy of +the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +License for the specific language governing permissions and limitations under +the License. +--> +<h1> <%= section %> </h1> +<p id="help"> <%= help %> </p> + +<div class="row"> + <div class="span6"> + <h3> Names </h3> + <ul id="items-names"></ul> + + <form class="permission-item-form form-inline"> + <input data-section="<%= section %>" data-type="names" type="text" class="item input-small" placeholder="Add Name"> + <input type="submit" class="btn" value="Add Name" > + </form> + + </div> + <div class="span6"> + <h3> Roles </h3> + <ul id="items-roles"></ul> + + <form class="permission-item-form form-inline"> + <input data-section="<%= section %>" data-type="roles" type="text" class="item input-small" placeholder="Add Role"> + <input type="submit" class="btn" value="Add Role" > + </form> + </div> +</div> diff --git a/src/fauxton/app/addons/permissions/views.js b/src/fauxton/app/addons/permissions/views.js new file mode 100644 index 000000000..3c5d3c2b1 --- /dev/null +++ b/src/fauxton/app/addons/permissions/views.js @@ -0,0 +1,189 @@ +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy of +// the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations under +// the License. + +define([ + "app", + "api", + "addons/permissions/resources" +], +function (app, FauxtonAPI, Permissions ) { + var events = {}; + Permissions.events = _.extend(events, Backbone.Events); + + Permissions.Permissions = FauxtonAPI.View.extend({ + template: "addons/permissions/templates/permissions", + + initialize: function (options) { + this.database = options.database; + this.listenTo(Permissions.events, 'itemRemoved', this.itemRemoved); + }, + + events: { + "submit .permission-item-form": "addItem", + 'click .close': "removeItem" + }, + + itemRemoved: function (event) { + console.log('item remove'); + this.model.set({ + admins: this.adminsView.items(), + members: this.membersView.items() + }); + + this.model.save().then(function () { + FauxtonAPI.addNotification({ + msg: 'Database permissions has been updated.' + }); + }); + }, + beforeRender: function () { + this.adminsView = this.insertView('#sections', new Permissions.PermissionSection({ + model: this.model, + section: 'admins', + help: 'Database admins can update design documents and edit the admin and member lists.' + })); + + this.membersView = this.insertView('#sections', new Permissions.PermissionSection({ + model: this.model, + section: 'members', + help: 'Database members can access the database. If no members are defined, the database is public.' + })); + }, + + + serialize: function () { + console.log('s', this.model.toJSON()); + return { + databaseName: this.database.id, + security: this.model.toJSON() + }; + } + }); + + Permissions.PermissionSection = FauxtonAPI.View.extend({ + template: "addons/permissions/templates/section", + initialize: function (options) { + this.section = options.section; + this.help = options.help; + }, + + events: { + "submit .permission-item-form": "addItem", + 'click .close': "removeItem" + }, + + beforeRender: function () { + var section = this.model.get(this.section); + + this.nameViews = []; + this.roleViews = []; + + _.each(section.names, function (name) { + var nameView = this.insertView('#items-names', new Permissions.PermissionItem({ + item: name, + })); + this.nameViews.push(nameView); + }, this); + + _.each(section.roles, function (role) { + var roleView = this.insertView('#items-roles', new Permissions.PermissionItem({ + item: role, + })); + this.roleViews.push(roleView); + }, this); + }, + + getItemFromView: function (viewList) { + return _.map(viewList, function (view) { + return view.item; + }); + }, + + discardRemovedViews: function () { + this.nameViews = _.filter(this.nameViews, function (view) { + return !view.removed; + }); + + this.roleViews = _.filter(this.roleViews, function (view) { + return !view.removed; + }); + }, + + items: function () { + this.discardRemovedViews(); + + return { + names: this.getItemFromView(this.nameViews), + roles: this.getItemFromView(this.roleViews) + }; + }, + + addItem: function (event) { + event.preventDefault(); + var $item = this.$(event.currentTarget).find('.item'), + value = $item.val(), + section = $item.data('section'), + type = $item.data('type'), + that = this; + + this.model.addItem(value, type, section); + this.model.save().then(function () { + that.render(); + FauxtonAPI.addNotification({ + msg: 'Database permissions has been updated.' + }); + }); + }, + + serialize: function () { + return { + section: this.section, + help: this.help + }; + } + + }); + + Permissions.PermissionItem = FauxtonAPI.View.extend({ + template: "addons/permissions/templates/item", + initialize: function (options) { + this.item = options.item; + this.viewsList = options.viewsList; + }, + + events: { + 'click .close': "removeItem" + }, + + removeItem: function (event) { + var that = this; + event.preventDefault(); + + this.removed = true; + Permissions.events.trigger('itemRemoved'); + + this.$el.hide('fast', function () { + that.remove(); + }); + }, + + + serialize: function () { + return { + item: this.item + }; + } + + }); + + return Permissions; +}); diff --git a/src/fauxton/settings.json.default b/src/fauxton/settings.json.default index 81cb4cbc3..30088f029 100644 --- a/src/fauxton/settings.json.default +++ b/src/fauxton/settings.json.default @@ -6,6 +6,7 @@ { "name": "stats" }, { "name": "replication" }, { "name": "contribute" }, + { "name": "permissions" }, { "name": "auth" } ], "template": { |