Skip to content

Commit

Permalink
feat(stark-rbac): update rbac actions style
Browse files Browse the repository at this point in the history
BREAKING CHANGE:
Due to an improvement on how actions are defined, the enum `StarkRBACAuthorizationActionsTypes`
became obsolete so it has been removed.

As a result, the following actions have been changed:
- `StarkUserNavigationUnauthorized(public targetState: string)`
  -> `StarkRBACAuthorizationActions.userNavigationUnauthorized({ targetState: string })`
- `StarkUserNavigationUnauthorizedRedirected(public targetState: string,
  public redirectionState: string)` ->
  `StarkRBACAuthorizationActions.userNavigationUnauthorizedRedirected({ targetState: string;
  redirectionState: string })`

And also the previous union type has been replaced:
`StarkRBACAuthorizationActions` -> `StarkRBACAuthorizationActions.Types`.

Change in effect:

```typescript
// Before
@effect({ dispatch: false })
public starkRBACNavigationUnauthorized$(): Observable<void> {
    return this.actions$.pipe(
        ofType<StarkUserNavigationUnauthorized>(
            StarkRBACAuthorizationActionsTypes.RBAC_USER_NAVIGATION_UNAUTHORIZED
        ),
        map((action: StarkUserNavigationUnauthorized) => {
            // some logic
        })
    );
}

// After
public starkRBACNavigationUnauthorizedRedirected$ = createEffect(
    () => this.actions$.pipe(
        ofType(StarkRBACAuthorizationActions.userNavigationUnauthorized),
        map((action) => {
            // some logic
        })
    ),
    { dispatch: false }
);
```

Change in `action` usage:
```typescript
// Before
this.store.dispatch(new StarkUserNavigationUnauthorized(transition.targetState().name()));

// After
this.store.dispatch(StarkRBACAuthorizationActions.userNavigationUnauthorized({
    targetState: transition.targetState().name()
}));
```
  • Loading branch information
SuperITMan committed May 3, 2021
1 parent 3ff099f commit c4efd6a
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 88 deletions.
6 changes: 6 additions & 0 deletions packages/stark-rbac/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/stark-rbac/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"@types/lodash-es": "^4.17.1"
},
"devDependencies": {
"@ngrx/store": "^8.6.1",
"@types/jasmine": "^3.5.0",
"@types/node": "^10.17.13"
},
Expand Down
3 changes: 2 additions & 1 deletion packages/stark-rbac/src/modules/authorization/actions.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./actions/authorization.actions";
import * as StarkRBACAuthorizationActions from "./actions/authorization.actions";
export { StarkRBACAuthorizationActions };
Original file line number Diff line number Diff line change
@@ -1,44 +1,27 @@
import { Action } from "@ngrx/store";

/**
* Actions related to {@link StarkRBACAuthorizationService}
*/
export enum StarkRBACAuthorizationActionsTypes {
RBAC_USER_NAVIGATION_UNAUTHORIZED = "[StarkRBAC] User navigation unauthorized",
RBAC_USER_NAVIGATION_UNAUTHORIZED_REDIRECTED = "[StarkRBAC] User navigation unauthorized redirected"
}
import { createAction, props, union } from "@ngrx/store";

/**
* Action to be triggered when the user has navigated to a route that he is not authorized to.
*
* Parameter:
* - targetState - The state the user is navigating to
*/
export class StarkUserNavigationUnauthorized implements Action {
/**
* The type of action
*/
public readonly type: StarkRBACAuthorizationActionsTypes.RBAC_USER_NAVIGATION_UNAUTHORIZED =
StarkRBACAuthorizationActionsTypes.RBAC_USER_NAVIGATION_UNAUTHORIZED;

/**
* Class constructor
* @param targetState - The state the user is navigating to.
*/
public constructor(public targetState: string) {}
}
export const userNavigationUnauthorized = createAction("[StarkRBAC] User navigation unauthorized", props<{ targetState: string }>());

