Skip to content

Commit

Permalink
new permissions related IAPIs
Browse files Browse the repository at this point in the history
  • Loading branch information
SorenEricMent committed Aug 11, 2022
1 parent fe886cf commit 72ce151
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
33 changes: 28 additions & 5 deletions modules/iapi.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ JSON.parse = parse.parse;
import {
generateNewToken, blake3Hash, objHasAllProperties,
strASCIIOnly, strStrictLegal, basicPasswordRequirement, isValidEmail, strNotOnlyNumber,
mergeJSON
mergeJSON, getPermissionSum
} from "./utils.mjs";
import { v4 as uuidv4 } from 'uuid';

Expand All @@ -16,6 +16,7 @@ class IAPI {
this.log = log;
this.salt = salt;
this.rp = redisPrefix;
this.rateLimitFallback = siteConfig.ratelimit_fallback;
//For util functions in IAPI, redis prefix will not be automatically added.
//Redis prefix needed to be added manually in caller function.
this.log("log", "IAPI", "IAPI instance created.");
Expand Down Expand Up @@ -146,7 +147,7 @@ class IAPI {
this.log("debug", "IAPI", "User not found in database: " + uid);
resolve(null);
}else{
resolve(results[0].roles);
resolve(results[0].roles.split(","));
}
}
}
Expand All @@ -168,7 +169,7 @@ class IAPI {
this.log("debug", "IAPI", "User not found in database.");
resolve(null);
}else{
resolve(results[0].roles);
resolve(results[0].roles.split(","));
}
}
}
Expand All @@ -177,8 +178,30 @@ class IAPI {
});
}
getUserPermissions(uid){
return new Promise((resolve, reject) => {

return new Promise(async (resolve, reject) => {
let userRoles = await this.getUserRoles(uid);
if(userRoles == null){
reject("User not found in database.");
}else if(typeof userRoles === "object" && userRoles[0] == ""){
reject("User has no roles.");
}else{
let promisePool = [];
for(const element of userRoles){
promisePool.push(new Promise((resolve, reject) => {
this.getRolePermissions(element).then((results) => {
resolve(results);
}).catch((err) => {
reject(err);
});
}));
}
Promise.allSettled(promisePool).then((results) => {
let permissions = getPermissionSum(this.rateLimitFallback, results);
resolve(permissions);
}).catch((err) => {
reject(err);
});
}
});
}
//Actual service functions
Expand Down
4 changes: 2 additions & 2 deletions modules/router.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import parse from "json5";
JSON.parse = parse.parse;

import {default as express} from "express";
import { default as express } from "express";
import { version, innerVersion, isAllString, strNotOnlyNumber, objHasAllProperties} from "./utils.mjs";
import { IAPI } from "./iapi.mjs";
import { default as fs } from "fs";
Expand All @@ -18,7 +18,7 @@ function initializeRouter(mysqlConnection, redisConnection, siteConfig, log, sal
let ip = null;
let ua = null;
if(siteConfig.ip_detect_method == "connection"){
ip = req.connection.remoteAddress;
ip = req.connection.remoteAddress;
}else if(siteConfig.ip_detect_method == "header"){
//Default header X-Forwarded-For.
if(req.headers.hasOwnProperty(siteConfig.ip_detect_header)){
Expand Down
17 changes: 15 additions & 2 deletions modules/utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,22 @@ function pureArray(arr){
//remove all empty items in an array
return arr.filter(item => item !== "");
}

function getPermissionSum(...p){

//return the greatest permissions of all given role permissions
let permSum = {
"with_rate_limit": 0,
"permissions": {
"flags": [],
"max_session": 10,
"cookie_expire_after": 13150000000
}
};
let isRateLimitContained = false;
for(const perm of p){

}
return permSum;
}

export {
Expand Down

0 comments on commit 72ce151

Please sign in to comment.