2014-03-27 09:41:18 +00:00
// Generated by CoffeeScript 1.6.3
2013-11-20 23:20:06 +00:00
/ *
2014-02-13 17:16:03 +00:00
Persistence
2013-11-20 23:20:06 +00:00
=== === === ===
2013-11-26 22:24:15 +00:00
> Handles the connection to the database and provides functionalities for
2014-02-10 21:28:10 +00:00
> event pollers , action invokers , rules and the encrypted storing of authentication tokens .
2013-11-26 22:24:15 +00:00
> General functionality as a wrapper for the module holds initialization ,
> encryption / decryption , the retrieval of modules and shut down .
>
> The general structure for linked data is that the key is stored in a set .
> By fetching all set entries we can then fetch all elements , which is
> automated in this function .
2014-02-10 21:28:10 +00:00
> For example , modules of the same group , e . g . action invokers are registered in an
2013-11-26 22:24:15 +00:00
> unordered set in the database , from where they can be retrieved again . For example
2014-02-10 21:28:10 +00:00
> a new action invoker has its ID ( e . g 'probinder' ) first registered in the set
> 'action-invokers' and then stored in the db with the key 'action-invoker:' + ID
> ( e . g . action - invoker : probinder ) .
2013-11-20 23:20:06 +00:00
>
2014-03-27 09:41:18 +00:00
* /
2013-11-20 23:20:06 +00:00
( function ( ) {
2014-03-23 21:52:16 +00:00
var IndexedModules , crypto , crypto _key , decrypt , encrypt , exports , getSetRecords , hash , redis , replyHandler ,
2014-03-27 09:41:18 +00:00
_this = this ,
2014-02-10 21:28:10 +00:00
_ _bind = function ( fn , me ) { return function ( ) { return fn . apply ( me , arguments ) ; } ; } ;
2013-11-20 23:20:06 +00:00
2013-11-28 15:05:47 +00:00
crypto = require ( 'crypto-js' ) ;
2013-11-20 23:20:06 +00:00
2013-11-28 15:05:47 +00:00
redis = require ( 'redis' ) ;
2013-11-20 23:20:06 +00:00
2014-03-23 21:52:16 +00:00
crypto _key = "}f6y1y}B{.an$}2c$Yl.$mSnF\\HX149u*y8C:@kmN/520Gt\\v'+KFBnQ!\\r<>5X/xRI`sT<Iw;:DPV;4gy:qf]Zq{\"6sgK{,}^\"!]O;qBM3G?]h_`Psw=b6bVXKXry7*" ;
2013-11-20 23:20:06 +00:00
/ *
2013-11-26 22:24:15 +00:00
Module call
-- -- -- -- -- -
2014-02-19 13:14:08 +00:00
Initializes the DB connection with the given ` db-port ` property in the ` args ` object .
2013-11-26 22:24:15 +00:00
2013-11-20 23:20:06 +00:00
@ param { Object } args
2014-03-27 09:41:18 +00:00
* /
2014-03-22 22:07:13 +00:00
2014-03-27 09:41:18 +00:00
exports = module . exports = function ( args ) {
if ( ! _this . db ) {
if ( ! args [ 'db-port' ] ) {
args [ 'db-port' ] = 6379 ;
2014-03-20 08:17:31 +00:00
}
2014-03-27 09:41:18 +00:00
_this . log = args . logger ;
exports . eventPollers = new IndexedModules ( 'event-poller' , _this . log ) ;
exports . actionInvokers = new IndexedModules ( 'action-invoker' , _this . log ) ;
return exports . initPort ( args [ 'db-port' ] ) ;
}
} ;
2014-03-22 22:07:13 +00:00
2014-03-27 09:41:18 +00:00
exports . initPort = function ( port ) {
var _ref ;
_this . connRefused = false ;
if ( ( _ref = _this . db ) != null ) {
_ref . quit ( ) ;
}
_this . db = redis . createClient ( port , 'localhost' , {
connect _timeout : 2000
} ) ;
_this . db . on ( 'error' , function ( err ) {
if ( err . message . indexOf ( 'ECONNREFUSED' ) > - 1 ) {
_this . connRefused = true ;
return _this . log . error ( err , 'DB | Wrong port?' ) ;
}
} ) ;
exports . eventPollers . setDB ( _this . db ) ;
return exports . actionInvokers . setDB ( _this . db ) ;
} ;
2013-11-20 23:20:06 +00:00
/ *
2013-11-21 21:07:39 +00:00
Checks whether the db is connected and passes either an error on failure after
ten attempts within five seconds , or nothing on success to the callback ( err ) .
2013-11-19 13:53:36 +00:00
2013-11-20 23:20:06 +00:00
@ public isConnected ( * cb * )
@ param { function } cb
2014-03-27 09:41:18 +00:00
* /
2014-03-22 22:07:13 +00:00
2014-03-27 09:41:18 +00:00
exports . isConnected = function ( cb ) {
var fCheckConnection , numAttempts ;
if ( ! _this . db ) {
return cb ( new Error ( 'DB | DB initialization did not occur or failed miserably!' ) ) ;
} else {
if ( _this . db . connected ) {
return cb ( ) ;
2014-03-22 22:07:13 +00:00
} else {
2014-03-27 09:41:18 +00:00
numAttempts = 0 ;
fCheckConnection = function ( ) {
2014-04-01 21:31:54 +00:00
var _ref ;
2014-03-27 09:41:18 +00:00
if ( _this . connRefused ) {
2014-04-01 21:31:54 +00:00
if ( ( _ref = _this . db ) != null ) {
_ref . quit ( ) ;
}
2014-03-27 09:41:18 +00:00
return cb ( new Error ( 'DB | Connection refused! Wrong port?' ) ) ;
} else {
if ( _this . db . connected ) {
_this . log . info ( 'DB | Successfully connected to DB!' ) ;
return cb ( ) ;
} else if ( numAttempts ++ < 10 ) {
return setTimeout ( fCheckConnection , 100 ) ;
2014-03-22 22:07:13 +00:00
} else {
2014-03-27 09:41:18 +00:00
return cb ( new Error ( 'DB | Connection to DB failed!' ) ) ;
2014-03-22 22:07:13 +00:00
}
2014-03-27 09:41:18 +00:00
}
} ;
return setTimeout ( fCheckConnection , 100 ) ;
2014-03-22 22:07:13 +00:00
}
2014-03-27 09:41:18 +00:00
}
} ;
2013-11-20 23:20:06 +00:00
2014-02-04 07:35:07 +00:00
/ *
Abstracts logging for simple action replies from the DB .
@ private replyHandler ( * action * )
@ param { String } action
2014-03-27 09:41:18 +00:00
* /
2014-03-19 22:28:59 +00:00
2014-03-22 22:07:13 +00:00
2014-03-27 09:41:18 +00:00
replyHandler = function ( action ) {
return function ( err , reply ) {
if ( err ) {
return _this . log . warn ( err , "during '" + action + "'" ) ;
} else {
return _this . log . info ( "DB | " + action + ": " + reply ) ;
}
} ;
} ;
2014-02-04 07:35:07 +00:00
2013-11-28 18:14:05 +00:00
/ *
Push an event into the event queue .
2014-02-04 15:40:32 +00:00
@ public pushEvent ( * oEvent * )
@ param { Object } oEvent
2014-03-27 09:41:18 +00:00
* /
2013-11-28 18:14:05 +00:00
2014-03-27 09:41:18 +00:00
exports . pushEvent = function ( oEvent ) {
if ( oEvent ) {
_this . log . info ( "DB | Event pushed into the queue: '" + oEvent . eventid + "'" ) ;
return _this . db . rpush ( 'event_queue' , JSON . stringify ( oEvent ) ) ;
} else {
return _this . log . warn ( 'DB | Why would you give me an empty event...' ) ;
}
} ;
2013-11-28 18:14:05 +00:00
/ *
2014-02-10 21:28:10 +00:00
Pop an event from the event queue and pass it to cb ( err , obj ) .
2013-11-28 18:14:05 +00:00
@ public popEvent ( * cb * )
@ param { function } cb
2014-03-27 09:41:18 +00:00
* /
exports . popEvent = function ( cb ) {
var makeObj ;
makeObj = function ( pcb ) {
return function ( err , obj ) {
return pcb ( err , JSON . parse ( obj ) ) ;
2014-02-04 07:35:07 +00:00
} ;
} ;
2014-03-27 09:41:18 +00:00
return _this . db . lpop ( 'event_queue' , makeObj ( cb ) ) ;
} ;
2014-02-04 07:35:07 +00:00
/ *
Purge the event queue .
@ public purgeEventQueue ( )
2014-03-27 09:41:18 +00:00
* /
2014-02-04 07:35:07 +00:00
2014-03-27 09:41:18 +00:00
exports . purgeEventQueue = function ( ) {
return _this . db . del ( 'event_queue' , replyHandler ( 'purging event queue' ) ) ;
} ;
2013-11-28 18:14:05 +00:00
2013-11-28 15:05:47 +00:00
/ *
Hashes a string based on SHA - 3 - 512.
@ private hash ( * plainText * )
@ param { String } plainText
2014-03-27 09:41:18 +00:00
* /
2013-11-28 15:05:47 +00:00
2014-03-27 09:41:18 +00:00
hash = function ( plainText ) {
var err ;
if ( plainText == null ) {
return null ;
}
try {
return ( crypto . SHA3 ( plainText , {
outputLength : 512
} ) ) . toString ( ) ;
} catch ( _error ) {
err = _error ;
_this . log . warn ( err , 'DB | during hashing' ) ;
return null ;
}
} ;
2013-11-28 15:05:47 +00:00
2013-11-20 23:20:06 +00:00
/ *
Encrypts a string using the crypto key from the config file , based on aes - 256 - cbc .
@ private encrypt ( * plainText * )
@ param { String } plainText
2014-03-27 09:41:18 +00:00
* /
2013-11-20 23:20:06 +00:00
2014-03-27 09:41:18 +00:00
encrypt = function ( plainText ) {
var err ;
if ( plainText == null ) {
return null ;
}
try {
return crypto . AES . encrypt ( plainText , crypto _key ) ;
} catch ( _error ) {
err = _error ;
_this . log . warn ( err , 'DB | during encryption' ) ;
return null ;
}
} ;
2013-11-20 23:20:06 +00:00
/ *
Decrypts an encrypted string and hands it back on success or null .
@ private decrypt ( * crypticText * )
@ param { String } crypticText
2014-03-27 09:41:18 +00:00
* /
2013-11-20 23:20:06 +00:00
2014-03-27 09:41:18 +00:00
decrypt = function ( crypticText ) {
var dec , err ;
if ( crypticText == null ) {
return null ;
}
try {
dec = crypto . AES . decrypt ( crypticText , crypto _key ) ;
return dec . toString ( crypto . enc . Utf8 ) ;
} catch ( _error ) {
err = _error ;
_this . log . warn ( err , 'DB | during decryption' ) ;
return null ;
}
} ;
2013-11-20 23:20:06 +00:00
/ *
2014-02-10 21:28:10 +00:00
Fetches all linked data set keys from a linking set , fetches the single
data objects via the provided function and returns the results to cb ( err , obj ) .
2013-11-20 23:20:06 +00:00
@ private getSetRecords ( * set , fSingle , cb * )
@ param { String } set the set name how it is stored in the DB
2014-02-10 21:28:10 +00:00
@ param { function } fSingle a function to retrieve a single data element
per set entry
@ param { function } cb the callback ( err , obj ) function that receives all
the retrieved data or an error
2014-03-27 09:41:18 +00:00
* /
getSetRecords = function ( set , fSingle , cb ) {
_this . log . info ( "DB | Fetching set records: '" + set + "'" ) ;
return _this . db . smembers ( set , function ( err , arrReply ) {
var fCallback , objReplies , reply , semaphore , _i , _len , _results ;
if ( err ) {
_this . log . warn ( err , "DB | fetching '" + set + "'" ) ;
return cb ( err ) ;
} else if ( arrReply . length === 0 ) {
return cb ( ) ;
} else {
semaphore = arrReply . length ;
objReplies = { } ;
setTimeout ( function ( ) {
if ( semaphore > 0 ) {
return cb ( new Error ( "Timeout fetching '" + set + "'" ) ) ;
}
} , 2000 ) ;
fCallback = function ( prop ) {
return function ( err , data ) {
-- semaphore ;
if ( err ) {
_this . log . warn ( err , "DB | fetching single element: '" + prop + "'" ) ;
} else if ( ! data ) {
_this . log . warn ( new Error ( "Empty key in DB: '" + prop + "'" ) ) ;
} else {
objReplies [ prop ] = data ;
}
if ( semaphore === 0 ) {
return cb ( null , objReplies ) ;
2013-11-20 23:20:06 +00:00
}
} ;
2014-03-27 09:41:18 +00:00
} ;
_results = [ ] ;
for ( _i = 0 , _len = arrReply . length ; _i < _len ; _i ++ ) {
reply = arrReply [ _i ] ;
_results . push ( fSingle ( reply , fCallback ( reply ) ) ) ;
2013-11-19 13:53:36 +00:00
}
2014-03-27 09:41:18 +00:00
return _results ;
}
} ) ;
} ;
2013-11-20 23:20:06 +00:00
2014-02-10 21:28:10 +00:00
IndexedModules = ( function ( ) {
2014-03-23 21:52:16 +00:00
function IndexedModules ( setname , log ) {
2014-02-10 21:28:10 +00:00
this . setname = setname ;
2014-02-18 21:34:36 +00:00
this . log = log ;
2014-03-20 12:24:09 +00:00
this . deleteUserParams = _ _bind ( this . deleteUserParams , this ) ;
this . getUserParamsIds = _ _bind ( this . getUserParamsIds , this ) ;
this . getUserParams = _ _bind ( this . getUserParams , this ) ;
this . storeUserParams = _ _bind ( this . storeUserParams , this ) ;
2014-02-10 21:28:10 +00:00
this . deleteModule = _ _bind ( this . deleteModule , this ) ;
this . getModules = _ _bind ( this . getModules , this ) ;
this . getModuleIds = _ _bind ( this . getModuleIds , this ) ;
2014-02-23 23:59:49 +00:00
this . getAvailableModuleIds = _ _bind ( this . getAvailableModuleIds , this ) ;
this . getModuleParams = _ _bind ( this . getModuleParams , this ) ;
2014-02-10 21:28:10 +00:00
this . getModule = _ _bind ( this . getModule , this ) ;
2014-02-23 23:59:49 +00:00
this . unpublish = _ _bind ( this . unpublish , this ) ;
this . publish = _ _bind ( this . publish , this ) ;
this . unlinkModule = _ _bind ( this . unlinkModule , this ) ;
this . linkModule = _ _bind ( this . linkModule , this ) ;
2014-02-10 21:28:10 +00:00
this . storeModule = _ _bind ( this . storeModule , this ) ;
2014-03-23 21:52:16 +00:00
this . log . info ( "DB | (IdxedMods) Instantiated indexed modules for '" + this . setname + "'" ) ;
2014-02-10 21:28:10 +00:00
}
2014-03-23 21:52:16 +00:00
IndexedModules . prototype . setDB = function ( db ) {
this . db = db ;
return this . log . info ( "DB | (IdxedMods) Registered new DB connection for '" + this . setname + "'" ) ;
} ;
2014-03-24 11:15:49 +00:00
/ *
2014-04-02 13:54:20 +00:00
Stores a module and links it to the user .
@ private storeModule ( * userId , oModule * )
2014-03-24 11:15:49 +00:00
@ param { String } userId
2014-04-02 13:54:20 +00:00
@ param { object } oModule
2014-03-27 09:41:18 +00:00
* /
2014-03-24 11:15:49 +00:00
2014-04-02 13:54:20 +00:00
IndexedModules . prototype . storeModule = function ( userId , oModule ) {
this . log . info ( "DB | (IdxedMods) " + this . setname + ".storeModule( " + userId + ", oModule )" ) ;
this . db . sadd ( "" + this . setname + "s" , oModule . id , replyHandler ( "sadd '" + oModule . id + "' to '" + this . setname + "'" ) ) ;
this . db . hmset ( "" + this . setname + ":" + oModule . id , oModule , replyHandler ( "hmset properties in hash '" + this . setname + ":" + oModule . id + "'" ) ) ;
return this . linkModule ( oModule . id , userId ) ;
2014-02-23 23:59:49 +00:00
} ;
IndexedModules . prototype . linkModule = function ( mId , userId ) {
2014-03-23 21:52:16 +00:00
this . log . info ( "DB | (IdxedMods) " + this . setname + ".linkModule( " + mId + ", " + userId + " )" ) ;
2014-03-22 22:07:13 +00:00
this . db . sadd ( "" + this . setname + ":" + mId + ":users" , userId , replyHandler ( "sadd " + userId + " to '" + this . setname + ":" + mId + ":users'" ) ) ;
return this . db . sadd ( "user:" + userId + ":" + this . setname + "s" , mId , replyHandler ( "sadd " + mId + " to 'user:" + userId + ":" + this . setname + "s'" ) ) ;
2014-02-23 23:59:49 +00:00
} ;
IndexedModules . prototype . unlinkModule = function ( mId , userId ) {
2014-03-23 21:52:16 +00:00
this . log . info ( "DB | (IdxedMods) " + this . setname + ".unlinkModule( " + mId + ", " + userId + " )" ) ;
2014-03-22 22:07:13 +00:00
this . db . srem ( "" + this . setname + ":" + mId + ":users" , userId , replyHandler ( "srem " + userId + " from '" + this . setname + ":" + mId + ":users'" ) ) ;
return this . db . srem ( "user:" + userId + ":" + this . setname + "s" , mId , replyHandler ( "srem " + mId + " from 'user:" + userId + ":" + this . setname + "s'" ) ) ;
2014-02-23 23:59:49 +00:00
} ;
IndexedModules . prototype . publish = function ( mId ) {
2014-03-23 21:52:16 +00:00
this . log . info ( "DB | (IdxedMods) " + this . setname + ".publish( " + mId + " )" ) ;
2014-03-22 22:07:13 +00:00
return this . db . sadd ( "public-" + this . setname + "s" , mId , replyHandler ( "sadd '" + mId + "' to 'public-" + this . setname + "s'" ) ) ;
2014-02-23 23:59:49 +00:00
} ;
IndexedModules . prototype . unpublish = function ( mId ) {
2014-03-23 21:52:16 +00:00
this . log . info ( "DB | (IdxedMods) " + this . setname + ".unpublish( " + mId + " )" ) ;
2014-03-22 22:07:13 +00:00
return this . db . srem ( "public-" + this . setname + "s" , mId , replyHandler ( "srem '" + mId + "' from 'public-" + this . setname + "s'" ) ) ;
2014-02-10 21:28:10 +00:00
} ;
IndexedModules . prototype . getModule = function ( mId , cb ) {
2014-03-23 21:52:16 +00:00
this . log . info ( "DB | (IdxedMods) " + this . setname + ".getModule( " + mId + " )" ) ;
2014-02-23 23:59:49 +00:00
return this . db . hgetall ( "" + this . setname + ":" + mId , cb ) ;
} ;
IndexedModules . prototype . getModuleParams = function ( mId , cb ) {
2014-03-23 21:52:16 +00:00
this . log . info ( "DB | (IdxedMods) " + this . setname + ".getModule( " + mId + " )" ) ;
2014-02-23 23:59:49 +00:00
return this . db . hget ( "" + this . setname + ":" + mId , "params" , cb ) ;
} ;
IndexedModules . prototype . getAvailableModuleIds = function ( userId , cb ) {
2014-03-24 11:15:49 +00:00
this . log . info ( "DB | (IdxedMods) " + this . setname + ".getPublicModuleIds( " + userId + " )" ) ;
2014-02-23 23:59:49 +00:00
return this . db . sunion ( "public-" + this . setname + "s" , "user:" + userId + ":" + this . setname + "s" , cb ) ;
2014-02-10 21:28:10 +00:00
} ;
IndexedModules . prototype . getModuleIds = function ( cb ) {
2014-03-23 21:52:16 +00:00
this . log . info ( "DB | (IdxedMods) " + this . setname + ".getModuleIds()" ) ;
2014-02-10 21:28:10 +00:00
return this . db . smembers ( "" + this . setname + "s" , cb ) ;
} ;
IndexedModules . prototype . getModules = function ( cb ) {
2014-03-23 21:52:16 +00:00
this . log . info ( "DB | (IdxedMods) " + this . setname + ".getModules()" ) ;
2014-02-10 21:28:10 +00:00
return getSetRecords ( "" + this . setname + "s" , this . getModule , cb ) ;
} ;
IndexedModules . prototype . deleteModule = function ( mId ) {
2014-03-27 09:41:18 +00:00
var _this = this ;
2014-03-23 21:52:16 +00:00
this . log . info ( "DB | (IdxedMods) " + this . setname + ".deleteModule( " + mId + " )" ) ;
2014-03-22 22:07:13 +00:00
this . db . srem ( "" + this . setname + "s" , mId , replyHandler ( "srem '" + mId + "' from " + this . setname + "s" ) ) ;
this . db . del ( "" + this . setname + ":" + mId , replyHandler ( "del of '" + this . setname + ":" + mId + "'" ) ) ;
2014-03-27 09:41:18 +00:00
return this . db . smembers ( "" + this . setname + ":" + mId + ":users" , function ( err , obj ) {
var userId , _i , _len , _results ;
_results = [ ] ;
for ( _i = 0 , _len = obj . length ; _i < _len ; _i ++ ) {
userId = obj [ _i ] ;
_results . push ( _this . unlinkModule ( mId , userId ) ) ;
}
return _results ;
} ) ;
2014-02-10 21:28:10 +00:00
} ;
2014-03-20 12:24:09 +00:00
IndexedModules . prototype . storeUserParams = function ( mId , userId , data ) {
2014-03-23 21:52:16 +00:00
this . log . info ( "DB | (IdxedMods) " + this . setname + ".storeUserParams( " + mId + ", " + userId + ", data )" ) ;
2014-03-22 22:07:13 +00:00
this . db . sadd ( "" + this . setname + "-params" , "" + mId + ":" + userId , replyHandler ( "sadd '" + mId + ":" + userId + "' to '" + this . setname + "-params'" ) ) ;
return this . db . set ( "" + this . setname + "-params:" + mId + ":" + userId , encrypt ( data ) , replyHandler ( "set user params in '" + this . setname + "-params:" + mId + ":" + userId + "'" ) ) ;
2014-02-10 21:28:10 +00:00
} ;
2014-03-20 12:24:09 +00:00
IndexedModules . prototype . getUserParams = function ( mId , userId , cb ) {
2014-03-23 21:52:16 +00:00
this . log . info ( "DB | (IdxedMods) " + this . setname + ".getUserParams( " + mId + ", " + userId + " )" ) ;
2014-02-10 21:28:10 +00:00
return this . db . get ( "" + this . setname + "-params:" + mId + ":" + userId , function ( err , data ) {
return cb ( err , decrypt ( data ) ) ;
} ) ;
} ;
2014-03-20 12:24:09 +00:00
IndexedModules . prototype . getUserParamsIds = function ( cb ) {
2014-03-23 21:52:16 +00:00
this . log . info ( "DB | (IdxedMods) " + this . setname + ".getUserParamsIds()" ) ;
2014-02-10 21:28:10 +00:00
return this . db . smembers ( "" + this . setname + "-params" , cb ) ;
} ;
2014-03-20 12:24:09 +00:00
IndexedModules . prototype . deleteUserParams = function ( mId , userId ) {
2014-03-23 21:52:16 +00:00
this . log . info ( "DB | (IdxedMods) " + this . setname + ".deleteUserParams(" + mId + ", " + userId + " )" ) ;
2014-03-22 22:07:13 +00:00
this . db . srem ( "" + this . setname + "-params" , "" + mId + ":" + userId , replyHandler ( "srem '" + mId + ":" + userId + "' from '" + this . setname + "-params'" ) ) ;
return this . db . del ( "" + this . setname + "-params:" + mId + ":" + userId , replyHandler ( "del '" + this . setname + "-params:" + mId + ":" + userId + "'" ) ) ;
2014-02-10 21:28:10 +00:00
} ;
return IndexedModules ;
} ) ( ) ;
2013-11-20 23:20:06 +00:00
/ *
2014-03-27 09:41:18 +00:00
# # Rules
* /
2014-02-10 21:28:10 +00:00
/ *
Query the DB for a rule and pass it to cb ( err , obj ) .
2013-11-20 23:20:06 +00:00
2014-02-04 15:40:32 +00:00
@ public getRule ( * ruleId , cb * )
@ param { String } ruleId
2013-11-21 21:07:39 +00:00
@ param { function } cb
2014-03-27 09:41:18 +00:00
* /
2013-11-20 23:20:06 +00:00
2014-03-27 09:41:18 +00:00
exports . getRule = function ( ruleId , cb ) {
_this . log . info ( "DB | getRule: '" + ruleId + "'" ) ;
return _this . db . get ( "rule:" + ruleId , cb ) ;
} ;
2013-11-20 23:20:06 +00:00
/ *
2014-02-10 21:28:10 +00:00
Fetch all rules and pass them to cb ( err , obj ) .
2013-11-20 23:20:06 +00:00
2013-11-21 21:07:39 +00:00
@ public getRules ( * cb * )
2013-11-20 23:20:06 +00:00
@ param { function } cb
2014-03-27 09:41:18 +00:00
* /
2013-11-20 23:20:06 +00:00
2014-03-27 09:41:18 +00:00
exports . getRules = function ( cb ) {
_this . log . info ( 'DB | Fetching all Rules' ) ;
return getSetRecords ( 'rules' , exports . getRule , cb ) ;
} ;
2013-11-20 23:20:06 +00:00
2014-02-10 21:28:10 +00:00
/ *
Fetch all rule IDs and hand it to cb ( err , obj ) .
@ public getRuleIds ( * cb * )
@ param { function } cb
2014-03-27 09:41:18 +00:00
* /
2014-02-10 21:28:10 +00:00
2014-03-27 09:41:18 +00:00
exports . getRuleIds = function ( cb ) {
_this . log . info ( 'DB | Fetching all Rule IDs' ) ;
return _this . db . smembers ( 'rules' , cb ) ;
} ;
2014-02-10 21:28:10 +00:00
/ *
Store a string representation of a rule in the DB .
@ public storeRule ( * ruleId , data * )
@ param { String } ruleId
@ param { String } data
2014-03-27 09:41:18 +00:00
* /
2014-03-22 22:07:13 +00:00
2014-02-10 21:28:10 +00:00
2014-03-27 09:41:18 +00:00
exports . storeRule = function ( ruleId , data ) {
_this . log . info ( "DB | storeRule: '" + ruleId + "'" ) ;
_this . db . sadd ( 'rules' , "" + ruleId , replyHandler ( "storing rule key '" + ruleId + "'" ) ) ;
return _this . db . set ( "rule:" + ruleId , data , replyHandler ( "storing rule '" + ruleId + "'" ) ) ;
} ;
2014-02-10 21:28:10 +00:00
/ *
Delete a string representation of a rule .
@ public deleteRule ( * ruleId , userId * )
@ param { String } ruleId
@ param { String } userId
2014-03-27 09:41:18 +00:00
* /
2014-02-10 21:28:10 +00:00
2014-03-27 09:41:18 +00:00
exports . deleteRule = function ( ruleId ) {
_this . log . info ( "DB | deleteRule: '" + ruleId + "'" ) ;
_this . db . srem ( "rules" , ruleId , replyHandler ( "Deleting rule key '" + ruleId + "'" ) ) ;
_this . db . del ( "rule:" + ruleId , replyHandler ( "Deleting rule '" + ruleId + "'" ) ) ;
_this . db . smembers ( "rule:" + ruleId + ":users" , function ( err , obj ) {
var delLinkedUserRule , id , _i , _len , _results ;
delLinkedUserRule = function ( userId ) {
return _this . db . srem ( "user:" + userId + ":rules" , ruleId , replyHandler ( "Deleting rule key '" + ruleId + "' in linked user '" + userId + "'" ) ) ;
} ;
_results = [ ] ;
for ( _i = 0 , _len = obj . length ; _i < _len ; _i ++ ) {
id = obj [ _i ] ;
_results . push ( delLinkedUserRule ( id ) ) ;
}
return _results ;
} ) ;
_this . db . del ( "rule:" + ruleId + ":users" , replyHandler ( "Deleting rule '" + ruleId + "' users" ) ) ;
_this . db . smembers ( "rule:" + ruleId + ":active-users" , function ( err , obj ) {
var delActiveUserRule , id , _i , _len , _results ;
delActiveUserRule = function ( userId ) {
return _this . db . srem ( "user:" + userId + ":active-rules" , ruleId , replyHandler ( "Deleting rule key '" + ruleId + "' in active user '" + userId + "'" ) ) ;
} ;
_results = [ ] ;
for ( _i = 0 , _len = obj . length ; _i < _len ; _i ++ ) {
id = obj [ _i ] ;
_results . push ( delActiveUserRule ( id ) ) ;
}
return _results ;
} ) ;
return _this . db . del ( "rule:" + ruleId + ":active-users" , replyHandler ( "Deleting rule '" + ruleId + "' active users" ) ) ;
} ;
2014-02-10 21:28:10 +00:00
/ *
Associate a rule to a user .
@ public linkRule ( * ruleId , userId * )
@ param { String } ruleId
@ param { String } userId
2014-03-27 09:41:18 +00:00
* /
2014-02-10 21:28:10 +00:00
2014-03-27 09:41:18 +00:00
exports . linkRule = function ( ruleId , userId ) {
_this . log . info ( "DB | linkRule: '" + ruleId + "' for user '" + userId + "'" ) ;
_this . db . sadd ( "rule:" + ruleId + ":users" , userId , replyHandler ( "storing user '" + userId + "' for rule key '" + ruleId + "'" ) ) ;
return _this . db . sadd ( "user:" + userId + ":rules" , ruleId , replyHandler ( "storing rule key '" + ruleId + "' for user '" + userId + "'" ) ) ;
} ;
2014-02-10 21:28:10 +00:00
/ *
Get rules linked to a user and hand it to cb ( err , obj ) .
@ public getUserLinkRule ( * userId , cb * )
@ param { String } userId
@ param { function } cb
2014-03-27 09:41:18 +00:00
* /
2014-02-10 21:28:10 +00:00
2014-03-27 09:41:18 +00:00
exports . getUserLinkedRules = function ( userId , cb ) {
_this . log . info ( "DB | getUserLinkedRules: for user '" + userId + "'" ) ;
return _this . db . smembers ( "user:" + userId + ":rules" , cb ) ;
} ;
2014-02-10 21:28:10 +00:00
/ *
Get users linked to a rule and hand it to cb ( err , obj ) .
@ public getRuleLinkedUsers ( * ruleId , cb * )
@ param { String } ruleId
@ param { function } cb
2014-03-27 09:41:18 +00:00
* /
2014-02-10 21:28:10 +00:00
2014-03-27 09:41:18 +00:00
exports . getRuleLinkedUsers = function ( ruleId , cb ) {
_this . log . info ( "DB | getRuleLinkedUsers: for rule '" + ruleId + "'" ) ;
return _this . db . smembers ( "rule:" + ruleId + ":users" , cb ) ;
} ;
2014-02-10 21:28:10 +00:00
/ *
Delete an association of a rule to a user .
@ public unlinkRule ( * ruleId , userId * )
@ param { String } ruleId
@ param { String } userId
2014-03-27 09:41:18 +00:00
* /
2014-02-10 21:28:10 +00:00
2014-03-27 09:41:18 +00:00
exports . unlinkRule = function ( ruleId , userId ) {
_this . log . info ( "DB | unlinkRule: '" + ruleId + ":" + userId + "'" ) ;
_this . db . srem ( "rule:" + ruleId + ":users" , userId , replyHandler ( "removing user '" + userId + "' for rule key '" + ruleId + "'" ) ) ;
return _this . db . srem ( "user:" + userId + ":rules" , ruleId , replyHandler ( "removing rule key '" + ruleId + "' for user '" + userId + "'" ) ) ;
} ;
2014-02-10 21:28:10 +00:00
/ *
Activate a rule .
@ public activateRule ( * ruleId , userId * )
@ param { String } ruleId
@ param { String } userId
2014-03-27 09:41:18 +00:00
* /
2014-02-10 21:28:10 +00:00
2014-03-27 09:41:18 +00:00
exports . activateRule = function ( ruleId , userId ) {
_this . log . info ( "DB | activateRule: '" + ruleId + "' for '" + userId + "'" ) ;
_this . db . sadd ( "rule:" + ruleId + ":active-users" , userId , replyHandler ( "storing activated user '" + userId + "' in rule '" + ruleId + "'" ) ) ;
return _this . db . sadd ( "user:" + userId + ":active-rules" , ruleId , replyHandler ( "storing activated rule '" + ruleId + "' in user '" + userId + "'" ) ) ;
} ;
2014-02-10 21:28:10 +00:00
/ *
Get rules activated for a user and hand it to cb ( err , obj ) .
@ public getUserLinkRule ( * userId , cb * )
@ param { String } userId
@ param { function } cb
2014-03-27 09:41:18 +00:00
* /
2014-02-10 21:28:10 +00:00
2014-03-27 09:41:18 +00:00
exports . getUserActivatedRules = function ( userId , cb ) {
_this . log . info ( "DB | getUserActivatedRules: for user '" + userId + "'" ) ;
return _this . db . smembers ( "user:" + userId + ":active-rules" , cb ) ;
} ;
2014-02-10 21:28:10 +00:00
/ *
Get users activated for a rule and hand it to cb ( err , obj ) .
@ public getRuleActivatedUsers ( * ruleId , cb * )
@ param { String } ruleId
@ param { function } cb
2014-03-27 09:41:18 +00:00
* /
2014-02-10 21:28:10 +00:00
2014-03-27 09:41:18 +00:00
exports . getRuleActivatedUsers = function ( ruleId , cb ) {
_this . log . info ( "DB | getRuleActivatedUsers: for rule '" + ruleId + "'" ) ;
return _this . db . smembers ( "rule:" + ruleId + ":active-users" , cb ) ;
} ;
2014-02-10 21:28:10 +00:00
/ *
Deactivate a rule .
@ public deactivateRule ( * ruleId , userId * )
@ param { String } ruleId
@ param { String } userId
2014-03-27 09:41:18 +00:00
* /
2014-02-10 21:28:10 +00:00
2014-03-27 09:41:18 +00:00
exports . deactivateRule = function ( ruleId , userId ) {
_this . log . info ( "DB | deactivateRule: '" + ruleId + "' for '" + userId + "'" ) ;
_this . db . srem ( "rule:" + ruleId + ":active-users" , userId , replyHandler ( "removing activated user '" + userId + "' in rule '" + ruleId + "'" ) ) ;
return _this . db . srem ( "user:" + userId + ":active-rules" , ruleId , replyHandler ( "removing activated rule '" + ruleId + "' in user '" + userId + "'" ) ) ;
} ;
2014-02-10 21:28:10 +00:00
/ *
Fetch all active ruleIds and pass them to cb ( err , obj ) .
@ public getAllActivatedRuleIds ( * cb * )
@ param { function } cb
2014-03-27 09:41:18 +00:00
* /
2014-03-22 22:07:13 +00:00
2014-02-10 21:28:10 +00:00
2014-03-27 09:41:18 +00:00
exports . getAllActivatedRuleIdsPerUser = function ( cb ) {
_this . log . info ( "DB | Fetching all active rules" ) ;
return _this . db . smembers ( 'users' , function ( err , obj ) {
var fFetchActiveUserRules , result , semaphore , user , _i , _len , _results ;
result = { } ;
if ( obj . length === 0 ) {
return cb ( null , result ) ;
} else {
semaphore = obj . length ;
fFetchActiveUserRules = function ( userId ) {
return _this . db . smembers ( "user:" + user + ":active-rules" , function ( err , obj ) {
if ( obj . length > 0 ) {
result [ userId ] = obj ;
}
if ( -- semaphore === 0 ) {
return cb ( null , result ) ;
}
} ) ;
} ;
_results = [ ] ;
for ( _i = 0 , _len = obj . length ; _i < _len ; _i ++ ) {
user = obj [ _i ] ;
_results . push ( fFetchActiveUserRules ( user ) ) ;
}
return _results ;
}
} ) ;
} ;
2014-02-10 21:28:10 +00:00
/ *
2014-03-27 09:41:18 +00:00
# # Users
* /
2014-02-10 21:28:10 +00:00
2013-11-20 23:20:06 +00:00
/ *
2013-11-21 21:07:39 +00:00
Store a user object ( needs to be a flat structure ) .
2014-02-11 20:51:13 +00:00
The password should be hashed before it is passed to this function .
2013-11-20 23:20:06 +00:00
2013-11-21 21:07:39 +00:00
@ public storeUser ( * objUser * )
2013-11-20 23:20:06 +00:00
@ param { Object } objUser
2014-03-27 09:41:18 +00:00
* /
2013-11-20 23:20:06 +00:00
2013-11-21 21:07:39 +00:00
2014-03-27 09:41:18 +00:00
exports . storeUser = function ( objUser ) {
_this . log . info ( "DB | storeUser: '" + objUser . username + "'" ) ;
if ( objUser && objUser . username && objUser . password ) {
_this . db . sadd ( 'users' , objUser . username , replyHandler ( "storing user key '" + objUser . username + "'" ) ) ;
objUser . password = objUser . password ;
return _this . db . hmset ( "user:" + objUser . username , objUser , replyHandler ( "storing user properties '" + objUser . username + "'" ) ) ;
} else {
return _this . log . warn ( new Error ( 'DB | username or password was missing' ) ) ;
}
} ;
2014-02-11 20:51:13 +00:00
/ *
Fetch all user IDs and pass them to cb ( err , obj ) .
@ public getUserIds ( * cb * )
@ param { function } cb
2014-03-27 09:41:18 +00:00
* /
2014-02-11 20:51:13 +00:00
2014-03-27 09:41:18 +00:00
exports . getUserIds = function ( cb ) {
_this . log . info ( "DB | getUserIds" ) ;
return _this . db . smembers ( "users" , cb ) ;
} ;
2014-02-11 20:51:13 +00:00
/ *
Fetch a user by id and pass it to cb ( err , obj ) .
@ public getUser ( * userId , cb * )
@ param { String } userId
@ param { function } cb
2014-03-27 09:41:18 +00:00
* /
2014-02-11 20:51:13 +00:00
2014-03-27 09:41:18 +00:00
exports . getUser = function ( userId , cb ) {
_this . log . info ( "DB | getUser: '" + userId + "'" ) ;
return _this . db . hgetall ( "user:" + userId , cb ) ;
} ;
2014-02-11 20:51:13 +00:00
/ *
Deletes a user and all his associated linked and active rules .
@ public deleteUser ( * userId * )
@ param { String } userId
2014-03-27 09:41:18 +00:00
* /
2014-03-20 08:17:31 +00:00
2014-02-11 20:51:13 +00:00
2014-03-27 09:41:18 +00:00
exports . deleteUser = function ( userId ) {
_this . log . info ( "DB | deleteUser: '" + userId + "'" ) ;
_this . db . srem ( "users" , userId , replyHandler ( "Deleting user key '" + userId + "'" ) ) ;
_this . db . del ( "user:" + userId , replyHandler ( "Deleting user '" + userId + "'" ) ) ;
_this . db . smembers ( "user:" + userId + ":rules" , function ( err , obj ) {
var delLinkedRuleUser , id , _i , _len , _results ;
delLinkedRuleUser = function ( ruleId ) {
return _this . db . srem ( "rule:" + ruleId + ":users" , userId , replyHandler ( "Deleting user key '" + userId + "' in linked rule '" + ruleId + "'" ) ) ;
} ;
_results = [ ] ;
for ( _i = 0 , _len = obj . length ; _i < _len ; _i ++ ) {
id = obj [ _i ] ;
_results . push ( delLinkedRuleUser ( id ) ) ;
}
return _results ;
} ) ;
_this . db . del ( "user:" + userId + ":rules" , replyHandler ( "Deleting user '" + userId + "' rules" ) ) ;
_this . db . smembers ( "user:" + userId + ":active-rules" , function ( err , obj ) {
var delActivatedRuleUser , id , _i , _len , _results ;
delActivatedRuleUser = function ( ruleId ) {
return _this . db . srem ( "rule:" + ruleId + ":active-users" , userId , replyHandler ( "Deleting user key '" + userId + "' in active rule '" + ruleId + "'" ) ) ;
} ;
_results = [ ] ;
for ( _i = 0 , _len = obj . length ; _i < _len ; _i ++ ) {
id = obj [ _i ] ;
_results . push ( delActivatedRuleUser ( id ) ) ;
}
return _results ;
} ) ;
_this . db . del ( "user:" + userId + ":active-rules" , replyHandler ( "Deleting user '" + userId + "' rules" ) ) ;
_this . db . smembers ( "user:" + userId + ":roles" , function ( err , obj ) {
var delRoleUser , id , _i , _len , _results ;
delRoleUser = function ( roleId ) {
return _this . db . srem ( "role:" + roleId + ":users" , userId , replyHandler ( "Deleting user key '" + userId + "' in role '" + roleId + "'" ) ) ;
} ;
_results = [ ] ;
for ( _i = 0 , _len = obj . length ; _i < _len ; _i ++ ) {
id = obj [ _i ] ;
_results . push ( delRoleUser ( id ) ) ;
}
return _results ;
} ) ;
return _this . db . del ( "user:" + userId + ":roles" , replyHandler ( "Deleting user '" + userId + "' roles" ) ) ;
} ;
2014-02-13 17:16:03 +00:00
/ *
Checks the credentials and on success returns the user object to the
callback ( err , obj ) function . The password has to be hashed ( SHA - 3 - 512 )
beforehand by the instance closest to the user that enters the password ,
because we only store hashes of passwords for security6 reasons .
@ public loginUser ( * userId , password , cb * )
@ param { String } userId
@ param { String } password
@ param { function } cb
2014-03-27 09:41:18 +00:00
* /
exports . loginUser = function ( userId , password , cb ) {
var fCheck ;
_this . log . info ( "DB | User '" + userId + "' tries to log in" ) ;
fCheck = function ( pw ) {
return function ( err , obj ) {
if ( err ) {
return cb ( err , null ) ;
} else if ( obj && obj . password ) {
if ( pw === obj . password ) {
_this . log . info ( "DB | User '" + obj . username + "' logged in!" ) ;
return cb ( null , obj ) ;
2014-02-13 17:16:03 +00:00
} else {
2014-03-27 09:41:18 +00:00
return cb ( new Error ( 'Wrong credentials!' ) , null ) ;
2014-02-13 17:16:03 +00:00
}
2014-03-27 09:41:18 +00:00
} else {
return cb ( new Error ( 'User not found!' ) , null ) ;
}
2014-02-13 17:16:03 +00:00
} ;
} ;
2014-03-27 09:41:18 +00:00
return _this . db . hgetall ( "user:" + userId , fCheck ( password ) ) ;
} ;
2014-02-13 17:16:03 +00:00
/ *
2014-03-27 09:41:18 +00:00
# # User Roles
* /
2014-02-13 17:16:03 +00:00
2013-11-21 21:07:39 +00:00
/ *
Associate a role with a user .
2014-02-04 15:40:32 +00:00
@ public storeUserRole ( * userId , role * )
@ param { String } userId
2013-11-21 21:07:39 +00:00
@ param { String } role
2014-03-27 09:41:18 +00:00
* /
2013-11-21 21:07:39 +00:00
2014-03-27 09:41:18 +00:00
exports . storeUserRole = function ( userId , role ) {
_this . log . info ( "DB | storeUserRole: '" + userId + ":" + role + "'" ) ;
_this . db . sadd ( 'roles' , role , replyHandler ( "adding role '" + role + "' to role index set" ) ) ;
_this . db . sadd ( "user:" + userId + ":roles" , role , replyHandler ( "adding role '" + role + "' to user '" + userId + "'" ) ) ;
return _this . db . sadd ( "role:" + role + ":users" , userId , replyHandler ( "adding user '" + userId + "' to role '" + role + "'" ) ) ;
} ;
2014-03-20 08:17:31 +00:00
2013-11-20 23:20:06 +00:00
/ *
2014-02-10 21:28:10 +00:00
Fetch all roles of a user and pass them to cb ( err , obj ) .
2013-11-21 21:07:39 +00:00
2014-02-04 15:40:32 +00:00
@ public getUserRoles ( * userId * )
@ param { String } userId
2014-02-13 17:16:03 +00:00
@ param { function } cb
2014-03-27 09:41:18 +00:00
* /
2013-11-21 21:07:39 +00:00
2014-03-27 09:41:18 +00:00
exports . getUserRoles = function ( userId , cb ) {
_this . log . info ( "DB | getUserRoles: '" + userId + "'" ) ;
return _this . db . smembers ( "user:" + userId + ":roles" , cb ) ;
} ;
2013-11-21 21:07:39 +00:00
/ *
2014-02-10 21:28:10 +00:00
Fetch all users of a role and pass them to cb ( err , obj ) .
2013-11-21 21:07:39 +00:00
@ public getUserRoles ( * role * )
@ param { String } role
2014-02-13 17:16:03 +00:00
@ param { function } cb
2014-03-27 09:41:18 +00:00
* /
2014-03-22 22:07:13 +00:00
2013-11-21 21:07:39 +00:00
2014-03-27 09:41:18 +00:00
exports . getRoleUsers = function ( role , cb ) {
_this . log . info ( "DB | getRoleUsers: '" + role + "'" ) ;
return _this . db . smembers ( "role:" + role + ":users" , cb ) ;
} ;
2013-11-21 21:07:39 +00:00
/ *
2014-02-13 17:16:03 +00:00
Remove a role from a user .
2013-11-21 21:07:39 +00:00
2014-02-13 17:16:03 +00:00
@ public removeRoleFromUser ( * role , userId * )
@ param { String } role
2014-02-04 15:40:32 +00:00
@ param { String } userId
2014-03-27 09:41:18 +00:00
* /
2013-11-20 23:20:06 +00:00
2014-03-27 09:41:18 +00:00
exports . removeUserRole = function ( userId , role ) {
_this . log . info ( "DB | removeRoleFromUser: role '" + role + "', user '" + userId + "'" ) ;
_this . db . srem ( "user:" + userId + ":roles" , role , replyHandler ( "Removing role '" + role + "' from user '" + userId + "'" ) ) ;
return _this . db . srem ( "role:" + role + ":users" , userId , replyHandler ( "Removing user '" + userId + "' from role '" + role + "'" ) ) ;
} ;
2013-11-21 21:07:39 +00:00
/ *
Shuts down the db link .
@ public shutDown ( )
2014-03-27 09:41:18 +00:00
* /
2013-11-20 23:20:06 +00:00
2014-03-27 09:41:18 +00:00
exports . shutDown = function ( ) {
var _ref ;
return ( _ref = _this . db ) != null ? _ref . quit ( ) : void 0 ;
} ;
2013-11-21 21:07:39 +00:00
2013-11-20 23:20:06 +00:00
} ) . call ( this ) ;