V14: refactor GetPermissions to use attempt pattern (#14470)

* Refactor GetPermissions to take params

* Rework GetPermissionsAsync to attempt pattern

---------

Co-authored-by: Zeegaan <nge@umbraco.dk>
This commit is contained in:
Nikolaj Geisle
2023-06-27 14:34:02 +02:00
committed by GitHub
parent 135a43f5a4
commit dfc7054720
3 changed files with 15 additions and 7 deletions

View File

@@ -2,10 +2,12 @@
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Umbraco.Cms.Api.Management.ViewModels.User.Current; using Umbraco.Cms.Api.Management.ViewModels.User.Current;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Mapping; using Umbraco.Cms.Core.Mapping;
using Umbraco.Cms.Core.Models; using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Security; using Umbraco.Cms.Core.Security;
using Umbraco.Cms.Core.Services; using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Services.OperationStatus;
namespace Umbraco.Cms.Api.Management.Controllers.User.Current; namespace Umbraco.Cms.Api.Management.Controllers.User.Current;
@@ -31,8 +33,14 @@ public class GetPermissionsCurrentUserController : CurrentUserControllerBase
[ProducesResponseType(typeof(IEnumerable<UserPermissionsResponseModel>), StatusCodes.Status200OK)] [ProducesResponseType(typeof(IEnumerable<UserPermissionsResponseModel>), StatusCodes.Status200OK)]
public async Task<IActionResult> GetPermissions([FromQuery(Name = "id")] HashSet<Guid> ids) public async Task<IActionResult> GetPermissions([FromQuery(Name = "id")] HashSet<Guid> ids)
{ {
IEnumerable<NodePermissions> permissions = await _userService.GetPermissionsAsync(CurrentUserKey(_backOfficeSecurityAccessor), ids); Attempt<IEnumerable<NodePermissions>, UserOperationStatus> permissionsAttempt = await _userService.GetPermissionsAsync(CurrentUserKey(_backOfficeSecurityAccessor), ids.ToArray());
List<UserPermissionViewModel> viewmodels = _mapper.MapEnumerable<NodePermissions, UserPermissionViewModel>(permissions);
if (permissionsAttempt.Success is false)
{
return UserOperationStatusResult(permissionsAttempt.Status);
}
List<UserPermissionViewModel> viewmodels = _mapper.MapEnumerable<NodePermissions, UserPermissionViewModel>(permissionsAttempt.Result);
return Ok(new UserPermissionsResponseModel { Permissions = viewmodels }); return Ok(new UserPermissionsResponseModel { Permissions = viewmodels });
} }

View File

@@ -209,7 +209,7 @@ public interface IUserService : IMembershipUserService
/// <param name="userKey">Key of user to retrieve permissions for. </param> /// <param name="userKey">Key of user to retrieve permissions for. </param>
/// <param name="nodeKeys">The keys of the nodes to get permissions for.</param> /// <param name="nodeKeys">The keys of the nodes to get permissions for.</param>
/// <returns>An enumerable list of <see cref="NodePermissions"/>.</returns> /// <returns>An enumerable list of <see cref="NodePermissions"/>.</returns>
Task<IEnumerable<NodePermissions>> GetPermissionsAsync(Guid userKey, IEnumerable<Guid> nodeKeys); Task<Attempt<IEnumerable<NodePermissions>, UserOperationStatus>> GetPermissionsAsync(Guid userKey, params Guid[] nodeKeys);
/// <summary> /// <summary>
/// Get explicitly assigned content permissions for a user and node keys. /// Get explicitly assigned content permissions for a user and node keys.

View File

@@ -2000,7 +2000,7 @@ internal class UserService : RepositoryService, IUserService
} }
/// <inheritdoc /> /// <inheritdoc />
public async Task<IEnumerable<NodePermissions>> GetPermissionsAsync(Guid userKey, IEnumerable<Guid> nodeKeys) public async Task<Attempt<IEnumerable<NodePermissions>, UserOperationStatus>> GetPermissionsAsync(Guid userKey, params Guid[] nodeKeys)
{ {
using ICoreScope scope = ScopeProvider.CreateCoreScope(); using ICoreScope scope = ScopeProvider.CreateCoreScope();
@@ -2008,13 +2008,13 @@ internal class UserService : RepositoryService, IUserService
if (user is null) if (user is null)
{ {
throw new InvalidOperationException("No user with that ID"); return Attempt.FailWithStatus(UserOperationStatus.UserNotFound, Enumerable.Empty<NodePermissions>());
} }
Guid[] keys = nodeKeys.ToArray(); Guid[] keys = nodeKeys.ToArray();
if (keys.Length == 0) if (keys.Length == 0)
{ {
return Enumerable.Empty<NodePermissions>(); return Attempt.SucceedWithStatus(UserOperationStatus.Success, Enumerable.Empty<NodePermissions>());
} }
// We don't know what the entity type may be, so we have to get the entire entity :( // We don't know what the entity type may be, so we have to get the entire entity :(
@@ -2029,7 +2029,7 @@ internal class UserService : RepositoryService, IUserService
results.Add(new NodePermissions { NodeKey = idKeyMap[nodeId], Permissions = permissions }); results.Add(new NodePermissions { NodeKey = idKeyMap[nodeId], Permissions = permissions });
} }
return results; return Attempt.SucceedWithStatus<IEnumerable<NodePermissions>, UserOperationStatus>(UserOperationStatus.UserNotFound, results);
} }
/// <summary> /// <summary>