From a2ecc824d08ed484571ec77d7a6ea1de60b4ca7f Mon Sep 17 00:00:00 2001 From: Nicolas Lagier Date: Sat, 28 Nov 2020 08:02:05 +0800 Subject: [PATCH] auth --- angular.json | 62 ++++++++----------- jest.config.js | 1 + libs/auth/.eslintrc.json | 1 + libs/auth/README.md | 31 ++++++++++ libs/auth/jest.config.js | 23 +++++++ libs/auth/src/index.ts | 2 + libs/auth/src/lib/auth.module.ts | 10 +++ libs/auth/src/lib/authorization.directive.ts | 29 +++++++++ .../src/lib/authorization.service.spec.ts | 16 +++++ libs/auth/src/lib/authorization.service.ts | 12 ++++ libs/auth/src/test-setup.ts | 1 + libs/auth/tsconfig.json | 13 ++++ libs/auth/tsconfig.lib.json | 19 ++++++ libs/auth/tsconfig.spec.json | 10 +++ nx.json | 3 + tsconfig.base.json | 3 +- 16 files changed, 199 insertions(+), 37 deletions(-) create mode 100644 libs/auth/.eslintrc.json create mode 100644 libs/auth/README.md create mode 100644 libs/auth/jest.config.js create mode 100644 libs/auth/src/index.ts create mode 100644 libs/auth/src/lib/auth.module.ts create mode 100644 libs/auth/src/lib/authorization.directive.ts create mode 100644 libs/auth/src/lib/authorization.service.spec.ts create mode 100644 libs/auth/src/lib/authorization.service.ts create mode 100644 libs/auth/src/test-setup.ts create mode 100644 libs/auth/tsconfig.json create mode 100644 libs/auth/tsconfig.lib.json create mode 100644 libs/auth/tsconfig.spec.json diff --git a/angular.json b/angular.json index c99b489..cd76c47 100644 --- a/angular.json +++ b/angular.json @@ -150,18 +150,6 @@ "sourceRoot": "libs/table/src", "prefix": "cnfs", "architect": { - // "build": { - // "builder": "@nrwl/angular:package", - // "options": { - // "tsConfig": "libs/table/tsconfig.lib.json", - // "project": "libs/table/ng-package.json" - // }, - // "configurations": { - // "production": { - // "tsConfig": "libs/table/tsconfig.lib.prod.json" - // } - // } - // }, "lint": { "builder": "@nrwl/linter:eslint", "options": { @@ -214,18 +202,6 @@ "sourceRoot": "libs/common/src", "prefix": "cnfs", "architect": { - // "build": { - // "builder": "@nrwl/angular:package", - // "options": { - // "tsConfig": "libs/common/tsconfig.lib.json", - // "project": "libs/common/ng-package.json" - // }, - // "configurations": { - // "production": { - // "tsConfig": "libs/common/tsconfig.lib.prod.json" - // } - // } - // }, "lint": { "builder": "@nrwl/linter:eslint", "options": { @@ -252,18 +228,6 @@ "sourceRoot": "libs/cognito/src", "prefix": "cnfs", "architect": { - // "build": { - // "builder": "@nrwl/angular:package", - // "options": { - // "tsConfig": "libs/cognito/tsconfig.lib.json", - // "project": "libs/cognito/ng-package.json" - // }, - // "configurations": { - // "production": { - // "tsConfig": "libs/cognito/tsconfig.lib.prod.json" - // } - // } - // }, "lint": { "builder": "@nrwl/linter:eslint", "options": { @@ -283,6 +247,32 @@ "style": "scss" } } + }, + "auth": { + "projectType": "library", + "root": "libs/auth", + "sourceRoot": "libs/auth/src", + "prefix": "cnfs", + "architect": { + "lint": { + "builder": "@nrwl/linter:eslint", + "options": { + "lintFilePatterns": ["libs/auth/src/**/*.ts"] + } + }, + "test": { + "builder": "@nrwl/jest:jest", + "options": { + "jestConfig": "libs/auth/jest.config.js", + "passWithNoTests": true + } + } + }, + "schematics": { + "@schematics/angular:component": { + "style": "scss" + } + } } }, "cli": { diff --git a/jest.config.js b/jest.config.js index 3ab1fee..8282319 100644 --- a/jest.config.js +++ b/jest.config.js @@ -6,5 +6,6 @@ module.exports = { '/libs/iam', '/libs/common', '/libs/cognito', + '/libs/auth', ], }; diff --git a/libs/auth/.eslintrc.json b/libs/auth/.eslintrc.json new file mode 100644 index 0000000..54ac395 --- /dev/null +++ b/libs/auth/.eslintrc.json @@ -0,0 +1 @@ +{ "extends": "../../.eslintrc.json", "ignorePatterns": ["!**/*"], "rules": {} } diff --git a/libs/auth/README.md b/libs/auth/README.md new file mode 100644 index 0000000..6c63a18 --- /dev/null +++ b/libs/auth/README.md @@ -0,0 +1,31 @@ +# auth + +This library was generated with [Nx](https://nx.dev). + +## Running unit tests + +Run `nx test auth` to execute the unit tests. + +## Usage + +### **authorization** directive + +Add the directive `*authorization` on any component. It takes an action string as an input. If current user has access to this action, component will be displayed, otherwise it will not be added to the dom. + +```html + +``` + +### **authorization** service + +Use the service to know programatically if the current user has access to an action or not. + +```ts +service.hasAccess('iam:user:create').subscribe((res) => { + if (res) { + //user has access + } else { + //user does not have access + } +}); +``` diff --git a/libs/auth/jest.config.js b/libs/auth/jest.config.js new file mode 100644 index 0000000..6446fb3 --- /dev/null +++ b/libs/auth/jest.config.js @@ -0,0 +1,23 @@ +module.exports = { + displayName: 'auth', + preset: '../../jest.preset.js', + setupFilesAfterEnv: ['/src/test-setup.ts'], + globals: { + 'ts-jest': { + tsConfig: '/tsconfig.spec.json', + stringifyContentPathRegex: '\\.(html|svg)$', + astTransformers: { + before: [ + 'jest-preset-angular/build/InlineFilesTransformer', + 'jest-preset-angular/build/StripStylesTransformer', + ], + }, + }, + }, + coverageDirectory: '../../coverage/libs/auth', + snapshotSerializers: [ + 'jest-preset-angular/build/AngularNoNgAttributesSnapshotSerializer.js', + 'jest-preset-angular/build/AngularSnapshotSerializer.js', + 'jest-preset-angular/build/HTMLCommentSerializer.js', + ], +}; diff --git a/libs/auth/src/index.ts b/libs/auth/src/index.ts new file mode 100644 index 0000000..66bf103 --- /dev/null +++ b/libs/auth/src/index.ts @@ -0,0 +1,2 @@ +export * from './lib/auth.module'; +export * from './lib/authorization.service'; diff --git a/libs/auth/src/lib/auth.module.ts b/libs/auth/src/lib/auth.module.ts new file mode 100644 index 0000000..8ef03e9 --- /dev/null +++ b/libs/auth/src/lib/auth.module.ts @@ -0,0 +1,10 @@ +import { NgModule } from '@angular/core'; +import { CommonModule } from '@angular/common'; +import { AuthorizationDirective } from './authorization.directive'; + +@NgModule({ + imports: [CommonModule], + declarations: [AuthorizationDirective], + exports: [AuthorizationDirective], +}) +export class AuthModule {} diff --git a/libs/auth/src/lib/authorization.directive.ts b/libs/auth/src/lib/authorization.directive.ts new file mode 100644 index 0000000..c8d5a19 --- /dev/null +++ b/libs/auth/src/lib/authorization.directive.ts @@ -0,0 +1,29 @@ +import { Directive, TemplateRef, ViewContainerRef, Input } from '@angular/core'; +import { AuthorizationService } from './authorization.service'; + +@Directive({ + selector: '[authorization]', +}) +export class AuthorizationDirective { + private hasView: boolean; + + public constructor( + private templateRef: TemplateRef, + private viewContainer: ViewContainerRef, + private authorizationService: AuthorizationService + ) {} + + @Input() public set authorization(resource: string) { + this.authorizationService + .hasAccess(resource) + .subscribe((condition: boolean) => { + if (condition && !this.hasView) { + this.viewContainer.createEmbeddedView(this.templateRef); + this.hasView = true; + } else if (!condition && this.hasView) { + this.viewContainer.clear(); + this.hasView = false; + } + }); + } +} diff --git a/libs/auth/src/lib/authorization.service.spec.ts b/libs/auth/src/lib/authorization.service.spec.ts new file mode 100644 index 0000000..7073e3c --- /dev/null +++ b/libs/auth/src/lib/authorization.service.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { AuthorizationService } from './authorization.service'; + +describe('AuthorizationService', () => { + let service: AuthorizationService; + + beforeEach(() => { + TestBed.configureTestingModule({}); + service = TestBed.inject(AuthorizationService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); +}); diff --git a/libs/auth/src/lib/authorization.service.ts b/libs/auth/src/lib/authorization.service.ts new file mode 100644 index 0000000..36d29e2 --- /dev/null +++ b/libs/auth/src/lib/authorization.service.ts @@ -0,0 +1,12 @@ +import { Injectable } from '@angular/core'; +import { of, Observable } from 'rxjs'; + +@Injectable({ + providedIn: 'root', +}) +export class AuthorizationService { + public hasAccess(resource: string): Observable { + //** @todo do a call to backend and cache result */ + return of(true); + } +} diff --git a/libs/auth/src/test-setup.ts b/libs/auth/src/test-setup.ts new file mode 100644 index 0000000..8d88704 --- /dev/null +++ b/libs/auth/src/test-setup.ts @@ -0,0 +1 @@ +import 'jest-preset-angular'; diff --git a/libs/auth/tsconfig.json b/libs/auth/tsconfig.json new file mode 100644 index 0000000..62ebbd9 --- /dev/null +++ b/libs/auth/tsconfig.json @@ -0,0 +1,13 @@ +{ + "extends": "../../tsconfig.base.json", + "files": [], + "include": [], + "references": [ + { + "path": "./tsconfig.lib.json" + }, + { + "path": "./tsconfig.spec.json" + } + ] +} diff --git a/libs/auth/tsconfig.lib.json b/libs/auth/tsconfig.lib.json new file mode 100644 index 0000000..d5befaa --- /dev/null +++ b/libs/auth/tsconfig.lib.json @@ -0,0 +1,19 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "../../dist/out-tsc", + "target": "es2015", + "declaration": true, + "declarationMap": true, + "inlineSources": true, + "types": [], + "lib": ["dom", "es2018"] + }, + "angularCompilerOptions": { + "skipTemplateCodegen": true, + "strictMetadataEmit": true, + "enableResourceInlining": true + }, + "exclude": ["src/test-setup.ts", "**/*.spec.ts"], + "include": ["**/*.ts"] +} diff --git a/libs/auth/tsconfig.spec.json b/libs/auth/tsconfig.spec.json new file mode 100644 index 0000000..cfff29a --- /dev/null +++ b/libs/auth/tsconfig.spec.json @@ -0,0 +1,10 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "../../dist/out-tsc", + "module": "commonjs", + "types": ["jest", "node"] + }, + "files": ["src/test-setup.ts"], + "include": ["**/*.spec.ts", "**/*.d.ts"] +} diff --git a/nx.json b/nx.json index 4e9cbad..86a66b4 100644 --- a/nx.json +++ b/nx.json @@ -47,6 +47,9 @@ }, "cognito": { "tags": [] + }, + "auth": { + "tags": [] } } } diff --git a/tsconfig.base.json b/tsconfig.base.json index 29ee99f..3b9c810 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -21,7 +21,8 @@ "@cnfs/angular-table": ["libs/table/src/index.ts"], "@cnfs/iam": ["libs/iam/src/index.ts"], "@cnfs/common": ["libs/common/src/index.ts"], - "@cnfs/cognito": ["libs/cognito/src/index.ts"] + "@cnfs/cognito": ["libs/cognito/src/index.ts"], + "@cnfs/auth": ["libs/auth/src/index.ts"] } }, "exclude": ["node_modules", "tmp"]