refactor: use boolean value instead of null (#74)

This commit is contained in:
Junyi 2021-04-12 18:10:59 +08:00 committed by GitHub
parent 214b227a6c
commit f6dcfae28c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 12 deletions

View File

@ -84,7 +84,7 @@ export type CollectionPermissions = {
tabs: any[] tabs: any[]
}; };
export type PermissionParams = true | null | { export type PermissionParams = boolean | {
filter: any, filter: any,
fields: any[] fields: any[]
}; };
@ -175,7 +175,7 @@ export default class AccessController<T extends typeof AccessController = typeof
if (!actionPermissions.length) { if (!actionPermissions.length) {
// 如果找不到可用的 action 记录 // 如果找不到可用的 action 记录
// 则认为没有权限 // 则认为没有权限
return null; return false;
} }
const filters = actionPermissions const filters = actionPermissions
@ -195,7 +195,7 @@ export default class AccessController<T extends typeof AccessController = typeof
async one(resourceKey): Promise<PermissionParams> { async one(resourceKey): Promise<PermissionParams> {
const any = await this.any(); const any = await this.any();
if (!any || any === true) { if (typeof any === 'boolean') {
return any; return any;
} }
@ -209,7 +209,7 @@ export default class AccessController<T extends typeof AccessController = typeof
} }
}); });
return existed ? any : null; return existed ? any : false;
} }
async getPermissions(): Promise<any> { async getPermissions(): Promise<any> {
@ -338,13 +338,12 @@ export default class AccessController<T extends typeof AccessController = typeof
}; };
if (this.roles) { if (this.roles) {
if ((this.constructor as T).isRoot(this.roles)) { if (!(this.constructor as T).isRoot(this.roles)) {
return this.roles;
}
for (const role of this.roles) { for (const role of this.roles) {
role.permissions = await role.getPermissions(permissionOptions); role.permissions = await role.getPermissions(permissionOptions);
role.set('permissions', role.permissions); role.set('permissions', role.permissions);
} }
}
return this.roles; return this.roles;
} }

View File

@ -4,7 +4,7 @@ import { Application } from '@nocobase/server';
import { Operator } from '@nocobase/database'; import { Operator } from '@nocobase/database';
import * as collectionsRolesActions from './actions/collections.roles'; import * as collectionsRolesActions from './actions/collections.roles';
import * as rolesCollectionsActions from './actions/roles.collections'; import * as rolesCollectionsActions from './actions/roles.collections';
import AccessController from './AccessController'; import AccessController, { PermissionParams } from './AccessController';
// API // API
// const permissions = ctx.app.getPluginInstance('permissions'); // const permissions = ctx.app.getPluginInstance('permissions');
@ -105,7 +105,7 @@ export class Permissions {
actionName actionName
} = ctx.action.params; } = ctx.action.params;
let result = null; let result: PermissionParams = false;
// 关系数据的权限 // 关系数据的权限
if (associatedName && resourceField) { if (associatedName && resourceField) {