This commit is contained in:
c
2025-03-12 20:11:03 +08:00
commit 70b7c82c97
1178 changed files with 148185 additions and 0 deletions
@@ -0,0 +1,50 @@
/**
* 权限验证中间件,一般情况下,无需修改此处的代码
*/
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[50403] };
}
if (settingPermission.length > 0 && settingPermission.every(item => !userPermission.includes(item))) {
throw { errCode: ERROR[50403] };
}
}
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
})
}
@@ -0,0 +1,28 @@
module.exports = async function(key = true) {
if (this.authInfo) { // 多次执行auth时如果第一次成功后续不再执行
return;
}
// 获取请求参数
const params = this.getParams();
// 获取token
let token = this.getUniIdToken();
// 如果没有token,尝试从参数中获取
if (!token && params[0]) {
token = params[0].uniIdToken || params[0].uni_id_token;
}
const payload = await this.uniIdCommon.checkToken(token);
if (payload.errCode) {
if (key) {
throw payload;
} else {
return;
}
}
this.authInfo = payload;
if (payload.token && typeof this.response === "object") {
this.response.newToken = {
token: payload.token,
tokenExpired: payload.tokenExpired
}
}
}
@@ -0,0 +1,7 @@
const accessControl = require("./access-control");
const auth = require("./auth");
module.exports = {
accessControl,
auth
}