smart-access
Version:
Smart Access Controller
101 lines (91 loc) • 3.76 kB
JavaScript
/**
* Created by amir on 4/12/2017.
*/
module.exports = class {
constructor(user,storage,debugging) {
this.action = storage.collection("action");
this.group = storage.collection("group");
this.user = user;
this.debugging = debugging;
this._log("Smart Access Is Connected.",false);
}
granted(collection_name,operation_order){
// find action by collection name and operation order
let action = this.action.find({name:collection_name,order:operation_order});
//process action is null returned false
if(action == null){
this._log("Smart Access ::: NotFound Action By name = "+collection_name+" AND order = "+operation_order+" In granted .");
return false;
}
//find access by id
let access = this.user.collection("access").find({action:action.id});
//process access is null returned false
if(access == null) {
this._log("Smart Access ::: NotFound Access By action = "+action.id+" In granted .");
return false;
}
//return status by process status equal 1
return access.status == 1;
}
allow(collection_name,operation_order){
// find action by collection name and operation order
let action = this.action.find({name:collection_name,order:operation_order});
//process action is null returned false
if(action == null){
this._log("Smart Access ::: NotFound Action By name = "+collection_name+" AND order = "+operation_order+" In allow .");
return false;
}
//add new access for user by action
this.user.collection("access").add({action:action.id,status:1});
return true;
}
deny(collection_name,operation_order){
// find action by collection name and operation order
let action = this.action.find({name:collection_name,order:operation_order});
//process action is null returned false
if(action == null){
this._log("Smart Access ::: NotFound Action By name = "+collection_name+" AND order = "+operation_order+" In deny .");
return false;
}
//find access by action id
let access = this.user.collection("access").find({action:action.id});
//process access is null returned false
if(access == null){
this._log("Smart Access ::: NotFound Access By action = "+action.id+" In deny .");
return true;
}
//change access status to 0 in the database
access.data = {status:0};
return true;
}
get group(){
//returned user group data
return this.user.related("group").data;
}
set group(group){
//find group by id
let check_group = this.group.find({id:group});
//process group is not null
if(check_group != null) {
this._log("Smart Access ::: NotFound Group By if = "+group+" In group .");
//change user group in the database
this.user.data = {group};
}
}
_log(text, force) {
if (force || this.debugging) {
let date = new Date();
let hour = date.getHours();
let minutes = date.getMinutes();
let seconds = date.getSeconds();
let time = '[' +
((hour < 10) ? '0' + hour : hour) +
':' +
((minutes < 10) ? '0' + minutes : minutes) +
':' +
((seconds < 10) ? '0' + seconds : seconds) +
'] ';
console.log(time + text);
}
}
}