diff --git a/docs/en/Authorization.md b/docs/en/Authorization.md index 270345f444..2fdc59c36b 100644 --- a/docs/en/Authorization.md +++ b/docs/en/Authorization.md @@ -405,17 +405,14 @@ public class SocialSecurityNumberClaimsPrincipalContributor : IAbpClaimsPrincipa public async Task ContributeAsync(AbpClaimsPrincipalContributorContext context) { var identity = context.ClaimsPrincipal.Identities.FirstOrDefault(); - if (identity != null) + var userId = identity?.FindUserId(); + if (userId.HasValue) { - var currentUser = context.ServiceProvider.GetRequiredService(); - if (currentUser.Id.HasValue) + var userService = context.ServiceProvider.GetRequiredService(); //Your custom service + var socialSecurityNumber = await userService.GetSocialSecurityNumberAsync(userId.Value); + if (socialSecurityNumber != null) { - var userService = context.ServiceProvider.GetRequiredService(); //Your custom service - var socialSecurityNumber = await userService.GetSocialSecurityNumberAsync(currentUser.Id.Value); - if (userService != null) - { - identity.AddOrReplace(new Claim("SocialSecurityNumber", userService)); - } + identity.AddOrReplace(new Claim("SocialSecurityNumber", socialSecurityNumber)); } } } diff --git a/modules/setting-management/src/Volo.Abp.SettingManagement.Blazor/Menus/SettingManagementMenuContributor.cs b/modules/setting-management/src/Volo.Abp.SettingManagement.Blazor/Menus/SettingManagementMenuContributor.cs index a3a18ffb33..5070cef3a6 100644 --- a/modules/setting-management/src/Volo.Abp.SettingManagement.Blazor/Menus/SettingManagementMenuContributor.cs +++ b/modules/setting-management/src/Volo.Abp.SettingManagement.Blazor/Menus/SettingManagementMenuContributor.cs @@ -21,8 +21,7 @@ namespace Volo.Abp.SettingManagement.Blazor.Menus { var settingManagementPageOptions = context.ServiceProvider.GetRequiredService>().Value; var settingPageCreationContext = new SettingComponentCreationContext(context.ServiceProvider); - if ( - !settingManagementPageOptions.Contributors.Any() || + if (!settingManagementPageOptions.Contributors.Any() || !(await CheckAnyOfPagePermissionsGranted(settingManagementPageOptions, settingPageCreationContext)) ) { @@ -31,6 +30,10 @@ namespace Volo.Abp.SettingManagement.Blazor.Menus var l = context.GetLocalizer(); + /* This may happen if MVC UI is being used in the same application. + * In this case, we are removing the MVC setting management UI. */ + context.Menu.GetAdministration().TryRemoveMenuItem(SettingManagementMenus.GroupName); + context.Menu .GetAdministration() .AddItem( diff --git a/modules/setting-management/src/Volo.Abp.SettingManagement.Web/Navigation/SettingManagementMainMenuContributor.cs b/modules/setting-management/src/Volo.Abp.SettingManagement.Web/Navigation/SettingManagementMainMenuContributor.cs index 20946c09ea..59fc10c174 100644 --- a/modules/setting-management/src/Volo.Abp.SettingManagement.Web/Navigation/SettingManagementMainMenuContributor.cs +++ b/modules/setting-management/src/Volo.Abp.SettingManagement.Web/Navigation/SettingManagementMainMenuContributor.cs @@ -19,12 +19,17 @@ namespace Volo.Abp.SettingManagement.Web.Navigation return; } + if (context.Menu.FindMenuItem(SettingManagementMenuNames.GroupName) != null) + { + /* This may happen if blazor server UI is being used in the same application. + * In this case, we don't add the MVC setting management UI. */ + return; + } + var settingManagementPageOptions = context.ServiceProvider.GetRequiredService>().Value; var settingPageCreationContext = new SettingPageCreationContext(context.ServiceProvider); - if ( - !settingManagementPageOptions.Contributors.Any() || - !(await CheckAnyOfPagePermissionsGranted(settingManagementPageOptions, settingPageCreationContext)) - ) + if (!settingManagementPageOptions.Contributors.Any() || + !(await CheckAnyOfPagePermissionsGranted(settingManagementPageOptions, settingPageCreationContext))) { return; } diff --git a/modules/setting-management/src/Volo.Abp.SettingManagement.Web/Pages/SettingManagement/Index.cshtml.cs b/modules/setting-management/src/Volo.Abp.SettingManagement.Web/Pages/SettingManagement/Index.cshtml.cs index 87eb2e5bf4..8cc7f62143 100644 --- a/modules/setting-management/src/Volo.Abp.SettingManagement.Web/Pages/SettingManagement/Index.cshtml.cs +++ b/modules/setting-management/src/Volo.Abp.SettingManagement.Web/Pages/SettingManagement/Index.cshtml.cs @@ -1,4 +1,3 @@ -using System; using System.Threading.Tasks; using Microsoft.Extensions.Options; using Microsoft.AspNetCore.Mvc; diff --git a/npm/ng-packs/packages/core/src/lib/strategies/auth-flow.strategy.ts b/npm/ng-packs/packages/core/src/lib/strategies/auth-flow.strategy.ts index 82f0ac413a..7cc3abddac 100644 --- a/npm/ng-packs/packages/core/src/lib/strategies/auth-flow.strategy.ts +++ b/npm/ng-packs/packages/core/src/lib/strategies/auth-flow.strategy.ts @@ -2,14 +2,21 @@ import { HttpHeaders } from '@angular/common/http'; import { Injector } from '@angular/core'; import { Params, Router } from '@angular/router'; import { Store } from '@ngxs/store'; -import { AuthConfig, OAuthInfoEvent, OAuthService, OAuthStorage } from 'angular-oauth2-oidc'; +import { + AuthConfig, + OAuthErrorEvent, + OAuthInfoEvent, + OAuthService, + OAuthStorage, +} from 'angular-oauth2-oidc'; import { from, Observable, of } from 'rxjs'; import { filter, switchMap, tap } from 'rxjs/operators'; import { RestOccurError } from '../actions/rest.actions'; import { AbpApplicationConfigurationService } from '../proxy/volo/abp/asp-net-core/mvc/application-configurations/abp-application-configuration.service'; -import { SessionStateService } from '../services/session-state.service'; import { ConfigStateService } from '../services/config-state.service'; import { EnvironmentService } from '../services/environment.service'; +import { SessionStateService } from '../services/session-state.service'; +import { noop } from '../utils/common-utils'; export interface LoginParams { username: string; @@ -28,6 +35,8 @@ export abstract class AuthFlowStrategy { protected configState: ConfigStateService; protected oAuthService: OAuthService; protected oAuthConfig: AuthConfig; + protected appConfigService: AbpApplicationConfigurationService; + abstract checkIfInternalAuth(): boolean; abstract navigateToLogin(queryParams?: Params): void; abstract logout(): Observable; @@ -40,7 +49,10 @@ export abstract class AuthFlowStrategy { this.environment = injector.get(EnvironmentService); this.configState = injector.get(ConfigStateService); this.oAuthService = injector.get(OAuthService); + this.appConfigService = injector.get(AbpApplicationConfigurationService); this.oAuthConfig = this.environment.getEnvironment().oAuthConfig; + + this.listenToOauthErrors(); } async init(): Promise { @@ -66,6 +78,18 @@ export abstract class AuthFlowStrategy { protected refreshToken() { return this.oAuthService.refreshToken().catch(() => clearOAuthStorage()); } + + protected listenToOauthErrors() { + this.oAuthService.events + .pipe( + filter(event => event instanceof OAuthErrorEvent), + tap(() => clearOAuthStorage()), + switchMap(() => this.appConfigService.get()), + ) + .subscribe(res => { + this.configState.setState(res); + }); + } } export class AuthCodeFlowStrategy extends AuthFlowStrategy { @@ -74,7 +98,7 @@ export class AuthCodeFlowStrategy extends AuthFlowStrategy { async init() { return super .init() - .then(() => this.oAuthService.tryLogin()) + .then(() => this.oAuthService.tryLogin().catch(noop)) .then(() => this.oAuthService.setupAutomaticSilentRefresh({}, 'access_token')); } @@ -101,7 +125,6 @@ export class AuthPasswordFlowStrategy extends AuthFlowStrategy { readonly isInternalAuth = true; private cookieKey = 'rememberMe'; private storageKey = 'passwordFlow'; - private appConfigService = this.injector.get(AbpApplicationConfigurationService); private listenToTokenExpiration() { this.oAuthService.events diff --git a/npm/ng-packs/packages/core/src/lib/utils/initial-utils.ts b/npm/ng-packs/packages/core/src/lib/utils/initial-utils.ts index c4edc800b5..87982d8bd0 100644 --- a/npm/ng-packs/packages/core/src/lib/utils/initial-utils.ts +++ b/npm/ng-packs/packages/core/src/lib/utils/initial-utils.ts @@ -41,9 +41,9 @@ export function getInitialData(injector: Injector) { injector.get(SessionStateService).setTenant(currentTenant); }), catchError(error => { - const appInitErrorHandlers = injector.get(APP_INIT_ERROR_HANDLERS); + const appInitErrorHandlers = injector.get(APP_INIT_ERROR_HANDLERS, null); if (appInitErrorHandlers && appInitErrorHandlers.length) { - appInitErrorHandlers.forEach(fn => fn(error)); + appInitErrorHandlers.forEach(func => func(error)); } return throwError(error); diff --git a/npm/ng-packs/tsconfig.json b/npm/ng-packs/tsconfig.json index 75ff03b711..ca40efc770 100644 --- a/npm/ng-packs/tsconfig.json +++ b/npm/ng-packs/tsconfig.json @@ -7,6 +7,7 @@ "declaration": false, "downlevelIteration": true, "experimentalDecorators": true, + "allowSyntheticDefaultImports": true, "module": "esnext", "moduleResolution": "node", "importHelpers": true, diff --git a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Server/MyProjectNameBlazorModule.cs b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Server/MyProjectNameBlazorModule.cs index 9d860a9030..ef3225458a 100644 --- a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Server/MyProjectNameBlazorModule.cs +++ b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Server/MyProjectNameBlazorModule.cs @@ -8,6 +8,7 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using Microsoft.OpenApi.Models; using MyCompanyName.MyProjectName.Blazor.Server.Menus; using MyCompanyName.MyProjectName.EntityFrameworkCore; using MyCompanyName.MyProjectName.Localization; @@ -86,6 +87,7 @@ namespace MyCompanyName.MyProjectName.Blazor.Server ConfigureAutoMapper(); ConfigureVirtualFileSystem(hostingEnvironment); ConfigureLocalizationServices(); + ConfigureSwaggerServices(context.Services); ConfigureAutoApiControllers(); ConfigureHttpClient(context); ConfigureBlazorise(context); @@ -186,6 +188,18 @@ namespace MyCompanyName.MyProjectName.Blazor.Server }); } + private void ConfigureSwaggerServices(IServiceCollection services) + { + services.AddSwaggerGen( + options => + { + options.SwaggerDoc("v1", new OpenApiInfo { Title = "MyProjectName API", Version = "v1" }); + options.DocInclusionPredicate((docName, description) => true); + options.CustomSchemaIds(type => type.FullName); + } + ); + } + private static void ConfigureHttpClient(ServiceConfigurationContext context) { context.Services.AddTransient(sp => new HttpClient @@ -265,6 +279,11 @@ namespace MyCompanyName.MyProjectName.Blazor.Server app.UseUnitOfWork(); app.UseIdentityServer(); app.UseAuthorization(); + app.UseSwagger(); + app.UseAbpSwaggerUI(options => + { + options.SwaggerEndpoint("/swagger/v1/swagger.json", "MyProjectName API"); + }); app.UseConfiguredEndpoints(); } }