Moves logic back to user service for inviting a user since its much simpler.
This commit is contained in:
@@ -69,18 +69,24 @@ namespace Umbraco.Web.Editors
|
|||||||
if (decoded.IsNullOrWhiteSpace())
|
if (decoded.IsNullOrWhiteSpace())
|
||||||
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
|
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
|
||||||
|
|
||||||
var user = await UserManager.FindByIdAsync(id);
|
var identityUser = await UserManager.FindByIdAsync(id);
|
||||||
if (user == null)
|
if (identityUser == null)
|
||||||
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
|
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
|
||||||
|
|
||||||
var result = await UserManager.ConfirmEmailAsync(id, decoded);
|
var result = await UserManager.ConfirmEmailAsync(id, decoded);
|
||||||
|
|
||||||
if (result.Succeeded == false)
|
if (result.Succeeded == false)
|
||||||
{
|
{
|
||||||
throw new HttpResponseException(Request.CreateNotificationValidationErrorResponse(string.Join(", ", result.Errors)));
|
throw new HttpResponseException(Request.CreateNotificationValidationErrorResponse(string.Join(", ", result.Errors)));
|
||||||
}
|
}
|
||||||
|
|
||||||
await SignInManager.SignInAsync(user, false, false);
|
Request.TryGetOwinContext().Result.Authentication.SignOut(
|
||||||
|
Core.Constants.Security.BackOfficeAuthenticationType,
|
||||||
|
Core.Constants.Security.BackOfficeExternalAuthenticationType);
|
||||||
|
|
||||||
|
await SignInManager.SignInAsync(identityUser, false, false);
|
||||||
|
|
||||||
|
var user = ApplicationContext.Services.UserService.GetUserById(id);
|
||||||
|
|
||||||
return Mapper.Map<UserDisplay>(user);
|
return Mapper.Map<UserDisplay>(user);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -253,8 +253,6 @@ namespace Umbraco.Web.Editors
|
|||||||
/// </remarks>
|
/// </remarks>
|
||||||
public async Task<UserDisplay> PostInviteUser(UserInvite userSave)
|
public async Task<UserDisplay> PostInviteUser(UserInvite userSave)
|
||||||
{
|
{
|
||||||
//TODO: Allow re-inviting the user!
|
|
||||||
|
|
||||||
if (userSave == null) throw new ArgumentNullException("userSave");
|
if (userSave == null) throw new ArgumentNullException("userSave");
|
||||||
|
|
||||||
if (ModelState.IsValid == false)
|
if (ModelState.IsValid == false)
|
||||||
@@ -269,54 +267,25 @@ namespace Umbraco.Web.Editors
|
|||||||
Request.CreateNotificationValidationErrorResponse("No Email server is configured"));
|
Request.CreateNotificationValidationErrorResponse("No Email server is configured"));
|
||||||
}
|
}
|
||||||
|
|
||||||
var identityUser = await UserManager.FindByEmailAsync(userSave.Email);
|
var user = Services.UserService.GetByEmail(userSave.Email);
|
||||||
if (identityUser != null && identityUser.LastLoginDateUtc.HasValue && identityUser.LastLoginDateUtc.Value == default(DateTime))
|
if (user != null && (user.LastLoginDate != default(DateTime) || user.EmailConfirmedDate.HasValue))
|
||||||
{
|
{
|
||||||
ModelState.AddModelError("Email", "A user with the email already exists");
|
ModelState.AddModelError("Email", "A user with the email already exists");
|
||||||
throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
|
throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (identityUser == null)
|
//map to new user or map to existing user (if they've already been invited but didn't accept it)
|
||||||
{
|
user = user == null ? Mapper.Map<IUser>(userSave) : Mapper.Map(userSave, user);
|
||||||
identityUser = new BackOfficeIdentityUser
|
|
||||||
{
|
|
||||||
Email = userSave.Email,
|
|
||||||
Name = userSave.Name,
|
|
||||||
UserName = userSave.Email
|
|
||||||
};
|
|
||||||
|
|
||||||
//Save the user first
|
//Save the user first
|
||||||
var result = await UserManager.CreateAsync(identityUser);
|
|
||||||
if (result.Succeeded == false)
|
|
||||||
{
|
|
||||||
throw new HttpResponseException(
|
|
||||||
Request.CreateNotificationValidationErrorResponse(string.Join(", ", result.Errors)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
identityUser.Name = userSave.Name;
|
|
||||||
var result = await UserManager.UpdateAsync(identityUser);
|
|
||||||
if (result.Succeeded == false)
|
|
||||||
{
|
|
||||||
throw new HttpResponseException(
|
|
||||||
Request.CreateNotificationValidationErrorResponse(string.Join(", ", result.Errors)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//Add/Update to roles
|
Services.UserService.Save(user);
|
||||||
var roleResult = await UserManager.AddToRolesAsync(identityUser.Id, userSave.UserGroups.ToArray());
|
|
||||||
if (roleResult.Succeeded == false)
|
|
||||||
{
|
|
||||||
throw new HttpResponseException(
|
|
||||||
Request.CreateNotificationValidationErrorResponse(string.Join(", ", roleResult.Errors)));
|
|
||||||
}
|
|
||||||
|
|
||||||
//now send the email
|
//now send the email
|
||||||
var token = await UserManager.GenerateEmailConfirmationTokenAsync(identityUser.Id);
|
var token = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
|
||||||
var link = string.Format("{0}#/login/false?invite={1}{2}{3}",
|
var link = string.Format("{0}#/login/false?invite={1}{2}{3}",
|
||||||
ApplicationContext.UmbracoApplicationUrl,
|
ApplicationContext.UmbracoApplicationUrl,
|
||||||
identityUser.Id,
|
user.Id,
|
||||||
WebUtility.UrlEncode("|"),
|
WebUtility.UrlEncode("|"),
|
||||||
token.ToUrlBase64());
|
token.ToUrlBase64());
|
||||||
|
|
||||||
@@ -327,7 +296,7 @@ namespace Umbraco.Web.Editors
|
|||||||
Subject = "You have been invited to the Umbraco Back Office!"
|
Subject = "You have been invited to the Umbraco Back Office!"
|
||||||
});
|
});
|
||||||
|
|
||||||
return Mapper.Map<UserDisplay>(identityUser);
|
return Mapper.Map<UserDisplay>(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user