mirror of
				https://github.com/zoriya/Kyoo.git
				synced 2025-11-03 19:17:16 -05:00 
			
		
		
		
	Reworking guards
This commit is contained in:
		
							parent
							
								
									23f1ed11b8
								
							
						
					
					
						commit
						26e174f3cc
					
				@ -16,16 +16,16 @@ import {LoginComponent} from "./login/login.component";
 | 
				
			|||||||
import {UnauthorizedComponent} from "./unauthorized/unauthorized.component";
 | 
					import {UnauthorizedComponent} from "./unauthorized/unauthorized.component";
 | 
				
			||||||
import {LogoutComponent} from "./logout/logout.component";
 | 
					import {LogoutComponent} from "./logout/logout.component";
 | 
				
			||||||
import {AutologinComponent} from "./autologin/autologin.component";
 | 
					import {AutologinComponent} from "./autologin/autologin.component";
 | 
				
			||||||
import {AuthenticatedGuard} from "./misc/guards/authenticated-guard.service";
 | 
					import {AuthGuard} from "./misc/guards/authenticated-guard.service";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const routes: Routes = [
 | 
					const routes: Routes = [
 | 
				
			||||||
	{ path: "browse", component: BrowseComponent, pathMatch: "full", resolve: { shows: LibraryResolverService }, canLoad: [AuthenticatedGuard], canActivate: [AuthenticatedGuard] },
 | 
						{ path: "browse", component: BrowseComponent, pathMatch: "full", resolve: { shows: LibraryResolverService }, },// canLoad: [AuthGuard.forPermissions("read")], canActivate: [AuthGuard.forPermissions("read")] },
 | 
				
			||||||
	{ path: "browse/:library-slug", component: BrowseComponent, resolve: { shows: LibraryResolverService }, canLoad: [AuthenticatedGuard], canActivate: [AuthenticatedGuard] },
 | 
						{ path: "browse/:library-slug", component: BrowseComponent, resolve: { shows: LibraryResolverService }, canLoad: [AuthGuard.forPermissions("read")], canActivate: [AuthGuard.forPermissions("read")] },
 | 
				
			||||||
	{ path: "show/:show-slug", component: ShowDetailsComponent, resolve: { show: ShowResolverService }, canLoad: [AuthenticatedGuard], canActivate: [AuthenticatedGuard] },
 | 
						{ path: "show/:show-slug", component: ShowDetailsComponent, resolve: { show: ShowResolverService }, canLoad: [AuthGuard.forPermissions("read")], canActivate: [AuthGuard.forPermissions("read")] },
 | 
				
			||||||
	{ path: "collection/:collection-slug", component: CollectionComponent, resolve: { collection: CollectionResolverService }, canLoad: [AuthenticatedGuard], canActivate: [AuthenticatedGuard] },
 | 
						{ path: "collection/:collection-slug", component: CollectionComponent, resolve: { collection: CollectionResolverService }, canLoad: [AuthGuard.forPermissions("read")], canActivate: [AuthGuard.forPermissions("read")] },
 | 
				
			||||||
	{ path: "people/:people-slug", component: CollectionComponent, resolve: { collection: PeopleResolverService }, canLoad: [AuthenticatedGuard], canActivate: [AuthenticatedGuard] },
 | 
						{ path: "people/:people-slug", component: CollectionComponent, resolve: { collection: PeopleResolverService }, canLoad: [AuthGuard.forPermissions("read")], canActivate: [AuthGuard.forPermissions("read")] },
 | 
				
			||||||
	{ path: "watch/:item", component: PlayerComponent, resolve: { item: StreamResolverService }, canLoad: [AuthenticatedGuard], canActivate: [AuthenticatedGuard] },
 | 
						{ path: "watch/:item", component: PlayerComponent, resolve: { item: StreamResolverService }, canLoad: [AuthGuard.forPermissions("play")], canActivate: [AuthGuard.forPermissions("play")] },
 | 
				
			||||||
	{ path: "search/:query", component: SearchComponent, resolve: { items: SearchResolverService }, canLoad: [AuthenticatedGuard], canActivate: [AuthenticatedGuard] },
 | 
						{ path: "search/:query", component: SearchComponent, resolve: { items: SearchResolverService }, canLoad: [AuthGuard.forPermissions("read")], canActivate: [AuthGuard.forPermissions("read")] },
 | 
				
			||||||
	{ path: "login", component: LoginComponent },
 | 
						{ path: "login", component: LoginComponent },
 | 
				
			||||||
	{ path: "logout", component: LogoutComponent },
 | 
						{ path: "logout", component: LogoutComponent },
 | 
				
			||||||
	{ path: "autologin", component: AutologinComponent },
 | 
						{ path: "autologin", component: AutologinComponent },
 | 
				
			||||||
 | 
				
			|||||||
@ -42,7 +42,7 @@ import { UnauthorizedComponent } from './unauthorized/unauthorized.component';
 | 
				
			|||||||
import { LogoutComponent } from './logout/logout.component';
 | 
					import { LogoutComponent } from './logout/logout.component';
 | 
				
			||||||
import {MatDialogModule} from '@angular/material/dialog';
 | 
					import {MatDialogModule} from '@angular/material/dialog';
 | 
				
			||||||
import {FallbackDirective} from "./misc/fallback.directive";
 | 
					import {FallbackDirective} from "./misc/fallback.directive";
 | 
				
			||||||
import {AuthenticatedGuard} from "./misc/guards/authenticated-guard.service";
 | 
					import {AuthGuard} from "./misc/guards/authenticated-guard.service";
 | 
				
			||||||
import { AutologinComponent } from './autologin/autologin.component';
 | 
					import { AutologinComponent } from './autologin/autologin.component';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export function loadConfig(oidcConfigService: OidcConfigService)
 | 
					export function loadConfig(oidcConfigService: OidcConfigService)
 | 
				
			||||||
@ -105,7 +105,7 @@ export function loadConfig(oidcConfigService: OidcConfigService)
 | 
				
			|||||||
			deps: [OidcConfigService],
 | 
								deps: [OidcConfigService],
 | 
				
			||||||
			multi: true
 | 
								multi: true
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
		AuthenticatedGuard
 | 
							AuthGuard
 | 
				
			||||||
	],
 | 
						],
 | 
				
			||||||
	bootstrap: [AppComponent]
 | 
						bootstrap: [AppComponent]
 | 
				
			||||||
})
 | 
					})
 | 
				
			||||||
 | 
				
			|||||||
