Created
June 30, 2022 21:01
-
-
Save doraemonxxx/39b707803cbbd8c23c486c79bf6fecca to your computer and use it in GitHub Desktop.
simply check user permission - for private project
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function hasRealPermission(p: string): boolean { | |
const permission = userPermission; | |
let allPermitted = false; | |
const cache = {}; | |
function getCA(perm: string): CA | null { | |
const p = perm?.split(/:/); | |
if (p?.length < 2) { | |
return null; | |
} | |
if (p) { | |
return { controller: p[0], action: p[1] }; | |
} else { | |
return null; | |
} | |
} | |
if (allPermitted) { | |
return true; | |
} | |
if (p in cache) { | |
return cache[p]; | |
} | |
if (permission.find((perm: string) => perm === '*:*')) { | |
allPermitted = true; | |
return true; | |
} | |
let pCA: string[] = []; | |
let tmp: any = null; | |
let permitted = false; | |
pCA.push(p); | |
if (!permitted) { | |
console.info('not permitted start'); | |
if ( | |
permission.find(function (perm: any) { | |
const c = p?.split(/:/)[0]; | |
const ca = getCA(perm); | |
if (c === ca?.controller && ca?.action === '*') { | |
return true; | |
} | |
}) | |
) { | |
permitted = true; | |
} | |
//view | |
tmp = clone(pCA); | |
each(pCA, function (act) { | |
const ca = getCA(act); | |
if (/^list|^view|^show|getjson$|listjson$|^get|^check|^trackback$/.test(ca?.action)) { | |
tmp.push(`${ca?.controller}:view`); | |
} | |
}); | |
pCA = clone(tmp); | |
// edit | |
tmp = clone(pCA); | |
each(pCA, function (act) { | |
const ca = getCA(act); | |
if (/^view$|^edit|^save|^update|^create|savejson$|updatejson$/.test(ca?.action)) { | |
tmp.push(`${ca?.controller}:edit`); | |
} | |
}); | |
pCA = clone(tmp); | |
// admin | |
tmp = clone(pCA); | |
each(pCA, function (act) { | |
const ca = getCA(act); | |
if (/^edit$|^delete|deletejson$/.test(ca?.action)) { | |
tmp.push(`${ca?.controller}:admin`); | |
} | |
}); | |
// | |
pCA = clone(tmp); | |
pCA = uniq(pCA); | |
each(pCA, function (ca) { | |
if ( | |
permission.find(function (p) { | |
return p === ca; | |
}) | |
) { | |
permitted = true; | |
} | |
if (permitted) { | |
return false; // break each | |
} | |
}); | |
} | |
cache[p] = permitted; | |
return permitted; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment