UNPKG

ernest

Version:

Web framework for HTTP and HTTPS, using ExpressJS, Session, Mongo, Socket IO, Redis

179 lines (162 loc) 3.3 kB
"use strict"; const str_user = 'user'; const str_pass = 'password'; const str_set = '$set'; const str_unset = '$unset'; const str_all = 'all_users'; class Ernest_Users { constructor(dbc,access) { this.dbc = dbc; this.access = access; }; isUser(user,password,callback) { var _this = this; var crit = {}; crit[str_user] = user; crit[str_pass] = password; _this.dbc.FindInCollection(crit,_this.access,function(e,r) { if(e) { console.log("Ernest Users Error:"); console.log(e); callback(false); } else { callback(r.length > 0,r[0]); } }); }; CreateUser(user,password,extra_properts,callback) { var _this = this; var crit = {}; var crit1 = {}; crit1[str_user] = user; crit[str_user] = user; crit[str_pass] = password; if(extra_properts != null) { for(var propert in extra_properts) { crit[propert] = extra_properts[propert]; }; }; _this.dbc.FindInCollection(crit1,_this.access,function(e,r) { if(e) { console.log("Ernest Users Error:"); console.log(e); callback(false); }; if(r.length > 0) { callback(false); }else { _this.dbc.InsertInCollection(crit,_this.access,function(e,d) { callback(true); }); }; }); }; DeleteUser(user,callback) { var _this = this; let crit = {}; crit[str_user] = user; _this.dbc.DeleteFromCollection(crit,_this.access,function(e,r) { callback(); }); }; UpdateUserPassword(user,password,callback) { var _this = this; var crit = {}; crit[str_user] = user; var set = {}; set[str_set] = {}; set[str_set][str_pass] = password; _this.dbc.UpdateOneinCollec(crit,set,_this.access,function(e,d) { callback(true); }); }; UpdateUserProperties(user,properties,callback) { var _this = this; var crit = {}; crit[str_user] = user; var set = {}; set[str_set]= properties; _this.dbc.UpdateOneinCollec(crit,set,_this.access,function(e,d) { callback(true); }); }; DeleteUserProperty(user,property,callback) { var _this = this; var crit = {}; crit[str_user] = user; var unset = {}; unset[str_unset] = {}; unset[str_unset][property] = true; _this.dbc.UpdateOneinCollec(crit,unset,_this.access,function(e,d) { callback(true); }); }; SetUserAccess(user,iurl,level,callback) { let url = iurl.replace(".","_").replace("/",""); let crit = {}; var _this = this; crit[str_user] = user; let set = {}; set[str_set] = {}; set[str_set][url] = level; _this.dbc.UpdateOneinCollec(crit,set,_this.access,function(e,d) { callback(true); }); }; UnsetUserAccess(user,iurl,callback) { let url = iurl.replace(".","_").replace("/",""); var _this = this; let crit = {}; crit[str_user] = user; let set = {}; set[str_unset] = {}; set[str_unset][url] = true; _this.dbc.UpdateOneinCollec(crit,set,_this.access,function(e,d) { callback(true); }); }; GetUserAccesses(user,callback) { var _this = this; let crit = {}; crit[str_user] = user; _this.dbc.FindInCollection(crit,_this.access,function(e,r) { if(e) { console.log("Ernest Users Error:"); console.log(e); callback(false); } callback(r); }); }; }; module.exports = Ernest_Users;