66using Microsoft . AspNetCore . Http ;
77using Microsoft . AspNetCore . Http . Extensions ;
88using Microsoft . Extensions . DependencyInjection ;
9- using Microsoft . FeatureManagement ;
109
1110namespace Grand . Web . Common . Middleware ;
1211
@@ -16,6 +15,7 @@ public class InstallUrlMiddleware
1615
1716 private readonly RequestDelegate _next ;
1817 private readonly ICacheBase _cacheBase ;
18+ private const string InstallUrl = "/install" ;
1919
2020 #endregion
2121
@@ -39,32 +39,14 @@ public InstallUrlMiddleware(RequestDelegate next, ICacheBase cacheBase)
3939 public async Task InvokeAsync ( HttpContext context )
4040 {
4141 var databaseIsInstalled = DataSettingsManager . DatabaseIsInstalled ( ) ;
42- const string installUrl = "/install" ;
43- //whether database is installed
44- var version = _cacheBase . Get ( CacheKey . GRAND_NODE_VERSION , ( ) =>
45- {
46- if ( databaseIsInstalled )
47- return context . RequestServices . GetRequiredService < IRepository < GrandNodeVersion > > ( ) . Table . FirstOrDefault ( ) ;
48-
49- return null ;
50- } , int . MaxValue ) ;
42+ var version = GetDatabaseVersion ( context ) ;
5143
5244 if ( version == null )
5345 {
54- var featureManager = context . RequestServices . GetRequiredService < IFeatureManager > ( ) ;
55- var isInstallerModuleEnabled = await featureManager . IsEnabledAsync ( "Grand.Module.Installer" ) ;
56- if ( ! isInstallerModuleEnabled )
57- {
58- // Return a response indicating the installer module is not enabled
59- context . Response . StatusCode = StatusCodes . Status403Forbidden ;
60- await context . Response . WriteAsync ( "The installation module is not enabled." ) ;
61- return ;
62- }
63-
64- if ( ! context . Request . GetEncodedPathAndQuery ( ) . StartsWith ( installUrl , StringComparison . OrdinalIgnoreCase ) )
46+ if ( ! IsInstallUrl ( context . Request ) )
6547 {
6648 //redirect
67- context . Response . Redirect ( installUrl ) ;
49+ context . Response . Redirect ( InstallUrl ) ;
6850 return ;
6951 }
7052 }
@@ -76,7 +58,7 @@ await context.Response.WriteAsync("The database version is not supported in this
7658 $ "Supported version: { GrandVersion . SupportedDBVersion } , your version: { version . DataBaseVersion } ") ;
7759 return ;
7860 }
79- if ( context . Request . GetEncodedPathAndQuery ( ) . StartsWith ( installUrl , StringComparison . OrdinalIgnoreCase ) )
61+ if ( IsInstallUrl ( context . Request ) )
8062 {
8163 //redirect
8264 context . Response . Redirect ( "/" ) ;
@@ -87,6 +69,19 @@ await context.Response.WriteAsync("The database version is not supported in this
8769 //call the next middleware in the request pipeline
8870 await _next ( context ) ;
8971 }
72+ private GrandNodeVersion ? GetDatabaseVersion ( HttpContext context )
73+ {
74+ return _cacheBase . Get ( CacheKey . GRAND_NODE_VERSION , ( ) =>
75+ {
76+ var repository = context . RequestServices . GetRequiredService < IRepository < GrandNodeVersion > > ( ) ;
77+ return repository . Table . FirstOrDefault ( ) ;
78+ } , int . MaxValue ) ;
79+ }
80+
81+ private bool IsInstallUrl ( HttpRequest request )
82+ {
83+ return request . GetEncodedPathAndQuery ( ) . StartsWith ( InstallUrl , StringComparison . OrdinalIgnoreCase ) ;
84+ }
9085
9186 #endregion
9287}
0 commit comments