2023-05-09 08:38:07 +02:00
|
|
|
|
using Asp.Versioning;
|
2023-12-11 08:25:29 +01:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2023-05-09 08:38:07 +02:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2023-02-16 09:39:17 +01:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
using Umbraco.Cms.Api.Management.Factories;
|
2023-04-20 09:40:48 +02:00
|
|
|
|
using Umbraco.Cms.Api.Management.ViewModels.UserGroup;
|
2023-02-16 09:39:17 +01:00
|
|
|
|
using Umbraco.Cms.Core.Models.Membership;
|
2024-02-29 09:41:56 +00:00
|
|
|
|
using Umbraco.Cms.Core.Security.Authorization;
|
2023-02-16 09:39:17 +01:00
|
|
|
|
using Umbraco.Cms.Core.Services;
|
2023-12-11 08:25:29 +01:00
|
|
|
|
using Umbraco.Cms.Web.Common.Authorization;
|
|
|
|
|
|
using Umbraco.Extensions;
|
2023-02-16 09:39:17 +01:00
|
|
|
|
|
2023-04-20 09:40:48 +02:00
|
|
|
|
namespace Umbraco.Cms.Api.Management.Controllers.UserGroup;
|
2023-02-16 09:39:17 +01:00
|
|
|
|
|
2023-05-09 08:38:07 +02:00
|
|
|
|
[ApiVersion("1.0")]
|
2023-04-20 09:40:48 +02:00
|
|
|
|
public class ByKeyUserGroupController : UserGroupControllerBase
|
2023-02-16 09:39:17 +01:00
|
|
|
|
{
|
2023-12-11 08:25:29 +01:00
|
|
|
|
private readonly IAuthorizationService _authorizationService;
|
2023-02-16 09:39:17 +01:00
|
|
|
|
private readonly IUserGroupService _userGroupService;
|
2023-03-13 10:49:21 +01:00
|
|
|
|
private readonly IUserGroupPresentationFactory _userGroupPresentationFactory;
|
2023-02-16 09:39:17 +01:00
|
|
|
|
|
|
|
|
|
|
public ByKeyUserGroupController(
|
2023-12-11 08:25:29 +01:00
|
|
|
|
IAuthorizationService authorizationService,
|
2023-02-16 09:39:17 +01:00
|
|
|
|
IUserGroupService userGroupService,
|
2023-03-13 10:49:21 +01:00
|
|
|
|
IUserGroupPresentationFactory userGroupPresentationFactory)
|
2023-02-16 09:39:17 +01:00
|
|
|
|
{
|
2023-12-11 08:25:29 +01:00
|
|
|
|
_authorizationService = authorizationService;
|
2023-02-16 09:39:17 +01:00
|
|
|
|
_userGroupService = userGroupService;
|
2023-03-13 10:49:21 +01:00
|
|
|
|
_userGroupPresentationFactory = userGroupPresentationFactory;
|
2023-02-16 09:39:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-04-04 13:20:29 +02:00
|
|
|
|
[HttpGet("{id:guid}")]
|
2023-02-16 09:39:17 +01:00
|
|
|
|
[MapToApiVersion("1.0")]
|
2023-05-04 10:02:07 +02:00
|
|
|
|
[ProducesResponseType(typeof(UserGroupResponseModel), StatusCodes.Status200OK)]
|
2023-08-04 10:51:20 +02:00
|
|
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
|
2024-04-09 08:18:45 +02:00
|
|
|
|
public async Task<IActionResult> ByKey(CancellationToken cancellationToken, Guid id)
|
2023-02-16 09:39:17 +01:00
|
|
|
|
{
|
2023-12-11 08:25:29 +01:00
|
|
|
|
AuthorizationResult authorizationResult = await _authorizationService.AuthorizeResourceAsync(
|
|
|
|
|
|
User,
|
|
|
|
|
|
UserGroupPermissionResource.WithKeys(id),
|
|
|
|
|
|
AuthorizationPolicies.UserBelongsToUserGroupInRequest);
|
|
|
|
|
|
|
|
|
|
|
|
if (!authorizationResult.Succeeded)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Forbidden();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-04-04 13:20:29 +02:00
|
|
|
|
IUserGroup? userGroup = await _userGroupService.GetAsync(id);
|
2023-02-16 09:39:17 +01:00
|
|
|
|
|
|
|
|
|
|
if (userGroup is null)
|
|
|
|
|
|
{
|
2023-08-04 10:51:20 +02:00
|
|
|
|
return UserGroupNotFound();
|
2023-02-16 09:39:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-08-04 10:51:20 +02:00
|
|
|
|
return Ok(await _userGroupPresentationFactory.CreateAsync(userGroup));
|
2023-02-16 09:39:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|