You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by ga...@apache.org on 2013/09/24 19:26:32 UTC
[1/2] git commit: updated refs/heads/1871-permissions to 7575287
Updated Branches:
refs/heads/1871-permissions [created] 75752877b
initial implementation
Project: http://git-wip-us.apache.org/repos/asf/couchdb/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb/commit/7386bd58
Tree: http://git-wip-us.apache.org/repos/asf/couchdb/tree/7386bd58
Diff: http://git-wip-us.apache.org/repos/asf/couchdb/diff/7386bd58
Branch: refs/heads/1871-permissions
Commit: 7386bd584c6e05fcebec6a822834fa31c21359f4
Parents: f714311
Author: Garren Smith <ga...@gmail.com>
Authored: Tue Sep 24 13:31:01 2013 +0200
Committer: Garren Smith <ga...@gmail.com>
Committed: Tue Sep 24 13:31:01 2013 +0200
----------------------------------------------------------------------
.gitignore | 1 +
src/fauxton/app/addons/permissions/base.js | 25 +++
src/fauxton/app/addons/permissions/resources.js | 57 ++++++
src/fauxton/app/addons/permissions/routes.js | 64 +++++++
.../app/addons/permissions/templates/item.html | 17 ++
.../permissions/templates/permissions.html | 15 ++
.../addons/permissions/templates/section.html | 37 ++++
src/fauxton/app/addons/permissions/views.js | 189 +++++++++++++++++++
src/fauxton/settings.json.default | 1 +
9 files changed, 406 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/couchdb/blob/7386bd58/.gitignore
----------------------------------------------------------------------
diff --git a/.gitignore b/.gitignore
index a4798c9..58f992e 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
http://git-wip-us.apache.org/repos/asf/couchdb/blob/7386bd58/src/fauxton/app/addons/permissions/base.js
----------------------------------------------------------------------
diff --git a/src/fauxton/app/addons/permissions/base.js b/src/fauxton/app/addons/permissions/base.js
new file mode 100644
index 0000000..016ba1c
--- /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;
+});
+
http://git-wip-us.apache.org/repos/asf/couchdb/blob/7386bd58/src/fauxton/app/addons/permissions/resources.js
----------------------------------------------------------------------
diff --git a/src/fauxton/app/addons/permissions/resources.js b/src/fauxton/app/addons/permissions/resources.js
new file mode 100644
index 0000000..6f4cb7d
--- /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;
+});
+
http://git-wip-us.apache.org/repos/asf/couchdb/blob/7386bd58/src/fauxton/app/addons/permissions/routes.js
----------------------------------------------------------------------
diff --git a/src/fauxton/app/addons/permissions/routes.js b/src/fauxton/app/addons/permissions/routes.js
new file mode 100644
index 0000000..f6c6587
--- /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;
+});
http://git-wip-us.apache.org/repos/asf/couchdb/blob/7386bd58/src/fauxton/app/addons/permissions/templates/item.html
----------------------------------------------------------------------
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 0000000..aa01d0b
--- /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>
http://git-wip-us.apache.org/repos/asf/couchdb/blob/7386bd58/src/fauxton/app/addons/permissions/templates/permissions.html
----------------------------------------------------------------------
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 0000000..d247425
--- /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>
http://git-wip-us.apache.org/repos/asf/couchdb/blob/7386bd58/src/fauxton/app/addons/permissions/templates/section.html
----------------------------------------------------------------------
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 0000000..102d857
--- /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>
http://git-wip-us.apache.org/repos/asf/couchdb/blob/7386bd58/src/fauxton/app/addons/permissions/views.js
----------------------------------------------------------------------
diff --git a/src/fauxton/app/addons/permissions/views.js b/src/fauxton/app/addons/permissions/views.js
new file mode 100644
index 0000000..3c5d3c2
--- /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;
+});
http://git-wip-us.apache.org/repos/asf/couchdb/blob/7386bd58/src/fauxton/settings.json.default
----------------------------------------------------------------------
diff --git a/src/fauxton/settings.json.default b/src/fauxton/settings.json.default
index 81cb4cb..30088f0 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": {
[2/2] git commit: updated refs/heads/1871-permissions to 7575287
Posted by ga...@apache.org.
added resource and some view tests
Project: http://git-wip-us.apache.org/repos/asf/couchdb/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb/commit/75752877
Tree: http://git-wip-us.apache.org/repos/asf/couchdb/tree/75752877
Diff: http://git-wip-us.apache.org/repos/asf/couchdb/diff/75752877
Branch: refs/heads/1871-permissions
Commit: 75752877bcd8ab3667f97a0de19ebce1e1e5ee4d
Parents: 7386bd5
Author: Garren Smith <ga...@gmail.com>
Authored: Tue Sep 24 19:26:14 2013 +0200
Committer: Garren Smith <ga...@gmail.com>
Committed: Tue Sep 24 19:26:14 2013 +0200
----------------------------------------------------------------------
src/fauxton/app/addons/permissions/resources.js | 4 +
.../addons/permissions/tests/resourceSpec.js | 51 +++++++++++++
.../app/addons/permissions/tests/viewsSpec.js | 79 ++++++++++++++++++++
3 files changed, 134 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/couchdb/blob/75752877/src/fauxton/app/addons/permissions/resources.js
----------------------------------------------------------------------
diff --git a/src/fauxton/app/addons/permissions/resources.js b/src/fauxton/app/addons/permissions/resources.js
index 6f4cb7d..5829971 100644
--- a/src/fauxton/app/addons/permissions/resources.js
+++ b/src/fauxton/app/addons/permissions/resources.js
@@ -46,6 +46,10 @@ function (app, FauxtonAPI ) {
addItem: function (value, type, section) {
var sectionValues = this.get(section);
+ if (!sectionValues || !sectionValues[type]) { return; }
+
+ if (sectionValues[type].indexOf(value) > -1) { return; }
+
sectionValues[type].push(value);
return this.set(section, sectionValues);
}
http://git-wip-us.apache.org/repos/asf/couchdb/blob/75752877/src/fauxton/app/addons/permissions/tests/resourceSpec.js
----------------------------------------------------------------------
diff --git a/src/fauxton/app/addons/permissions/tests/resourceSpec.js b/src/fauxton/app/addons/permissions/tests/resourceSpec.js
new file mode 100644
index 0000000..f73687a
--- /dev/null
+++ b/src/fauxton/app/addons/permissions/tests/resourceSpec.js
@@ -0,0 +1,51 @@
+// 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([
+ 'api',
+ 'addons/permissions/resources',
+ 'testUtils'
+], function (FauxtonAPI, Models, testUtils) {
+ var assert = testUtils.assert;
+
+ describe('Permissions', function () {
+
+ describe('#addItem', function () {
+ var security;
+
+ beforeEach(function () {
+ security = new Models.Security(null, {database: 'fakedb'});
+ });
+
+ it('Should add value to section', function () {
+
+ security.addItem('_user', 'names', 'admins');
+ assert.equal(security.get('admins').names[0], '_user');
+ });
+
+ it('Should handle incorrect type', function () {
+ security.addItem('_user', 'asdasd', 'admins');
+ });
+
+ it('Should handle incorrect section', function () {
+ security.addItem('_user', 'names', 'Asdasd');
+ });
+
+ it('Should reject duplicates', function () {
+ security.addItem('_user', 'names', 'admins');
+ security.addItem('_user', 'names', 'admins');
+ assert.equal(security.get('admins').names.length, 1);
+ });
+ });
+
+ });
+
+});
http://git-wip-us.apache.org/repos/asf/couchdb/blob/75752877/src/fauxton/app/addons/permissions/tests/viewsSpec.js
----------------------------------------------------------------------
diff --git a/src/fauxton/app/addons/permissions/tests/viewsSpec.js b/src/fauxton/app/addons/permissions/tests/viewsSpec.js
new file mode 100644
index 0000000..73afec2
--- /dev/null
+++ b/src/fauxton/app/addons/permissions/tests/viewsSpec.js
@@ -0,0 +1,79 @@
+// 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([
+ 'api',
+ 'addons/permissions/views',
+ 'addons/permissions/resources',
+ 'testUtils'
+], function (FauxtonAPI, Views, Models, testUtils) {
+ var assert = testUtils.assert,
+ ViewSandbox = testUtils.ViewSandbox;
+
+ describe('PermissionsSection', function () {
+ var section, security;
+
+ beforeEach(function () {
+ security = new Models.Security({'admins': {
+ 'names': ['_user'],
+ 'roles': []
+ }
+ }, {database: 'fakedb'});
+
+ section = new Views.PermissionSection({
+ section: 'admins'
+ });
+
+ viewSandbox = new ViewSandbox();
+ viewSandbox.renderView(section);
+ });
+
+ afterEach(function () {
+ viewSandbox.remove();
+ });
+
+
+ });
+
+ describe('PermissionItem', function () {
+ var item;
+
+ beforeEach(function () {
+ item = new Views.PermissionItem({
+ item: '_user'
+ });
+
+ viewSandbox = new ViewSandbox();
+ viewSandbox.renderView(item);
+ });
+
+ afterEach(function () {
+ viewSandbox.remove();
+ });
+
+ it('should trigger event on remove item', function () {
+ var eventSpy = sinon.spy();
+
+ Views.events.on('itemRemoved', eventSpy);
+
+ item.$('.close').click();
+
+ assert.ok(eventSpy.calledOnce);
+ });
+
+ it('should set removed to true', function () {
+ item.$('.close').click();
+
+ assert.ok(item.removed);
+ });
+ });
+
+});