Skip to content

Commit

Permalink
feat: add guard (#2345)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 authored Oct 6, 2022
1 parent a7c6e03 commit 1b952a1
Show file tree
Hide file tree
Showing 123 changed files with 694 additions and 173 deletions.
2 changes: 1 addition & 1 deletion packages-legacy/egg-layer/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testPathIgnorePatterns: ['<rootDir>/test/fixtures'],
coveragePathIgnorePatterns: ['<rootDir>/test/'],
coveragePathIgnorePatterns: ['<rootDir>/test/', '<rootDir>/dist/'],
setupFilesAfterEnv: ['./jest.setup.js'],
};
2 changes: 1 addition & 1 deletion packages-legacy/express-layer/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testPathIgnorePatterns: ['<rootDir>/test/fixtures'],
coveragePathIgnorePatterns: ['<rootDir>/test/'],
coveragePathIgnorePatterns: ['<rootDir>/test/', '<rootDir>/dist/'],
setupFilesAfterEnv: ['./jest.setup.js'],
};
2 changes: 1 addition & 1 deletion packages-legacy/koa-layer/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testPathIgnorePatterns: ['<rootDir>/test/fixtures'],
coveragePathIgnorePatterns: ['<rootDir>/test/'],
coveragePathIgnorePatterns: ['<rootDir>/test/', '<rootDir>/dist/'],
setupFilesAfterEnv: ['./jest.setup.js'],
};
2 changes: 1 addition & 1 deletion packages-legacy/orm/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testPathIgnorePatterns: ['<rootDir>/test/fixtures'],
coveragePathIgnorePatterns: ['<rootDir>/test/'],
coveragePathIgnorePatterns: ['<rootDir>/test/', '<rootDir>/dist/'],
setupFilesAfterEnv: ['./jest.setup.js'],
};
2 changes: 1 addition & 1 deletion packages-legacy/runtime-engine/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testPathIgnorePatterns: ['<rootDir>/test/fixtures'],
coveragePathIgnorePatterns: ['<rootDir>/test/'],
coveragePathIgnorePatterns: ['<rootDir>/test/', '<rootDir>/dist/'],
setupFilesAfterEnv: ['./jest.setup.js'],
};
2 changes: 1 addition & 1 deletion packages-legacy/runtime-mock/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testPathIgnorePatterns: ['<rootDir>/test/fixtures'],
coveragePathIgnorePatterns: ['<rootDir>/test/'],
coveragePathIgnorePatterns: ['<rootDir>/test/', '<rootDir>/dist/'],
setupFilesAfterEnv: ['./jest.setup.js'],
};
2 changes: 1 addition & 1 deletion packages-legacy/serverless-app/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testPathIgnorePatterns: ['<rootDir>/test/fixtures'],
coveragePathIgnorePatterns: ['<rootDir>/test/'],
coveragePathIgnorePatterns: ['<rootDir>/test/', '<rootDir>/dist/'],
setupFilesAfterEnv: ['./jest.setup.js'],
};
2 changes: 1 addition & 1 deletion packages-legacy/serverless-aws-starter/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testPathIgnorePatterns: ['<rootDir>/test/fixtures'],
coveragePathIgnorePatterns: ['<rootDir>/test/'],
coveragePathIgnorePatterns: ['<rootDir>/test/', '<rootDir>/dist/'],
setupFilesAfterEnv: ['./jest.setup.js'],
};
2 changes: 1 addition & 1 deletion packages-legacy/serverless-fc-starter/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testPathIgnorePatterns: ['<rootDir>/test/fixtures'],
coveragePathIgnorePatterns: ['<rootDir>/test/'],
coveragePathIgnorePatterns: ['<rootDir>/test/', '<rootDir>/dist/'],
setupFilesAfterEnv: ['./jest.setup.js'],
};
2 changes: 1 addition & 1 deletion packages-legacy/serverless-fc-trigger/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testPathIgnorePatterns: ['<rootDir>/test/fixtures'],
coveragePathIgnorePatterns: ['<rootDir>/test/'],
coveragePathIgnorePatterns: ['<rootDir>/test/', '<rootDir>/dist/'],
setupFilesAfterEnv: ['./jest.setup.js'],
};
2 changes: 1 addition & 1 deletion packages-legacy/serverless-scf-starter/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testPathIgnorePatterns: ['<rootDir>/test/fixtures'],
coveragePathIgnorePatterns: ['<rootDir>/test/'],
coveragePathIgnorePatterns: ['<rootDir>/test/', '<rootDir>/dist/'],
setupFilesAfterEnv: ['./jest.setup.js'],
};
2 changes: 1 addition & 1 deletion packages-legacy/serverless-scf-trigger/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testPathIgnorePatterns: ['<rootDir>/test/fixtures'],
coveragePathIgnorePatterns: ['<rootDir>/test/'],
coveragePathIgnorePatterns: ['<rootDir>/test/', '<rootDir>/dist/'],
setupFilesAfterEnv: ['./jest.setup.js'],
};
2 changes: 1 addition & 1 deletion packages-legacy/serverless-vercel-starter/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testPathIgnorePatterns: ['<rootDir>/test/fixtures'],
coveragePathIgnorePatterns: ['<rootDir>/test/'],
coveragePathIgnorePatterns: ['<rootDir>/test/', '<rootDir>/dist/'],
setupFilesAfterEnv: ['./jest.setup.js'],
};
2 changes: 1 addition & 1 deletion packages-legacy/serverless-vercel-trigger/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testPathIgnorePatterns: ['<rootDir>/test/fixtures'],
coveragePathIgnorePatterns: ['<rootDir>/test/'],
coveragePathIgnorePatterns: ['<rootDir>/test/', '<rootDir>/dist/'],
setupFilesAfterEnv: ['./jest.setup.js'],
};
2 changes: 1 addition & 1 deletion packages-legacy/serverless-worker-starter/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ module.exports = {
preset: 'ts-jest',
testEnvironment: '@midwayjs/jest-environment-service-worker',
testPathIgnorePatterns: ['<rootDir>/test/fixtures'],
coveragePathIgnorePatterns: ['<rootDir>/test/'],
coveragePathIgnorePatterns: ['<rootDir>/test/', '<rootDir>/dist/'],
setupFilesAfterEnv: ['./jest.setup.js'],
};
2 changes: 1 addition & 1 deletion packages-legacy/static-layer/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testPathIgnorePatterns: ['<rootDir>/test/fixtures'],
coveragePathIgnorePatterns: ['<rootDir>/test/'],
coveragePathIgnorePatterns: ['<rootDir>/test/', '<rootDir>/dist/'],
setupFilesAfterEnv: ['./jest.setup.js'],
};
2 changes: 1 addition & 1 deletion packages-serverless/midway-fc-starter/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testPathIgnorePatterns: ['<rootDir>/test/fixtures'],
coveragePathIgnorePatterns: ['<rootDir>/test/'],
coveragePathIgnorePatterns: ['<rootDir>/test/', '<rootDir>/dist/'],
setupFilesAfterEnv: ['./jest.setup.js'],
};
2 changes: 1 addition & 1 deletion packages-serverless/serverless-http-parser/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testPathIgnorePatterns: ['<rootDir>/test/fixtures'],
coveragePathIgnorePatterns: ['<rootDir>/test/'],
coveragePathIgnorePatterns: ['<rootDir>/test/', '<rootDir>/dist/'],
setupFilesAfterEnv: ['./jest.setup.js'],
};
2 changes: 1 addition & 1 deletion packages/async-hooks-context-manager/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testPathIgnorePatterns: ['<rootDir>/test/fixtures'],
coveragePathIgnorePatterns: ['<rootDir>/test/'],
coveragePathIgnorePatterns: ['<rootDir>/test/', '<rootDir>/dist/'],
setupFilesAfterEnv: ['./jest.setup.js'],
};
2 changes: 1 addition & 1 deletion packages/axios/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testPathIgnorePatterns: ['<rootDir>/test/fixtures'],
coveragePathIgnorePatterns: ['<rootDir>/test/'],
coveragePathIgnorePatterns: ['<rootDir>/test/', '<rootDir>/dist/'],
setupFilesAfterEnv: ['./jest.setup.js'],
};
2 changes: 1 addition & 1 deletion packages/bootstrap/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testPathIgnorePatterns: ['<rootDir>/test/fixtures'],
coveragePathIgnorePatterns: ['<rootDir>/test/'],
coveragePathIgnorePatterns: ['<rootDir>/test/', '<rootDir>/dist/'],
setupFilesAfterEnv: ['./jest.setup.js'],
};
2 changes: 1 addition & 1 deletion packages/cache/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testPathIgnorePatterns: ['<rootDir>/test/fixtures'],
coveragePathIgnorePatterns: ['<rootDir>/test/'],
coveragePathIgnorePatterns: ['<rootDir>/test/', '<rootDir>/dist/'],
setupFilesAfterEnv: ['./jest.setup.js'],
};
2 changes: 1 addition & 1 deletion packages/code-dye/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testPathIgnorePatterns: ['<rootDir>/test/fixtures'],
coveragePathIgnorePatterns: ['<rootDir>/test/'],
coveragePathIgnorePatterns: ['<rootDir>/test/', '<rootDir>/dist/'],
setupFilesAfterEnv: ['./jest.setup.js'],
};
2 changes: 1 addition & 1 deletion packages/consul/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testPathIgnorePatterns: ['<rootDir>/test/fixtures'],
coveragePathIgnorePatterns: ['<rootDir>/test/'],
coveragePathIgnorePatterns: ['<rootDir>/test/', '<rootDir>/dist/'],
setupFilesAfterEnv: ['./jest.setup.js'],
};
2 changes: 1 addition & 1 deletion packages/core/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testPathIgnorePatterns: ['<rootDir>/test/fixtures'],
coveragePathIgnorePatterns: ['<rootDir>/test/'],
coveragePathIgnorePatterns: ['<rootDir>/test/', '<rootDir>/dist/'],
setupFilesAfterEnv: ['./jest.setup.js'],
};
3 changes: 2 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"build": "tsc",
"test": "node --require=ts-node/register ../../node_modules/.bin/jest --runInBand",
"cov": "node --require=ts-node/register ../../node_modules/.bin/jest --runInBand --coverage --forceExit",
"link": "npm link"
"link": "npm link",
"madge": "madge --circular --extensions ts,tsx --exclude 'node_modules|test|dist' src"
},
"keywords": [
"midway",
Expand Down
39 changes: 35 additions & 4 deletions packages/core/src/baseFramework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ import {
MidwayProcessTypeEnum,
CommonFilterUnion,
MiddlewareRespond,
CommonGuardUnion,
} from './interface';
import {
REQUEST_CTX_LOGGER_CACHE_KEY,
ASYNC_CONTEXT_KEY,
ASYNC_CONTEXT_MANAGER_KEY,
} from './interface';
} from './constants';
import { Inject, Destroy, Init } from './decorator';
import {
ILogger,
Expand All @@ -29,13 +32,13 @@ import { ContextMiddlewareManager } from './common/middlewareManager';
import { MidwayMiddlewareService } from './service/middlewareService';
import { FilterManager } from './common/filterManager';
import { MidwayMockService } from './service/mockService';

import * as util from 'util';
import {
ASYNC_ROOT_CONTEXT,
AsyncContextManager,
NoopContextManager,
} from './common/asyncContextManager';
import { GuardManager } from './common/guardManager';
const debug = util.debuglog('midway:debug');

export abstract class BaseFramework<
Expand All @@ -55,6 +58,7 @@ export abstract class BaseFramework<
protected contextLoggerFormat: LoggerContextFormat;
protected middlewareManager = this.createMiddlewareManager();
protected filterManager = this.createFilterManager();
protected guardManager = this.createGuardManager();
protected composeMiddleware = null;
protected bootstrapOptions: IMidwayBootstrapOptions;
protected asyncContextManager: AsyncContextManager;
Expand Down Expand Up @@ -250,6 +254,10 @@ export abstract class BaseFramework<
return this.createLogger(name, options);
},

