2023-05-09 08:38:07 +02:00
|
|
|
|
using Asp.Versioning;
|
2023-12-11 08:25:29 +01:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2024-02-29 10:40:48 +01:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2023-05-09 08:38:07 +02:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2023-12-11 08:25:29 +01:00
|
|
|
|
using Umbraco.Cms.Api.Management.Security.Authorization.User;
|
2023-08-29 15:51:20 +02:00
|
|
|
|
using Umbraco.Cms.Core.Security;
|
2023-03-29 08:14:47 +02:00
|
|
|
|
using Umbraco.Cms.Core.Services;
|
|
|
|
|
|
using Umbraco.Cms.Core.Services.OperationStatus;
|
2023-12-11 08:25:29 +01:00
|
|
|
|
using Umbraco.Cms.Web.Common.Authorization;
|
|
|
|
|
|
using Umbraco.Extensions;
|
2023-03-29 08:14:47 +02:00
|
|
|
|
|
2023-04-20 09:40:48 +02:00
|
|
|
|
namespace Umbraco.Cms.Api.Management.Controllers.User;
|
2023-03-29 08:14:47 +02:00
|
|
|
|
|
2023-05-09 08:38:07 +02:00
|
|
|
|
[ApiVersion("1.0")]
|
2023-04-20 09:40:48 +02:00
|
|
|
|
public class DeleteUserController : UserControllerBase
|
2023-03-29 08:14:47 +02:00
|
|
|
|
{
|
2023-12-11 08:25:29 +01:00
|
|
|
|
private readonly IAuthorizationService _authorizationService;
|
|
|
|
|
|
private readonly IUserService _userService;
|
|
|
|
|
|
private readonly IBackOfficeSecurityAccessor _backOfficeSecurityAccessor;
|
|
|
|
|
|
|
|
|
|
|
|
public DeleteUserController(
|
|
|
|
|
|
IAuthorizationService authorizationService,
|
|
|
|
|
|
IUserService userService,
|
|
|
|
|
|
IBackOfficeSecurityAccessor backOfficeSecurityAccessor)
|
2023-08-29 15:51:20 +02:00
|
|
|
|
{
|
2023-12-11 08:25:29 +01:00
|
|
|
|
_authorizationService = authorizationService;
|
2023-08-29 15:51:20 +02:00
|
|
|
|
_userService = userService;
|
|
|
|
|
|
_backOfficeSecurityAccessor = backOfficeSecurityAccessor;
|
|
|
|
|
|
}
|
2023-03-29 08:14:47 +02:00
|
|
|
|
|
2023-05-09 08:38:07 +02:00
|
|
|
|
[MapToApiVersion("1.0")]
|
2023-03-29 08:14:47 +02:00
|
|
|
|
[HttpDelete("{id:guid}")]
|
2024-02-29 10:40:48 +01:00
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
|
|
|
|
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)]
|
2023-03-29 08:14:47 +02:00
|
|
|
|
public async Task<IActionResult> DeleteUser(Guid id)
|
|
|
|
|
|
{
|
2023-12-11 08:25:29 +01:00
|
|
|
|
AuthorizationResult authorizationResult = await _authorizationService.AuthorizeResourceAsync(
|
|
|
|
|
|
User,
|
|
|
|
|
|
UserPermissionResource.WithKeys(id),
|
|
|
|
|
|
AuthorizationPolicies.AdminUserEditsRequireAdmin);
|
|
|
|
|
|
|
|
|
|
|
|
if (!authorizationResult.Succeeded)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Forbidden();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-08-29 15:51:20 +02:00
|
|
|
|
UserOperationStatus result = await _userService.DeleteAsync(CurrentUserKey(_backOfficeSecurityAccessor), id);
|
2023-03-29 08:14:47 +02:00
|
|
|
|
|
|
|
|
|
|
return result is UserOperationStatus.Success
|
|
|
|
|
|
? Ok()
|
|
|
|
|
|
: UserOperationStatusResult(result);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|