This commit is contained in:
2025-03-28 16:27:26 +08:00
parent f654e16c18
commit a3e6a6d8b8
494 changed files with 4072 additions and 47246 deletions
@@ -0,0 +1,59 @@
const methodPermission = require('../config/permission')
const {
ERROR
} = require('../common/error')
function isAccessAllowed (user, setting) {
const {
role: userRole = [],
permission: userPermission = []
} = user
const {
role: settingRole = [],
permission: settingPermission = []
} = setting
if (userRole.includes('admin')) {
return
}
if (
settingRole.length > 0 &&
settingRole.every(item => !userRole.includes(item))
) {
throw {
errCode: ERROR.PERMISSION_ERROR
}
}
if (
settingPermission.length > 0 &&
settingPermission.every(item => !userPermission.includes(item))
) {
throw {
errCode: ERROR.PERMISSION_ERROR
}
}
}
module.exports = async function () {
const methodName = this.getMethodName()
if (!(methodName in methodPermission)) {
return
}
const {
auth,
role,
permission
} = methodPermission[methodName]
if (auth || role || permission) {
await this.middleware.auth()
}
if (role && role.length === 0) {
throw new Error('[AccessControl]Empty role array is not supported')
}
if (permission && permission.length === 0) {
throw new Error('[AccessControl]Empty permission array is not supported')
}
return isAccessAllowed(this.authInfo, {
role,
permission
})
}