Permalink
Browse files

Issue #19304 Initial stub code for restRouter.

  • Loading branch information...
1 parent c13c6cb commit a562b2ded0e649832c7009b99b014a81bb62a627 @bendiy committed Apr 8, 2013
View
8 node-datasource/main.js
@@ -323,6 +323,14 @@ app.post('/oauth/token', oauth2.token);
app.get('/api/userinfo', user.info);
+// TODO - May not need relations at all.
+app.all('/api/v1alpha1/:model/:id/:relation/:relid/*', routes.restRouter);
+app.all('/api/v1alpha1/:model/:id/:relation/*', routes.restRouter);
+
+app.all('/api/v1alpha1/:model/:id/*', routes.restRouter);
+app.all('/api/v1alpha1/:model/*', routes.restRouter);
+app.all('/api/v1alpha1/*', routes.restRouter);
+
app.get('/', routes.loginForm);
app.post('/login', routes.login);
app.get('/login/scope', routes.scopeForm);
View
17 node-datasource/oauth2/passport.js
@@ -175,16 +175,27 @@ passport.use(new BearerStrategy(
if (!user) { return done(null, false); }
var scopes = token.get("scope"),
- info = {};
+ sql = 'select orm_namespace, orm_type from xt.orm group by orm_namespace, orm_type;',
+ ormCallback = function (ormErr, orms) {
+ if (ormErr) { return done(ormErr); }
+
+ var info = {};
+
+ info = { scope: scopes, orm: orms.rows };
+ done(null, user, info);
+ };
try {
scopes = JSON.parse(scopes);
} catch (error) {
if (!Array.isArray(scopes)) { scopes = [ scopes ]; }
}
- info = { scope: scopes };
- done(null, user, info);
+ // Get ORM models for this org.
+// TODO - If there are multiple scopes, they should only relate to one org.
+// e.g. [dev.contact, dev.customer, dev.salesorder.readonly] can all be distilled to the "dev" org.
+// TODO - Extract a single org from the scopes.
+ X.database.query(scopes[0], sql, ormCallback);
});
} else {
return done(null, false);
View
90 node-datasource/routes/restRouter.js
@@ -0,0 +1,90 @@
+/*jshint node:true, indent:2, curly:false, eqeqeq:true, immed:true, latedef:true, newcap:true, noarg:true,
+regexp:true, undef:true, strict:true, trailing:true, white:true */
+/*global X:true */
+
+(function () {
+ "use strict";
+
+ exports.router = function (req, res, next) {
+ var id,
+ model,
+ relation,
+ relid;
+
+ // Get the model id from this req URI.
+ if (req.params.model && req.params.id) {
+ id = req.params.id;
+ }
+
+ // TODO - May not need relations at all.
+ // Get the relation id from this req URI.
+ if (req.params.model && req.params.id && req.params.relation && req.params.relid) {
+ relid = req.params.relid;
+ }
+
+ _.each(req.authInfo.orm, function (value, key, list) {
+ // Find the matching model from this req URI.
+ if (req.params.model && value && value.orm_namespace && value.orm_type
+ && req.params.model === value.orm_type.camelToHyphen()) {
+ model = value.orm_namespace + "." + value.orm_type;
+ }
+
+ // TODO - May not need relations at all.
+ // Find the matching relation model from this req URI.
+ if (req.params.model && req.params.id && req.params.relation && value && value.orm_namespace
+ && value.orm_type && req.params.relation === value.orm_type.camelToHyphen()) {
+ relation = value.orm_namespace + "." + value.orm_type;
+ }
+ });
+
+ if (!model) {
+ return next(new Error("Invalid REST Request."));
+ } else {
+ switch(req.method) {
+ case "DELETE":
+ // Deletes the specified resource.
+ // TODO - call delete method.
+ return res.send('REST API DELETE call to model: ' + model);
+ break;
+ case "GET":
+ // Requests a representation of the specified resource.
+ // TODO - call get method.
+ return res.send('REST API GET call to model: ' + model);
+ break;
+ case "HEAD":
+ // Asks for the response identical to the one that would correspond to a GET request, but without the response body.
+ // This is useful for retrieving meta-information written in response headers, without having to transport the entire content.
+ // TODO - call head method.
+ return res.send(); // HEAD doesn't send a body.
+ break;
+ case "OPTIONS":
+ // Returns the HTTP methods that the server supports for specified URL.
+ // This can be used to check the functionality of a web server by requesting '*' instead of a specific resource.
+ // TODO - call options method.
+ return res.send('REST API OPTIONS call to model: ' + model);
+ break;
+ case "PATCH":
+ // Is used to apply partial modifications to a resource.
+ // TODO - call patch method.
+ return res.send('REST API PATCH call to model: ' + model);
+ break;
+ case "POST":
+ // Requests that the server accept the entity enclosed in the request as a new subordinate of the web resource identified by the URI.
+ // TODO - call post method.
+ return res.send('REST API POST call to model: ' + model);
+ break;
+ case "PUT":
+ // Requests that the enclosed entity be stored under the supplied URI.
+ // TODO - call put method.
+ return res.send('REST API PUT call to model: ' + model);
+ break;
+ default:
+ return next(new Error("Invalid REST Request."));
+ }
+ }
+
+ //res.send('OAuth 2.0 Server');
+ //res.json({ id: req.user.id, name: req.user.get("properName"), scope: req.authInfo.scope });
+ };
+
+}());
View
9 node-datasource/routes/routes.js
@@ -34,9 +34,11 @@ regexp:true, undef:true, strict:true, trailing:true, white:true */
dataFromKey = require('./dataFromKey'),
file = require('./file'),
maintenance = require('./maintenance'),
+ passport = require('passport'),
redirector = require('./redirector'),
report = require('./report'),
resetPassword = require('./resetPassword'),
+ restRouter = require('./restRouter'),
syncUser = require('./syncUser');
//
@@ -51,7 +53,6 @@ regexp:true, undef:true, strict:true, trailing:true, white:true */
//
// Data-passthrough routes
//
- //
exports.commit = [ensureLogin, data.commit];
exports.commitEngine = data.commitEngine;
exports.fetch = [ensureLogin, data.fetch];
@@ -62,6 +63,12 @@ regexp:true, undef:true, strict:true, trailing:true, white:true */
exports.retrieveEngine = data.retrieveEngine;
//
+ // REST API Routes
+ exports.restRouter = [
+ passport.authenticate('bearer', { session: false }),
+ restRouter.router];
+
+ //
// Custom routes
//
exports.changePassword = [ensureLogin, changePassword.changePassword];

0 comments on commit a562b2d

Please sign in to comment.