2023-06-07 21:38:04 +02:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-06-04 15:15:34 +02:00
|
|
|
/**
|
|
|
|
* 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[]) {
|
2023-06-07 21:38:04 +02:00
|
|
|
let answer = false, tl = 0, rql = reqPerm.length;
|
2023-06-04 15:15:34 +02:00
|
|
|
|
2023-06-07 21:38:04 +02:00
|
|
|
for (const rp of reqPerm) {
|
|
|
|
for (const p of perm) {
|
|
|
|
if (checkSingle(p, rp)) tl++;
|
|
|
|
}
|
2023-06-04 15:15:34 +02:00
|
|
|
}
|
|
|
|
|
2023-06-07 21:38:04 +02:00
|
|
|
if (rql == tl) answer = true
|
|
|
|
|
2023-06-04 15:15:34 +02:00
|
|
|
return answer;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
|
|
|
checkList,
|
2023-06-07 21:38:04 +02:00
|
|
|
checkSingle
|
2023-06-04 15:15:34 +02:00
|
|
|
}
|