From 5b9fc97c67f877c6d2d77a48ba5f09324ae65af7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roland=20M=C3=A1rkus?= Date: Sat, 8 Jun 2024 16:15:45 +0200 Subject: [PATCH 1/4] Removing warnings. --- Lombiq.TrainingDemo/Controllers/ApiController.cs | 5 +++++ .../Controllers/CrossTenantServicesController.cs | 4 ++++ .../Controllers/DatabaseStorageController.cs | 5 ++--- .../Controllers/FileManagementController.cs | 2 +- .../Controllers/YourFirstOrchardCoreController.cs | 1 + .../Permissions/DemoSettingsPermissions.cs | 7 +++---- Lombiq.TrainingDemo/Permissions/PersonPermissions.cs | 11 +++++------ .../Services/DateTimeCachingService.cs | 5 +++++ Lombiq.TrainingDemo/ViewModels/PersonPartViewModel.cs | 2 +- 9 files changed, 27 insertions(+), 15 deletions(-) diff --git a/Lombiq.TrainingDemo/Controllers/ApiController.cs b/Lombiq.TrainingDemo/Controllers/ApiController.cs index da0f3a5e..8c93c912 100644 --- a/Lombiq.TrainingDemo/Controllers/ApiController.cs +++ b/Lombiq.TrainingDemo/Controllers/ApiController.cs @@ -29,6 +29,10 @@ namespace Lombiq.TrainingDemo.Controllers; // endpoints should most of the time use the "Api" authentication scheme: This is not the same that standard users are // authenticated with (via cookies). [Authorize(AuthenticationSchemes = "Api"), IgnoreAntiforgeryToken, AllowAnonymous] +[System.Diagnostics.CodeAnalysis.SuppressMessage( + "Major Code Smell", + "S6961:API Controllers should derive from ControllerBase instead of Controller", + Justification = "Seems to be a false positive as ChallangeOrForbid() method is used in the class.")] public class ApiController : Controller { private readonly IAuthorizationService _authorizationService; @@ -46,6 +50,7 @@ public ApiController(IAuthorizationService authorizationService, IContentManager // authorize with a client ID and secret of an OpenID app set up from the Orchard admin. For more info see: // https://docs.orchardcore.net/en/latest/docs/reference/modules/OpenId/. If you just want to quickly test this API // then remove the Authorize attribute above. + [HttpGet] public async Task Get(string contentItemId) { // Authorization is important in API endpoints as well of course. We're re-using the previously created diff --git a/Lombiq.TrainingDemo/Controllers/CrossTenantServicesController.cs b/Lombiq.TrainingDemo/Controllers/CrossTenantServicesController.cs index 79fbd419..e48cc9ef 100644 --- a/Lombiq.TrainingDemo/Controllers/CrossTenantServicesController.cs +++ b/Lombiq.TrainingDemo/Controllers/CrossTenantServicesController.cs @@ -24,6 +24,7 @@ namespace Lombiq.TrainingDemo.Controllers; // This is a controller just for the sake of easy demonstration, you can do the same thing anywhere. In the Index // action, we'll fetch content items from another tenant with the IContentManager service that you already know. This is // just an example though, really you can access any other service as well. +[Route("[controller]/{action=Index}")] public class CrossTenantServicesController : Controller { private readonly IShellHost _shellHost; @@ -38,6 +39,9 @@ public class CrossTenantServicesController : Controller [Route("CrossTenantServices")] public async Task Index(string contentItemId) { + // If ModelState is not in a valid state we should abort the action and return null. + if (!ModelState.IsValid) return null; + // Even if you don't create tenants, there will still be a single tenant in an Orchard app, the Default tenant. // For all other tenants you create you can provide the technical name. diff --git a/Lombiq.TrainingDemo/Controllers/DatabaseStorageController.cs b/Lombiq.TrainingDemo/Controllers/DatabaseStorageController.cs index 3878818b..18d915ed 100644 --- a/Lombiq.TrainingDemo/Controllers/DatabaseStorageController.cs +++ b/Lombiq.TrainingDemo/Controllers/DatabaseStorageController.cs @@ -103,8 +103,7 @@ public async Task JKRosenzweigBooks() // NEXT STATION: Models/PersonPart.cs private static Book[] CreateDemoBooks() => - new[] - { + [ new Book { CoverPhotoUrl = "/Lombiq.TrainingDemo/images/HarryPotter.jpg", @@ -128,5 +127,5 @@ private static Book[] CreateDemoBooks() => Description = "The nation of Panay, formed from a post-apocalyptic North America, is a country " + "that consists of a wealthy Capitol region surrounded by 12 poorer districts.", }, - }; + ]; } diff --git a/Lombiq.TrainingDemo/Controllers/FileManagementController.cs b/Lombiq.TrainingDemo/Controllers/FileManagementController.cs index fe46b6a1..f40055f7 100644 --- a/Lombiq.TrainingDemo/Controllers/FileManagementController.cs +++ b/Lombiq.TrainingDemo/Controllers/FileManagementController.cs @@ -104,7 +104,7 @@ public async Task ReadFileFromMediaFolder() [HttpPost, ActionName(nameof(UploadFileToMedia)), ValidateAntiForgeryToken] public async Task UploadFileToMediaPost(IFormFile file) { - if (file == null) return BadRequest(); + if (!ModelState.IsValid || file == null) return BadRequest(); // You can use the Combine method to combine paths which is pretty much equivalent to the built-in method. var mediaFilePath = _mediaFileStore.Combine(UploadedFileFolderRelativePath, file.FileName); diff --git a/Lombiq.TrainingDemo/Controllers/YourFirstOrchardCoreController.cs b/Lombiq.TrainingDemo/Controllers/YourFirstOrchardCoreController.cs index dc2cadcb..4671aa69 100644 --- a/Lombiq.TrainingDemo/Controllers/YourFirstOrchardCoreController.cs +++ b/Lombiq.TrainingDemo/Controllers/YourFirstOrchardCoreController.cs @@ -17,6 +17,7 @@ namespace Lombiq.TrainingDemo.Controllers; +[Route("[controller]/{action=Index}")] public class YourFirstOrchardCoreController : Controller { private readonly INotifier _notifier; diff --git a/Lombiq.TrainingDemo/Permissions/DemoSettingsPermissions.cs b/Lombiq.TrainingDemo/Permissions/DemoSettingsPermissions.cs index 57dfa28c..59441d47 100644 --- a/Lombiq.TrainingDemo/Permissions/DemoSettingsPermissions.cs +++ b/Lombiq.TrainingDemo/Permissions/DemoSettingsPermissions.cs @@ -19,12 +19,11 @@ public Task> GetPermissionsAsync() => .AsEnumerable()); public IEnumerable GetDefaultStereotypes() => - new[] - { + [ new PermissionStereotype { Name = "Administrator", - Permissions = new[] { ManageDemoSettings }, + Permissions = [ManageDemoSettings], }, - }; + ]; } diff --git a/Lombiq.TrainingDemo/Permissions/PersonPermissions.cs b/Lombiq.TrainingDemo/Permissions/PersonPermissions.cs index 3663a942..2ede0ed6 100644 --- a/Lombiq.TrainingDemo/Permissions/PersonPermissions.cs +++ b/Lombiq.TrainingDemo/Permissions/PersonPermissions.cs @@ -22,7 +22,7 @@ public class PersonPermissions : IPermissionProvider public static readonly Permission AccessPersonListDashboard = new( nameof(AccessPersonListDashboard), "Access the Person List dashboard", - new[] { ManagePersons }); + [ManagePersons]); public Task> GetPermissionsAsync() => Task.FromResult(new[] @@ -34,21 +34,20 @@ public Task> GetPermissionsAsync() => public IEnumerable GetDefaultStereotypes() => // Giving some defaults: which roles should possess which permissions. - new[] - { + [ new PermissionStereotype { // Administrators will have all the permissions by default. Name = "Administrator", // Since AccessPersonListDashboard is implied by EditPersonList we don't have to list the former here. - Permissions = new[] { ManagePersons }, + Permissions = [ManagePersons], }, new PermissionStereotype { Name = "Editor", - Permissions = new[] { AccessPersonListDashboard }, + Permissions = [AccessPersonListDashboard], }, - }; + ]; } // NEXT STATION: Go back to AuthorizationController and find the CanManagePersons action. diff --git a/Lombiq.TrainingDemo/Services/DateTimeCachingService.cs b/Lombiq.TrainingDemo/Services/DateTimeCachingService.cs index b448b005..3efbafb4 100644 --- a/Lombiq.TrainingDemo/Services/DateTimeCachingService.cs +++ b/Lombiq.TrainingDemo/Services/DateTimeCachingService.cs @@ -4,11 +4,16 @@ using OrchardCore.Environment.Cache; using OrchardCore.Modules; using System; +using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Threading.Tasks; namespace Lombiq.TrainingDemo.Services; +[SuppressMessage( + "Usage", + "VSTHRD003:Avoid awaiting foreign Tasks", + Justification = "We need LocalNowAsync to create cachedDate and we don't need any overhead for workarounds as it is working as expected.")] public class DateTimeCachingService : IDateTimeCachingService { public const string MemoryCacheKey = "Lombiq.TrainingDemo.MemoryCache.DateTime"; diff --git a/Lombiq.TrainingDemo/ViewModels/PersonPartViewModel.cs b/Lombiq.TrainingDemo/ViewModels/PersonPartViewModel.cs index 204cc1e3..59d5af83 100644 --- a/Lombiq.TrainingDemo/ViewModels/PersonPartViewModel.cs +++ b/Lombiq.TrainingDemo/ViewModels/PersonPartViewModel.cs @@ -34,7 +34,7 @@ public IEnumerable Validate(ValidationContext validationContex if (BirthDateUtc.HasValue && clock.UtcNow < BirthDateUtc.Value.AddYears(18)) { - yield return new ValidationResult(localizer["The person must be 18 or older."], new[] { nameof(BirthDateUtc) }); + yield return new ValidationResult(localizer["The person must be 18 or older."], [nameof(BirthDateUtc)]); } // Now go back to the PersonPartDisplayDriver. From ebe947b8e77e9eecd46611c025efb53869247b2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roland=20M=C3=A1rkus?= Date: Sat, 8 Jun 2024 16:44:37 +0200 Subject: [PATCH 2/4] Updating comments. --- Lombiq.TrainingDemo/Controllers/ApiController.cs | 2 +- Lombiq.TrainingDemo/Services/DateTimeCachingService.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Lombiq.TrainingDemo/Controllers/ApiController.cs b/Lombiq.TrainingDemo/Controllers/ApiController.cs index 8c93c912..1c6056dc 100644 --- a/Lombiq.TrainingDemo/Controllers/ApiController.cs +++ b/Lombiq.TrainingDemo/Controllers/ApiController.cs @@ -32,7 +32,7 @@ namespace Lombiq.TrainingDemo.Controllers; [System.Diagnostics.CodeAnalysis.SuppressMessage( "Major Code Smell", "S6961:API Controllers should derive from ControllerBase instead of Controller", - Justification = "Seems to be a false positive as ChallangeOrForbid() method is used in the class.")] + Justification = "Seems to be a false positive as per line 62.")] public class ApiController : Controller { private readonly IAuthorizationService _authorizationService; diff --git a/Lombiq.TrainingDemo/Services/DateTimeCachingService.cs b/Lombiq.TrainingDemo/Services/DateTimeCachingService.cs index 3efbafb4..d07f0927 100644 --- a/Lombiq.TrainingDemo/Services/DateTimeCachingService.cs +++ b/Lombiq.TrainingDemo/Services/DateTimeCachingService.cs @@ -13,7 +13,7 @@ namespace Lombiq.TrainingDemo.Services; [SuppressMessage( "Usage", "VSTHRD003:Avoid awaiting foreign Tasks", - Justification = "We need LocalNowAsync to create cachedDate and we don't need any overhead for workarounds as it is working as expected.")] + Justification = "We need LocalNowAsync to create cachedDate.")] public class DateTimeCachingService : IDateTimeCachingService { public const string MemoryCacheKey = "Lombiq.TrainingDemo.MemoryCache.DateTime"; From 8cce21f1b3527fceeff8a2a9cc585f17eec77951 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roland=20M=C3=A1rkus?= <92299130+Psichorex@users.noreply.github.com> Date: Tue, 11 Jun 2024 14:00:23 +0200 Subject: [PATCH 3/4] Update Lombiq.TrainingDemo/Controllers/ApiController.cs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Zoltán Lehóczky --- Lombiq.TrainingDemo/Controllers/ApiController.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lombiq.TrainingDemo/Controllers/ApiController.cs b/Lombiq.TrainingDemo/Controllers/ApiController.cs index 1c6056dc..c2b1de10 100644 --- a/Lombiq.TrainingDemo/Controllers/ApiController.cs +++ b/Lombiq.TrainingDemo/Controllers/ApiController.cs @@ -32,7 +32,8 @@ namespace Lombiq.TrainingDemo.Controllers; [System.Diagnostics.CodeAnalysis.SuppressMessage( "Major Code Smell", "S6961:API Controllers should derive from ControllerBase instead of Controller", - Justification = "Seems to be a false positive as per line 62.")] + Justification = "Can't be changed due to line 62. Will be applicable after an Orchard upgrade due to " + + "https://github.com/OrchardCMS/OrchardCore/issues/16186 being fixed.")] public class ApiController : Controller { private readonly IAuthorizationService _authorizationService; From b2e7eb7f5b83b70f4921feaef372429e2bdbc03f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roland=20M=C3=A1rkus?= Date: Tue, 11 Jun 2024 17:08:32 +0200 Subject: [PATCH 4/4] Removing Routes --- Lombiq.TrainingDemo/Controllers/CrossTenantServicesController.cs | 1 - .../Controllers/YourFirstOrchardCoreController.cs | 1 - 2 files changed, 2 deletions(-) diff --git a/Lombiq.TrainingDemo/Controllers/CrossTenantServicesController.cs b/Lombiq.TrainingDemo/Controllers/CrossTenantServicesController.cs index e48cc9ef..c637cb34 100644 --- a/Lombiq.TrainingDemo/Controllers/CrossTenantServicesController.cs +++ b/Lombiq.TrainingDemo/Controllers/CrossTenantServicesController.cs @@ -24,7 +24,6 @@ namespace Lombiq.TrainingDemo.Controllers; // This is a controller just for the sake of easy demonstration, you can do the same thing anywhere. In the Index // action, we'll fetch content items from another tenant with the IContentManager service that you already know. This is // just an example though, really you can access any other service as well. -[Route("[controller]/{action=Index}")] public class CrossTenantServicesController : Controller { private readonly IShellHost _shellHost; diff --git a/Lombiq.TrainingDemo/Controllers/YourFirstOrchardCoreController.cs b/Lombiq.TrainingDemo/Controllers/YourFirstOrchardCoreController.cs index 4671aa69..dc2cadcb 100644 --- a/Lombiq.TrainingDemo/Controllers/YourFirstOrchardCoreController.cs +++ b/Lombiq.TrainingDemo/Controllers/YourFirstOrchardCoreController.cs @@ -17,7 +17,6 @@ namespace Lombiq.TrainingDemo.Controllers; -[Route("[controller]/{action=Index}")] public class YourFirstOrchardCoreController : Controller { private readonly INotifier _notifier;