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 { Observable } from 'rxjs'; |
||||
|
import { tap, map } from 'rxjs/operators'; |
||||
|
|
||||
|
import { ConfigStateService, IAbpGuard } from '@abp/ng.core'; |
||||
import { |
import { |
||||
ExtensionsService, |
ExtensionsService, |
||||
getObjectExtensionEntitiesFromStore, |
getObjectExtensionEntitiesFromStore, |
||||
mapEntitiesToContributors, |
mapEntitiesToContributors, |
||||
mergeWithDefaultProps, |
mergeWithDefaultProps, |
||||
} from '@abp/ng.theme.shared/extensions'; |
} from '@abp/ng.theme.shared/extensions'; |
||||
import { ConfigStateService } from '@abp/ng.core'; |
|
||||
import { tap, map, mapTo } from 'rxjs/operators'; |
|
||||
import { |
import { |
||||
ACCOUNT_EDIT_FORM_PROP_CONTRIBUTORS, |
ACCOUNT_EDIT_FORM_PROP_CONTRIBUTORS, |
||||
DEFAULT_ACCOUNT_FORM_PROPS, |
DEFAULT_ACCOUNT_FORM_PROPS, |
||||
} from '../tokens/extensions.token'; |
} from '../tokens/extensions.token'; |
||||
import { AccountEditFormPropContributors } from '../models/config-options'; |
|
||||
import { eAccountComponents } from '../enums/components'; |
import { eAccountComponents } from '../enums/components'; |
||||
|
|
||||
@Injectable() |
@Injectable() |
||||
export class AccountExtensionsGuard { |
export class AccountExtensionsGuard implements IAbpGuard { |
||||
constructor(private injector: Injector) {} |
protected readonly configState = inject(ConfigStateService); |
||||
|
protected readonly extensions = inject(ExtensionsService); |
||||
|
|
||||
canActivate(): Observable<boolean> { |
canActivate(): Observable<boolean> { |
||||
const extensions: ExtensionsService = this.injector.get(ExtensionsService); |
const config = { optional: true }; |
||||
|
|
||||
const editFormContributors: AccountEditFormPropContributors = |
const editFormContributors = inject(ACCOUNT_EDIT_FORM_PROP_CONTRIBUTORS, config) || {}; |
||||
this.injector.get(ACCOUNT_EDIT_FORM_PROP_CONTRIBUTORS, null) || {}; |
|
||||
|
|
||||
const configState = this.injector.get(ConfigStateService); |
return getObjectExtensionEntitiesFromStore(this.configState, 'Identity').pipe( |
||||
return getObjectExtensionEntitiesFromStore(configState, 'Identity').pipe( |
|
||||
map(entities => ({ |
map(entities => ({ |
||||
[eAccountComponents.PersonalSettings]: entities.User, |
[eAccountComponents.PersonalSettings]: entities.User, |
||||
})), |
})), |
||||
mapEntitiesToContributors(configState, 'AbpIdentity'), |
mapEntitiesToContributors(this.configState, 'AbpIdentity'), |
||||
tap(objectExtensionContributors => { |
tap(objectExtensionContributors => { |
||||
mergeWithDefaultProps( |
mergeWithDefaultProps( |
||||
extensions.editFormProps, |
this.extensions.editFormProps, |
||||
DEFAULT_ACCOUNT_FORM_PROPS, |
DEFAULT_ACCOUNT_FORM_PROPS, |
||||
objectExtensionContributors.editForm, |
objectExtensionContributors.editForm, |
||||
editFormContributors, |
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 { UrlTree } from '@angular/router'; |
||||
import { OAuthService } from 'angular-oauth2-oidc'; |
import { HttpErrorResponse } from '@angular/common/http'; |
||||
|
|
||||
import { Observable } from 'rxjs'; |
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({ |
@Injectable({ |
||||
providedIn: 'root', |
providedIn: 'root', |
||||
}) |
}) |
||||
export class AbpOAuthGuard implements IAuthGuard { |
export class AbpOAuthGuard implements IAbpGuard { |
||||
constructor(private oauthService: OAuthService, private authService: AuthService) {} |
protected readonly oAuthService = inject(OAuthService); |
||||
|
protected readonly authService = inject(AuthService); |
||||
|
protected readonly httpErrorReporter = inject(HttpErrorReporterService); |
||||
|
|
||||
canActivate(): Observable<boolean> | boolean | UrlTree { |
canActivate(): Observable<boolean> | boolean | UrlTree { |
||||
const hasValidAccessToken = this.oauthService.hasValidAccessToken(); |
const hasValidAccessToken = this.oAuthService.hasValidAccessToken(); |
||||
if (hasValidAccessToken) { |
if (hasValidAccessToken) { |
||||
return true; |
return true; |
||||
} |
} |
||||
|
|
||||
this.authService.navigateToLogin(); |
this.httpErrorReporter.reportError({ status: 401 } as HttpErrorResponse); |
||||
return false; |
return false; |
||||
} |
} |
||||
} |
} |
||||
|
|||||
Loading…
Reference in new issue