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:
@@ -2,10 +2,12 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Umbraco.Cms.Api.Management.ViewModels.User.Current;
|
||||
using Umbraco.Cms.Core;
|
||||
using Umbraco.Cms.Core.Mapping;
|
||||
using Umbraco.Cms.Core.Models;
|
||||
using Umbraco.Cms.Core.Security;
|
||||
using Umbraco.Cms.Core.Services;
|
||||
using Umbraco.Cms.Core.Services.OperationStatus;
|
||||
|
||||
namespace Umbraco.Cms.Api.Management.Controllers.User.Current;
|
||||
|
||||
@@ -31,8 +33,14 @@ public class GetPermissionsCurrentUserController : CurrentUserControllerBase
|
||||
[ProducesResponseType(typeof(IEnumerable<UserPermissionsResponseModel>), StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> GetPermissions([FromQuery(Name = "id")] HashSet<Guid> ids)
|
||||
{
|
||||
IEnumerable<NodePermissions> permissions = await _userService.GetPermissionsAsync(CurrentUserKey(_backOfficeSecurityAccessor), ids);
|
||||
List<UserPermissionViewModel> viewmodels = _mapper.MapEnumerable<NodePermissions, UserPermissionViewModel>(permissions);
|
||||
Attempt<IEnumerable<NodePermissions>, UserOperationStatus> permissionsAttempt = await _userService.GetPermissionsAsync(CurrentUserKey(_backOfficeSecurityAccessor), ids.ToArray());
|
||||
|
||||
if (permissionsAttempt.Success is false)
|
||||
{
|
||||
return UserOperationStatusResult(permissionsAttempt.Status);
|
||||
}
|
||||
|
||||
List<UserPermissionViewModel> viewmodels = _mapper.MapEnumerable<NodePermissions, UserPermissionViewModel>(permissionsAttempt.Result);
|
||||
|
||||
return Ok(new UserPermissionsResponseModel { Permissions = viewmodels });
|
||||
}
|
||||
|
||||
@@ -209,7 +209,7 @@ public interface IUserService : IMembershipUserService
|
||||
/// <param name="userKey">Key of user to retrieve 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>
|
||||
Task<IEnumerable<NodePermissions>> GetPermissionsAsync(Guid userKey, IEnumerable<Guid> nodeKeys);
|
||||
Task<Attempt<IEnumerable<NodePermissions>, UserOperationStatus>> GetPermissionsAsync(Guid userKey, params Guid[] nodeKeys);
|
||||
|
||||
/// <summary>
|
||||
/// Get explicitly assigned content permissions for a user and node keys.
|
||||
|
||||
@@ -2000,7 +2000,7 @@ internal class UserService : RepositoryService, IUserService
|
||||
}
|
||||
|
||||
/// <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();
|
||||
|
||||
@@ -2008,13 +2008,13 @@ internal class UserService : RepositoryService, IUserService
|
||||
|
||||
if (user is null)
|
||||
{
|
||||
throw new InvalidOperationException("No user with that ID");
|
||||
return Attempt.FailWithStatus(UserOperationStatus.UserNotFound, Enumerable.Empty<NodePermissions>());
|
||||
}
|
||||
|
||||
Guid[] keys = nodeKeys.ToArray();
|
||||
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 :(
|
||||
@@ -2029,7 +2029,7 @@ internal class UserService : RepositoryService, IUserService
|
||||
results.Add(new NodePermissions { NodeKey = idKeyMap[nodeId], Permissions = permissions });
|
||||
}
|
||||
|
||||
return results;
|
||||
return Attempt.SucceedWithStatus<IEnumerable<NodePermissions>, UserOperationStatus>(UserOperationStatus.UserNotFound, results);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user