fix isRoot error & add getCollections api
This commit is contained in:
parent
c6caa7aade
commit
bfdb45a1ef
@ -155,6 +155,10 @@ export default class AccessController<T extends typeof AccessController = typeof
|
|||||||
if ((this.constructor as T).isRoot(roles)) {
|
if ((this.constructor as T).isRoot(roles)) {
|
||||||
return true;
|
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') {
|
if (this.resourceName === 'china_regions' && this.actionName === 'list') {
|
||||||
console.log(`skip ${this.resourceName}:${this.actionName}`);
|
console.log(`skip ${this.resourceName}:${this.actionName}`);
|
||||||
return true;
|
return true;
|
||||||
@ -208,6 +212,22 @@ export default class AccessController<T extends typeof AccessController = typeof
|
|||||||
return existed ? any : null;
|
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> {
|
async isRoot(): Promise<boolean> {
|
||||||
const { context } = this;
|
const { context } = this;
|
||||||
const { currentUser } = context.state;
|
const { currentUser } = context.state;
|
||||||
@ -215,16 +235,13 @@ export default class AccessController<T extends typeof AccessController = typeof
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const rootRoles = await currentUser.countRoles({
|
const count = await currentUser.countRoles({
|
||||||
where: {
|
where: {
|
||||||
type: ROLE_TYPE_ROOT
|
type: ROLE_TYPE_ROOT
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
if (!rootRoles.length) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
return !!count;
|
||||||
}
|
}
|
||||||
|
|
||||||
async getRoles() {
|
async getRoles() {
|
||||||
|
@ -76,6 +76,19 @@ export class Permissions {
|
|||||||
|
|
||||||
resourcer.use(this.injection);
|
resourcer.use(this.injection);
|
||||||
resourcer.use(this.middleware);
|
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) => {
|
injection = async (ctx, next) => {
|
||||||
|
Loading…
Reference in New Issue
Block a user