Skip to content

Commit

Permalink
add service account access guard
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas-Avery committed Oct 18, 2023
1 parent 53ac02d commit 0a3c0ce
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { inject } from "@angular/core";
import { ActivatedRouteSnapshot, CanActivateFn, createUrlTreeFromSnapshot } from "@angular/router";

import { ServiceAccountService } from "../service-account.service";

/**
* Redirects to service accounts page if the user doesn't have access to service account.
*/
export const serviceAccountAccessGuard: CanActivateFn = async (route: ActivatedRouteSnapshot) => {
const serviceAccountService = inject(ServiceAccountService);

try {
const serviceAccount = await serviceAccountService.getByServiceAccountId(
route.params.serviceAccountId,
route.params.organizationId
);
if (serviceAccount) {
return true;
}
} catch {
return createUrlTreeFromSnapshot(route, [
"/sm",
route.params.organizationId,
"service-accounts",
]);
}
return createUrlTreeFromSnapshot(route, ["/sm", route.params.organizationId, "service-accounts"]);
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { RouterModule, Routes } from "@angular/router";

import { AccessTokenComponent } from "./access/access-tokens.component";
import { ServiceAccountEventsComponent } from "./event-logs/service-accounts-events.component";
import { serviceAccountAccessGuard } from "./guards/service-account-access.guard";
import { ServiceAccountPeopleComponent } from "./people/service-account-people.component";
import { ServiceAccountProjectsComponent } from "./projects/service-account-projects.component";
import { ServiceAccountComponent } from "./service-account.component";
Expand All @@ -16,6 +17,7 @@ const routes: Routes = [
{
path: ":serviceAccountId",
component: ServiceAccountComponent,
canActivate: [serviceAccountAccessGuard],
children: [
{
path: "",
Expand Down

0 comments on commit 0a3c0ce

Please sign in to comment.