Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

auth #22

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

auth #22

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 26 additions & 36 deletions angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -149,18 +149,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": {
Expand Down Expand Up @@ -213,18 +201,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": {
Expand All @@ -251,18 +227,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": {
Expand All @@ -282,6 +246,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": {
Expand Down
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ module.exports = {
'<rootDir>/libs/iam',
'<rootDir>/libs/common',
'<rootDir>/libs/cognito',
'<rootDir>/libs/auth',
],
};
1 change: 1 addition & 0 deletions libs/auth/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "extends": "../../.eslintrc.json", "ignorePatterns": ["!**/*"], "rules": {} }
31 changes: 31 additions & 0 deletions libs/auth/README.md
Original file line number Diff line number Diff line change
@@ -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
<button *authorization="'iam:user:create'">New User</button>
```

### **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
}
});
```
23 changes: 23 additions & 0 deletions libs/auth/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module.exports = {
displayName: 'auth',
preset: '../../jest.preset.js',
setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'],
globals: {
'ts-jest': {
tsConfig: '<rootDir>/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',
],
};
2 changes: 2 additions & 0 deletions libs/auth/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './lib/auth.module';
export * from './lib/authorization.service';
10 changes: 10 additions & 0 deletions libs/auth/src/lib/auth.module.ts
Original file line number Diff line number Diff line change
@@ -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 {}
29 changes: 29 additions & 0 deletions libs/auth/src/lib/authorization.directive.ts
Original file line number Diff line number Diff line change
@@ -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<unknown>,
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;
}
});
}
}
16 changes: 16 additions & 0 deletions libs/auth/src/lib/authorization.service.spec.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
12 changes: 12 additions & 0 deletions libs/auth/src/lib/authorization.service.ts
Original file line number Diff line number Diff line change
@@ -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<boolean> {
//** @todo do a call to backend and cache result */
return of(true);
}
}
1 change: 1 addition & 0 deletions libs/auth/src/test-setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import 'jest-preset-angular';
13 changes: 13 additions & 0 deletions libs/auth/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": "../../tsconfig.base.json",
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.lib.json"
},
{
"path": "./tsconfig.spec.json"
}
]
}
19 changes: 19 additions & 0 deletions libs/auth/tsconfig.lib.json
Original file line number Diff line number Diff line change
@@ -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"]
}
10 changes: 10 additions & 0 deletions libs/auth/tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -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"]
}
3 changes: 3 additions & 0 deletions nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
},
"cognito": {
"tags": []
},
"auth": {
"tags": []
}
}
}
3 changes: 2 additions & 1 deletion tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down