Skip to content

Commit

Permalink
add linting to guards model (#877)
Browse files Browse the repository at this point in the history
  • Loading branch information
bossenti committed Jan 2, 2023
1 parent 3760347 commit 938fb55
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 100 deletions.
1 change: 0 additions & 1 deletion ui/.eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ dist
# Remove these in the future to lint additional modules
# Please also see .prettierignore
projects
src/app/_guards
src/app/_models
src/app/add
src/app/apidocs
Expand Down
1 change: 0 additions & 1 deletion ui/.prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ dist
# Remove these in the future to format additional modules
# Please also see .eslintignore
projects
src/app/_guards
src/app/_models
src/app/add
src/app/apidocs
Expand Down
20 changes: 9 additions & 11 deletions ui/src/app/_guards/already-configured.can-activate.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,15 @@ import { BaseConfiguredCanActivateGuard } from './base-configured.can-activate.g

@Injectable()
export class AlreadyConfiguredCanActivateGuard extends BaseConfiguredCanActivateGuard {
constructor(router: Router, authService: AuthService) {
super(router, authService);
}

constructor(router: Router,
authService: AuthService) {
super(router, authService);
}
onIsConfigured(): boolean | UrlTree {
return this.router.parseUrl('login');
}

onIsConfigured(): boolean | UrlTree {
return this.router.parseUrl('login');
}

onIsUnconfigured(): boolean | UrlTree {
return true;
}
onIsUnconfigured(): boolean | UrlTree {
return true;
}
}
33 changes: 16 additions & 17 deletions ui/src/app/_guards/auth.can-activate-children.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,27 @@

import { Injectable } from '@angular/core';
import {
ActivatedRouteSnapshot,
CanActivateChild,
Router,
RouterStateSnapshot
ActivatedRouteSnapshot,
CanActivateChild,
Router,
RouterStateSnapshot,
} from '@angular/router';
import { AuthService } from '../services/auth.service';

@Injectable()
export class AuthCanActivateChildrenGuard implements CanActivateChild {
constructor(private authService: AuthService, private router: Router) {}

constructor(private authService: AuthService,
private router: Router) {
}
canActivateChild(
childRoute: ActivatedRouteSnapshot,
state: RouterStateSnapshot,
): boolean {
if (this.authService.authenticated()) {
return true;
}
this.authService.logout();
this.router.navigate(['/login']);

canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
if (this.authService.authenticated()) {
return true;
}
this.authService.logout();
this.router.navigate(['/login']);

return false;

}
return false;
}
}
54 changes: 32 additions & 22 deletions ui/src/app/_guards/base-configured.can-activate.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,44 @@
*/

import {
ActivatedRouteSnapshot,
CanActivate,
Router,
RouterStateSnapshot,
UrlTree
ActivatedRouteSnapshot,
CanActivate,
Router,
RouterStateSnapshot,
UrlTree,
} from '@angular/router';
import { Observable } from 'rxjs';
import { AuthService } from '../services/auth.service';

export abstract class BaseConfiguredCanActivateGuard implements CanActivate {
constructor(protected router: Router, protected authService: AuthService) {}

constructor(protected router: Router,
protected authService: AuthService) { }
canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot,
):
| Observable<boolean | UrlTree>
| Promise<boolean | UrlTree>
| boolean
| UrlTree {
return new Promise(resolve =>
this.authService.checkConfiguration().subscribe(
configured => {
if (!configured) {
resolve(this.onIsUnconfigured());
} else {
resolve(this.onIsConfigured());
}
},
error => {
const url = this.router.parseUrl('startup');
resolve(url);
},
),
);
}

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
return new Promise((resolve) => this.authService.checkConfiguration().subscribe((configured) => {
if (!configured) {
resolve(this.onIsUnconfigured());
} else {
resolve(this.onIsConfigured());
}
}, error => {
const url = this.router.parseUrl('startup');
resolve(url);
}));
}
abstract onIsConfigured(): boolean | UrlTree;

abstract onIsConfigured(): boolean | UrlTree;

abstract onIsUnconfigured(): boolean | UrlTree;
abstract onIsUnconfigured(): boolean | UrlTree;
}
21 changes: 9 additions & 12 deletions ui/src/app/_guards/configured.can-activate.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,15 @@ import { BaseConfiguredCanActivateGuard } from './base-configured.can-activate.g

