Beispiel #1
0
        /// <summary>
        /// Edits the avatar.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="userProfileManager">The user profile manager.</param>
        private void EditAvatar(ProfileEditViewModel model, UserProfileManager userProfileManager)
        {
            if (model.UploadedImage != null)
            {
                var image = this.UploadAvatar(model.UploadedImage, model.UserName);
                this.ChangeProfileAvatar(this.GetUserId(), image, userProfileManager);

                Image avatarImage;
                model.AvatarImageUrl = new UserDisplayNameBuilder().GetAvatarImageUrl(model.User.Id, out avatarImage);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Edits the password.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <exception cref="System.ArgumentException">Both passwords must match</exception>
        private void EditPassword(ProfileEditViewModel model)
        {
            if (!string.IsNullOrEmpty(model.OldPassword))
            {
                if (model.NewPassword != model.RepeatPassword)
                {
                    throw new ArgumentException("Both passwords must match");
                }

                var userId      = this.GetUserId();
                var userManager = UserManager.GetManager(SecurityManager.GetUser(userId).ProviderName);
                UserManager.ChangePasswordForUser(userManager, userId, model.OldPassword, model.NewPassword, this.SendEmailOnChangePassword);
            }
        }
        public void IndexPostAction_ShowMessage_ShowsSuccessMessage()
        {
            var controller = new DummyProfileController();
            controller.Model.SaveChangesAction = SaveAction.ShowMessage;
            var viewModel = new ProfileEditViewModel();

            var result = controller.Index(viewModel);

            Assert.IsNotNull(result, "The action result is null.");
            Assert.IsInstanceOfType(result, typeof(ViewResult), "The action result is not of the expected type.");

            var viewResult = (ViewResult)result;
            var model = viewResult.Model as ProfileEditViewModel;
            Assert.IsNotNull(model, "The action result should return view with ProfileEditViewModel.");
            Assert.AreEqual(true, model.ShowProfileChangedMsg, "The action did not set correctly option for displaying success message.");
        }
Beispiel #4
0
        /// <inheritDoc/>
        public virtual void InitializeUserRelatedData(ProfileEditViewModel model)
        {
            model.User = SecurityManager.GetUser(this.GetUserId());

            model.UserName = model.User.UserName;
            model.Email    = model.User.Email;
            model.UserName = model.User.UserName;
            Libraries.Model.Image avatarImage;

            var displayNameBuilder = new SitefinityUserDisplayNameBuilder();

            model.DisplayName      = displayNameBuilder.GetUserDisplayName(model.User.Id);
            model.AvatarImageUrl   = displayNameBuilder.GetAvatarImageUrl(model.User.Id, out avatarImage);
            model.DefaultAvatarUrl = displayNameBuilder.GetAvatarImageUrl(Guid.Empty, out avatarImage);

            model.SelectedUserProfiles = UserProfileManager.GetManager(this.ProfileProvider).GetUserProfiles(model.User).Select(p => new CustomProfileViewModel(p)).ToList();
        }
Beispiel #5
0
        /// <summary>
        /// Check if email is changed
        /// </summary>
        /// <param name="model">The profile properties.</param>
        /// <returns>changed/not changed</returns>
        public bool IsEmailChanged(ProfileEditViewModel model)
        {
            if (!string.IsNullOrEmpty(model.Email))
            {
                var userId      = this.GetUserId();
                var userManager = UserManager.GetManager(SecurityManager.GetUser(userId).ProviderName);
                var user        = userManager.GetUser(userId);
                if (user.Email != model.Email)
                {
                    return(true);
                }

                return(false);
            }

            return(false);
        }
        public void IndexPostAction_InvalidModel_ReturnsEditViewWithSameModel()
        {
            var controller = new DummyProfileController();
            controller.EditModeTemplateName = "TestTemplate";
            var viewModel = new ProfileEditViewModel();
            controller.ModelState.AddModelError("TestError", "The model is invalid");

            var result = controller.Index(viewModel);

            Assert.IsNotNull(result, "The action result is null.");
            Assert.IsInstanceOfType(result, typeof(ViewResult), "The action result is not of the expected type.");

            var viewResult = (ViewResult)result;
            Assert.AreEqual("Edit.TestTemplate", viewResult.ViewName, "The Index did not return the configured view according to its convention.");
            Assert.IsNotNull(viewResult.Model, "The Index action did not assign a view model.");
            Assert.IsInstanceOfType(viewResult.Model, typeof(ProfileEditViewModel), "The Index action did not assign a view model of the expected type.");
        }
Beispiel #7
0
        /// <inheritDoc/>
        public virtual void ValidateProfileData(ProfileEditViewModel viewModel, System.Web.Mvc.ModelStateDictionary modelState)
        {
            List <ProfileBindingsContract> profileBindingsList = this.GetDeserializedProfileBindings();

            foreach (var profile in this.SelectedUserProfiles)
            {
                var readOnlyFields  = string.IsNullOrEmpty(profile.User.ExternalProviderName) ? new string[0] : UserManager.GetReadOnlyFields(profile.GetType().Name, profile.User.ExternalProviderName);
                var profileBindings = profileBindingsList.SingleOrDefault(p => p.ProfileType == profile.GetType().FullName);
                if (profileBindings != null)
                {
                    var requiredProperties = profileBindings.Properties.Where(p => p.Required);

                    foreach (var prop in requiredProperties)
                    {
                        if (readOnlyFields.Any(x => x.Equals(prop.Name, StringComparison.OrdinalIgnoreCase)))
                        {
                            // skip validation for read-only fields
                            continue;
                        }

                        string propValue;

                        if (!viewModel.Profile.TryGetValue(prop.Name, out propValue) || string.IsNullOrWhiteSpace(propValue))
                        {
                            modelState.AddModelError(string.Format("Profile[{0}]", prop.Name), string.Format(Res.Get <ProfileResources>().RequiredProfileField, prop.Name));
                        }
                    }
                }
            }

            var minPassLength = UserManager.GetManager(this.MembershipProvider).MinRequiredPasswordLength;

            if (!string.IsNullOrEmpty(viewModel.OldPassword) && !string.IsNullOrEmpty(viewModel.NewPassword) && !string.IsNullOrEmpty(viewModel.RepeatPassword))
            {
                if (viewModel.NewPassword.Length < minPassLength)
                {
                    modelState.AddModelError("NewPassword", string.Format(Res.Get <ProfileResources>().MinimumPasswordLength, minPassLength));
                }

                if (viewModel.RepeatPassword.Length < minPassLength)
                {
                    modelState.AddModelError("RepeatPassword", string.Format(Res.Get <ProfileResources>().MinimumPasswordLength, minPassLength));
                }
            }
        }
Beispiel #8
0
        /// <inheritdoc />
        public ProfileEditViewModel GetProfileEditViewModel()
        {
            if (this.SelectedUserProfiles == null || this.SelectedUserProfiles.Count == 0)
            {
                return(null);
            }

            var profileFields = this.GetProfileFieldValues();
            var viewModel     = new ProfileEditViewModel(profileFields)
            {
                CssClass = this.CssClass,
                CanEdit  = this.CanEdit()
            };

            this.InitializeUserRelatedData(viewModel);

            return(viewModel);
        }
Beispiel #9
0
        /// <summary>
        /// Edits the user profile.
        /// </summary>
        /// <param name="profileProperties">The profile properties.</param>
        public bool EditUserProfile(ProfileEditViewModel model)
        {
            if (!this.CanEdit())
            {
                return(false);
            }

            var userProfileManager = UserProfileManager.GetManager(this.ProfileProvider);

            this.EditProfileProperties(model.Profile, userProfileManager);

            this.EditPassword(model);

            this.EditAvatar(model, userProfileManager);

            userProfileManager.SaveChanges();

            return(true);
        }
Beispiel #10
0
        /// <summary>
        /// Edits the password.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <exception cref="System.ArgumentException">Both passwords must match</exception>
        private void EditPassword(ProfileEditViewModel model)
        {
            if (!string.IsNullOrEmpty(model.OldPassword))
            {
                if (model.NewPassword != model.RepeatPassword)
                {
                    throw new ArgumentException("Both passwords must match");
                }

                var userId = this.GetUserId();
                var userManager = UserManager.GetManager(SecurityManager.GetUser(userId).ProviderName);
                UserManager.ChangePasswordForUser(userManager, userId, model.OldPassword, model.NewPassword, this.SendEmailOnChangePassword);
            }
        }
 public void InitializeUserRelatedData(ProfileEditViewModel model)
 {
     // Do nothing.
 }
 public bool EditUserProfile(ProfileEditViewModel model)
 {
     return true;
 }
 public void ValidateProfileData(ProfileEditViewModel viewModel, System.Web.Mvc.ModelStateDictionary modelState)
 {
     // Do nothing.
 }
        public void IndexPostAction_ShowPage_Redirects()
        {
            var controller = new DummyProfileController();
            controller.Model.SaveChangesAction = SaveAction.ShowPage;
            controller.Model.ProfileSavedPageId = new Guid("3bf29da0-1074-4a71-bb23-7ae43f36d8f9");
            var viewModel = new ProfileEditViewModel();

            var result = controller.Index(viewModel);

            Assert.IsNotNull(result, "The action result is null.");
            Assert.IsInstanceOfType(result, typeof(RedirectResult), "The action result is not of the expected type.");

            var redirectResult = (RedirectResult)result;
            Assert.AreEqual("http://3bf29da0-1074-4a71-bb23-7ae43f36d8f9", redirectResult.Url, "The action did not return the expected message.");
        }
Beispiel #15
0
        /// <summary>
        /// Edits the avatar.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="userProfileManager">The user profile manager.</param>
        private void EditAvatar(ProfileEditViewModel model, UserProfileManager userProfileManager)
        {
            if (model.UploadedImage != null)
            {
                var image = this.UploadAvatar(model.UploadedImage, model.UserName);
                this.ChangeProfileAvatar(this.GetUserId(), image, userProfileManager);

                Image avatarImage;
                model.AvatarImageUrl = new UserDisplayNameBuilder().GetAvatarImageUrl(model.User.Id, out avatarImage);
            }
        }
Beispiel #16
0
        /// <inheritDoc/>
        public virtual void ValidateProfileData(ProfileEditViewModel viewModel, System.Web.Mvc.ModelStateDictionary modelState)
        {
            List<ProfileBindingsContract> profileBindingsList = this.GetDeserializedProfileBindings();

            foreach (var profile in this.SelectedUserProfiles)
            {
                var profileBindings = profileBindingsList.SingleOrDefault(p=>p.ProfileType == profile.GetType().FullName);
                if (profileBindings != null)
                {
                    var requiredProperties = profileBindings.Properties.Where(p => p.Required);

                    foreach (var prop in requiredProperties)
                    {
                        string propValue;

                        if (!viewModel.Profile.TryGetValue(prop.Name, out propValue) || string.IsNullOrWhiteSpace(propValue))
                        {
                            modelState.AddModelError(string.Format("Profile[{0}]", prop.Name), string.Format(Res.Get<ProfileResources>().RequiredProfileField, prop.Name));
                        }
                    }
                }
            }

            var minPassLength = UserManager.GetManager(this.MembershipProvider).MinRequiredPasswordLength;
            if (!string.IsNullOrEmpty(viewModel.OldPassword) && !string.IsNullOrEmpty(viewModel.NewPassword) && !string.IsNullOrEmpty(viewModel.RepeatPassword))
            {
                if (viewModel.NewPassword.Length < minPassLength)
                {
                    modelState.AddModelError("NewPassword", string.Format(Res.Get<ProfileResources>().MinimumPasswordLength, minPassLength));
                }

                if (viewModel.RepeatPassword.Length < minPassLength)
                {
                    modelState.AddModelError("RepeatPassword", string.Format(Res.Get<ProfileResources>().MinimumPasswordLength, minPassLength));
                }
            }
        }
Beispiel #17
0
        /// <inheritDoc/>
        public virtual void InitializeUserRelatedData(ProfileEditViewModel model)
        {
            model.User = SecurityManager.GetUser(this.GetUserId());

            model.UserName = model.User.UserName;
            model.Email = model.User.Email;
            model.UserName = model.User.UserName;
            Libraries.Model.Image avatarImage;

            var displayNameBuilder = new SitefinityUserDisplayNameBuilder();
            model.DisplayName = displayNameBuilder.GetUserDisplayName(model.User.Id);
            model.AvatarImageUrl = displayNameBuilder.GetAvatarImageUrl(model.User.Id, out avatarImage);
            model.DefaultAvatarUrl = displayNameBuilder.GetAvatarImageUrl(Guid.Empty, out avatarImage);

            model.SelectedUserProfiles = UserProfileManager.GetManager(this.ProfileProvider).GetUserProfiles(model.User).Select(p => new CustomProfileViewModel(p)).ToList();
        }
Beispiel #18
0
        /// <inheritdoc />
        public ProfileEditViewModel GetProfileEditViewModel()
        {
            if (this.SelectedUserProfiles == null || this.SelectedUserProfiles.Count == 0)
                return null;

            var profileFields = this.GetProfileFieldValues();
            var viewModel = new ProfileEditViewModel(profileFields)
            {
                CssClass = this.CssClass,
                CanEdit = this.CanEdit()
            };

            this.InitializeUserRelatedData(viewModel);

            return viewModel;
        }
Beispiel #19
0
        /// <summary>
        /// Edits the user profile.
        /// </summary>
        /// <param name="profileProperties">The profile properties.</param>
        public bool EditUserProfile(ProfileEditViewModel model)
        {
            if (!this.CanEdit())
            {
                return false;
            }

            var userProfileManager = UserProfileManager.GetManager(this.ProfileProvider);

            this.EditProfileProperties(model.Profile, userProfileManager);

            this.EditPassword(model);

            this.EditAvatar(model, userProfileManager);

            userProfileManager.SaveChanges();

            return true;
        }
        public ActionResult Index(ProfileEditViewModel viewModel)
        {
            this.Model.ValidateProfileData(viewModel, this.ModelState);
            this.Model.InitializeUserRelatedData(viewModel);

            if (ModelState.IsValid)
            {
                try
                {
                    var isUpdated = this.Model.EditUserProfile(viewModel);
                    if (!isUpdated)
                    {
                        return this.Content(Res.Get<ProfileResources>().EditNotAllowed);
                    }

                    switch (this.Model.SaveChangesAction)
                    {
                        case SaveAction.SwitchToReadMode:
                            return this.ReadProfile();
                        case SaveAction.ShowMessage:
                            viewModel.ShowProfileChangedMsg = true;
                            break;
                        case SaveAction.ShowPage:
                            return this.Redirect(this.Model.GetPageUrl(this.Model.ProfileSavedPageId));
                    }
                }
                catch (ProviderException ex)
                {
                    this.ViewBag.ErrorMessage = ex.Message;
                }
                catch (Exception)
                {
                    this.ViewBag.ErrorMessage = Res.Get<ProfileResources>().ChangePasswordGeneralErrorMessage;
                }
            }

            this.ViewBag.HasPasswordErrors = !this.ModelState.IsValidField("OldPassword") ||
                                             !this.ModelState.IsValidField("NewPassword") ||
                                             !this.ModelState.IsValidField("RepeatPassword") ||
                                             !string.IsNullOrEmpty(this.ViewBag.ErrorMessage);

            var fullTemplateName = ProfileController.EditModeTemplatePrefix + this.EditModeTemplateName;
            return this.View(fullTemplateName, viewModel);
        }
        public void IndexPostAction_SwitchToReadMode_ReturnsReadView()
        {
            var controller = new DummyProfileController();
            controller.ReadModeTemplateName = "TestTemplate";
            controller.Model.SaveChangesAction = SaveAction.SwitchToReadMode;
            var viewModel = new ProfileEditViewModel();

            var result = controller.Index(viewModel);

            Assert.IsNotNull(result, "The action result is null.");
            Assert.IsInstanceOfType(result, typeof(ViewResult), "The action result is not of the expected type.");

            var viewResult = (ViewResult)result;
            Assert.AreEqual("Read.TestTemplate", viewResult.ViewName, "The Index did not return the configured view according to its convention.");
            Assert.IsNotNull(viewResult.Model, "The Index action did not assign a view model.");
            Assert.IsInstanceOfType(viewResult.Model, typeof(ProfilePreviewViewModel), "The Index action did not assign a view model of the expected type.");
        }