This repository has been archived on 2024-10-16. You can view files and clone it, but cannot push or open issues or pull requests.
Permission-Checker-1/index.ts
2023-06-07 21:38:04 +02:00

57 lines
No EOL
1.3 KiB
TypeScript

/**
* Check if a permission is granted
* @param {String} perm - Is the permission that the user have
* @param {String} reqPerm - Is the required permissions
* @returns {Boolean}
*/
function checkSingle(perm: string, reqPerm: string) {
let answer = false
if (perm === "*") answer = true;
else {
let subRP = reqPerm.split('.');
let sub = perm.split(".")
if (subRP.length < sub.length) answer = false;
else {
let i = 0;
for (const rp of subRP) {
i = subRP.findIndex((e) => e === rp)
console.log(i, rp, sub[i], subRP.length)
if (rp != sub[i]) break;
else i++;
}
if (i == subRP.length || i == sub.length) answer = true;
}
}
return answer;
}
/**
* Check if a permission is granted
* @param {Array<String>} perm - Is the permission that the user have
* @param {Array<String>} reqPerm - Is the required permissions
* @returns {Boolean}
*/
function checkList(perm: string[], reqPerm: string[]) {
let answer = false, tl = 0, rql = reqPerm.length;
for (const rp of reqPerm) {
for (const p of perm) {
if (checkSingle(p, rp)) tl++;
}
}
if (rql == tl) answer = true
return answer;
}
export default {
checkList,
checkSingle
}