getFramework: () => {
return this;
},

getProjectName: () => {
return this.getProjectName();
},
Expand Down Expand Up @@ -294,17 +302,24 @@ export abstract class BaseFramework<
getAttr: <T>(key: string): T => {
return this.getApplicationContext().getAttr(key);
},

useMiddleware: (
middleware: CommonMiddlewareUnion<CTX, ResOrNext, Next>
) => {
return this.useMiddleware(middleware);
},

getMiddleware: (): ContextMiddlewareManager<CTX, ResOrNext, Next> => {
return this.getMiddleware();
},

useFilter: (Filter: CommonFilterUnion<CTX, ResOrNext, Next>) => {
return this.useFilter(Filter);
},

useGuard: (guard: CommonGuardUnion<CTX>) => {
return this.useGuard(guard);
},
};
for (const method of whiteList) {
delete defaultApplicationProperties[method];
Expand Down Expand Up @@ -439,8 +454,20 @@ export abstract class BaseFramework<
return this.middlewareManager;
}

public useFilter(Filter: CommonFilterUnion<CTX, ResOrNext, Next>) {
return this.filterManager.useFilter(Filter);
public useFilter(filter: CommonFilterUnion<CTX, ResOrNext, Next>) {
return this.filterManager.useFilter(filter);
}

public useGuard(guards: CommonGuardUnion<CTX>) {
return this.guardManager.addGlobalGuard(guards);
}

public async runGuard(
ctx: CTX,
supplierClz: new (...args) => any,
methodName: string
): Promise<boolean> {
return this.guardManager.runGuard(ctx, supplierClz, methodName);
}

protected createMiddlewareManager() {
Expand All @@ -450,4 +477,8 @@ export abstract class BaseFramework<
protected createFilterManager() {
return new FilterManager<CTX, ResOrNext, Next>();
}

protected createGuardManager() {
return new GuardManager<CTX>();
}
}
2 changes: 1 addition & 1 deletion packages/core/src/common/dataSourceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { MidwayCommonError, MidwayParameterError } from '../error';
import { run } from '@midwayjs/glob';
import { join } from 'path';
import { Types } from '../util/types';
import { DEFAULT_PATTERN, IGNORE_PATTERN } from '../interface';
import { DEFAULT_PATTERN, IGNORE_PATTERN } from '../constants';

export abstract class DataSourceManager<T> {
protected dataSource: Map<string, T> = new Map();
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/common/fileDetector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
import { Types } from '../util/types';
import { run } from '@midwayjs/glob';
import { MidwayDuplicateClassNameError } from '../error';
import { DEFAULT_PATTERN, IGNORE_PATTERN } from '../interface';
import { DEFAULT_PATTERN, IGNORE_PATTERN } from '../constants';
import { getProviderName } from '../decorator';

export abstract class AbstractFileDetector<T> implements IFileDetector {
Expand Down
58 changes: 58 additions & 0 deletions packages/core/src/common/guardManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { CommonGuardUnion, IGuard, IMidwayContext } from '../interface';
import { getClassMetadata, getPropertyMetadata, GUARD_KEY } from '../decorator';

export class GuardManager<
CTX extends IMidwayContext = IMidwayContext
> extends Array<new (...args) => IGuard<any>> {
public addGlobalGuard(guards: CommonGuardUnion<CTX>) {
if (!Array.isArray(guards)) {
this.push(guards);
} else {
this.push(...guards);
}
}

public async runGuard(
ctx: CTX,
supplierClz: new (...args) => any,
methodName: string
) {
// check global guard
for (const Guard of this) {
const guard = await ctx.requestContext.getAsync<IGuard<any>>(Guard);
const isPassed = await guard.canActivate(ctx, supplierClz, methodName);
if (!isPassed) {
return false;
}
}

// check class Guard
const classGuardList = getClassMetadata(GUARD_KEY, supplierClz);
if (classGuardList) {
for (const Guard of classGuardList) {
const guard = await ctx.requestContext.getAsync<IGuard<any>>(Guard);
const isPassed = await guard.canActivate(ctx, supplierClz, methodName);
if (!isPassed) {
return false;
}
}
}

// check method Guard
const methodGuardList = getPropertyMetadata(
GUARD_KEY,
supplierClz,
methodName
);
if (methodGuardList) {
for (const Guard of methodGuardList) {
const guard = await ctx.requestContext.getAsync<IGuard<any>>(Guard);
const isPassed = await guard.canActivate(ctx, supplierClz, methodName);
if (!isPassed) {
return false;
}
}
}
return true;
}
}
Loading

0 comments on commit 1b952a1

Please sign in to comment.