Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert to file-scope namespaces #16539

Merged
merged 18 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ dotnet_sort_system_directives_first = true
# Code-block preferences
csharp_prefer_braces = true
csharp_prefer_simple_using_statement = true
csharp_style_namespace_declarations = file_scoped:suggestion
csharp_style_namespace_declarations = file_scoped:warning

# Range operator
csharp_style_prefer_range_operator = false:warning
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/pr_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ jobs:
# warnings and other better approaches don't work, see https://github.com/OrchardCMS/OrchardCore/pull/16317.
run: |
dotnet build -c Release -warnaserror /p:TreatWarningsAsErrors=true /p:RunAnalyzers=true /p:NuGetAudit=false
# - name: Set Git line ending configuration
# run: git config --global core.autocrlf true
# - name: Validate code format
# run: dotnet format --verify-no-changes --verbosity diagnostic
- name: Unit Tests
run: |
dotnet test -c Release --no-build ./test/OrchardCore.Tests/OrchardCore.Tests.csproj
Expand Down
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<PackageVersion Include="Microsoft.IdentityModel.Protocols.OpenIdConnect" Version="8.0.1" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="8.0.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.CodeStyle" Version="4.10.0"/>
<PackageVersion Include="MimeKit" Version="4.7.1" />
<PackageVersion Include="MiniProfiler.AspNetCore.Mvc" Version="4.3.8" />
<PackageVersion Include="Moq" Version="4.20.70" />
Expand Down
3 changes: 2 additions & 1 deletion OrchardCore.sln
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
ProjectSection(SolutionItems) = preProject
.coderabbit.yaml = .coderabbit.yaml
.editorconfig = .editorconfig
.gitattributes = .gitattributes
gulpfile.js = gulpfile.js
package.json = package.json
README.md = README.md
Expand Down Expand Up @@ -523,7 +524,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OrchardCore.Email.Smtp", "s
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OrchardCore.Rules.Core", "src\OrchardCore\OrchardCore.Rules.Core\OrchardCore.Rules.Core.csproj", "{4BAA08A2-878C-4B96-86BF-5B3DB2B6C2C7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OrchardCore.Queries.Core", "src\OrchardCore\OrchardCore.Queries.Core\OrchardCore.Queries.Core.csproj", "{61B358F2-702C-40AA-9DF7-7121248FE6DE}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OrchardCore.Queries.Core", "src\OrchardCore\OrchardCore.Queries.Core\OrchardCore.Queries.Core.csproj", "{61B358F2-702C-40AA-9DF7-7121248FE6DE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
5 changes: 4 additions & 1 deletion src/OrchardCore.Build/OrchardCore.Commons.props
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>

<!-- Common NuGet properties-->
<OCFrameworkDescription>Orchard Core Framework is an application framework for building modular, multi-tenant applications on ASP.NET Core.</OCFrameworkDescription>
<OCCMSDescription>Orchard Core CMS is a Web Content Management System (CMS) built on top of the Orchard Core Framework.</OCCMSDescription>
Expand Down Expand Up @@ -48,6 +47,10 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.CodeStyle">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="StyleCop.Analyzers" PrivateAssets="all" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,52 @@
using Microsoft.Extensions.Options;
using OrchardCore.Mvc.Routing;

namespace OrchardCore.Admin
namespace OrchardCore.Admin;

public class AdminAreaControllerRouteMapper : IAreaControllerRouteMapper
{
public class AdminAreaControllerRouteMapper : IAreaControllerRouteMapper
private const string _defaultAreaPattern = "{area}/{controller}/{action}/{id?}";
private readonly string _adminUrlPrefix;

public int Order => -1000;

public AdminAreaControllerRouteMapper(IOptions<AdminOptions> adminOptions)
{
private const string _defaultAreaPattern = "{area}/{controller}/{action}/{id?}";
private readonly string _adminUrlPrefix;
_adminUrlPrefix = adminOptions.Value.AdminUrlPrefix;
}

public int Order => -1000;
public bool TryMapAreaControllerRoute(IEndpointRouteBuilder routes, ControllerActionDescriptor descriptor)
{
var controllerAttribute = descriptor.ControllerTypeInfo.GetCustomAttribute<AdminAttribute>();
var actionAttribute = descriptor.MethodInfo.GetCustomAttribute<AdminAttribute>();

public AdminAreaControllerRouteMapper(IOptions<AdminOptions> adminOptions)
if (descriptor.ControllerName != "Admin" && controllerAttribute == null && actionAttribute == null)
{
_adminUrlPrefix = adminOptions.Value.AdminUrlPrefix;
return false;
}

public bool TryMapAreaControllerRoute(IEndpointRouteBuilder routes, ControllerActionDescriptor descriptor)
string name = null;
var pattern = _defaultAreaPattern;

if (!string.IsNullOrWhiteSpace(actionAttribute?.Template))
{
var controllerAttribute = descriptor.ControllerTypeInfo.GetCustomAttribute<AdminAttribute>();
var actionAttribute = descriptor.MethodInfo.GetCustomAttribute<AdminAttribute>();

if (descriptor.ControllerName != "Admin" && controllerAttribute == null && actionAttribute == null)
{
return false;
}

string name = null;
var pattern = _defaultAreaPattern;

if (!string.IsNullOrWhiteSpace(actionAttribute?.Template))
{
name = actionAttribute.RouteName;
pattern = actionAttribute.Template;
}
else if (!string.IsNullOrWhiteSpace(controllerAttribute?.Template))
{
name = controllerAttribute.RouteName;
pattern = controllerAttribute.Template;
}

var (area, controller, action) = RoutingHelper.GetMvcRouteValues(descriptor);

routes.MapControllerRoute(
name: RoutingHelper.ReplaceMvcPlaceholders(name, area, controller, action) ?? descriptor.DisplayName,
pattern: $"{_adminUrlPrefix}/{RoutingHelper.ReplaceMvcPlaceholders(pattern.TrimStart('/'), area, controller, action)}",
defaults: new { area, controller, action }
);

return true;
name = actionAttribute.RouteName;
pattern = actionAttribute.Template;
}
else if (!string.IsNullOrWhiteSpace(controllerAttribute?.Template))
{
name = controllerAttribute.RouteName;
pattern = controllerAttribute.Template;
}

var (area, controller, action) = RoutingHelper.GetMvcRouteValues(descriptor);

routes.MapControllerRoute(
name: RoutingHelper.ReplaceMvcPlaceholders(name, area, controller, action) ?? descriptor.DisplayName,
pattern: $"{_adminUrlPrefix}/{RoutingHelper.ReplaceMvcPlaceholders(pattern.TrimStart('/'), area, controller, action)}",
defaults: new { area, controller, action }
);

return true;
}
}
85 changes: 42 additions & 43 deletions src/OrchardCore.Modules/OrchardCore.Admin/AdminFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,63 +4,62 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;

namespace OrchardCore.Admin
namespace OrchardCore.Admin;

/// <summary>
/// Intercepts any request to check whether it applies to the admin site.
/// If so it marks the request as such and ensures the user as the right to access it.
/// </summary>
public sealed class AdminFilter : ActionFilterAttribute, IAsyncPageFilter
{
/// <summary>
/// Intercepts any request to check whether it applies to the admin site.
/// If so it marks the request as such and ensures the user as the right to access it.
/// </summary>
public sealed class AdminFilter : ActionFilterAttribute, IAsyncPageFilter
private readonly IAuthorizationService _authorizationService;

public AdminFilter(IAuthorizationService authorizationService)
{
private readonly IAuthorizationService _authorizationService;
_authorizationService = authorizationService;
}

public AdminFilter(IAuthorizationService authorizationService)
{
_authorizationService = authorizationService;
}
public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
ArgumentNullException.ThrowIfNull(context);

public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
if (!await AuthorizeAsync(context.HttpContext))
{
ArgumentNullException.ThrowIfNull(context);
context.Result = context.HttpContext.User?.Identity?.IsAuthenticated ?? false
? new ForbidResult()
: new ChallengeResult();
return;
}

if (!await AuthorizeAsync(context.HttpContext))
{
context.Result = context.HttpContext.User?.Identity?.IsAuthenticated ?? false
? new ForbidResult()
: new ChallengeResult();
return;
}
await base.OnActionExecutionAsync(context, next);
}

await base.OnActionExecutionAsync(context, next);
}
public async Task OnPageHandlerExecutionAsync(PageHandlerExecutingContext context, PageHandlerExecutionDelegate next)
{
ArgumentNullException.ThrowIfNull(context);

public async Task OnPageHandlerExecutionAsync(PageHandlerExecutingContext context, PageHandlerExecutionDelegate next)
if (!await AuthorizeAsync(context.HttpContext))
{
ArgumentNullException.ThrowIfNull(context);
context.Result = new ChallengeResult();
return;
}

if (!await AuthorizeAsync(context.HttpContext))
{
context.Result = new ChallengeResult();
return;
}
// Do post work.
await next.Invoke();
}

// Do post work.
await next.Invoke();
}
public Task OnPageHandlerSelectionAsync(PageHandlerSelectedContext context)
{
return Task.CompletedTask;
}

