2020-12-29 12:55:48 +01:00
|
|
|
using System;
|
2020-06-22 10:08:08 +02:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2021-03-04 17:29:11 +00:00
|
|
|
using System.Threading.Tasks;
|
2020-11-19 19:23:41 +11:00
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2021-03-04 17:29:11 +00:00
|
|
|
using Microsoft.AspNetCore.Identity;
|
2020-06-22 10:08:08 +02:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2021-02-18 11:06:02 +01:00
|
|
|
using Umbraco.Cms.Core;
|
|
|
|
|
using Umbraco.Cms.Core.Mapping;
|
|
|
|
|
using Umbraco.Cms.Core.Models;
|
|
|
|
|
using Umbraco.Cms.Core.Models.ContentEditing;
|
|
|
|
|
using Umbraco.Cms.Core.Services;
|
|
|
|
|
using Umbraco.Cms.Web.Common.Attributes;
|
|
|
|
|
using Umbraco.Cms.Web.Common.Authorization;
|
|
|
|
|
using Umbraco.Extensions;
|
|
|
|
|
using Constants = Umbraco.Cms.Core.Constants;
|
2020-06-22 10:08:08 +02:00
|
|
|
|
2021-02-18 11:06:02 +01:00
|
|
|
namespace Umbraco.Cms.Web.BackOffice.Controllers
|
2020-06-22 10:08:08 +02:00
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// An API controller used for dealing with member groups
|
|
|
|
|
/// </summary>
|
2020-08-04 12:27:21 +10:00
|
|
|
[PluginController(Constants.Web.Mvc.BackOfficeApiArea)]
|
2020-11-19 19:23:41 +11:00
|
|
|
[Authorize(Policy = AuthorizationPolicies.TreeAccessMemberGroups)]
|
2021-01-29 10:30:28 +01:00
|
|
|
[ParameterSwapControllerActionSelector(nameof(GetById), "id", typeof(int), typeof(Guid), typeof(Udi))]
|
2020-06-22 10:08:08 +02:00
|
|
|
public class MemberGroupController : UmbracoAuthorizedJsonController
|
|
|
|
|
{
|
|
|
|
|
private readonly IMemberGroupService _memberGroupService;
|
|
|
|
|
private readonly UmbracoMapper _umbracoMapper;
|
|
|
|
|
private readonly ILocalizedTextService _localizedTextService;
|
2021-03-06 10:53:34 +00:00
|
|
|
private readonly RoleManager<IdentityRole> _roleManager;
|
2021-03-04 17:29:11 +00:00
|
|
|
|
2020-06-22 10:08:08 +02:00
|
|
|
public MemberGroupController(
|
|
|
|
|
IMemberGroupService memberGroupService,
|
|
|
|
|
UmbracoMapper umbracoMapper,
|
2021-03-04 17:29:11 +00:00
|
|
|
ILocalizedTextService localizedTextService,
|
2021-03-06 10:53:34 +00:00
|
|
|
RoleManager<IdentityRole> roleManager
|
2020-06-22 10:08:08 +02:00
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
_memberGroupService = memberGroupService ?? throw new ArgumentNullException(nameof(memberGroupService));
|
2021-03-04 17:29:11 +00:00
|
|
|
_roleManager = roleManager ?? throw new ArgumentNullException(nameof(roleManager));
|
2020-06-22 10:08:08 +02:00
|
|
|
_umbracoMapper = umbracoMapper ?? throw new ArgumentNullException(nameof(umbracoMapper));
|
2021-03-04 17:29:11 +00:00
|
|
|
_localizedTextService = localizedTextService ?? throw new ArgumentNullException(nameof(localizedTextService));
|
2020-06-22 10:08:08 +02:00
|
|
|
}
|
|
|
|
|
|
2020-08-26 08:05:15 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the member group json for the member group id
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="id"></param>
|
|
|
|
|
/// <returns></returns>
|
2021-03-06 23:09:19 +00:00
|
|
|
public async Task<ActionResult<MemberGroupDisplay>> GetById(int id)
|
2020-06-22 10:08:08 +02:00
|
|
|
{
|
2021-03-06 23:09:19 +00:00
|
|
|
//TODO: did we envisage this - combination of service and identity manager?
|
|
|
|
|
IdentityRole identityRole = await _roleManager.FindByIdAsync(id.ToString());
|
|
|
|
|
if (identityRole == null)
|
|
|
|
|
{
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IMemberGroup memberGroup = _memberGroupService.GetById(id);
|
2020-06-22 10:08:08 +02:00
|
|
|
if (memberGroup == null)
|
|
|
|
|
{
|
2021-01-12 14:00:14 +01:00
|
|
|
return NotFound();
|
2020-06-22 10:08:08 +02:00
|
|
|
}
|
|
|
|
|
|
2021-03-06 23:09:19 +00:00
|
|
|
//TODO: the default identity role doesn't have all the properties IMemberGroup had, e.g. CreatorId
|
|
|
|
|
MemberGroupDisplay dto = _umbracoMapper.Map<IMemberGroup, MemberGroupDisplay>(memberGroup);
|
2020-06-22 10:08:08 +02:00
|
|
|
return dto;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-26 08:05:15 +02:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the member group json for the member group guid
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="id"></param>
|
|
|
|
|
/// <returns></returns>
|
2021-03-06 23:09:19 +00:00
|
|
|
public async Task<ActionResult<MemberGroupDisplay>> GetById(Guid id)
|
2020-08-26 08:05:15 +02:00
|
|
|
{
|
2021-03-06 23:09:19 +00:00
|
|
|
//TODO: did we envisage just identity or a combination of service and identity manager?
|
|
|
|
|
IdentityRole identityRole = await _roleManager.FindByIdAsync(id.ToString());
|
|
|
|
|
if (identityRole == null)
|
2020-08-26 08:05:15 +02:00
|
|
|
{
|
2021-01-12 14:00:14 +01:00
|
|
|
return NotFound();
|
2020-08-26 08:05:15 +02:00
|
|
|
}
|
2021-03-06 23:09:19 +00:00
|
|
|
//IMemberGroup memberGroup = _memberGroupService.GetById(id);
|
|
|
|
|
//if (memberGroup == null)
|
|
|
|
|
//{
|
|
|
|
|
// return NotFound();
|
|
|
|
|
//}
|
2020-08-26 08:05:15 +02:00
|
|
|
|
2021-03-06 23:09:19 +00:00
|
|
|
return _umbracoMapper.Map<IdentityRole, MemberGroupDisplay>(identityRole);
|
2020-08-26 08:05:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the member group json for the member group udi
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="id"></param>
|
|
|
|
|
/// <returns></returns>
|
2021-03-06 23:09:19 +00:00
|
|
|
public async Task<ActionResult<MemberGroupDisplay>> GetById(Udi id)
|
2020-08-26 08:05:15 +02:00
|
|
|
{
|
|
|
|
|
var guidUdi = id as GuidUdi;
|
|
|
|
|
if (guidUdi == null)
|
2021-03-04 17:29:11 +00:00
|
|
|
{
|
2021-01-12 14:00:14 +01:00
|
|
|
return NotFound();
|
2021-03-04 17:29:11 +00:00
|
|
|
}
|
2020-08-26 08:05:15 +02:00
|
|
|
|
2021-03-06 23:09:19 +00:00
|
|
|
//TODO: can we do this via identity?
|
|
|
|
|
IdentityRole identityRole = await _roleManager.FindByIdAsync(id.ToString());
|
|
|
|
|
if (identityRole == null)
|
2020-08-26 08:05:15 +02:00
|
|
|
{
|
2021-01-12 14:00:14 +01:00
|
|
|
return NotFound();
|
2020-08-26 08:05:15 +02:00
|
|
|
}
|
|
|
|
|
|
2021-03-06 23:09:19 +00:00
|
|
|
return _umbracoMapper.Map<IdentityRole, MemberGroupDisplay>(identityRole);
|
2020-08-26 08:05:15 +02:00
|
|
|
}
|
|
|
|
|
|
2021-03-06 23:09:19 +00:00
|
|
|
public async Task<IEnumerable<MemberGroupDisplay>> GetByIds([FromQuery]int[] ids)
|
2020-06-22 10:08:08 +02:00
|
|
|
{
|
2021-03-06 23:09:19 +00:00
|
|
|
var roles = new List<IdentityRole>();
|
2021-03-04 17:29:11 +00:00
|
|
|
|
|
|
|
|
foreach (int id in ids)
|
|
|
|
|
{
|
2021-03-06 23:09:19 +00:00
|
|
|
IdentityRole role = await _roleManager.FindByIdAsync(id.ToString());
|
|
|
|
|
roles.Add(role);
|
2021-03-04 17:29:11 +00:00
|
|
|
}
|
|
|
|
|
|
2021-03-06 23:09:19 +00:00
|
|
|
return roles.Select(x=> _umbracoMapper.Map<IdentityRole, MemberGroupDisplay>(x));
|
2020-06-22 10:08:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpDelete]
|
|
|
|
|
[HttpPost]
|
2021-03-06 23:09:19 +00:00
|
|
|
public async Task<IActionResult> DeleteById(int id)
|
2020-06-22 10:08:08 +02:00
|
|
|
{
|
2021-03-06 23:09:19 +00:00
|
|
|
//TODO: are there any repercussions elsewhere for us changing these to async?
|
|
|
|
|
IdentityRole role = await _roleManager.FindByIdAsync(id.ToString());
|
|
|
|
|
|
|
|
|
|
if (role == null)
|
2020-06-22 10:08:08 +02:00
|
|
|
{
|
2021-01-12 14:00:14 +01:00
|
|
|
return NotFound();
|
2020-06-22 10:08:08 +02:00
|
|
|
}
|
|
|
|
|
|
2021-03-06 23:09:19 +00:00
|
|
|
IdentityResult roleDeleted = await _roleManager.DeleteAsync(role);
|
|
|
|
|
if (roleDeleted.Succeeded)
|
|
|
|
|
{
|
|
|
|
|
return Ok();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return Problem("Issue during deletion - please see logs");
|
|
|
|
|
}
|
2020-06-22 10:08:08 +02:00
|
|
|
}
|
|
|
|
|
|
2021-03-06 23:09:19 +00:00
|
|
|
public IEnumerable<MemberGroupDisplay> GetAllGroups() => _roleManager.Roles.Select(x => _umbracoMapper.Map<IdentityRole, MemberGroupDisplay>(x));
|
2020-06-22 10:08:08 +02:00
|
|
|
|
|
|
|
|
public MemberGroupDisplay GetEmpty()
|
|
|
|
|
{
|
|
|
|
|
var item = new MemberGroup();
|
|
|
|
|
return _umbracoMapper.Map<IMemberGroup, MemberGroupDisplay>(item);
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-06 23:09:19 +00:00
|
|
|
public async Task<ActionResult<MemberGroupDisplay>> PostSave(MemberGroupSave saveModel)
|
2020-06-22 10:08:08 +02:00
|
|
|
{
|
2021-03-06 23:09:19 +00:00
|
|
|
int id = int.Parse(saveModel.Id.ToString());
|
2020-06-22 10:08:08 +02:00
|
|
|
|
2021-03-06 23:09:19 +00:00
|
|
|
IdentityRole role = id > 0 ? await _roleManager.FindByIdAsync(saveModel.Id.ToString()) : null;
|
|
|
|
|
|
|
|
|
|
if (role == null)
|
2020-06-22 10:08:08 +02:00
|
|
|
{
|
2021-01-12 14:00:14 +01:00
|
|
|
return NotFound();
|
2020-06-22 10:08:08 +02:00
|
|
|
}
|
|
|
|
|
|
2021-03-06 23:09:19 +00:00
|
|
|
role.Name = saveModel.Name;
|
|
|
|
|
IdentityResult updatedResult = await _roleManager.UpdateAsync(role);
|
|
|
|
|
|
|
|
|
|
if (!updatedResult.Succeeded)
|
|
|
|
|
{
|
|
|
|
|
//TODO: what to retrun if there is a failed identity result
|
|
|
|
|
return Problem();
|
|
|
|
|
}
|
2020-06-22 10:08:08 +02:00
|
|
|
|
2021-03-06 23:09:19 +00:00
|
|
|
//TODO: should we return the identity role or return the group from the service?
|
|
|
|
|
MemberGroupDisplay display = _umbracoMapper.Map<IdentityRole, MemberGroupDisplay>(role);
|
2020-06-22 10:08:08 +02:00
|
|
|
|
|
|
|
|
display.AddSuccessNotification(
|
|
|
|
|
_localizedTextService.Localize("speechBubbles/memberGroupSavedHeader"),
|
|
|
|
|
string.Empty);
|
|
|
|
|
|
|
|
|
|
return display;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|