From 947e5a1218cdf711ccfb8159fc610d0635bbc8d5 Mon Sep 17 00:00:00 2001 From: Frederick Katsura Date: Wed, 13 Nov 2024 20:17:20 -0500 Subject: [PATCH] Updated Moderator Check to Use Permissions --- src/app/constants/UserPermissions.ts | 3 ++- .../authentication/auth-user.service.ts | 2 +- .../authentication/is-moderator.guard.ts | 17 +++++------------ 3 files changed, 8 insertions(+), 14 deletions(-) diff --git a/src/app/constants/UserPermissions.ts b/src/app/constants/UserPermissions.ts index 7b69c9c..a609269 100644 --- a/src/app/constants/UserPermissions.ts +++ b/src/app/constants/UserPermissions.ts @@ -4,5 +4,6 @@ export enum UserPermissions { UPDATE_USERS = "update_users", ADD_ENTRY = "add_entry", DELETE_ENTRY = "delete_entry", - MODIFY_ENTRY = "modify_entry" + MODIFY_ENTRY = "modify_entry", + STAFF = "staff" } diff --git a/src/app/services/authentication/auth-user.service.ts b/src/app/services/authentication/auth-user.service.ts index 65f6720..404473c 100644 --- a/src/app/services/authentication/auth-user.service.ts +++ b/src/app/services/authentication/auth-user.service.ts @@ -29,7 +29,7 @@ export class AuthUserService { } isModerator() { - return this.user !== null && !this.user.role_name.includes("user"); + return this.user !== null && this.userPermissions.includes(UserPermissions.STAFF); } isSameUser(username: string) { diff --git a/src/app/services/authentication/is-moderator.guard.ts b/src/app/services/authentication/is-moderator.guard.ts index 59bda25..5013c12 100644 --- a/src/app/services/authentication/is-moderator.guard.ts +++ b/src/app/services/authentication/is-moderator.guard.ts @@ -1,12 +1,9 @@ import { inject } from "@angular/core"; import { Router } from "@angular/router"; -import { tap } from "rxjs"; -import { AuthenticationService } from "./authentication.service"; import { AuthUserService } from "./auth-user.service"; export const IsModeratorGuard = () => { const router = inject(Router); - const authService = inject(AuthenticationService); const AuthUser = inject(AuthUserService); if (!AuthUser.user) { @@ -14,14 +11,10 @@ export const IsModeratorGuard = () => { return; } - return authService.validateCookies().pipe( - tap((value) => { - if (!value || !AuthUser.isModerator()) { - router.navigate(["/login"]); - return; - } + if (!AuthUser.isModerator()) { + router.navigate(["/login"]); + return; + } - return true; - }) - ); + return true; };