Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding guards to filter user roles on pages #21

Merged
merged 3 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { LoggedInGuard } from './login-basic/loggedin.guard';
import { AboutComponent } from './about/about.component';
import { NotFoundComponent } from './error-handler/error-alert/not-found.component';
import { UserListComponent } from './user/user-list/user-list.component';
Expand All @@ -14,19 +13,20 @@ import { RouteListComponent } from './routes/route-list/route-list.component';
import { RouteCreateComponent } from './routes/route-create/route-create.component';
import { RouteDetailComponent } from './routes/route-detail/route-detail.component';
import { RouteEditComponent } from './routes/routes-edit/route-edit.component';
import {CheckIsAdminGuard, CheckIsNotAdminGuard,CheckLoggedInGuard} from "./login-basic/authentication.guard";

const routes: Routes = [
{ path: 'users/create', component: UserRegisterComponent},
{ path: 'users/:id/delete', component: UserDeleteComponent, canActivate: [LoggedInGuard]},
{ path: 'users/:id/edit', component: UserEditComponent, canActivate: [LoggedInGuard]},
{ path: 'users/:id', component: UserDetailComponent, canActivate: [LoggedInGuard]},
{ path: 'users', component: UserListComponent, canActivate: [LoggedInGuard]},
{ path: 'users/:id/delete', component: UserDeleteComponent, canActivate: [CheckLoggedInGuard]},
{ path: 'users/:id/edit', component: UserEditComponent, canActivate: [CheckLoggedInGuard]},
{ path: 'users/:id', component: UserDetailComponent, canActivate: [CheckLoggedInGuard]},
{ path: 'users', component: UserListComponent, canActivate: [CheckLoggedInGuard]},

{ path: 'routes/create', component: RouteCreateComponent, canActivate: [LoggedInGuard] },
{ path: 'routes/:id/delete', component: RouteDeleteComponent, canActivate: [LoggedInGuard]},
{ path: 'routes/:id/edit', component: RouteEditComponent, canActivate: [LoggedInGuard]},
{ path: 'routes/:id', component: RouteDetailComponent, canActivate: [LoggedInGuard]},
{ path: 'routes', component: RouteListComponent, canActivate: [LoggedInGuard] },
{ path: 'routes/create', component: RouteCreateComponent, canActivate: [CheckIsNotAdminGuard] },
{ path: 'routes/:id/delete', component: RouteDeleteComponent, canActivate: [CheckIsAdminGuard]},
{ path: 'routes/:id/edit', component: RouteEditComponent, canActivate: [CheckIsNotAdminGuard]},
{ path: 'routes/:id', component: RouteDetailComponent, canActivate: [CheckIsNotAdminGuard]},
{ path: 'routes', component: RouteListComponent, canActivate: [CheckIsNotAdminGuard] },

{ path: 'about', component: AboutComponent},
{ path: '404', component: NotFoundComponent},
Expand Down
4 changes: 2 additions & 2 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {ErrorHandlerModule} from './error-handler/error-handler.module';
import {AuthInterceptor} from './login-basic/auth-interceptor';
import {HttpErrorInterceptor} from './error-handler/http-error-interceptor';
import {AuthenticationBasicService} from './login-basic/authentication-basic.service';
import {LoggedInGuard} from './login-basic/loggedin.guard';
import {UserService} from './user/user.service';

import {RouteCreateComponent} from "./routes/route-create/route-create.component";
Expand All @@ -31,6 +30,7 @@ import { RouteEditComponent } from './routes/routes-edit/route-edit.component';
import { RouteDeleteComponent } from './routes/route-delete/route-delete.component';
import { RouteSearchComponent } from './routes/route-search/route-search.component';
import { RouteFilterComponent } from './routes/route-filter/route-filter.component';
import {PermissionsService } from "./login-basic/authentication.guard";


@NgModule({
Expand Down Expand Up @@ -72,7 +72,7 @@ import { RouteFilterComponent } from './routes/route-filter/route-filter.compone
providers: [
{ provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true },
{ provide: HTTP_INTERCEPTORS, useClass: HttpErrorInterceptor, multi: true },
AuthenticationBasicService, LoggedInGuard, UserService
AuthenticationBasicService, PermissionsService, UserService
],
bootstrap: [AppComponent]
})
Expand Down
51 changes: 51 additions & 0 deletions src/app/login-basic/authentication.guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import {inject, Injectable} from "@angular/core";
import {ErrorMessageService} from "../error-handler/error-message.service";
import {AuthenticationBasicService} from "./authentication-basic.service";
import {CanActivateFn} from "@angular/router";


@Injectable()
export class PermissionsService{
constructor(private authentication: AuthenticationBasicService,
private errorMessageService: ErrorMessageService) {
}

checkLoggedIn(): boolean {
if (!this.authentication.isLoggedIn()) {
this.errorMessageService.showErrorMessage('You should be logged in to perform this action');
}
return this.authentication.isLoggedIn();
}

checkUserAdmin(): boolean {
if (this.checkLoggedIn()){
if (!this.authentication.isRole('admin')) {
this.errorMessageService.showErrorMessage('You should be role admin to perform this action');
}
return this.authentication.isRole('admin');
}else
return false;
}

checkUserNotAdmin(): boolean {
if (this.checkLoggedIn()){
if (this.authentication.isRole('admin')) {
this.errorMessageService.showErrorMessage('You should be role user to perform this action');
}
return !this.authentication.isRole('admin');
}else
return false;
}
}

export const CheckLoggedInGuard: CanActivateFn = (route, state) => {
return inject(PermissionsService).checkLoggedIn();
};

export const CheckIsAdminGuard: CanActivateFn = (route, state) => {
return inject(PermissionsService).checkUserAdmin();
};

export const CheckIsNotAdminGuard: CanActivateFn = (route, state) => {
return inject(PermissionsService).checkUserNotAdmin();
};
18 changes: 0 additions & 18 deletions src/app/login-basic/loggedin.guard.ts

This file was deleted.

Loading