public Task OnPageHandlerSelectionAsync(PageHandlerSelectedContext context)
private Task<bool> AuthorizeAsync(Microsoft.AspNetCore.Http.HttpContext context)
{
if (AdminAttribute.IsApplied(context))
{
return Task.CompletedTask;
return _authorizationService.AuthorizeAsync(context.User, AdminPermissions.AccessAdminPanel);
}

private Task<bool> AuthorizeAsync(Microsoft.AspNetCore.Http.HttpContext context)
{
if (AdminAttribute.IsApplied(context))
{
return _authorizationService.AuthorizeAsync(context.User, AdminPermissions.AccessAdminPanel);
}

return Task.FromResult(true);
}
return Task.FromResult(true);
}
}
57 changes: 28 additions & 29 deletions src/OrchardCore.Modules/OrchardCore.Admin/AdminMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,42 @@
using OrchardCore.Admin.Drivers;
using OrchardCore.Navigation;

namespace OrchardCore.Admin
namespace OrchardCore.Admin;

public sealed class AdminMenu : INavigationProvider
{
public sealed class AdminMenu : INavigationProvider
private static readonly RouteValueDictionary _routeValues = new()
{
private static readonly RouteValueDictionary _routeValues = new()
{
{ "area", "OrchardCore.Settings" },
{ "groupId", AdminSiteSettingsDisplayDriver.GroupId },
};
{ "area", "OrchardCore.Settings" },
{ "groupId", AdminSiteSettingsDisplayDriver.GroupId },
};

internal readonly IStringLocalizer S;
internal readonly IStringLocalizer S;

public AdminMenu(IStringLocalizer<AdminMenu> localizer)
{
S = localizer;
}
public AdminMenu(IStringLocalizer<AdminMenu> localizer)
{
S = localizer;
}

public Task BuildNavigationAsync(string name, NavigationBuilder builder)
public Task BuildNavigationAsync(string name, NavigationBuilder builder)
{
if (!NavigationHelper.IsAdminMenu(name))
{
if (!NavigationHelper.IsAdminMenu(name))
{
return Task.CompletedTask;
}
return Task.CompletedTask;
}

builder
.Add(S["Configuration"], configuration => configuration
.Add(S["Settings"], settings => settings
.Add(S["Admin"], S["Admin"].PrefixPosition(), admin => admin
.AddClass("admin").Id("admin")
.Action("Index", "Admin", _routeValues)
.Permission(PermissionsAdminSettings.ManageAdminSettings)
.LocalNav()
)
builder
.Add(S["Configuration"], configuration => configuration
.Add(S["Settings"], settings => settings
.Add(S["Admin"], S["Admin"].PrefixPosition(), admin => admin
.AddClass("admin").Id("admin")
.Action("Index", "Admin", _routeValues)
.Permission(PermissionsAdminSettings.ManageAdminSettings)
.LocalNav()
)
);
)
);

return Task.CompletedTask;
}
return Task.CompletedTask;
}
}
Loading