Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Replaces hasAccess with 'restricted', but exists() always returns tru…

…e. Works only in callback.
  • Loading branch information...
commit 8c79dc984891fccd3edcc470c3a9f2b8b93c08b5 1 parent 6c06fa1
Patrick Heneise authored
Showing with 23 additions and 36 deletions.
  1. +23 −36 lib/gandalf.js
View
59 lib/gandalf.js
@@ -1,8 +1,8 @@
-var util = require('util'),
- crypto = require('crypto'),
- redis = require('redis'),
- client = redis.createClient(),
- events = require('events');
+var util = require('util'),
+ crypto = require('crypto'),
+ redis = require('redis'),
+ client = redis.createClient(),
+ events = require('events');
var Gandalf = function() {
events.EventEmitter.call(this);
@@ -20,20 +20,27 @@ var Gandalf = function() {
self.finalize = next;
var hash = crypto.createHash('md5').update(req.url + req.method).digest('hex');
client.exists(hash, function(error, exists) {
+ console.log("Exists " + exists);
if(typeof req.user !== "undefined") {
if(req.user.admin == true) {
return next();
}
- }
- if(exists == 1) {
- if((new RegExp('^(' + req.user.capabilities.join('|') + ')$').test(hash))) {
- return next();
+ if(exists == 1) {
+ if((new RegExp('^(' + req.user.capabilities.join('|') + ')$').test(hash))) {
+ return next();
+ } else {
+ // YOU SHALL NOT PASS!
+ return next(new Error(423));
+ }
} else {
- // YOU SHALL NOT PASS!
- return next(new Error(423));
+ return next();
}
} else {
- return next();
+ if(exists == 1) {
+ return next(new Error(423));
+ } else {
+ return next();
+ }
}
});
};
@@ -42,33 +49,13 @@ var Gandalf = function() {
/**
* Provides a pre-check if a url/method is available. For navigation menus etc.
- */
- this.hasAccess = function(url, method, user) {
- var hash = crypto.createHash('md5').update(url + method).digest('hex');
- var exists = client.exists(hash);
- if(typeof user !== "undefined")
- if(user.admin == true)
- return true;
- else {
- if(exists == 1) {
- if((new RegExp('^(' + user.capabilities.join('|') + ')$').test(hash))) {
- return true;
- } else {
- // YOU SHALL NOT PASS!
- return false;
- }
- } else {
- return true;
- }
- }
- };
-
- /**
- * Provides a pre-check if a url/method is available. For navigation menus etc.
+ * Doesn't work properly yet. :(
*/
this.restricted = function(url, method) {
var hash = crypto.createHash('md5').update(url + method).digest('hex');
- var exists = client.exists(hash);
+ var exists = client.exists(hash, function(error, exists) {
+ return exists;
+ });
if(exists == 1) {
return true;
} else {
Please sign in to comment.
Something went wrong with that request. Please try again.