Skip to content

Commit a3abb6c

Browse files
committed
feat(authz): hide invoice for non admin users
1 parent b6fe348 commit a3abb6c

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

packages/modules/main/src/lib/components/template/private-template.component.ts

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export class PrivateTemplateComponent
4141

4242
ngOnInit(): void {
4343
this.subscriptions.add(this.notifications$.subscribe());
44+
// TODO Move this to the Api handling services...
4445
this.subscriptions.add(
4546
this.authFacade.expiresIn$.subscribe((expiresIn) => {
4647
const expires = new Date(expiresIn as string);

packages/modules/ui/src/lib/components/templates/private/private-template.component.html

+4
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,16 @@
5151
[routerLinkActive]="['selected']"
5252
>Products</rc-drawer-navigation-item
5353
>
54+
<!-- Show for only superadmins. -->
55+
@if (isAdmin$ | async) {
5456
<rc-drawer-navigation-item
5557
icon="mdi:invoice-outline"
5658
[routerLink]="ROUTER.pages.main.children.invoices.link"
5759
[routerLinkActive]="['selected']"
5860
>Invoices</rc-drawer-navigation-item
5961
>
62+
}
63+
6064
<rc-drawer-navigation-item
6165
icon="mdi:truck-outline"
6266
[routerLink]="ROUTER.pages.main.children.fulfillments.link"

packages/modules/ui/src/lib/components/templates/private/private-template.component.ts

+59-2
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,38 @@ import {
88
HostListener,
99
} from '@angular/core';
1010
import { NavigationEnd, Router } from '@angular/router';
11+
import { combineLatest } from 'rxjs';
1112
import { filter, map, startWith } from 'rxjs/operators';
1213
import { SubSink } from 'subsink';
1314

1415
import { VCLBreakpoints } from '@vcl/ng-vcl';
1516

1617
import { APP, ROUTER } from '@console-core/config';
18+
import { AccountFacade, OrganizationContextFacade } from '@console-core/state';
19+
import { IOrganization } from '@console-core/types';
1720

1821
import { RcDrawerService } from '../../../services';
1922

23+
const isHierarchical = (
24+
root: string,
25+
decendant: string | undefined | null,
26+
organizations: IOrganization[]
27+
): boolean => {
28+
if (!root || !decendant) return false;
29+
30+
const parentMap = new Map<string, string | null>(
31+
organizations.map((org) => [org.id, String(org.parentId)])
32+
);
33+
34+
while (decendant && parentMap.has(decendant)) {
35+
if (decendant === root) return true;
36+
decendant = parentMap.get(decendant) ?? null;
37+
if (decendant === null) break;
38+
}
39+
40+
return false;
41+
};
42+
2043
@Component({
2144
selector: 'rc-private-template',
2245
templateUrl: './private-template.component.html',
@@ -59,11 +82,45 @@ export class RcPrivateTemplateComponent implements OnInit, OnDestroy {
5982
ROUTER.pages.main.children.fulfillments.link,
6083
];
6184

85+
isSuperAdmin$ = this.accountFacade.user$.pipe(
86+
map((user) => {
87+
return user?.roleAssociations.some(
88+
(ra) => ra.role === 'superadministrator-r-id'
89+
);
90+
})
91+
);
92+
93+
isAdmin$ = combineLatest([
94+
this.accountFacade.user$,
95+
this.organizationContextFacade.selectedId$,
96+
this.organizationContextFacade.all$,
97+
]).pipe(
98+
map(([user, organizationId, organizations]) => {
99+
return user?.roleAssociations.some(
100+
(ra) =>
101+
ra.role === 'administrator-r-id' &&
102+
ra.attributes?.some((attr) =>
103+
attr.attributes?.some(
104+
(inst) =>
105+
inst.value === organizationId ||
106+
isHierarchical(
107+
String(inst.value),
108+
String(organizationId),
109+
organizations
110+
)
111+
)
112+
)
113+
);
114+
})
115+
);
116+
62117
constructor(
118+
private readonly accountFacade: AccountFacade,
63119
private readonly breakpointObserver: BreakpointObserver,
64-
private readonly router: Router,
120+
private readonly changeDetectorRef: ChangeDetectorRef,
65121
private readonly drawerService: RcDrawerService,
66-
private readonly changeDetectorRef: ChangeDetectorRef
122+
private readonly organizationContextFacade: OrganizationContextFacade,
123+
private readonly router: Router
67124
) {}
68125

69126
ngOnInit(): void {

0 commit comments

Comments
 (0)