/// <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);
        }
        public static ProfileWidgetViewModel BindWidgetModel(ICoreWidgetInstance instance, ICorePrincipal user)
        {
            if (user == null)
            {
                return(null);
            }

            var widgetService      = ServiceLocator.Current.GetInstance <IProfileWidgetService>();
            var userProfileService = ServiceLocator.Current.GetInstance <IUserProfileService>();
            var userService        = ServiceLocator.Current.GetInstance <IUserService>();

            var currentUser = userService.Find(user.PrincipalId);

            if (currentUser == null)
            {
                return(null);
            }

            var userProfile = userProfileService.GetUserProfile(user);
            var widget      = widgetService.Find(instance.InstanceId ?? 0);

            var model = new ProfileWidgetViewModel().MapFrom(currentUser);

            if (widget != null)
            {
                model.PageWidgetId = widget.Id;
                model.Profile      = userProfile;
                model.Widget       = widget;
            }

            return(model);
        }
        public static ProfileWidgetViewModel BindWidgetModel(ICoreWidgetInstance instance, ICorePrincipal user)
        {
            if (user == null)
                return null;

            var widgetService = ServiceLocator.Current.GetInstance<IProfileWidgetService>();
            var userProfileService = ServiceLocator.Current.GetInstance<IUserProfileService>();
            var userService = ServiceLocator.Current.GetInstance<IUserService>();

            var currentUser = userService.Find(user.PrincipalId);

            if (currentUser == null)
                return null;

            var userProfile = userProfileService.GetUserProfile(user);
            var widget = widgetService.Find(instance.InstanceId ?? 0);

            var model = new ProfileWidgetViewModel().MapFrom(currentUser);
            if (widget != null)
            {
                model.PageWidgetId = widget.Id;
                model.Profile = userProfile;
                model.Widget = widget;
            }

            return model;
        }
Exemple #4
0
        public virtual ActionResult ProfileWidget()
        {
            ProfileWidgetViewModel profileWidgetVm = null;

            if (User.Identity.IsAuthenticated)
            {
                User user = _cService.GetUser(User.Identity.GetUserId <int>());
                profileWidgetVm = Mapper.Map <User, ProfileWidgetViewModel>(user);
            }
            return(PartialView("Widgets/_ProfileWidget", profileWidgetVm));
        }
Exemple #5
0
        public virtual ActionResult SaveUser(ProfileWidgetViewModel model, FormCollection collection)
        {
            var widgetService        = ServiceLocator.Current.GetInstance <IProfileWidgetService>();
            var authenticationHelper = ServiceLocator.Current.GetInstance <IAuthenticationHelper>();
            var widget             = widgetService.Find(model.PageWidgetId);
            var userProfileService = ServiceLocator.Current.GetInstance <IUserProfileService>();

            if (widget != null)
            {
                if (widget.DisplayMode == ProfileWidgetDisplayMode.ProfileDetails)
                {
                    ModelState.Clear();
                }

                var userProfile = userProfileService.GetUserProfile(this.CorePrincipal());

                if (widget.DisplayMode != ProfileWidgetDisplayMode.CommonDetails)
                {
                    if (userProfile != null)
                    {
                        ProfileWidgetHelper.Validate(collection, ModelState, userProfile);
                    }
                }

                if (ModelState.IsValid)
                {
                    User user;
                    if (ProfileWidgetHelper.SaveUser(model, collection, userProfile, this.CorePrincipal(), widget, out user))
                    {
                        Success(HttpContext.Translate("Messages.UserUpdated", String.Empty));

                        if (widget.DisplayMode != ProfileWidgetDisplayMode.ProfileDetails)
                        {
                            authenticationHelper.LogoutUser();
                            authenticationHelper.LoginUser(user, true);
                        }
                    }
                }
                else
                {
                    ViewData[String.Format("FormCollection{0}", widget.Id)] = collection;
                    Error(HttpContext.Translate("Messages.ValidationError", String.Empty));
                }

                model.Widget  = widget;
                model.Profile = userProfile;
            }

            return(PartialView("ViewWidget", model));
        }
        public virtual ActionResult SaveUser(ProfileWidgetViewModel model, FormCollection collection)
        {
            var widgetService = ServiceLocator.Current.GetInstance<IProfileWidgetService>();
            var authenticationHelper = ServiceLocator.Current.GetInstance<IAuthenticationHelper>();
            var widget = widgetService.Find(model.PageWidgetId);
            var userProfileService = ServiceLocator.Current.GetInstance<IUserProfileService>();

            if (widget != null)
            {
                if (widget.DisplayMode == ProfileWidgetDisplayMode.ProfileDetails)
                {
                    ModelState.Clear();
                }

                var userProfile = userProfileService.GetUserProfile(this.CorePrincipal());

                if (widget.DisplayMode != ProfileWidgetDisplayMode.CommonDetails)
                {
                    if (userProfile != null)
                    {
                        ProfileWidgetHelper.Validate(collection, ModelState, userProfile);
                    }
                }
              
                if (ModelState.IsValid)
                {
                    User user;
                    if (ProfileWidgetHelper.SaveUser(model, collection, userProfile, this.CorePrincipal(), widget, out user))
                    {
                        Success(HttpContext.Translate("Messages.UserUpdated", String.Empty));

                        if (widget.DisplayMode != ProfileWidgetDisplayMode.ProfileDetails)
                        {
                            authenticationHelper.LogoutUser();
                            authenticationHelper.LoginUser(user, true);
                        }
                    }
                }
                else
                {
                    ViewData[String.Format("FormCollection{0}", widget.Id)] = collection;
                    Error(HttpContext.Translate("Messages.ValidationError", String.Empty));
                }

                model.Widget = widget;
                model.Profile = userProfile;
            }

            return PartialView("ViewWidget", model);
        }
        /// <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;
        }