(ctx, next)
| 66 | * 多对多: role -> menu(删除动态/修改动态) |
| 67 | */ |
| 68 | const verifyPermission = async (ctx, next) => { |
| 69 | console.log("验证权限的middleware~"); |
| 70 | |
| 71 | // 1.获取参数 { commentId: '1' } |
| 72 | const [resourceKey] = Object.keys(ctx.params); |
| 73 | const tableName = resourceKey.replace('Id', ''); |
| 74 | const resourceId = ctx.params[resourceKey]; |
| 75 | const { id } = ctx.user; |
| 76 | |
| 77 | // 2.查询是否具备权限 |
| 78 | try { |
| 79 | const isPermission = await authService.checkResource(tableName, resourceId, id); |
| 80 | if (!isPermission) throw new Error(); |
| 81 | await next(); |
| 82 | } catch (err) { |
| 83 | const error = new Error(errorTypes.UNPERMISSION); |
| 84 | return ctx.app.emit('error', error, ctx); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | // const verifyPermission = (tableName) => { |
| 89 | // return async (ctx, next) => { |
nothing calls this directly
no test coverage detected