Fixing up user invite event

This commit is contained in:
Shannon
2020-09-04 15:02:28 +10:00
parent 56a73d0395
commit f2b8f355e1
5 changed files with 73 additions and 35 deletions

View File

@@ -347,25 +347,6 @@ namespace Umbraco.Web.Editors
throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
}
var userMgr = TryGetOwinContext().Result.GetBackOfficeUserManager();
var inviteArgs = new UserInviteEventArgs(
Request.TryGetHttpContext().Result.GetCurrentRequestIpAddress(),
performingUser: Security.GetUserId().Result);
userMgr.RaiseSendingUserInvite(inviteArgs);
if (inviteArgs.InviteHandled)
{
// TODO: now what? We'll need to return a different response to the back office, i don't think we want to create
// a local user since that will be part of the auto-link logic. We should ask lars though since that might be
// a requirement for him so might need to make it flexible.
}
if (EmailSender.CanSendRequiredEmail == false)
{
throw new HttpResponseException(
Request.CreateNotificationValidationErrorResponse("No Email server is configured"));
}
IUser user;
if (Current.Configs.Settings().Security.UsernameIsEmail)
{
@@ -375,9 +356,47 @@ namespace Umbraco.Web.Editors
else
{
//first validate the username if we're showing it
user = CheckUniqueUsername(userSave.Username, u => u.LastLoginDate != default(DateTime) || u.EmailConfirmedDate.HasValue);
user = CheckUniqueUsername(userSave.Username, u => u.LastLoginDate != default || u.EmailConfirmedDate.HasValue);
}
user = CheckUniqueEmail(userSave.Email, u => u.LastLoginDate != default || u.EmailConfirmedDate.HasValue);
var userMgr = TryGetOwinContext().Result.GetBackOfficeUserManager();
var inviteArgs = new UserInviteEventArgs(
Request.TryGetHttpContext().Result.GetCurrentRequestIpAddress(),
performingUser: Security.GetUserId().Result,
userSave);
userMgr.RaiseSendingUserInvite(inviteArgs);
// If the event is handled then return the data
if (inviteArgs.InviteHandled)
{
// if no local user was created then map the args manually for the UI
if (inviteArgs.User == null)
{
return new UserDisplay
{
Name = userSave.Name,
Email = userSave.Email,
Username = userSave.Username
};
}
else
{
//map the save info over onto the user
user = Mapper.Map(userSave, user);
//ensure the invited date is set
user.InvitedDate = DateTime.Now;
//Save the updated user
Services.UserService.Save(user);
return Mapper.Map<UserDisplay>(user);
}
}
if (EmailSender.CanSendRequiredEmail == false)
{
throw new HttpResponseException(
Request.CreateNotificationValidationErrorResponse("No Email server is configured"));
}
user = CheckUniqueEmail(userSave.Email, u => u.LastLoginDate != default(DateTime) || u.EmailConfirmedDate.HasValue);
//Perform authorization here to see if the current user can actually save this user with the info being requested
var authHelper = new UserEditorAuthorizationHelper(Services.ContentService, Services.MediaService, Services.UserService, Services.EntityService);

View File

@@ -5,19 +5,6 @@ using Umbraco.Core.Security;
namespace Umbraco.Web.Security
{
public class UserInviteEventArgs : IdentityAuditEventArgs
{
public UserInviteEventArgs(string ipAddress, int performingUser, string comment = null)
: base(AuditEvent.SendingUserInvite, ipAddress, comment, performingUser)
{
}
/// <summary>
/// If event handler sets this to true it indicates that Umbraco will no try to send the invite itself
/// </summary>
public bool InviteHandled { get; set; }
}
/// <summary>
/// This class is used by events raised from the BackofficeUserManager
/// </summary>

View File

@@ -0,0 +1,30 @@
using Umbraco.Core.Models.Membership;
using Umbraco.Web.Models.ContentEditing;
namespace Umbraco.Web.Security
{
public class UserInviteEventArgs : IdentityAuditEventArgs
{
public UserInviteEventArgs(string ipAddress, int performingUser, UserInvite invitedUser, string comment = null)
: base(AuditEvent.SendingUserInvite, ipAddress, comment, performingUser)
{
InvitedUser = invitedUser ?? throw new System.ArgumentNullException(nameof(invitedUser));
}
public UserInvite InvitedUser { get; }
/// <summary>
/// If event handler sets this to true it indicates that Umbraco will no try to send the invite itself
/// </summary>
public bool InviteHandled { get; set; }
/// <summary>
/// If the event handler has created a local user then this is the result which is used to return the details to the UI
/// </summary>
/// <remarks>
/// It is optional to create a local user in this event. In many cases the custom invite flow will be for external logins and then local users will
/// be created via the auto-linking process.
/// </remarks>
public IUser User { get; set; }
}
}

View File

@@ -261,6 +261,7 @@
<Compile Include="Security\BackOfficeExternalLoginProviderErrors.cs" />
<Compile Include="Security\BackOfficeExternalLoginProviderOptions.cs" />
<Compile Include="Security\SignOutAuditEventArgs.cs" />
<Compile Include="Security\UserInviteEventArgs.cs" />
<Compile Include="Services\DashboardService.cs" />
<Compile Include="Services\IDashboardService.cs" />
<Compile Include="Models\Link.cs" />