Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev' into issue/OCC-245
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahelsaig committed Jul 11, 2024
2 parents c0ee56a + c90748b commit f24bdc5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using OrchardCore.Security.Permissions;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -38,6 +39,11 @@ public static Task<bool> AuthorizeCurrentUserAsync(
/// The information used by <see cref="ChallengeResult"/> or <see cref="ForbidResult"/> if the authorization fails.
/// This is conventionally <c>Api</c> so usually the parameter can be skipped.
/// </param>
/// <param name="checkModelState">
/// If <see langword="true"/>, the <see cref="ControllerBase.ModelState"/> of the <paramref name="controller"/> is
/// checked and if it's not valid, then returns a result indicating validation problem via <see
/// cref="ControllerBase.ValidationProblem(ModelStateDictionary)"/>. Otherwise nothing happens.
/// </param>
/// <typeparam name="TData">
/// The type of the intermediate result received from <paramref name="validateAsync"/> and passed to <paramref
/// name="executeAsync"/>.
Expand All @@ -49,7 +55,8 @@ public static async Task<IActionResult> AuthorizeForCurrentUserValidateAndExecut
IEnumerable<Permission> permissions,
Func<Task<(bool IsSuccess, TData Data)>> validateAsync,
Func<TData, Task<TResult>> executeAsync,
string authenticationScheme = "Api")
string authenticationScheme = "Api",
bool checkModelState = true)
{
foreach (var permission in permissions)
{
Expand All @@ -63,6 +70,12 @@ public static async Task<IActionResult> AuthorizeForCurrentUserValidateAndExecut
if (!isSuccess) return controller.NotFound();

var result = await executeAsync(data);

if (checkModelState && !controller.ModelState.IsValid)
{
return controller.ValidationProblem(controller.ModelState);
}

return result is IActionResult actionResult ? actionResult : controller.Ok(result);
}

Expand All @@ -72,13 +85,15 @@ public static Task<IActionResult> AuthorizeForCurrentUserAndExecuteAsync<TResult
Controller controller,
IEnumerable<Permission> permissions,
Func<Task<TResult>> executeAsync,
string authenticationScheme = "Api") =>
string authenticationScheme = "Api",
bool checkModelState = true) =>
service.AuthorizeForCurrentUserValidateAndExecuteAsync<object, TResult>(
controller,
permissions,
validateAsync: null,
_ => executeAsync(),
authenticationScheme);
authenticationScheme,
checkModelState);

/// <inheritdoc cref="AuthorizeForCurrentUserValidateAndExecuteAsync{TData,TResult}"/>
public static Task<IActionResult> AuthorizeForCurrentUserValidateNotNullAndExecuteAsync<TData, TResult>(
Expand All @@ -87,12 +102,14 @@ public static Task<IActionResult> AuthorizeForCurrentUserValidateNotNullAndExecu
IEnumerable<Permission> permissions,
Func<Task<TData>> validateAsync,
Func<TData, Task<TResult>> executeAsync,
string authenticationScheme = "Api")
string authenticationScheme = "Api",
bool checkModelState = true)
where TData : class =>
service.AuthorizeForCurrentUserValidateAndExecuteAsync(
controller,
permissions,
async () => await validateAsync() is { } data ? (true, data) : (false, default),
executeAsync,
authenticationScheme);
authenticationScheme,
checkModelState);
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
<ItemGroup>
<!-- Take a private dependency on with (PrivateAssets=all) Consumers of this generator will not reference it.
Set GeneratePathProperty=true so we can reference the binaries -->
<PackageReference Include="System.Text.Json" Version="8.0.0" PrivateAssets="all" GeneratePathProperty="true" />
<PackageReference Include="System.Text.Json" Version="8.0.4" PrivateAssets="all" GeneratePathProperty="true" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" GeneratePathProperty="true">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down

0 comments on commit f24bdc5

Please sign in to comment.