From e757868d0936dd2fc21f17a7d7de373766e7da2b Mon Sep 17 00:00:00 2001 From: Shannon Date: Tue, 13 Jun 2017 18:47:20 +0200 Subject: [PATCH] Moves logic back to user service for inviting a user since its much simpler. --- .../Editors/AuthenticationController.cs | 18 ++++--- src/Umbraco.Web/Editors/UsersController.cs | 49 ++++--------------- 2 files changed, 21 insertions(+), 46 deletions(-) diff --git a/src/Umbraco.Web/Editors/AuthenticationController.cs b/src/Umbraco.Web/Editors/AuthenticationController.cs index 64be61d862..ab5f973724 100644 --- a/src/Umbraco.Web/Editors/AuthenticationController.cs +++ b/src/Umbraco.Web/Editors/AuthenticationController.cs @@ -69,18 +69,24 @@ namespace Umbraco.Web.Editors if (decoded.IsNullOrWhiteSpace()) throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound)); - var user = await UserManager.FindByIdAsync(id); - if (user == null) + var identityUser = await UserManager.FindByIdAsync(id); + if (identityUser == null) throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound)); var result = await UserManager.ConfirmEmailAsync(id, decoded); if (result.Succeeded == false) - { + { 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(user); } diff --git a/src/Umbraco.Web/Editors/UsersController.cs b/src/Umbraco.Web/Editors/UsersController.cs index 1114a068c8..f76066da34 100644 --- a/src/Umbraco.Web/Editors/UsersController.cs +++ b/src/Umbraco.Web/Editors/UsersController.cs @@ -253,8 +253,6 @@ namespace Umbraco.Web.Editors /// public async Task PostInviteUser(UserInvite userSave) { - //TODO: Allow re-inviting the user! - if (userSave == null) throw new ArgumentNullException("userSave"); if (ModelState.IsValid == false) @@ -269,54 +267,25 @@ namespace Umbraco.Web.Editors Request.CreateNotificationValidationErrorResponse("No Email server is configured")); } - var identityUser = await UserManager.FindByEmailAsync(userSave.Email); - if (identityUser != null && identityUser.LastLoginDateUtc.HasValue && identityUser.LastLoginDateUtc.Value == default(DateTime)) + var user = Services.UserService.GetByEmail(userSave.Email); + if (user != null && (user.LastLoginDate != default(DateTime) || user.EmailConfirmedDate.HasValue)) { ModelState.AddModelError("Email", "A user with the email already exists"); throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } - if (identityUser == null) - { - identityUser = new BackOfficeIdentityUser - { - Email = userSave.Email, - Name = userSave.Name, - UserName = userSave.Email - }; + //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(userSave) : Mapper.Map(userSave, user); - //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))); - } - } + //Save the user first - //Add/Update to roles - var roleResult = await UserManager.AddToRolesAsync(identityUser.Id, userSave.UserGroups.ToArray()); - if (roleResult.Succeeded == false) - { - throw new HttpResponseException( - Request.CreateNotificationValidationErrorResponse(string.Join(", ", roleResult.Errors))); - } + Services.UserService.Save(user); //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}", ApplicationContext.UmbracoApplicationUrl, - identityUser.Id, + user.Id, WebUtility.UrlEncode("|"), token.ToUrlBase64()); @@ -327,7 +296,7 @@ namespace Umbraco.Web.Editors Subject = "You have been invited to the Umbraco Back Office!" }); - return Mapper.Map(identityUser); + return Mapper.Map(user); } ///