2020-12-05 23:44:50 +00:00
|
|
|
using System;
|
2020-06-18 21:03:11 +02:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Net.Mime;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
2020-11-19 22:17:42 +11:00
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2021-01-13 16:14:20 +01:00
|
|
|
using Microsoft.AspNetCore.Http;
|
2020-12-05 23:44:50 +00:00
|
|
|
using Microsoft.AspNetCore.Identity;
|
2020-06-18 21:03:11 +02:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2020-09-21 09:52:58 +02:00
|
|
|
using Microsoft.Extensions.Logging;
|
2020-06-18 21:03:11 +02:00
|
|
|
using Umbraco.Core;
|
|
|
|
|
using Umbraco.Core.Dictionary;
|
|
|
|
|
using Umbraco.Core.Events;
|
2020-08-20 22:18:50 +01:00
|
|
|
using Umbraco.Core.Mapping;
|
2020-06-18 21:03:11 +02:00
|
|
|
using Umbraco.Core.Models;
|
|
|
|
|
using Umbraco.Core.Models.ContentEditing;
|
2020-12-05 23:44:50 +00:00
|
|
|
using Umbraco.Core.Models.Membership;
|
2020-06-18 21:03:11 +02:00
|
|
|
using Umbraco.Core.PropertyEditors;
|
|
|
|
|
using Umbraco.Core.Security;
|
2020-08-20 22:18:50 +01:00
|
|
|
using Umbraco.Core.Serialization;
|
2020-06-18 21:03:11 +02:00
|
|
|
using Umbraco.Core.Services;
|
|
|
|
|
using Umbraco.Core.Strings;
|
|
|
|
|
using Umbraco.Extensions;
|
2020-12-08 01:57:14 +00:00
|
|
|
using Umbraco.Infrastructure.Security;
|
2021-01-11 17:01:44 +00:00
|
|
|
using Umbraco.Infrastructure.Services.Implement;
|
2020-06-18 21:03:11 +02:00
|
|
|
using Umbraco.Web.BackOffice.Filters;
|
|
|
|
|
using Umbraco.Web.BackOffice.ModelBinders;
|
2020-12-22 16:36:07 +01:00
|
|
|
using Umbraco.Web.Common.ActionsResults;
|
2020-06-18 21:03:11 +02:00
|
|
|
using Umbraco.Web.Common.Attributes;
|
2020-11-20 12:40:29 +11:00
|
|
|
using Umbraco.Web.Common.Authorization;
|
2020-06-18 21:03:11 +02:00
|
|
|
using Umbraco.Web.Common.Filters;
|
2020-08-20 22:18:50 +01:00
|
|
|
using Umbraco.Web.ContentApps;
|
|
|
|
|
using Umbraco.Web.Models.ContentEditing;
|
2020-06-18 21:03:11 +02:00
|
|
|
|
|
|
|
|
namespace Umbraco.Web.BackOffice.Controllers
|
|
|
|
|
{
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// This controller is decorated with the UmbracoApplicationAuthorizeAttribute which means that any user requesting
|
|
|
|
|
/// access to ALL of the methods on this controller will need access to the member application.
|
|
|
|
|
/// </remarks>
|
2020-08-04 12:27:21 +10:00
|
|
|
[PluginController(Constants.Web.Mvc.BackOfficeApiArea)]
|
2020-11-19 22:17:42 +11:00
|
|
|
[Authorize(Policy = AuthorizationPolicies.SectionAccessMembers)]
|
2020-06-18 21:03:11 +02:00
|
|
|
[OutgoingNoHyphenGuidFormat]
|
|
|
|
|
public class MemberController : ContentControllerBase
|
|
|
|
|
{
|
|
|
|
|
private readonly PropertyEditorCollection _propertyEditors;
|
|
|
|
|
private readonly UmbracoMapper _umbracoMapper;
|
|
|
|
|
private readonly IMemberService _memberService;
|
|
|
|
|
private readonly IMemberTypeService _memberTypeService;
|
2021-02-12 14:38:20 +00:00
|
|
|
private readonly IMemberManager _memberManager;
|
2020-06-18 21:03:11 +02:00
|
|
|
private readonly IDataTypeService _dataTypeService;
|
|
|
|
|
private readonly ILocalizedTextService _localizedTextService;
|
2020-12-05 23:44:50 +00:00
|
|
|
private readonly IBackOfficeSecurityAccessor _backOfficeSecurityAccessor;
|
2020-06-18 21:03:11 +02:00
|
|
|
private readonly IJsonSerializer _jsonSerializer;
|
2021-02-03 17:33:48 +00:00
|
|
|
private readonly IShortStringHelper _shortStringHelper;
|
2020-06-18 21:03:11 +02:00
|
|
|
|
2020-12-05 23:44:50 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="MemberController"/> class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="cultureDictionary">The culture dictionary</param>
|
|
|
|
|
/// <param name="loggerFactory">The logger factory</param>
|
|
|
|
|
/// <param name="shortStringHelper">The string helper</param>
|
|
|
|
|
/// <param name="eventMessages">The event messages factory</param>
|
|
|
|
|
/// <param name="localizedTextService">The entry point for localizing key services</param>
|
|
|
|
|
/// <param name="propertyEditors">The property editors</param>
|
|
|
|
|
/// <param name="umbracoMapper">The mapper</param>
|
|
|
|
|
/// <param name="memberService">The member service</param>
|
|
|
|
|
/// <param name="memberTypeService">The member type service</param>
|
|
|
|
|
/// <param name="memberManager">The member manager</param>
|
|
|
|
|
/// <param name="dataTypeService">The data-type service</param>
|
|
|
|
|
/// <param name="backOfficeSecurityAccessor">The back office security accessor</param>
|
|
|
|
|
/// <param name="jsonSerializer">The JSON serializer</param>
|
2020-06-18 21:03:11 +02:00
|
|
|
public MemberController(
|
|
|
|
|
ICultureDictionary cultureDictionary,
|
2020-09-21 09:52:58 +02:00
|
|
|
ILoggerFactory loggerFactory,
|
2020-06-18 21:03:11 +02:00
|
|
|
IShortStringHelper shortStringHelper,
|
|
|
|
|
IEventMessagesFactory eventMessages,
|
|
|
|
|
ILocalizedTextService localizedTextService,
|
|
|
|
|
PropertyEditorCollection propertyEditors,
|
|
|
|
|
UmbracoMapper umbracoMapper,
|
|
|
|
|
IMemberService memberService,
|
|
|
|
|
IMemberTypeService memberTypeService,
|
2021-02-12 14:38:20 +00:00
|
|
|
IMemberManager memberManager,
|
2020-06-18 21:03:11 +02:00
|
|
|
IDataTypeService dataTypeService,
|
2020-12-05 23:44:50 +00:00
|
|
|
IBackOfficeSecurityAccessor backOfficeSecurityAccessor,
|
2020-06-18 21:03:11 +02:00
|
|
|
IJsonSerializer jsonSerializer)
|
2020-11-17 20:27:10 +01:00
|
|
|
: base(cultureDictionary, loggerFactory, shortStringHelper, eventMessages, localizedTextService, jsonSerializer)
|
2020-06-18 21:03:11 +02:00
|
|
|
{
|
|
|
|
|
_propertyEditors = propertyEditors;
|
|
|
|
|
_umbracoMapper = umbracoMapper;
|
|
|
|
|
_memberService = memberService;
|
|
|
|
|
_memberTypeService = memberTypeService;
|
2020-11-30 00:45:38 +00:00
|
|
|
_memberManager = memberManager;
|
2020-06-18 21:03:11 +02:00
|
|
|
_dataTypeService = dataTypeService;
|
|
|
|
|
_localizedTextService = localizedTextService;
|
2020-12-05 23:44:50 +00:00
|
|
|
_backOfficeSecurityAccessor = backOfficeSecurityAccessor;
|
2020-06-18 21:03:11 +02:00
|
|
|
_jsonSerializer = jsonSerializer;
|
2021-02-03 17:33:48 +00:00
|
|
|
_shortStringHelper = shortStringHelper;
|
2020-06-18 21:03:11 +02:00
|
|
|
}
|
|
|
|
|
|
2020-12-05 23:44:50 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// The paginated list of members
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="pageNumber">The page number to display</param>
|
|
|
|
|
/// <param name="pageSize">The size of the page</param>
|
|
|
|
|
/// <param name="orderBy">The ordering of the member list</param>
|
|
|
|
|
/// <param name="orderDirection">The direction of the member list</param>
|
|
|
|
|
/// <param name="orderBySystemField">The system field to order by</param>
|
|
|
|
|
/// <param name="filter">The current filter for the list</param>
|
|
|
|
|
/// <param name="memberTypeAlias">The member type</param>
|
|
|
|
|
/// <returns>The paged result of members</returns>
|
2020-06-18 21:03:11 +02:00
|
|
|
public PagedResult<MemberBasic> GetPagedResults(
|
|
|
|
|
int pageNumber = 1,
|
|
|
|
|
int pageSize = 100,
|
|
|
|
|
string orderBy = "username",
|
|
|
|
|
Direction orderDirection = Direction.Ascending,
|
|
|
|
|
bool orderBySystemField = true,
|
|
|
|
|
string filter = "",
|
|
|
|
|
string memberTypeAlias = null)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if (pageNumber <= 0 || pageSize <= 0)
|
|
|
|
|
{
|
|
|
|
|
throw new NotSupportedException("Both pageNumber and pageSize must be greater than zero");
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-30 00:45:38 +00:00
|
|
|
IMember[] members = _memberService.GetAll(
|
|
|
|
|
pageNumber - 1,
|
|
|
|
|
pageSize,
|
|
|
|
|
out var totalRecords,
|
|
|
|
|
orderBy,
|
|
|
|
|
orderDirection,
|
|
|
|
|
orderBySystemField,
|
|
|
|
|
memberTypeAlias,
|
|
|
|
|
filter).ToArray();
|
2020-06-18 21:03:11 +02:00
|
|
|
if (totalRecords == 0)
|
|
|
|
|
{
|
|
|
|
|
return new PagedResult<MemberBasic>(0, 0, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var pagedResult = new PagedResult<MemberBasic>(totalRecords, pageNumber, pageSize)
|
|
|
|
|
{
|
2020-11-30 00:45:38 +00:00
|
|
|
Items = members.Select(x => _umbracoMapper.Map<MemberBasic>(x))
|
2020-06-18 21:03:11 +02:00
|
|
|
};
|
|
|
|
|
return pagedResult;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns a display node with a list view to render members
|
|
|
|
|
/// </summary>
|
2020-12-05 23:44:50 +00:00
|
|
|
/// <param name="listName">The member type to list</param>
|
|
|
|
|
/// <returns>The member list for display</returns>
|
2020-06-18 21:03:11 +02:00
|
|
|
public MemberListDisplay GetListNodeDisplay(string listName)
|
|
|
|
|
{
|
2020-12-05 23:44:50 +00:00
|
|
|
IMemberType foundType = _memberTypeService.Get(listName);
|
2020-06-18 21:03:11 +02:00
|
|
|
var name = foundType != null ? foundType.Name : listName;
|
|
|
|
|
|
2020-11-30 00:45:38 +00:00
|
|
|
var apps = new List<ContentApp>
|
|
|
|
|
{
|
|
|
|
|
ListViewContentAppFactory.CreateContentApp(
|
|
|
|
|
_dataTypeService,
|
|
|
|
|
_propertyEditors,
|
|
|
|
|
listName,
|
|
|
|
|
Constants.Security.DefaultMemberTypeAlias.ToLower(),
|
|
|
|
|
Constants.DataTypes.DefaultMembersListView)
|
|
|
|
|
};
|
2020-06-18 21:03:11 +02:00
|
|
|
apps[0].Active = true;
|
|
|
|
|
|
|
|
|
|
var display = new MemberListDisplay
|
|
|
|
|
{
|
|
|
|
|
ContentTypeAlias = listName,
|
|
|
|
|
ContentTypeName = name,
|
|
|
|
|
Id = listName,
|
|
|
|
|
IsContainer = true,
|
|
|
|
|
Name = listName == Constants.Conventions.MemberTypes.AllMembersListId ? "All Members" : name,
|
|
|
|
|
Path = "-1," + listName,
|
|
|
|
|
ParentId = -1,
|
|
|
|
|
ContentApps = apps
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return display;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the content json for the member
|
|
|
|
|
/// </summary>
|
2020-12-05 23:44:50 +00:00
|
|
|
/// <param name="key">The Guid key of the member</param>
|
|
|
|
|
/// <returns>The member for display</returns>
|
2020-12-02 14:21:05 +00:00
|
|
|
[OutgoingEditorModelEvent]
|
2020-06-18 21:03:11 +02:00
|
|
|
public MemberDisplay GetByKey(Guid key)
|
|
|
|
|
{
|
2020-11-30 00:45:38 +00:00
|
|
|
IMember foundMember = _memberService.GetByKey(key);
|
2020-06-18 21:03:11 +02:00
|
|
|
if (foundMember == null)
|
|
|
|
|
{
|
|
|
|
|
HandleContentNotFound(key);
|
|
|
|
|
}
|
2020-12-05 23:44:50 +00:00
|
|
|
|
2020-06-18 21:03:11 +02:00
|
|
|
return _umbracoMapper.Map<MemberDisplay>(foundMember);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets an empty content item for the
|
|
|
|
|
/// </summary>
|
2020-12-05 23:44:50 +00:00
|
|
|
/// <param name="contentTypeAlias">The content type</param>
|
|
|
|
|
/// <returns>The empty member for display</returns>
|
2020-12-02 14:21:05 +00:00
|
|
|
[OutgoingEditorModelEvent]
|
2020-12-22 16:36:07 +01:00
|
|
|
public ActionResult<MemberDisplay> GetEmpty(string contentTypeAlias = null)
|
2020-06-18 21:03:11 +02:00
|
|
|
{
|
|
|
|
|
if (contentTypeAlias == null)
|
|
|
|
|
{
|
2021-01-12 14:00:14 +01:00
|
|
|
return NotFound();
|
2020-06-18 21:03:11 +02:00
|
|
|
}
|
|
|
|
|
|
2020-11-30 00:45:38 +00:00
|
|
|
IMemberType contentType = _memberTypeService.Get(contentTypeAlias);
|
2020-06-18 21:03:11 +02:00
|
|
|
if (contentType == null)
|
|
|
|
|
{
|
2021-01-12 14:00:14 +01:00
|
|
|
return NotFound();
|
2020-06-18 21:03:11 +02:00
|
|
|
}
|
|
|
|
|
|
2020-11-30 00:45:38 +00:00
|
|
|
string newPassword = _memberManager.GeneratePassword();
|
2020-12-03 14:59:57 +00:00
|
|
|
|
|
|
|
|
IMember emptyContent = new Member(contentType);
|
2020-11-30 00:45:38 +00:00
|
|
|
emptyContent.AdditionalData["NewPassword"] = newPassword;
|
2020-06-18 21:03:11 +02:00
|
|
|
return _umbracoMapper.Map<MemberDisplay>(emptyContent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Saves member
|
|
|
|
|
/// </summary>
|
2020-12-05 23:44:50 +00:00
|
|
|
/// <param name="contentItem">The content item to save as a member</param>
|
|
|
|
|
/// <returns>The resulting member display object</returns>
|
2020-06-18 21:03:11 +02:00
|
|
|
[FileUploadCleanupFilter]
|
2020-12-02 14:21:05 +00:00
|
|
|
[OutgoingEditorModelEvent]
|
2020-06-18 21:03:11 +02:00
|
|
|
[MemberSaveValidation]
|
2020-12-05 23:44:50 +00:00
|
|
|
public async Task<ActionResult<MemberDisplay>> PostSave([ModelBinder(typeof(MemberBinder))] MemberSave contentItem)
|
2020-06-18 21:03:11 +02:00
|
|
|
{
|
2020-12-11 14:32:13 +00:00
|
|
|
if (contentItem == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("The member content item was null");
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-05 23:44:50 +00:00
|
|
|
// If we've reached here it means:
|
2020-06-18 21:03:11 +02:00
|
|
|
// * Our model has been bound
|
|
|
|
|
// * and validated
|
|
|
|
|
// * any file attachments have been saved to their temporary location for us to use
|
|
|
|
|
// * we have a reference to the DTO object and the persisted object
|
|
|
|
|
// * Permissions are valid
|
|
|
|
|
|
2020-12-05 23:44:50 +00:00
|
|
|
// map the properties to the persisted entity
|
2020-06-18 21:03:11 +02:00
|
|
|
MapPropertyValues(contentItem);
|
|
|
|
|
|
2020-12-09 18:36:39 +00:00
|
|
|
await ValidateMemberDataAsync(contentItem);
|
2020-06-18 21:03:11 +02:00
|
|
|
|
2020-12-05 23:44:50 +00:00
|
|
|
// Unlike content/media - if there are errors for a member, we do NOT proceed to save them, we cannot so return the errors
|
2020-06-18 21:03:11 +02:00
|
|
|
if (ModelState.IsValid == false)
|
|
|
|
|
{
|
2020-12-05 23:44:50 +00:00
|
|
|
MemberDisplay forDisplay = _umbracoMapper.Map<MemberDisplay>(contentItem.PersistedContent);
|
2020-06-18 21:03:11 +02:00
|
|
|
forDisplay.Errors = ModelState.ToErrorDictionary();
|
2020-12-22 16:36:07 +01:00
|
|
|
return new ValidationErrorResult(forDisplay);
|
2020-06-18 21:03:11 +02:00
|
|
|
}
|
|
|
|
|
|
2020-12-05 23:44:50 +00:00
|
|
|
// Depending on the action we need to first do a create or update using the membership manager
|
|
|
|
|
// this ensures that passwords are formatted correctly and also performs the validation on the provider itself.
|
2020-12-09 18:36:39 +00:00
|
|
|
|
2020-06-18 21:03:11 +02:00
|
|
|
switch (contentItem.Action)
|
|
|
|
|
{
|
|
|
|
|
case ContentSaveAction.Save:
|
2021-01-15 16:51:23 +00:00
|
|
|
Task<ActionResult<bool>> updateSuccessful = UpdateMemberAsync(contentItem);
|
2020-06-18 21:03:11 +02:00
|
|
|
break;
|
|
|
|
|
case ContentSaveAction.SaveNew:
|
2021-01-15 16:51:23 +00:00
|
|
|
Task<ActionResult<bool>> createSuccessful = CreateMemberAsync(contentItem);
|
2020-06-18 21:03:11 +02:00
|
|
|
break;
|
|
|
|
|
default:
|
2020-12-05 23:44:50 +00:00
|
|
|
// we don't support anything else for members
|
2021-01-12 14:00:14 +01:00
|
|
|
return NotFound();
|
2020-06-18 21:03:11 +02:00
|
|
|
}
|
|
|
|
|
|
2021-01-26 11:58:02 +00:00
|
|
|
// TODO: There's 3 things saved here and we should do this all in one transaction, which we can do here by wrapping in a scope
|
2020-06-18 21:03:11 +02:00
|
|
|
// but it would be nicer to have this taken care of within the Save method itself
|
|
|
|
|
|
2020-12-05 23:44:50 +00:00
|
|
|
// return the updated model
|
|
|
|
|
MemberDisplay display = _umbracoMapper.Map<MemberDisplay>(contentItem.PersistedContent);
|
2020-06-18 21:03:11 +02:00
|
|
|
|
2020-12-05 23:44:50 +00:00
|
|
|
// lastly, if it is not valid, add the model state to the outgoing object and throw a 403
|
2021-01-13 16:14:20 +01:00
|
|
|
if (!ModelState.IsValid)
|
|
|
|
|
{
|
|
|
|
|
display.Errors = ModelState.ToErrorDictionary();
|
|
|
|
|
return new ValidationErrorResult(display, StatusCodes.Status403Forbidden);
|
|
|
|
|
}
|
2020-06-18 21:03:11 +02:00
|
|
|
|
2020-12-05 23:44:50 +00:00
|
|
|
ILocalizedTextService localizedTextService = _localizedTextService;
|
|
|
|
|
|
|
|
|
|
// put the correct messages in
|
2020-06-18 21:03:11 +02:00
|
|
|
switch (contentItem.Action)
|
|
|
|
|
{
|
|
|
|
|
case ContentSaveAction.Save:
|
|
|
|
|
case ContentSaveAction.SaveNew:
|
2020-11-30 00:45:38 +00:00
|
|
|
display.AddSuccessNotification(
|
|
|
|
|
localizedTextService.Localize("speechBubbles/editMemberSaved"),
|
|
|
|
|
localizedTextService.Localize("speechBubbles/editMemberSaved"));
|
2020-06-18 21:03:11 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return display;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-03 01:27:54 +00:00
|
|
|
/// <summary>
|
2020-12-03 14:59:57 +00:00
|
|
|
/// Maps the property values to the persisted entity
|
|
|
|
|
/// </summary>
|
2020-12-05 23:44:50 +00:00
|
|
|
/// <param name="contentItem">The member content item to map properties from</param>
|
2020-12-03 14:59:57 +00:00
|
|
|
private void MapPropertyValues(MemberSave contentItem)
|
|
|
|
|
{
|
2020-12-05 23:44:50 +00:00
|
|
|
// Don't update the name if it is empty
|
2020-12-03 14:59:57 +00:00
|
|
|
if (contentItem.Name.IsNullOrWhiteSpace() == false)
|
|
|
|
|
{
|
|
|
|
|
contentItem.PersistedContent.Name = contentItem.Name;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-05 23:44:50 +00:00
|
|
|
// map the custom properties - this will already be set for new entities in our member binder
|
2020-12-03 14:59:57 +00:00
|
|
|
contentItem.PersistedContent.Email = contentItem.Email;
|
|
|
|
|
contentItem.PersistedContent.Username = contentItem.Username;
|
|
|
|
|
|
2020-12-05 23:44:50 +00:00
|
|
|
// use the base method to map the rest of the properties
|
|
|
|
|
MapPropertyValuesForPersistence<IMember, MemberSave>(
|
2020-12-03 14:59:57 +00:00
|
|
|
contentItem,
|
|
|
|
|
contentItem.PropertyCollectionDto,
|
2020-12-05 23:44:50 +00:00
|
|
|
(save, property) => property.GetValue(), // get prop val
|
|
|
|
|
(save, property, v) => property.SetValue(v), // set prop val
|
2020-12-03 14:59:57 +00:00
|
|
|
null); // member are all invariant
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Create a member from the supplied member content data
|
2020-12-05 23:44:50 +00:00
|
|
|
///
|
|
|
|
|
/// All member password processing and creation is done via the identity manager
|
2020-12-03 01:27:54 +00:00
|
|
|
/// </summary>
|
2020-12-05 23:44:50 +00:00
|
|
|
/// <param name="contentItem">Member content data</param>
|
|
|
|
|
/// <returns>The identity result of the created member</returns>
|
2021-01-15 16:51:23 +00:00
|
|
|
private async Task<ActionResult<bool>> CreateMemberAsync(MemberSave contentItem)
|
2020-12-03 01:27:54 +00:00
|
|
|
{
|
2020-12-08 01:57:14 +00:00
|
|
|
IMemberType memberType = _memberTypeService.Get(contentItem.ContentTypeAlias);
|
|
|
|
|
if (memberType == null)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException($"No member type found with alias {contentItem.ContentTypeAlias}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var identityMember = MembersIdentityUser.CreateNew(
|
|
|
|
|
contentItem.Username,
|
|
|
|
|
contentItem.Email,
|
|
|
|
|
memberType.Alias,
|
|
|
|
|
contentItem.Name);
|
2020-12-08 17:27:22 +00:00
|
|
|
|
2020-12-03 01:27:54 +00:00
|
|
|
IdentityResult created = await _memberManager.CreateAsync(identityMember, contentItem.Password.NewPassword);
|
2020-12-08 01:57:14 +00:00
|
|
|
|
2020-12-03 01:27:54 +00:00
|
|
|
if (created.Succeeded == false)
|
|
|
|
|
{
|
2021-01-15 16:51:23 +00:00
|
|
|
return new ValidationErrorResult(created.Errors.ToErrorMessage());
|
2020-12-03 01:27:54 +00:00
|
|
|
}
|
|
|
|
|
|
2021-02-03 17:33:48 +00:00
|
|
|
// now re-look up the member, which will now exist
|
2020-12-03 01:27:54 +00:00
|
|
|
IMember member = _memberService.GetByEmail(contentItem.Email);
|
|
|
|
|
|
2021-02-03 17:33:48 +00:00
|
|
|
// map the save info over onto the user
|
|
|
|
|
member = _umbracoMapper.Map<MemberSave, IMember>(contentItem, member);
|
|
|
|
|
|
2021-01-28 11:35:02 +00:00
|
|
|
int creatorId = _backOfficeSecurityAccessor.BackOfficeSecurity.CurrentUser.Id;
|
2020-12-09 18:36:39 +00:00
|
|
|
member.CreatorId = creatorId;
|
2020-12-03 01:27:54 +00:00
|
|
|
|
2021-02-03 17:33:48 +00:00
|
|
|
// assign the mapped property values that are not part of the identity properties
|
|
|
|
|
string[] builtInAliases = ConventionsHelper.GetStandardPropertyTypeStubs(_shortStringHelper).Select(x => x.Key).ToArray();
|
|
|
|
|
foreach (ContentPropertyBasic property in contentItem.Properties)
|
|
|
|
|
{
|
|
|
|
|
if (builtInAliases.Contains(property.Alias) == false)
|
|
|
|
|
{
|
|
|
|
|
member.Properties[property.Alias].SetValue(property.Value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//TODO: do we need to resave the key?
|
|
|
|
|
//contentItem.PersistedContent.Key = contentItem.Key;
|
2020-12-09 18:36:39 +00:00
|
|
|
|
|
|
|
|
// now the member has been saved via identity, resave the member with mapped content properties
|
|
|
|
|
_memberService.Save(member);
|
2020-12-03 01:27:54 +00:00
|
|
|
contentItem.PersistedContent = member;
|
2020-12-09 18:36:39 +00:00
|
|
|
|
2021-02-01 18:13:23 +00:00
|
|
|
await AddOrUpdateRoles(contentItem, identityMember);
|
2021-01-15 16:51:23 +00:00
|
|
|
return true;
|
2020-12-03 01:27:54 +00:00
|
|
|
}
|
|
|
|
|
|
2020-12-08 01:57:14 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Update the member security data
|
|
|
|
|
/// If the password has been reset then this method will return the reset/generated password, otherwise will return null.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="contentItem">The member to save</param>
|
2021-01-15 16:51:23 +00:00
|
|
|
private async Task<ActionResult<bool>> UpdateMemberAsync(MemberSave contentItem)
|
2020-06-18 21:03:11 +02:00
|
|
|
{
|
2020-12-08 01:57:14 +00:00
|
|
|
contentItem.PersistedContent.WriterId = _backOfficeSecurityAccessor.BackOfficeSecurity.CurrentUser.Id;
|
2020-06-18 21:03:11 +02:00
|
|
|
|
|
|
|
|
// If the user doesn't have access to sensitive values, then we need to check if any of the built in member property types
|
|
|
|
|
// have been marked as sensitive. If that is the case we cannot change these persisted values no matter what value has been posted.
|
|
|
|
|
// There's only 3 special ones we need to deal with that are part of the MemberSave instance: Comments, IsApproved, IsLockedOut
|
|
|
|
|
// but we will take care of this in a generic way below so that it works for all props.
|
2020-12-05 23:44:50 +00:00
|
|
|
if (!_backOfficeSecurityAccessor.BackOfficeSecurity.CurrentUser.HasAccessToSensitiveData())
|
2020-06-18 21:03:11 +02:00
|
|
|
{
|
2020-12-08 01:57:14 +00:00
|
|
|
IMemberType memberType = _memberTypeService.Get(contentItem.PersistedContent.ContentTypeId);
|
2020-06-18 21:03:11 +02:00
|
|
|
var sensitiveProperties = memberType
|
|
|
|
|
.PropertyTypes.Where(x => memberType.IsSensitiveProperty(x.Alias))
|
|
|
|
|
.ToList();
|
|
|
|
|
|
2020-12-05 23:44:50 +00:00
|
|
|
foreach (IPropertyType sensitiveProperty in sensitiveProperties)
|
2020-06-18 21:03:11 +02:00
|
|
|
{
|
2020-12-08 01:57:14 +00:00
|
|
|
ContentPropertyBasic destProp = contentItem.Properties.FirstOrDefault(x => x.Alias == sensitiveProperty.Alias);
|
2020-06-18 21:03:11 +02:00
|
|
|
if (destProp != null)
|
|
|
|
|
{
|
2020-12-05 23:44:50 +00:00
|
|
|
// if found, change the value of the contentItem model to the persisted value so it remains unchanged
|
2020-12-08 01:57:14 +00:00
|
|
|
object origValue = contentItem.PersistedContent.GetValue(sensitiveProperty.Alias);
|
2020-06-18 21:03:11 +02:00
|
|
|
destProp.Value = origValue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-08 01:57:14 +00:00
|
|
|
bool isLockedOut = contentItem.IsLockedOut;
|
2020-06-18 21:03:11 +02:00
|
|
|
|
2020-12-05 23:44:50 +00:00
|
|
|
// if they were locked but now they are trying to be unlocked
|
2020-12-08 01:57:14 +00:00
|
|
|
if (contentItem.PersistedContent.IsLockedOut && isLockedOut == false)
|
2020-06-18 21:03:11 +02:00
|
|
|
{
|
2020-12-08 01:57:14 +00:00
|
|
|
contentItem.PersistedContent.IsLockedOut = false;
|
|
|
|
|
contentItem.PersistedContent.FailedPasswordAttempts = 0;
|
2020-06-18 21:03:11 +02:00
|
|
|
}
|
2020-12-08 01:57:14 +00:00
|
|
|
else if (!contentItem.PersistedContent.IsLockedOut && isLockedOut)
|
2020-06-18 21:03:11 +02:00
|
|
|
{
|
2020-12-05 23:44:50 +00:00
|
|
|
// NOTE: This should not ever happen unless someone is mucking around with the request data.
|
|
|
|
|
// An admin cannot simply lock a user, they get locked out by password attempts, but an admin can un-approve them
|
2020-06-18 21:03:11 +02:00
|
|
|
ModelState.AddModelError("custom", "An admin cannot lock a user");
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-09 18:36:39 +00:00
|
|
|
MembersIdentityUser identityMember = await _memberManager.FindByIdAsync(contentItem.Id.ToString());
|
|
|
|
|
if (identityMember == null)
|
|
|
|
|
{
|
2021-01-15 16:51:23 +00:00
|
|
|
return new ValidationErrorResult("Member was not found");
|
2020-12-09 18:36:39 +00:00
|
|
|
}
|
|
|
|
|
|
2020-12-08 01:57:14 +00:00
|
|
|
if (contentItem.Password != null)
|
|
|
|
|
{
|
2020-12-09 18:36:39 +00:00
|
|
|
IdentityResult validatePassword = await _memberManager.ValidatePasswordAsync(contentItem.Password.NewPassword);
|
|
|
|
|
if (validatePassword.Succeeded == false)
|
|
|
|
|
{
|
2021-01-15 16:51:23 +00:00
|
|
|
return new ValidationErrorResult(validatePassword.Errors.ToErrorMessage());
|
2020-12-09 18:36:39 +00:00
|
|
|
}
|
|
|
|
|
|
2021-01-29 13:01:59 +00:00
|
|
|
string newPassword = _memberManager.HashPassword(contentItem.Password.NewPassword);
|
|
|
|
|
identityMember.PasswordHash = newPassword;
|
2021-01-29 15:03:06 +00:00
|
|
|
contentItem.PersistedContent.RawPasswordValue = identityMember.PasswordHash;
|
|
|
|
|
if (identityMember.LastPasswordChangeDateUtc != null)
|
|
|
|
|
{
|
|
|
|
|
contentItem.PersistedContent.LastPasswordChangeDate = DateTime.UtcNow;
|
|
|
|
|
identityMember.LastPasswordChangeDateUtc = contentItem.PersistedContent.LastPasswordChangeDate;
|
|
|
|
|
}
|
2020-12-08 01:57:14 +00:00
|
|
|
}
|
2020-12-09 18:36:39 +00:00
|
|
|
|
|
|
|
|
IdentityResult updatedResult = await _memberManager.UpdateAsync(identityMember);
|
|
|
|
|
|
|
|
|
|
if (updatedResult.Succeeded == false)
|
|
|
|
|
{
|
2021-01-15 16:51:23 +00:00
|
|
|
return new ValidationErrorResult(updatedResult.Errors.ToErrorMessage());
|
2020-12-09 18:36:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_memberService.Save(contentItem.PersistedContent);
|
|
|
|
|
|
2021-02-01 18:13:23 +00:00
|
|
|
await AddOrUpdateRoles(contentItem, identityMember);
|
2021-01-15 16:51:23 +00:00
|
|
|
return true;
|
2020-12-08 01:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
2020-12-09 18:36:39 +00:00
|
|
|
private async Task<bool> ValidateMemberDataAsync(MemberSave contentItem)
|
2020-12-08 01:57:14 +00:00
|
|
|
{
|
|
|
|
|
if (contentItem.Name.IsNullOrWhiteSpace())
|
|
|
|
|
{
|
|
|
|
|
ModelState.AddPropertyError(
|
|
|
|
|
new ValidationResult("Invalid user name", new[] { "value" }),
|
|
|
|
|
$"{Constants.PropertyEditors.InternalGenericPropertiesPrefix}login");
|
2020-12-09 18:36:39 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (contentItem.Password != null && !contentItem.Password.NewPassword.IsNullOrWhiteSpace())
|
|
|
|
|
{
|
|
|
|
|
IdentityResult validPassword = await _memberManager.ValidatePasswordAsync(contentItem.Password.NewPassword);
|
|
|
|
|
if (!validPassword.Succeeded)
|
|
|
|
|
{
|
|
|
|
|
ModelState.AddPropertyError(
|
|
|
|
|
new ValidationResult("Invalid password: " + MapErrors(validPassword.Errors), new[] { "value" }),
|
|
|
|
|
$"{Constants.PropertyEditors.InternalGenericPropertiesPrefix}password");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2021-01-29 16:43:50 +00:00
|
|
|
}
|
2020-12-08 01:57:14 +00:00
|
|
|
|
|
|
|
|
IMember byUsername = _memberService.GetByUsername(contentItem.Username);
|
|
|
|
|
if (byUsername != null && byUsername.Key != contentItem.Key)
|
2020-12-05 23:44:50 +00:00
|
|
|
{
|
2020-12-08 01:57:14 +00:00
|
|
|
ModelState.AddPropertyError(
|
|
|
|
|
new ValidationResult("Username is already in use", new[] { "value" }),
|
|
|
|
|
$"{Constants.PropertyEditors.InternalGenericPropertiesPrefix}login");
|
2020-12-09 18:36:39 +00:00
|
|
|
return false;
|
2020-12-05 23:44:50 +00:00
|
|
|
}
|
2020-12-08 01:57:14 +00:00
|
|
|
|
|
|
|
|
IMember byEmail = _memberService.GetByEmail(contentItem.Email);
|
|
|
|
|
if (byEmail != null && byEmail.Key != contentItem.Key)
|
|
|
|
|
{
|
|
|
|
|
ModelState.AddPropertyError(
|
|
|
|
|
new ValidationResult("Email address is already in use", new[] { "value" }),
|
|
|
|
|
$"{Constants.PropertyEditors.InternalGenericPropertiesPrefix}email");
|
2020-12-09 18:36:39 +00:00
|
|
|
return false;
|
2020-12-08 01:57:14 +00:00
|
|
|
}
|
2020-12-09 18:36:39 +00:00
|
|
|
|
|
|
|
|
return true;
|
2020-12-08 01:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
2020-12-09 18:36:39 +00:00
|
|
|
private string MapErrors(IEnumerable<IdentityError> result)
|
2020-12-08 01:57:14 +00:00
|
|
|
{
|
|
|
|
|
var sb = new StringBuilder();
|
2020-12-09 18:36:39 +00:00
|
|
|
IEnumerable<IdentityError> identityErrors = result.ToList();
|
|
|
|
|
foreach (IdentityError error in identityErrors)
|
2020-12-08 01:57:14 +00:00
|
|
|
{
|
2020-12-09 18:36:39 +00:00
|
|
|
string errorString = $"{error.Description}";
|
|
|
|
|
sb.AppendLine(errorString);
|
2020-12-08 01:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sb.ToString();
|
2020-06-18 21:03:11 +02:00
|
|
|
}
|
|
|
|
|
|
2020-12-09 18:36:39 +00:00
|
|
|
/// <summary>
|
2021-01-29 16:43:50 +00:00
|
|
|
/// Add or update the identity roles
|
2020-12-09 18:36:39 +00:00
|
|
|
/// </summary>
|
2021-01-29 16:43:50 +00:00
|
|
|
/// <param name="contentItem">The member content item</param>
|
|
|
|
|
/// <param name="identityMember">The member as an identity user</param>
|
|
|
|
|
private async Task AddOrUpdateRoles(MemberSave contentItem, MembersIdentityUser identityMember)
|
2020-12-09 18:36:39 +00:00
|
|
|
{
|
|
|
|
|
// We're gonna look up the current roles now because the below code can cause
|
|
|
|
|
// events to be raised and developers could be manually adding roles to members in
|
|
|
|
|
// their handlers. If we don't look this up now there's a chance we'll just end up
|
|
|
|
|
// removing the roles they've assigned.
|
2021-01-29 16:43:50 +00:00
|
|
|
IEnumerable<string> currentRoles = await _memberManager.GetRolesAsync(identityMember);
|
2020-12-09 18:36:39 +00:00
|
|
|
|
|
|
|
|
// find the ones to remove and remove them
|
|
|
|
|
IEnumerable<string> roles = currentRoles.ToList();
|
|
|
|
|
string[] rolesToRemove = roles.Except(contentItem.Groups).ToArray();
|
|
|
|
|
|
|
|
|
|
// Now let's do the role provider stuff - now that we've saved the content item (that is important since
|
|
|
|
|
// if we are changing the username, it must be persisted before looking up the member roles).
|
|
|
|
|
if (rolesToRemove.Any())
|
|
|
|
|
{
|
2021-02-01 18:13:23 +00:00
|
|
|
IdentityResult rolesIdentityResult = await _memberManager.RemoveFromRolesAsync(identityMember, rolesToRemove);
|
2020-12-09 18:36:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// find the ones to add and add them
|
|
|
|
|
string[] toAdd = contentItem.Groups.Except(roles).ToArray();
|
|
|
|
|
if (toAdd.Any())
|
|
|
|
|
{
|
|
|
|
|
// add the ones submitted
|
2021-01-29 16:43:50 +00:00
|
|
|
IdentityResult identityResult = await _memberManager.AddToRolesAsync(identityMember, toAdd);
|
2020-12-09 18:36:39 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-18 21:03:11 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Permanently deletes a member
|
|
|
|
|
/// </summary>
|
2020-12-05 23:44:50 +00:00
|
|
|
/// <param name="key">Guid of the member to delete</param>
|
|
|
|
|
/// <returns>The result of the deletion</returns>
|
2020-06-18 21:03:11 +02:00
|
|
|
///
|
|
|
|
|
[HttpPost]
|
|
|
|
|
public IActionResult DeleteByKey(Guid key)
|
|
|
|
|
{
|
2021-01-29 16:43:50 +00:00
|
|
|
//TODO: move to MembersUserStore
|
2020-12-05 23:44:50 +00:00
|
|
|
IMember foundMember = _memberService.GetByKey(key);
|
2020-06-18 21:03:11 +02:00
|
|
|
if (foundMember == null)
|
|
|
|
|
{
|
2021-01-14 19:41:32 +01:00
|
|
|
return HandleContentNotFound(key);
|
2020-06-18 21:03:11 +02:00
|
|
|
}
|
2020-12-05 23:44:50 +00:00
|
|
|
|
2020-06-18 21:03:11 +02:00
|
|
|
_memberService.Delete(foundMember);
|
|
|
|
|
|
|
|
|
|
return Ok();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Exports member data based on their unique Id
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="key">The unique <see cref="Guid">member identifier</see></param>
|
|
|
|
|
/// <returns><see cref="HttpResponseMessage"/></returns>
|
|
|
|
|
[HttpGet]
|
|
|
|
|
public IActionResult ExportMemberData(Guid key)
|
|
|
|
|
{
|
2020-12-05 23:44:50 +00:00
|
|
|
IUser currentUser = _backOfficeSecurityAccessor.BackOfficeSecurity.CurrentUser;
|
2020-06-18 21:03:11 +02:00
|
|
|
|
|
|
|
|
if (currentUser.HasAccessToSensitiveData() == false)
|
|
|
|
|
{
|
|
|
|
|
return Forbid();
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-05 23:44:50 +00:00
|
|
|
MemberExportModel member = ((MemberService)_memberService).ExportMember(key);
|
|
|
|
|
if (member is null)
|
|
|
|
|
{
|
|
|
|
|
throw new NullReferenceException("No member found with key " + key);
|
|
|
|
|
}
|
2020-06-18 21:03:11 +02:00
|
|
|
|
|
|
|
|
var json = _jsonSerializer.Serialize(member);
|
|
|
|
|
|
|
|
|
|
var fileName = $"{member.Name}_{member.Email}.txt";
|
2020-12-05 23:44:50 +00:00
|
|
|
|
2020-06-18 21:03:11 +02:00
|
|
|
// Set custom header so umbRequestHelper.downloadFile can save the correct filename
|
|
|
|
|
HttpContext.Response.Headers.Add("x-filename", fileName);
|
|
|
|
|
|
2020-11-30 00:45:38 +00:00
|
|
|
return File(Encoding.UTF8.GetBytes(json), MediaTypeNames.Application.Octet, fileName);
|
2020-06-18 21:03:11 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|