ee-soa-service
Advanced tools
Comparing version 0.2.14 to 0.2.15
@@ -22,2 +22,18 @@ | ||
, _permissionMap: { | ||
list: 'read' | ||
, listOne: 'read' | ||
, create: 'create' | ||
, createOrUpdate: 'create' | ||
, update: 'update' | ||
, updateRelation: 'update' | ||
, delete: 'delete' | ||
, deleteRelation: 'delete' | ||
, describe: 'describe' | ||
} | ||
, init: function(options, dirname) { | ||
@@ -30,10 +46,15 @@ this.options = options || {}; | ||
this._conf = { | ||
'tablesToLoad' : [] | ||
, 'controllersToLoad' : [] | ||
}; | ||
// load permissions management if its present | ||
if (options.permissionManager) this.permissionManager = options.permissionManager; | ||
if (options.accessToken) this.accessToken = options.accessToken; | ||
this._conf = { | ||
tablesToLoad: [] | ||
, controllersToLoad: [] | ||
}; | ||
this._controllerCollection = {}; | ||
this._middleware = []; | ||
this._middleware = []; | ||
@@ -105,3 +126,3 @@ // set name if not defined | ||
this._request(req, function(err, data, status, headers) { | ||
if(headers) { | ||
@@ -122,3 +143,3 @@ Object.keys(headers).forEach(function(headerKey) { | ||
if(debug) log('['+this.name+'] send data to response', data); | ||
if(debug) log('['+this.name+'] send data to response', data); | ||
@@ -144,2 +165,7 @@ res.send(status || res.statusCodes.OK, data); | ||
/** | ||
* handle requests after the middelwares were executed | ||
*/ | ||
, _request: function(req, callback) { | ||
@@ -156,25 +182,60 @@ var collection = req.getCollection(); | ||
var controllerAction = '_' + action + 'Action'; | ||
if(this[controllerAction]) { | ||
this[controllerAction](collection, req, callback); | ||
} | ||
else if(this._controllerCollection[collection][action]) | ||
{ | ||
this._controllerCollection[collection][action](req, function(err, result, status, headers) { | ||
this._handleResponse(err, result, req, callback, status, headers); | ||
// check permissions if the permissions manager | ||
// was loaded | ||
if (this.permissionManager) { | ||
// load the permissions | ||
this.permissionManager.getPermission(req.accessTokens || []).then(function(permission) { | ||
// check permissions, go through or return an error | ||
if (permission.isActionAllowed(collection, this._permissionMap[action] || action)) { | ||
// store the permissions object on the request | ||
req.permissions = permission; | ||
// go :) | ||
this._executeRequest(collection, action, req, callback); | ||
} | ||
else this._handleResponse(new Error('You are not allowed to access the controller «'+collection+'», action «'+action+'» on the service «'+this.name+'»!'), {}, req, callback, SOAResponse.statusCodes.ACCESS_UNAUTHORIZED); | ||
}.bind(this)).catch(function(err) { | ||
// failed to load the permissions | ||
callback(new Error('Failed to load the permissions for the controller «'+collection+'», action «'+action+'» on the service «'+this.name+'»: '+err)); | ||
}.bind(this)); | ||
} | ||
else { | ||
callback(new Error(action + " action not known")); | ||
} | ||
// permissions not implemented (i fuckin hope so :/) | ||
else this._executeRequest(collection, action, req, callback); | ||
} | ||
else { | ||
callback(new Error(collection + " not found in service " + this.name)); | ||
else callback(new Error(collection + " not found in service " + this.name)); | ||
} | ||
/** | ||
* execute the request after the permissions check | ||
*/ | ||
, _executeRequest: function(collection, action, req, callback) { | ||
var controllerAction = '_' + action + 'Action'; | ||
// it's not exactly known why this works like this works :/ | ||
if (this[controllerAction]) this[controllerAction](collection, req, callback); | ||
else if (this._controllerCollection[collection][action]) { | ||
this._controllerCollection[collection][action](req, function(err, result, status, headers) { | ||
this._handleResponse(err, result, req, callback, status, headers); | ||
}.bind(this)); | ||
} | ||
else callback(new Error(action + " action not known")); | ||
} | ||
, _handleRequest: function(req, callback) { | ||
var collection = req.getCollection(); | ||
// add my own accesstoken if the feature is loaded | ||
if (this.accessToken && req.accessTokens) req.accessTokens.push(this.accessToken); | ||
var res = new SOAResponse(); | ||
@@ -181,0 +242,0 @@ res.on('end', function(status, result) { |
@@ -5,3 +5,3 @@ { | ||
, "keywords" : ["ee", "soa", "service"] | ||
, "version" : "0.2.14" | ||
, "version" : "0.2.15" | ||
, "author": { | ||
@@ -8,0 +8,0 @@ "name" : "Tobias Kneubuehler" |
76178
1305