Added routing by permissions #130

This commit is contained in:
2023-02-16 21:24:29 +01:00
parent e2ef4f3bde
commit f847841582
17 changed files with 260 additions and 26 deletions

View File

@@ -1,13 +1,28 @@
import { AuthRoles } from "./auth-roles.enum";
import {AuthRoles} from "./auth-roles.enum";
export interface AuthUserDTO {
id?: number;
firstName: string | null;
lastName: string | null;
email: string | null;
password: string | null;
isConfirmed?: boolean
authRole?: AuthRoles;
createdAt?: string;
modifiedAt?: string;
id?: number;
firstName: string | null;
lastName: string | null;
email: string | null;
password: string | null;
isConfirmed?: boolean;
authRole?: AuthRoles;
users?: UserDTO[];
createdAt?: string;
modifiedAt?: string;
}
export interface UserDTO {
id: number;
discordId: number;
xp: number;
minecraftId: number | null;
server: number;
createdAt: string;
modifiedAt: string;
isTechnician: boolean;
isAdmin: boolean;
IsModerator: boolean;
}

View File

@@ -122,7 +122,7 @@ export class DashboardComponent implements OnInit {
}
selectServer(server: Server) {
this.sidebar.serverName$.next(server.name ?? "");
this.sidebar.server$.next(server);
this.router.navigate(["/server", server.id]);
}

View File

@@ -0,0 +1 @@
<p>profile works!</p>

View File

@@ -0,0 +1,23 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ProfileComponent } from './profile.component';
describe('ProfileComponent', () => {
let component: ProfileComponent;
let fixture: ComponentFixture<ProfileComponent>;
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ ProfileComponent ]
})
.compileComponents();
fixture = TestBed.createComponent(ProfileComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});

View File

@@ -0,0 +1,10 @@
import { Component } from '@angular/core';
@Component({
selector: 'app-profile',
templateUrl: './profile.component.html',
styleUrls: ['./profile.component.scss']
})
export class ProfileComponent {
}

View File

@@ -42,7 +42,7 @@ export class ServerDashboardComponent implements OnInit {
}
).subscribe(server => {
this.server = server;
this.sidebar.serverName$.next(server.name ?? "");
this.sidebar.server$.next(server);
this.spinner.hideSpinner();
});
}

View File

@@ -1,9 +1,11 @@
import { NgModule } from "@angular/core";
import { RouterModule, Routes } from "@angular/router";
import { ServerDashboardComponent } from "./server-dashboard/server-dashboard.component";
import { ProfileComponent } from "./profile/profile.component";
const routes: Routes = [
{ path: '', component: ServerDashboardComponent },
{ path: 'profile', component: ProfileComponent },
];
@NgModule({

View File

@@ -3,12 +3,14 @@ import { CommonModule } from '@angular/common';
import { ServerDashboardComponent } from './server-dashboard/server-dashboard.component';
import { ServerRoutingModule } from './server-routing.module';
import { SharedModule } from '../../shared/shared.module';
import { ProfileComponent } from './profile/profile.component';
@NgModule({
declarations: [
ServerDashboardComponent
ServerDashboardComponent,
ProfileComponent
],
imports: [
CommonModule,

View File

@@ -241,6 +241,20 @@ export class AuthService {
return null
}
async getLoggedInUser(): Promise<AuthUserDTO | null> {
if (!await this.isUserLoggedInAsync()) {
return null;
}
const token = this.getDecodedToken();
if (!token) return null;
try {
return await firstValueFrom(this.findUserByEMail(token["email"]));
} catch (error: unknown) {
return null;
}
}
async isUserLoggedInAsync(): Promise<boolean> {
const token = this.getToken();

View File

@@ -4,8 +4,10 @@ import { BehaviorSubject } from "rxjs";
import { AuthRoles } from "../../models/auth/auth-roles.enum";
import { AuthService } from "../auth/auth.service";
import { TranslateService } from "@ngx-translate/core";
import { ActivatedRoute, NavigationEnd, Router } from "@angular/router";
import { NavigationEnd, Router } from "@angular/router";
import { ThemeService } from "../theme/theme.service";
import { Server } from "../../models/data/server.model";
import { UserDTO } from "../../models/auth/auth-user.dto";
@Injectable({
providedIn: "root"
@@ -13,14 +15,14 @@ import { ThemeService } from "../theme/theme.service";
export class SidebarService {
isSidebarOpen: boolean = true;
menuItems$: BehaviorSubject<MenuItem[]> = new BehaviorSubject(new Array<MenuItem>());
serverName$: BehaviorSubject<string> = new BehaviorSubject("");
menuItems$ = new BehaviorSubject<MenuItem[]>(new Array<MenuItem>());
server$ = new BehaviorSubject<Server | null>(null);
constructor(
private themeService: ThemeService,
private authService: AuthService,
private translateService: TranslateService,
private router: Router,
private router: Router
) {
this.themeService.isSidebarOpen$.subscribe(value => {
this.isSidebarOpen = value;
@@ -28,7 +30,7 @@ export class SidebarService {
});
this.serverName$.subscribe(value => {
this.server$.subscribe(value => {
this.setMenu();
});
@@ -40,18 +42,27 @@ export class SidebarService {
}
setMenu() {
this.authService.hasUserPermission(AuthRoles.Admin).then(hasPermission => {
this.authService.hasUserPermission(AuthRoles.Admin).then(async hasPermission => {
let menuItems: MenuItem[] = [
{ label: this.isSidebarOpen ? this.translateService.instant("sidebar.dashboard") : "", icon: "pi pi-th-large", routerLink: "dashboard" }
];
const serverMenu = {
label: this.isSidebarOpen ? this.serverName$.value : "", icon: "pi pi-server", items: [
{ label: this.isSidebarOpen ? this.translateService.instant("sidebar.settings") : "", icon: "pi pi-cog", routerLink: "server/settings" },
{ label: this.isSidebarOpen ? this.translateService.instant("sidebar.members") : "", icon: "pi pi-users", routerLink: "server/members" }
label: this.isSidebarOpen ? this.server$.value?.name : "", icon: "pi pi-server", items: [
{ label: this.isSidebarOpen ? this.translateService.instant("sidebar.profile") : "", icon: "pi pi-user", routerLink: `server/${this.server$.value?.id}/profile` },
// { label: this.isSidebarOpen ? this.translateService.instant("sidebar.members") : "", icon: "pi pi-users", routerLink: "server/members" }
]
};
if (this.serverName$.value != "") {
if (this.server$.value) {
let authUser = await this.authService.getLoggedInUser();
let user: UserDTO | null = authUser?.users?.find(u => u.server == this.server$.value?.id) ?? null;
if (user?.isAdmin) {
serverMenu.items.push(
{ label: this.isSidebarOpen ? this.translateService.instant("sidebar.members") : "", icon: "pi pi-users", routerLink: `server/${this.server$.value?.id}/members` }
);
}
menuItems.push(serverMenu);
} else if (menuItems.find(x => x.icon == "pi pi-server")) {
menuItems.splice(menuItems.indexOf(serverMenu), 1);