mirror of https://github.com/abpframework/abp.git
13 changed files with 138 additions and 128 deletions
@ -1,46 +1,46 @@ |
|||
import { Injectable, Injector } from '@angular/core'; |
|||
import { Injectable, inject } from '@angular/core'; |
|||
|
|||
import { Observable } from 'rxjs'; |
|||
import { tap, map } from 'rxjs/operators'; |
|||
|
|||
import { ConfigStateService, IAbpGuard } from '@abp/ng.core'; |
|||
import { |
|||
ExtensionsService, |
|||
getObjectExtensionEntitiesFromStore, |
|||
mapEntitiesToContributors, |
|||
mergeWithDefaultProps, |
|||
} from '@abp/ng.theme.shared/extensions'; |
|||
import { ConfigStateService } from '@abp/ng.core'; |
|||
import { tap, map, mapTo } from 'rxjs/operators'; |
|||
|
|||
import { |
|||
ACCOUNT_EDIT_FORM_PROP_CONTRIBUTORS, |
|||
DEFAULT_ACCOUNT_FORM_PROPS, |
|||
} from '../tokens/extensions.token'; |
|||
import { AccountEditFormPropContributors } from '../models/config-options'; |
|||
import { eAccountComponents } from '../enums/components'; |
|||
|
|||
@Injectable() |
|||
export class AccountExtensionsGuard { |
|||
constructor(private injector: Injector) {} |
|||
export class AccountExtensionsGuard implements IAbpGuard { |
|||
protected readonly configState = inject(ConfigStateService); |
|||
protected readonly extensions = inject(ExtensionsService); |
|||
|
|||
canActivate(): Observable<boolean> { |
|||
const extensions: ExtensionsService = this.injector.get(ExtensionsService); |
|||
const config = { optional: true }; |
|||
|
|||
const editFormContributors: AccountEditFormPropContributors = |
|||
this.injector.get(ACCOUNT_EDIT_FORM_PROP_CONTRIBUTORS, null) || {}; |
|||
const editFormContributors = inject(ACCOUNT_EDIT_FORM_PROP_CONTRIBUTORS, config) || {}; |
|||
|
|||
const configState = this.injector.get(ConfigStateService); |
|||
return getObjectExtensionEntitiesFromStore(configState, 'Identity').pipe( |
|||
return getObjectExtensionEntitiesFromStore(this.configState, 'Identity').pipe( |
|||
map(entities => ({ |
|||
[eAccountComponents.PersonalSettings]: entities.User, |
|||
})), |
|||
mapEntitiesToContributors(configState, 'AbpIdentity'), |
|||
mapEntitiesToContributors(this.configState, 'AbpIdentity'), |
|||
tap(objectExtensionContributors => { |
|||
mergeWithDefaultProps( |
|||
extensions.editFormProps, |
|||
this.extensions.editFormProps, |
|||
DEFAULT_ACCOUNT_FORM_PROPS, |
|||
objectExtensionContributors.editForm, |
|||
editFormContributors, |
|||
); |
|||
}), |
|||
mapTo(true), |
|||
map(() => true), |
|||
); |
|||
} |
|||
} |
|||
|
|||
@ -0,0 +1,9 @@ |
|||
import { ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree } from '@angular/router'; |
|||
import { Observable } from 'rxjs'; |
|||
|
|||
export interface IAbpGuard { |
|||
canActivate( |
|||
route: ActivatedRouteSnapshot, |
|||
state: RouterStateSnapshot, |
|||
): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree; |
|||
} |
|||
@ -1,22 +1,27 @@ |
|||
import { Injectable } from '@angular/core'; |
|||
import { Injectable, inject } from '@angular/core'; |
|||
import { UrlTree } from '@angular/router'; |
|||
import { OAuthService } from 'angular-oauth2-oidc'; |
|||
import { HttpErrorResponse } from '@angular/common/http'; |
|||
|
|||
import { Observable } from 'rxjs'; |
|||
import { AuthService, IAuthGuard } from '@abp/ng.core'; |
|||
import { OAuthService } from 'angular-oauth2-oidc'; |
|||
|
|||
import { AuthService, HttpErrorReporterService, IAbpGuard } from '@abp/ng.core'; |
|||
|
|||
@Injectable({ |
|||
providedIn: 'root', |
|||
}) |
|||
export class AbpOAuthGuard implements IAuthGuard { |
|||
constructor(private oauthService: OAuthService, private authService: AuthService) {} |
|||
export class AbpOAuthGuard implements IAbpGuard { |
|||
protected readonly oAuthService = inject(OAuthService); |
|||
protected readonly authService = inject(AuthService); |
|||
protected readonly httpErrorReporter = inject(HttpErrorReporterService); |
|||
|
|||
canActivate(): Observable<boolean> | boolean | UrlTree { |
|||
const hasValidAccessToken = this.oauthService.hasValidAccessToken(); |
|||
const hasValidAccessToken = this.oAuthService.hasValidAccessToken(); |
|||
if (hasValidAccessToken) { |
|||
return true; |
|||
} |
|||
|
|||
this.authService.navigateToLogin(); |
|||
this.httpErrorReporter.reportError({ status: 401 } as HttpErrorResponse); |
|||
return false; |
|||
} |
|||
} |
|||
|
|||
Loading…
Reference in new issue