@Injectable()
export class ConfiguredCanActivateGuard extends BaseConfiguredCanActivateGuard {
constructor(router: Router, authService: AuthService) {
super(router, authService);
}

constructor(router: Router,
authService: AuthService) {
super(router, authService);
}

onIsConfigured(): boolean | UrlTree {
return true;
}

onIsUnconfigured(): boolean | UrlTree {
return this.router.parseUrl('setup');
}
onIsConfigured(): boolean | UrlTree {
return true;
}

onIsUnconfigured(): boolean | UrlTree {
return this.router.parseUrl('setup');
}
}
18 changes: 10 additions & 8 deletions ui/src/app/_guards/page-auth.can-active.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,22 @@
*
*/

import { ActivatedRouteSnapshot, CanActivateChild, Router } from '@angular/router';
import {
ActivatedRouteSnapshot,
CanActivateChild,
Router,
} from '@angular/router';
import { AuthService } from '../services/auth.service';
import { PageName } from '../_enums/page-name.enum';
import { Injectable } from '@angular/core';

@Injectable()
export class PageAuthGuard implements CanActivateChild {
constructor(private router: Router, private authService: AuthService) {}

constructor(private router: Router,
private authService: AuthService) {}
canActivateChild(activatedRouteSnapshot: ActivatedRouteSnapshot): boolean {
const pageNames: PageName[] = activatedRouteSnapshot.data.authPageNames;

canActivateChild(activatedRouteSnapshot: ActivatedRouteSnapshot): boolean {
const pageNames: PageName[] = activatedRouteSnapshot.data.authPageNames;

return this.authService.isAnyAccessGranted(pageNames, true);
}
return this.authService.isAnyAccessGranted(pageNames, true);
}
}
37 changes: 22 additions & 15 deletions ui/src/app/_guards/registration-allowed.can-activate.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,34 @@

import { Injectable } from '@angular/core';
import {
ActivatedRouteSnapshot,
CanActivate,
Router,
RouterStateSnapshot,
UrlTree
ActivatedRouteSnapshot,
CanActivate,
Router,
RouterStateSnapshot,
UrlTree,
} from '@angular/router';
import { LoginService } from '../login/services/login.service';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

@Injectable()
export class RegistrationAllowedCanActivateGuard implements CanActivate {
constructor(private router: Router, private loginService: LoginService) {}

constructor(private router: Router,
private loginService: LoginService) {
}

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
return this.loginService.fetchLoginSettings().pipe(map(config => {
return config.allowSelfRegistration ? true : this.router.parseUrl('register');
}));
}

canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot,
):
| Observable<boolean | UrlTree>
| Promise<boolean | UrlTree>
| boolean
| UrlTree {
return this.loginService.fetchLoginSettings().pipe(
map(config => {
return config.allowSelfRegistration
? true
: this.router.parseUrl('register');
}),
);
}
}
37 changes: 24 additions & 13 deletions ui/src/app/_guards/restore-password-allowed.can-activate.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,36 @@

import { Injectable } from '@angular/core';
import {
ActivatedRouteSnapshot,
CanActivate,
Router,
RouterStateSnapshot,
UrlTree
ActivatedRouteSnapshot,
CanActivate,
Router,
RouterStateSnapshot,
UrlTree,
} from '@angular/router';
import { LoginService } from '../login/services/login.service';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

@Injectable()
export class RestorePasswordAllowedCanActivateGuard implements CanActivate {
constructor(private router: Router, private loginService: LoginService) {}

constructor(private router: Router,
private loginService: LoginService) {
}

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
return this.loginService.fetchLoginSettings().pipe(map(config => config.allowPasswordRecovery ? true : this.router.parseUrl('login')));
}

canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot,
):
| Observable<boolean | UrlTree>
| Promise<boolean | UrlTree>
| boolean
| UrlTree {
return this.loginService
.fetchLoginSettings()
.pipe(
map(config =>
config.allowPasswordRecovery
? true
: this.router.parseUrl('login'),
),
);
}
}

0 comments on commit 938fb55

Please sign in to comment.