fix isRoot error & add getCollections api

This commit is contained in:
chenos 2021-03-16 14:40:47 +08:00
parent c6caa7aade
commit bfdb45a1ef
2 changed files with 35 additions and 5 deletions

View File

@ -155,6 +155,10 @@ export default class AccessController<T extends typeof AccessController = typeof
if ((this.constructor as T).isRoot(roles)) {
return true;
}
if (this.resourceName === 'action_logs' && this.actionName === 'list') {
console.log(`skip ${this.resourceName}:${this.actionName}`);
return true;
}
if (this.resourceName === 'china_regions' && this.actionName === 'list') {
console.log(`skip ${this.resourceName}:${this.actionName}`);
return true;
@ -208,6 +212,22 @@ export default class AccessController<T extends typeof AccessController = typeof
return existed ? any : null;
}
async getCollections(): Promise<any> {
const [Collection, Permission] = this.context.db.getModels(['collections', 'permissions']);
const isRoot = await this.isRoot();
if (isRoot) {
const collections = await Collection.findAll();
return collections.map(collection => collection.name);
}
const roles = await this.getRoles();
const permissions = await Permission.findAll({
where: {
role_id: roles.map(role => role.id),
}
});
return permissions.map(permission => permission.collection_name);
}
async isRoot(): Promise<boolean> {
const { context } = this;
const { currentUser } = context.state;
@ -215,16 +235,13 @@ export default class AccessController<T extends typeof AccessController = typeof
return false;
}
const rootRoles = await currentUser.countRoles({
const count = await currentUser.countRoles({
where: {
type: ROLE_TYPE_ROOT
}
});
if (!rootRoles.length) {
return false;
}
return true;
return !!count;
}
async getRoles() {

View File

@ -76,6 +76,19 @@ export class Permissions {
resourcer.use(this.injection);
resourcer.use(this.middleware);
resourcer.use(async (ctx, next) => {
const { resourceName } = ctx.action.params;
if (resourceName === 'action_logs' && !await ctx.ac.isRoot()) {
const collections = await ctx.ac.getCollections();
ctx.action.mergeParams({
filter: {
'collection_name.in': collections
},
});
}
await next();
});
}
injection = async (ctx, next) => {