Add authentication plugin and example module
Project: http://git-wip-us.apache.org/repos/asf/couchdb/repo Commit: http://git-wip-us.apache.org/repos/asf/couchdb/commit/ad55290b Tree: http://git-wip-us.apache.org/repos/asf/couchdb/tree/ad55290b Diff: http://git-wip-us.apache.org/repos/asf/couchdb/diff/ad55290b Branch: refs/heads/route-events Commit: ad55290bf874fe8eafaf02c25d1e2292e2824d50 Parents: 19a3456 Author: Garren Smith <[email protected]> Authored: Wed May 8 17:11:42 2013 +0200 Committer: Garren Smith <[email protected]> Committed: Thu May 9 10:01:51 2013 +0200 ---------------------------------------------------------------------- src/fauxton/app/api.js | 61 +++++++++++++++++++++- src/fauxton/app/modules/documents/routes.js | 7 ++- src/fauxton/app/router.js | 14 ++++- 3 files changed, 75 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/couchdb/blob/ad55290b/src/fauxton/app/api.js ---------------------------------------------------------------------- diff --git a/src/fauxton/app/api.js b/src/fauxton/app/api.js index 1c0b8f8..b50bc02 100644 --- a/src/fauxton/app/api.js +++ b/src/fauxton/app/api.js @@ -108,6 +108,48 @@ function(app, Fauxton) { } }); + // This is not exposed externally as it should not need to be accessed or overridden + var Auth = function (options) { + this._options = options; + this.initialize.apply(this, arguments); + }; + + // Piggy-back on Backbone's self-propagating extend function, + Auth.extend = Backbone.Model.extend; + + _.extend(Auth.prototype, Backbone.Events, { + initialize: function() {}, + authDeniedCb: undefined, + + authHandlerCb : function (roles, layout) { + var deferred = $.Deferred(); + deferred.resolve(); + return deferred; + }, + + registerAuth: function (authHandlerCb) { + this.authHandlerCb = authHandlerCb; + }, + + registerAuthDenied: function (authDeniedCb) { + this.authDeniedCb = authDeniedCb; + }, + + checkAccess: function (roles, layout) { + var requiredRoles = roles || [], + authDeniedCb = this.authDeniedCb, + promise = $.when.apply(null, this.authHandlerCb(requiredRoles, layout)); + + if (authDeniedCb) { + promise.fail(function () { authDeniedCb(layout);}); + } + + return promise; + } + }); + + FauxtonAPI.auth = new Auth(); + FauxtonAPI.RouteObject = function(options) { this._options = options; @@ -133,6 +175,7 @@ function(app, Fauxton) { renderedState: false, establish: function() {}, route: function() {}, + roles: [], initialize: function() {} }, { @@ -248,7 +291,23 @@ function(app, Fauxton) { routeCallback: function (route) { var routes = this.get('routes'); - return this[routes[route]]; + var routeObj = routes[route]; + + if (typeof routeObj === 'object') { + return this[routeObj.route]; + } else { + return this[routeObj]; + } + }, + + getRouteRoles: function (routeUrl) { + var route = this.get('routes')[routeUrl]; + + if ((typeof route === 'object') && route.roles) { + return route.roles; + } + + return this.roles; } }); http://git-wip-us.apache.org/repos/asf/couchdb/blob/ad55290b/src/fauxton/app/modules/documents/routes.js ---------------------------------------------------------------------- diff --git a/src/fauxton/app/modules/documents/routes.js b/src/fauxton/app/modules/documents/routes.js index 103d904..e75601a 100644 --- a/src/fauxton/app/modules/documents/routes.js +++ b/src/fauxton/app/modules/documents/routes.js @@ -173,7 +173,10 @@ function(app, FauxtonAPI, Documents, Databases) { routes: { "database/:database/_all_docs(:extra)": "allDocs", - "database/:database/_design/:ddoc/_view/:view": "viewFn", + "database/:database/_design/:ddoc/_view/:view": { + route: "viewFn", + roles: ['admin'] + }, "database/:database/new_view": "newViewEditor" }, @@ -281,8 +284,6 @@ function(app, FauxtonAPI, Documents, Databases) { }); - - var ChangesRouteObject = FauxtonAPI.RouteObject.extend({ layout: "with_tabs", http://git-wip-us.apache.org/repos/asf/couchdb/blob/ad55290b/src/fauxton/app/router.js ---------------------------------------------------------------------- diff --git a/src/fauxton/app/router.js b/src/fauxton/app/router.js index 45cc8b2..6c417ed 100644 --- a/src/fauxton/app/router.js +++ b/src/fauxton/app/router.js @@ -64,10 +64,16 @@ function(req, app, Initialize, FauxtonAPI, Fauxton, Layout, Databases, Documents } var routeObject = self.activeRouteObject, - routeCallback = routeObject.routeCallback(route); + routeCallback = routeObject.routeCallback(route), + roles = routeObject.getRouteRoles(route); + + var authPromise = app.auth.checkAccess(roles, masterLayout); + + authPromise.then(function () { + routeCallback.apply(routeObject, args); + routeObject.render(route, masterLayout, args); + }); - routeCallback.apply(routeObject, args); - routeObject.render(route, masterLayout, args); }); }, this); }, @@ -105,9 +111,11 @@ function(req, app, Initialize, FauxtonAPI, Fauxton, Layout, Databases, Documents }, initialize: function() { + console.log('HI ROUTS'); //TODO: It would be nice to handle this with a router this.navBar = app.navBar = new Fauxton.NavBar(); this.apiBar = app.apiBar = new Fauxton.ApiBar(); + this.auth = app.auth = FauxtonAPI.auth; app.masterLayout = this.masterLayout = new Layout(this.navBar, this.apiBar); app.footer = new Fauxton.Footer({el: "#footer-content"});
