Exemplo n.º 1
0
        /// <summary>
        /// Converts UserProfileModel object to WebUser model object
        /// </summary>
        /// <param name="webUser">WebUser object</param>
        /// <param name="model">UserProfileModel object</param>
        public static WebUser FromUserProfile(this WebUser webUser, UserProfileModel model)
        {
            if (model == null)
            {
                return webUser;
            }

            webUser.BirthDate = model.BirthDate;
            webUser.FirstName = model.FirstName;
            webUser.LastName = model.LastName;
            webUser.Mobile = model.Mobile;
            webUser.UserName = model.Email;
            webUser.PersonCode = model.PersonCode;

            return webUser;
        }
 public async Task AccountStrongUserCreateNewUserSuccessCallsAllOperations()
 {
     this.userManagerMock.Setup(mock => mock.GenerateEmailConfirmationTokenAsyncWrap(this.webUser.Id)).Returns(Task.FromResult("123123123"));
     this.userManagerMock.Setup(mock => mock.FindByEmailAsyncWrap(It.IsAny<string>())).Returns(Task.FromResult<WebUser>(null));
     this.userManagerMock.Setup(m => m.CreateAsyncWrap(It.IsAny<string>(), null))
         .Returns(Task.FromResult(new Tuple<IdentityResult, int>(IdentityResult.Success, this.webUser.Id)));
     this.userManagerMock.Setup(m => m.UpdateAsyncWrap(this.webUser.Id, It.IsAny<WebUser>()))
         .Returns(Task.FromResult(IdentityResult.Success));
     this.accountController.UserManager = this.userManagerMock.Object;
     var model = new UserProfileModel { Email = this.webUser.UserName };
     var result = await this.accountController.StrongUserCreate(model);
     result.Should().BeViewResult().WithViewName("~/Features/Account/DisplayEmailConfirmation.cshtml");
     this.userManagerMock.Verify(m => m.CreateAsyncWrap(It.IsAny<string>(), It.IsAny<string>()), Times.Once);
     this.userManagerMock.Verify(m => m.UpdateAsyncWrap(It.IsAny<int>(), It.IsAny<WebUser>()), Times.Once);
     this.userManagerMock.Verify(m => m.GenerateEmailConfirmationTokenAsyncWrap(this.webUser.Id), Times.Once);
 }
 public async Task AccountStrongUserCreateNewUserFailureReturnsToRegister()
 {
     this.userManagerMock.Setup(mock => mock.FindByEmailAsyncWrap(It.IsAny<string>())).Returns(Task.FromResult<WebUser>(null));
     this.userManagerMock.Setup(m => m.CreateAsyncWrap(It.IsAny<string>(), null))
         .Returns(Task.FromResult(new Tuple<IdentityResult, int>(IdentityResult.Failed(new string[] { "kaka" }), 0)));
     this.accountController.UserManager = this.userManagerMock.Object;
     var model = new UserProfileModel { Email = RandomData.GetEmailAddress() };
     var result = await this.accountController.StrongUserCreate(model);
     result.Should().BeViewResult().WithViewName("~/Features/Account/RegistrationForm.cshtml");
     this.userManagerMock.Verify(m => m.CreateAsyncWrap(It.IsAny<string>(), It.IsAny<string>()), Times.Once);
 }
 public async Task AccountStrongUserCreateProhibitEmailReuse()
 {
     var model = new UserProfileModel();
     var result = await this.accountController.StrongUserCreate(model);
     result.Should().BeRedirectToRouteResult().WithAction("login");
     this.userManagerMock.Verify(m => m.CreateAsyncWrap(It.IsAny<string>(), It.IsAny<string>()), Times.Never);
 }
        public virtual async Task<ActionResult> StrongUserCreate(UserProfileModel model)
        {
            if (model == null)
            {
                // TODO: Add master data translations for those
                this.WebMessages.AddErrorMessage(this.T("User profile"), this.T("User profile data was not present in request"));
                return this.RedirectBack();
            }

            if (!this.ModelState.IsValid)
            {
                return this.View(MVC.Account.Views.UserProfileStrongEdit, model);
            }

            var existingUser = await this.UserManager.FindByEmailAsyncWrap(model.Email);
            if (existingUser != null)
            {
                // TODO: Add master data translations for those
                this.WebMessages.AddInfoMessage(this.T("User account"), this.T("User with given e-mail address already exists. Please, signIn with e-mail address and password and then upgrade account."));
                return this.RedirectToAction(MVC.Account.Login());
            }

            var result = await this.UserManager.CreateAsyncWrap(model.Email, null);
            if (result.Item1.Succeeded && result.Item2 != 0)
            {
                WebUser user = new WebUser().FromUserProfile(model);
                user.Id = result.Item2;
                user.IsStronglyAuthenticated = true;
                var updateResult = await this.UserManager.UpdateAsyncWrap(result.Item2, user);
                if (updateResult.Succeeded)
                {
                    // Sign in Vetuma user automatically
                    await this.SignInManager.SignInAsyncWrap(user, true, false);

                    // Create all necessary information to send e-mail verification
                    string callbackUrl = await this.SendEmailVerificationToken(user.Id);
#if !PROD
                    // For testing this will show same link in next page, to ease confirmation procedure
                    ViewBag.Link = callbackUrl;
#endif
                    return this.View(MVC.Account.Views.DisplayEmailConfirmation);
                }

                return this.RedirectBack();
            }

            foreach (var errorMessage in result.Item1.Errors)
            {
                this.WebMessages.AddErrorMessage(errorMessage);
            }

            return this.View(MVC.Account.Views.RegistrationForm, model);
        }
        public virtual async Task<ActionResult> UserProfileFullSave(UserProfileModel model)
        {
            WebUser oldUserData = await this.UserManager.FindByIdAsyncWrap(int.Parse(User.Identity.GetUserId()));
            if (!ModelState.IsValid)
            {
                if (Request.UrlReferrer.LocalPath == "/account/userprofilefulledit")
                {
                    return this.View(MVC.Account.Views.UserProfileFullEdit, model);
                }

                if (oldUserData.IsStronglyAuthenticated)
                {
                    return this.View(MVC.Account.Views.UserProfileStrong, model);
                }

                return this.View(MVC.Account.Views.UserProfile, model);
            }

            bool isUserNameChanged = oldUserData.UserName != model.Email;
            WebUser userData = oldUserData.FromUserProfile(model);
            var result = await this.UserManager.UpdateAsyncWrap(int.Parse(User.Identity.GetUserId()), userData);
            IdentityResult emailResetResult = new IdentityResult();
            if (isUserNameChanged)
            {
                emailResetResult = await this.UserManager.SetEmailAsyncWrap(int.Parse(User.Identity.GetUserId()), model.Email);
            }

            if (result.Succeeded)
            {
                var user = await this.UserManager.FindByIdAsyncWrap(int.Parse(User.Identity.GetUserId()));
                if (user != null)
                {
                    await this.SignInManager.RelogonUserAsyncWrap(user, false);

                    // Check whether e-mail confirmation is required
                    if (isUserNameChanged && emailResetResult.Succeeded)
                    {
                        // Create all necessary information to send e-mail verification
                        string callbackUrl = await this.SendEmailVerificationToken(int.Parse(User.Identity.GetUserId()));

                        // Need to logoff user, so he is forced and guided to log in back again with new username/e-mail
                        this.AuthenticationManager.SignOut();
#if !PROD
                        // For testing this will show same link in next page, to ease confirmation procedure
                        ViewBag.Link = callbackUrl;
#endif
                        return this.View(MVC.Account.Views.ViewNames.DisplayEmailConfirmation);
                    }
                }

                // TODO: Add master data translations for those
                this.WebMessages.AddSuccessMessage(this.T("User profile"), this.T("User profile data changed succesfully"));
                return this.RedirectToAction(MVC.Account.UserProfile());
            }
            
            if (this.Request.UrlReferrer.LocalPath == "/account/userprofilefulledit")
            {
                return this.View(MVC.Account.Views.UserProfileFullEdit, model);
            }

            if (oldUserData.IsStronglyAuthenticated)
            {
                return this.View(MVC.Account.Views.UserProfileStrong, model);
            }

            return this.View(MVC.Account.Views.UserProfile, model);
        }