/**
* Action to be triggered when the user is redirected because he is not authorized to navigate to the original route.
*
* Parameters:
* - targetState - The state the user is navigating to
* - redirectionState - The redirection to be performed instead of the original navigation
*/
export class StarkUserNavigationUnauthorizedRedirected implements Action {
/**
* The type of action
*/
public readonly type: StarkRBACAuthorizationActionsTypes.RBAC_USER_NAVIGATION_UNAUTHORIZED_REDIRECTED =
StarkRBACAuthorizationActionsTypes.RBAC_USER_NAVIGATION_UNAUTHORIZED_REDIRECTED;
export const userNavigationUnauthorizedRedirected = createAction(
"[StarkRBAC] User navigation unauthorized redirected",
props<{ targetState: string; redirectionState: string }>()
);

/**
* Class constructor
* @param targetState - The state the user is navigating to
* @param redirectionState - The redirection to be performed instead of the original navigation
*/
public constructor(public targetState: string, public redirectionState: string) {}
}
/**
* @ignore
*/
const all = union({ userNavigationUnauthorized, userNavigationUnauthorizedRedirected });
export type Types = typeof all;
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { MockStarkLoggingService, MockStarkRoutingService, MockStarkSessionServi

import { StarkRBACStatePermissions, StarkStateRedirection, StarkStateRedirectionFn } from "../entities";
import { StarkRBACAuthorizationServiceImpl, starkUnauthorizedUserError } from "./authorization.service";
import { StarkUserNavigationUnauthorized, StarkUserNavigationUnauthorizedRedirected } from "../actions";
import { StarkRBACAuthorizationActions } from "../actions";
import createSpyObj = jasmine.createSpyObj;
import createSpy = jasmine.createSpy;
import Spy = jasmine.Spy;
Expand Down Expand Up @@ -412,7 +412,9 @@ describe("StarkRBACAuthorizationService", () => {
expect(mockLogger.warn).toHaveBeenCalledTimes(1);
expect(mockLogger.warn).toHaveBeenCalledWith(starkUnauthorizedUserError);
expect(mockStore.dispatch).toHaveBeenCalledTimes(1);
expect(mockStore.dispatch).toHaveBeenCalledWith(new StarkUserNavigationUnauthorized(dummyUnauthorizedStateName));
expect(mockStore.dispatch).toHaveBeenCalledWith(
StarkRBACAuthorizationActions.userNavigationUnauthorized({ targetState: dummyUnauthorizedStateName })
);

mockPermissions = <any>undefined;
mockLogger.warn.calls.reset();
Expand Down Expand Up @@ -481,7 +483,10 @@ describe("StarkRBACAuthorizationService", () => {
expect(mockLogger.warn.calls.argsFor(0)[0]).toContain("redirecting");
expect(mockStore.dispatch).toHaveBeenCalledTimes(1);
expect(mockStore.dispatch).toHaveBeenCalledWith(
new StarkUserNavigationUnauthorizedRedirected(dummyUnauthorizedStateName, mockRedirectToObj.stateName)
StarkRBACAuthorizationActions.userNavigationUnauthorizedRedirected({
targetState: dummyUnauthorizedStateName,
redirectionState: mockRedirectToObj.stateName
})
);
});

Expand All @@ -504,7 +509,10 @@ describe("StarkRBACAuthorizationService", () => {
expect(mockLogger.warn.calls.argsFor(0)[0]).toContain("redirecting");
expect(mockStore.dispatch).toHaveBeenCalledTimes(1);
expect(mockStore.dispatch).toHaveBeenCalledWith(
new StarkUserNavigationUnauthorizedRedirected(dummyUnauthorizedStateName, mockRedirectToObj.stateName)
StarkRBACAuthorizationActions.userNavigationUnauthorizedRedirected({
targetState: dummyUnauthorizedStateName,
redirectionState: mockRedirectToObj.stateName
})
);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
} from "@nationalbankbelgium/stark-core";
import { StarkRBACAuthorizationService, starkRBACAuthorizationServiceName } from "./authorization.service.intf";
import { StarkRBACStatePermissions, StarkStateRedirection, StarkStateRedirectionFn } from "../entities";
import { StarkUserNavigationUnauthorized, StarkUserNavigationUnauthorizedRedirected } from "../actions";
import { StarkRBACAuthorizationActions } from "../actions";

/**
* @ignore
Expand Down Expand Up @@ -143,7 +143,7 @@ export class StarkRBACAuthorizationServiceImpl implements StarkRBACAuthorization
}

// dispatch action so an effect can run any logic if needed
this.store.dispatch(new StarkUserNavigationUnauthorized(transition.targetState().name()));
this.store.dispatch(StarkRBACAuthorizationActions.userNavigationUnauthorized({ targetState: transition.targetState().name() }));
throw new Error(starkUnauthorizedUserError);
}

Expand All @@ -159,7 +159,12 @@ export class StarkRBACAuthorizationServiceImpl implements StarkRBACAuthorization
this.logger.warn(starkRBACAuthorizationServiceName + ": redirecting to state '" + stateRedirection.stateName + "'");
const originalTargetState: TargetState = transition.targetState();
// dispatch action so an effect can run any logic if needed
this.store.dispatch(new StarkUserNavigationUnauthorizedRedirected(originalTargetState.name(), stateRedirection.stateName));
this.store.dispatch(
StarkRBACAuthorizationActions.userNavigationUnauthorizedRedirected({
targetState: originalTargetState.name(),
redirectionState: stateRedirection.stateName
})
);
// overriding the target state with that one to be redirected to
return originalTargetState.withState(stateRedirection.stateName).withParams(stateRedirection.params || {}, true);
}
Expand Down
1 change: 1 addition & 0 deletions packages/stark-rbac/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@nationalbankbelgium/stark-rbac/testing": ["./testing/public_api.ts"],
"@ng-idle/*": ["../stark-core/node_modules/@ng-idle/*"],
"@ngrx/*": ["../stark-core/node_modules/@ngrx/*"],
"@ngrx/store": ["./node_modules/@ngrx/store"],
"@ngx-translate/*": ["../stark-core/node_modules/@ngx-translate/*"],
"@uirouter/*": ["../stark-core/node_modules/@uirouter/*"],
"cerialize": ["../stark-core/node_modules/cerialize"],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { Injectable, Injector, NgZone } from "@angular/core";
import { Actions, Effect, ofType } from "@ngrx/effects";
import { Observable } from "rxjs";
import { Actions, createEffect, ofType } from "@ngrx/effects";
import { map } from "rxjs/operators";
import {
StarkRBACAuthorizationActionsTypes,
StarkUserNavigationUnauthorized,
StarkUserNavigationUnauthorizedRedirected
} from "@nationalbankbelgium/stark-rbac";
import { StarkRBACAuthorizationActions } from "@nationalbankbelgium/stark-rbac";
import { STARK_TOAST_NOTIFICATION_SERVICE, StarkMessageType, StarkToastNotificationService } from "@nationalbankbelgium/stark-ui";
import uniqueId from "lodash-es/uniqueId";

Expand All @@ -25,46 +20,46 @@ export class StarkRbacUnauthorizedNavigationEffects {
*/
public constructor(private actions$: Actions, private injector: Injector, private zone: NgZone) {}

@Effect({ dispatch: false })
public starkRBACNavigationUnauthorized$(): Observable<void> {
return this.actions$.pipe(
ofType<StarkUserNavigationUnauthorized>(StarkRBACAuthorizationActionsTypes.RBAC_USER_NAVIGATION_UNAUTHORIZED),
map((action: StarkUserNavigationUnauthorized) => {
this.zone.run(() => {
this.toastNotificationService
.show({
id: uniqueId(),
type: StarkMessageType.ERROR,
key: action.type,
code: "Stark-RBAC: unauthorized navigation"
})
.subscribe();
});
})
);
}
public starkRBACNavigationUnauthorized$ = createEffect(
() =>
this.actions$.pipe(
ofType(StarkRBACAuthorizationActions.userNavigationUnauthorized),
map((action) => {
this.zone.run(() => {
this.toastNotificationService
.show({
id: uniqueId(),
type: StarkMessageType.ERROR,
key: action.type,
code: "Stark-RBAC: unauthorized navigation"
})
.subscribe();
});
})
),
{ dispatch: false }
);

@Effect({ dispatch: false })
public starkRBACNavigationUnauthorizedRedirected$(): Observable<void> {
return this.actions$.pipe(
ofType<StarkUserNavigationUnauthorizedRedirected>(
StarkRBACAuthorizationActionsTypes.RBAC_USER_NAVIGATION_UNAUTHORIZED_REDIRECTED
public starkRBACNavigationUnauthorizedRedirected$ = createEffect(
() =>
this.actions$.pipe(
ofType(StarkRBACAuthorizationActions.userNavigationUnauthorizedRedirected),
map((action) => {
this.zone.run(() => {
this.toastNotificationService
.show({
id: uniqueId(),
type: StarkMessageType.WARNING,
key: "SHOWCASE.DEMO_RBAC.SERVICES.AUTHORIZATION.REDIRECTION_MESSAGE",
interpolateValues: { rbacActionType: action.type },
code: "Stark-RBAC: unauthorized navigation redirected"
})
.subscribe();
});
})
),
map((action: StarkUserNavigationUnauthorizedRedirected) => {
this.zone.run(() => {
this.toastNotificationService
.show({
id: uniqueId(),
type: StarkMessageType.WARNING,
key: "SHOWCASE.DEMO_RBAC.SERVICES.AUTHORIZATION.REDIRECTION_MESSAGE",
interpolateValues: { rbacActionType: action.type },
code: "Stark-RBAC: unauthorized navigation redirected"
})
.subscribe();
});
})
);
}
{ dispatch: false }
);

/**
* Gets the StarkToastNotificationService from the Injector.
Expand Down

0 comments on commit c4efd6a

Please sign in to comment.