Added: Alembic for migrations
Moving from Pyramid to FastAPI
This commit is contained in:
@ -1,39 +1,34 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot } from '@angular/router';
|
||||
import { Observable } from 'rxjs/internal/Observable';
|
||||
import { map } from 'rxjs/operators';
|
||||
import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
|
||||
|
||||
import { AuthService } from './auth.service';
|
||||
import { ToasterService } from '../core/toaster.service';
|
||||
import { User } from '../core/user';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
@Injectable({providedIn: 'root'})
|
||||
export class AuthGuard implements CanActivate {
|
||||
|
||||
constructor(private auth: AuthService, private router: Router, private toaster: ToasterService) {
|
||||
constructor(
|
||||
private router: Router,
|
||||
private authService: AuthService,
|
||||
private toaster: ToasterService
|
||||
) {
|
||||
}
|
||||
|
||||
checkUser(permission: string, user: User, state: RouterStateSnapshot): boolean {
|
||||
if (!user.isAuthenticated) {
|
||||
this.router.navigate(['login'], {queryParams: {returnUrl: state.url}});
|
||||
this.toaster.show('Danger', 'User is not authenticated');
|
||||
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
|
||||
const user = this.authService.user;
|
||||
const permission = (route.data['permission'] === undefined) ? route.data['permission'] : route.data['permission']
|
||||
.replace(/ /g, '-')
|
||||
.toLowerCase();
|
||||
if (!user) {
|
||||
// not logged in so redirect to login page with the return url
|
||||
this.router.navigate(['/login'], {queryParams: {returnUrl: state.url}});
|
||||
return false;
|
||||
}
|
||||
|
||||
const hasPermission = this.auth.hasPermission(permission);
|
||||
if (!hasPermission) {
|
||||
if (permission !== undefined && user.perms.indexOf(permission) === -1) {
|
||||
this.toaster.show('Danger', 'You do not have the permission to access this area.');
|
||||
return false;
|
||||
}
|
||||
return hasPermission;
|
||||
}
|
||||
// logged in so return true
|
||||
return true;
|
||||
|
||||
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
|
||||
return this.auth.userObservable
|
||||
.pipe(
|
||||
map<User, boolean>((value: User) => this.checkUser(
|
||||
next.data['permission'], value, state
|
||||
))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user