@ -12,28 +12,36 @@ import {
 | 
				
			|||||||
import { Observable } from 'rxjs';
 | 
					import { Observable } from 'rxjs';
 | 
				
			||||||
import {AuthService} from "../../services/auth.service";
 | 
					import {AuthService} from "../../services/auth.service";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@Injectable({
 | 
					@Injectable({providedIn: "root"})
 | 
				
			||||||
 	providedIn: 'root'
 | 
					export class AuthGuard 
 | 
				
			||||||
})
 | 
					 | 
				
			||||||
export class AuthenticatedGuard implements CanActivate, CanLoad 
 | 
					 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	constructor(private router: Router, private authManager: AuthService) {}
 | 
						static forPermissions(permissions: string | string[])
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							@Injectable()
 | 
				
			||||||
 | 
							class AuthenticatedGuard implements CanActivate, CanLoad
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								constructor(private router: Router, private authManager: AuthService)  {}
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
 	canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree 
 | 
								canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree
 | 
				
			||||||
 	{
 | 
								{
 | 
				
			||||||
 	 	return this.checkPermissions();
 | 
									return this.checkPermissions();
 | 
				
			||||||
 	}
 | 
								}
 | 
				
			||||||
 	
 | 
					 | 
				
			||||||
 	canLoad(route: Route, segments: UrlSegment[]): Observable<boolean> | Promise<boolean> | boolean 
 | 
					 | 
				
			||||||
 	{
 | 
					 | 
				
			||||||
 	 	return this.checkPermissions();
 | 
					 | 
				
			||||||
 	}
 | 
					 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
 	checkPermissions() : boolean
 | 
								canLoad(route: Route, segments: UrlSegment[]): Observable<boolean> | Promise<boolean> | boolean
 | 
				
			||||||
    {
 | 
								{
 | 
				
			||||||
    	if (this.authManager.isAuthenticated)
 | 
									return this.checkPermissions();
 | 
				
			||||||
    		return true;
 | 
								}
 | 
				
			||||||
	    this.router.navigate(["/unauthorized"]);
 | 
						
 | 
				
			||||||
    	return false;
 | 
								checkPermissions(): boolean
 | 
				
			||||||
    }
 | 
								{
 | 
				
			||||||
}
 | 
									if (this.authManager.isAuthenticated)
 | 
				
			||||||
 | 
									{
 | 
				
			||||||
 | 
										// if (this.authManager.user.claims)
 | 
				
			||||||
 | 
										return true;
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									this.router.navigate(["/unauthorized"]);
 | 
				
			||||||
 | 
									return false;
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user