Skip to content

Commit

Permalink
style(angular): fix linting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
draconisNoctis committed Jul 4, 2024
1 parent f0eaf21 commit a265cd5
Show file tree
Hide file tree
Showing 6 changed files with 6 additions and 61 deletions.
3 changes: 3 additions & 0 deletions pkg/angular/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"version": "0.0.1",
"scripts": {
"build": "ng build",
"lint": "biome lint .",
"lint:changed": "biome check . --changed --no-errors-on-unmatched",
"lint:staged": "biome check . --staged --no-errors-on-unmatched --write",
"ng": "ng",
"start": "ng serve",
"test": "rm -rf dist && NODE_OPTIONS='--no-deprecation --no-warnings' ng test",
Expand Down
2 changes: 1 addition & 1 deletion pkg/angular/src/lib/directives.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Component, NgZone } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { A38Module } from './a38.module';
import { A38AllowedDirective, A38DeniedDirective } from './directives';
import { DEFAULT_ROLE, RoleStore } from './role-store';
import { RoleStore } from './role-store';

@Component({
standalone: true,
Expand Down
6 changes: 1 addition & 5 deletions pkg/angular/src/lib/guard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { type ComponentFixture, TestBed } from '@angular/core/testing';
import { Router, RouterModule } from '@angular/router';

import { A38Module } from './a38.module';
import { a38Allowed, defineRouteResourceData } from './guard';
import { a38Allowed } from './guard';
import { RoleStore } from './role-store';

@Component({
Expand Down Expand Up @@ -87,25 +87,21 @@ describe('a38Allowed', () => {
path: '',
pathMatch: 'full',
component: DashboardComponent,
// data: { ...defineRouteResourceData('dashboard') },
canActivate: [a38Allowed('dashboard')]
},
{
path: 'login',
component: LoginComponent,
// data: { ...defineRouteResourceData('login') },
canActivate: [a38Allowed('login')]
},
{
path: 'admin',
component: AdminComponent,
// data: { ...defineRouteResourceData('admin') },
canActivate: [a38Allowed('admin')]
},
{
path: 'error/:code',
component: ErrorComponent,
// data: { ...defineRouteResourceData('error') },
canActivate: [a38Allowed('error')]
},
{
Expand Down
51 changes: 0 additions & 51 deletions pkg/angular/src/lib/guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,20 @@ import {
type ActivatedRouteSnapshot,
type CanActivateChildFn,
type CanActivateFn,
type Data,
type GuardResult,
type MaybeAsync,
Router,
type RouterStateSnapshot
} from '@angular/router';
import { HrbacService } from './hrbac.service';
import { RoleStore } from './role-store';

const RESOURCE_ID = Symbol('RESOURCE_ID');
const PRIVILEGE = Symbol('PRIVILEGE');

export type GuardDenyHandler = (
roleId: string,
resource: RouteResource,
privilege: string | null
) => GuardResult | string | Promise<GuardResult | string>;
export const GUARD_DENY_HANDLER = new InjectionToken<GuardDenyHandler>('A38 Guard Deny Handler');

export interface RouteResourceData {
[RESOURCE_ID]: Resource | string;
[PRIVILEGE]: string | null;
}

export class RouteResource extends Resource {
constructor(
resourceId: string,
Expand All @@ -38,46 +28,6 @@ export class RouteResource extends Resource {
}
}

export function defineRouteResourceData(resourceId: string, privilege: string | null = null): RouteResourceData {
return { [RESOURCE_ID]: resourceId, [PRIVILEGE]: privilege };
}

function getRouteResourceData(data: Data): [resourceId: string, privilege: string | null] {
if (!(RESOURCE_ID in data)) {
throw new Error(`Missing RESOURCE_ID in route data, ${JSON.stringify(data)} given.`);
}
if (!(PRIVILEGE in data)) {
throw new Error(`Missing PRIVILEGE in route data, ${JSON.stringify(data)} given.`);
}

return [data[RESOURCE_ID], data[PRIVILEGE]];
}

/**
* @todo: try to use a factory method to provide resourceId and privilege
*/
export const _a38Allowed: CanActivateFn & CanActivateChildFn = async (route, state) => {
const router = inject(Router);
const injector = inject(Injector);
const guardDenyHandler = inject(GUARD_DENY_HANDLER);
const roleId = inject(RoleStore).role();
const [resourceId, privilege] = getRouteResourceData(route.data);
const resource = new RouteResource(resourceId, route, state);

const isAllowed = await inject(HrbacService).isAllowed(roleId, resource, privilege);
// console.log({ roleId, resourceId, isAllowed });
if (!isAllowed) {
const result = await runInInjectionContext(injector, () => guardDenyHandler(roleId, resource, privilege));

if (typeof result === 'string') {
return router.parseUrl(result);
}

return result;
}
return true;
};

export function a38Allowed(resourceId: string, privilege: string | null = null): CanActivateFn & CanActivateChildFn {
return async (route, state) => {
const router = inject(Router);
Expand All @@ -87,7 +37,6 @@ export function a38Allowed(resourceId: string, privilege: string | null = null):
const resource = new RouteResource(resourceId, route, state);

const isAllowed = await inject(HrbacService).isAllowed(roleId, resource, privilege);
// console.log({ roleId, resourceId, isAllowed });
if (!isAllowed) {
const result = await runInInjectionContext(injector, () => guardDenyHandler(roleId, resource, privilege));

Expand Down
2 changes: 1 addition & 1 deletion pkg/angular/src/lib/pipes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Component, NgZone } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { A38Module } from './a38.module';
import { A38AllowedPipe, A38DeniedPipe } from './pipes';
import { DEFAULT_ROLE, RoleStore } from './role-store';
import { RoleStore } from './role-store';

@Component({
standalone: true,
Expand Down
3 changes: 0 additions & 3 deletions pkg/angular/src/lib/role-store.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { InjectionToken, computed, inject } from '@angular/core';
import { patchState, signalStore, withComputed, withMethods, withState } from '@ngrx/signals';

const defaultRole = Symbol('defaultRole');
const currentRole = Symbol('currentRole');

export interface RoleStoreState {
defaultRole: string;
currentRole: string | undefined;
Expand Down

0 comments on commit a265cd5

Please sign in to comment.