ernest
Version:
Web framework for HTTP and HTTPS, using ExpressJS, Session, Mongo, Socket IO, Redis
197 lines (178 loc) • 3.73 kB
JavaScript
;
const str_user = 'user';
const str_pass = 'password';
const str_set = '$set';
const str_unset = '$unset';
const str_all = 'all_users';
class Ernest_Permission
{
constructor(app,dbc)
{
this.app = app;
this.dbc = dbc;
this.access_col = null;
};
SetPermissionCollection(iaccess,login_url,this_deniedtext,show_log)
{
var deniedtext = this_deniedtext.deniedtext;
var _this = this;
if(iaccess !== undefined)
{
if(iaccess !== null)
{
this.access_col = iaccess;
_this.app.use(function(req,res,next)
{
(show_log !== undefined) ? ShowLogReq(show_log,req): null;
if(isPage(req))
{
_this.IsPublicAccess(req.originalUrl,function(is)
{
is ? next() : (() => {isNotAuth(req) ? res.send(deniedtext) : _this.AccessGranted(req,res,deniedtext,() => {next();}); })();
});
}
else
{
if(isResourceRequest(req))
{
next();
}
else
{
if((req.originalUrl==login_url)||(req.originalUrl=="/"))
{
next();
}
else
{
if(isNotAuth(req))
{
res.send(deniedtext)
}
else
{
next();
};
};
};
};
});
}
else
{
(show_log !== undefined) ? ShowLogReq(show_log,req): null;
}
};
};
IsPublicAccess(iurl,callback)
{
var url = iurl.replace(".","_").replace("/","");
var _this = this;
var crit = {};
crit[str_user] = str_all;
crit[url] = true;
_this.dbc.FindInCollection(crit,_this.access_col,function(e,r)
{
if(e)
{
console.log("Ernest Permision Error:");
console.log(e);
callback(false);
}
else
{
callback(r.length > 0);
}
});
};
SetPublicAccess(iurl,callback)
{
var _this = this;
var url = iurl.replace(".","_").replace("/","");
var crit = {};
crit[str_user] = str_all;
var set = {};
set[str_set] = {};
set[str_set][url] = true;
_this.dbc.UpdateOneinCollec(crit,set,_this.access_col,function(e,d)
{
callback(true);
});
};
UnSetPublicAccess(iurl,callback)
{
var _this = this;
var url = iurl.replace(".","_").replace("/","");
var crit = {};
crit[str_user] = str_all;
var set = {};
set[str_unset] = {};
set[str_unset][url] = true;
_this.dbc.UpdateOneinCollec(crit,set,_this.access_col,function(e,d)
{
callback(true);
});
};
CreatePublicAccess(iaccess,callback)
{
var _this = this;
var crit = {};
crit[str_user] = str_all;
_this.dbc.FindInCollection(crit,iaccess,function(e,r)
{
if(e)
{
console.log("Ernest Permision Error");
console.log(e);
};
if(r.length > 0)
{
callback(true);
}else
{
_this.InsertInCollection(crit,iaccess,function(e,d)
{
callback(true);
});
};
});
};
AccessGranted(req,res,deniedtext,next)
{
let url = req.originalUrl.replace(".","_").replace("/","");
var _this = this;
let crit = {};
crit[str_user] = req.session.user;
crit[url] = {};
crit[url]['$gt']= 0;
_this.dbc.FindInCollection(crit,_this.access_col,function(e,r)
{
if(e)
{
console.log("Ernest Permision Error");
console.log(e);
};
(r.length > 0) ? next(): res.send(deniedtext);
});
};
};
module.exports = Ernest_Permission;
function isNotAuth(req)
{
return (typeof req.session.user === "undefined");
};
function isPage(req)
{
return ((req.originalUrl.indexOf(".html")> 0));
};
function isResourceRequest(req)
{
return ((req.originalUrl.indexOf(".")> 0));
};
function ShowLogReq(show_log,req)
{
if(show_log)
{
console.log("- Incoming Request = { url: " + req.originalUrl + ", ip: " + req.connection.remoteAddress + ", time: " + new Date()+"}");
};
};