/// <summary> /// Validates the user profile. /// </summary> /// <param name="collection">The collection.</param> /// <param name="modelState">State of the model.</param> /// <param name="user">The user.</param> /// <param name="userProfile">The user profile.</param> public static void Validate(FormCollection collection, ModelStateDictionary modelState, UserProfile userProfile) { if (userProfile == null) return; foreach (var item in userProfile.ProfileType.ProfileHeaders) { foreach (var element in item.ProfileElements) { var elementName = String.Format("{0}_{1}", (ElementType)element.Type, element.Id); var value = collection[elementName]; if (value == null) continue; // check if the item is required if (element.IsRequired && String.IsNullOrEmpty(value)) { modelState.AddModelError(elementName, String.Format("The {0} field is required.", element.Title)); } else { ElementTypeUtility.ValidateElement((ElementType)element.Type, elementName, value, modelState); } } } }
/// <summary> /// Registers the user. /// </summary> /// <param name="model">The model.</param> /// <param name="collection">The collection.</param> /// <param name="userProfile">The user profile.</param> /// <param name="currentUser">The current user.</param> /// <param name="widget">The widget.</param> /// <param name="user">The user.</param> /// <returns></returns> public static bool SaveUser(ProfileWidgetViewModel model, FormCollection collection, UserProfile userProfile, ICorePrincipal currentUser, ProfileWidget widget, out User user) { user = null; var isSuccess = true; if (currentUser == null) return false; var userService = ServiceLocator.Current.GetInstance<IUserService>(); user = userService.Find(currentUser.PrincipalId); if (user == null) return false; if (widget.DisplayMode != ProfileWidgetDisplayMode.ProfileDetails) { model.MapTo(user); userService.SetPassword(user, model.Password); isSuccess = userService.Save(user); } if (isSuccess && widget.DisplayMode != ProfileWidgetDisplayMode.CommonDetails) { if (userProfile != null) { foreach (var item in userProfile.ProfileType.ProfileHeaders) { foreach (var element in item.ProfileElements) { var elementName = String.Format("{0}_{1}", (ElementType)element.Type, element.Id); var value = collection[elementName]; var existingValue = userProfile.ProfileElements.FirstOrDefault(el=>el.ProfileElement.Id == element.Id); if (existingValue !=null) { existingValue.Value = value; } else { userProfile.ProfileElements.Add(new UserProfileElement { UserProfile = userProfile, ProfileElement = element, Value = value }); } } } var userProfileService = ServiceLocator.Current.GetInstance<IUserProfileService>(); isSuccess = userProfileService.Save(userProfile); } } return isSuccess; }
/// <summary> /// Registers the user. /// </summary> /// <param name="widget">The widget.</param> /// <param name="model">The model.</param> /// <param name="collection">The collection.</param> /// <param name="user">The user.</param> /// <returns></returns> public static bool RegisterUser(RegistrationWidget widget, RegistrationWidgetViewModel model, FormCollection collection, out User user) { var userService = ServiceLocator.Current.GetInstance<IUserService>(); var userProfileService = ServiceLocator.Current.GetInstance<IUserProfileService>(); user = new User(); model.MapTo(user); userService.SetPassword(user, model.Password); var isSuccess = userService.Save(user); if (isSuccess) { var profile = new UserProfile { User = user, ProfileType = widget.ProfileType }; foreach (var item in widget.ProfileType.ProfileHeaders) { foreach (var element in item.ProfileElements) { var elementName = String.Format("{0}_{1}", (ElementType)element.Type, element.Id); var value = collection[elementName]; if (value == null) continue; profile.AddProfileElement(new UserProfileElement { UserProfile = profile, ProfileElement = element, Value = value }); } } isSuccess = userProfileService.Save(profile); } return isSuccess; }