Inheritance: Core.Framework.Permissions.Models.BaseUser, IPermissible
 /// <summary>
 /// Builds the role assignment model.
 /// </summary>
 /// <param name="user">The user.</param>
 /// <param name="userGroup">The user group.</param>
 /// <returns></returns>
 public static AssignedUserGroupModel BuildRoleAssignmentModel(User user, UserGroup userGroup)
 {
     return new AssignedUserGroupModel
     {
         Id = userGroup.Id,
         Name = userGroup.Name,
         Assigned = user.UserGroups.Contains(userGroup)
     };
 }
 /// <summary>
 /// Builds the user assignment model.
 /// </summary>
 /// <param name="userGroup">The user group for assignment.</param>
 /// <param name="user">The user for binding.</param>
 /// <returns>User assignment model.</returns>
 public static AssignedUserModel BuildUserAssignmentModel(UserGroup userGroup, User user)
 {
     return new AssignedUserModel
     {
         Id = user.Id,
         Name = user.Username,
         Assigned = userGroup.Users.Contains(user)
     };
 }
 /// <summary>
 /// Saves the specified model.
 /// </summary>
 /// <param name="model">The model.</param>
 /// <returns>Created user.</returns>
 public static User Save(UserViewModel model)
 {
     var userService = ServiceLocator.Current.GetInstance<IUserService>();
     var user = new User();
     model.MapTo(user);
     userService.SetPassword(user, model.Password);
     userService.Save(user);
     return user;
 }
 /// <summary>
 /// Builds the user assignment model.
 /// </summary>
 /// <param name="role">The role for assignment.</param>
 /// <param name="user">The user for binding.</param>
 /// <returns>User assignment model.</returns>
 public static AssignedUserModel BuildUserAssignmentModel(Role role, User user)
 {
     return new AssignedUserModel
     {
         Id = user.Id,
         Name = user.Username,
         Assigned = role.Users.Contains(user)
     };
 }
        /// <summary>
        /// Builds the assignment model.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <returns></returns>
        public static UserGroupToUserAssignmentModel BuildAssignmentModel(User user)
        {
            IUserGroupService userGroupService = ServiceLocator.Current.GetInstance<IUserGroupService>();
            var allUserGroups = userGroupService.GetAll();

            return new UserGroupToUserAssignmentModel
            {
                User = new UserViewModel().MapFrom(user),
                UserGroups = allUserGroups.Select(userGroup => BuildRoleAssignmentModel(user, userGroup)).ToArray()
            };
        }
 /// <summary>
 /// Updates the specified user.
 /// </summary>
 /// <param name="user">The user to update.</param>
 /// <param name="model">The model.</param>
 /// <returns>Updated user.</returns>
 public static User Update(User user, UserViewModel model)
 {
     var userService = ServiceLocator.Current.GetInstance<IUserService>();
     model.MapTo(user);
     if (!String.IsNullOrEmpty(model.Password))
     {
         userService.SetPassword(user, model.Password);
     }
     userService.Save(user);
     return user;
 }
        /// <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 String GetUserUserGroups(User user)
 {
     return String.Join("; ", user.UserGroups.Select(userGroup => userGroup.Name));
 }
 public static String GetUserRoles(User user)
 {
     return String.Join("; ", user.Roles.Select(role => role.Name));
 }
        /// <summary>
        /// Updates the role to users assignment.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <param name="model">The model.</param>
        /// <returns></returns>
        public static bool UpdateUserGroupToUsersAssignment(User user, UserGroupToUserAssignmentModel model)
        {
            var userGroupService = ServiceLocator.Current.GetInstance<IUserGroupService>();
            var userService = ServiceLocator.Current.GetInstance<IUserService>();

            user.UserGroups.Clear();
            foreach (var userGroup in model.UserGroups)
            {
                if (userGroup.Assigned)
                {
                    user.UserGroups.Add(userGroupService.Find(userGroup.Id));
                }
            }

            return userService.Save(user);
        }
        public static bool UpdatRoleToUsersAssignment(User user, IEnumerable<String> ids, IEnumerable<String> selids)
        {
            var roleService = ServiceLocator.Current.GetInstance<IRoleService>();
            var userService = ServiceLocator.Current.GetInstance<IUserService>();

            var notselids = ids.Where(t => !selids.Contains(t)).ToList();

            var noselected = user.Roles.Where(t => notselids.Contains(t.Id.ToString())).ToList();
            foreach (var role in noselected)
            {
                user.Roles.Remove(role);
            }

            foreach (var selid in selids)
            {
                String selid1 = selid;
                if (!user.Roles.Any(t => t.Id.ToString() == selid1))
                {
                    long selectedID;
                    if (long.TryParse(selid1, out selectedID))
                    {
                        user.Roles.Add(roleService.Find(selectedID));
                    }
                }
            }

            return userService.Save(user);
        }
        /// <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;
        }
        /// <summary>
        /// Creates new user session.
        /// </summary>
        /// <param name="model">The login details.</param>
        /// <returns>Authentication result.</returns>
        public virtual ActionResult CreateUserSession(OpenIDLoginWidgetViewModel model)
        {
            var userService = ServiceLocator.Current.GetInstance<IUserService>();
            var authenticationHelper = ServiceLocator.Current.GetInstance<IAuthenticationHelper>();
            //            if (ModelState.IsValid)
            //            {
            IAuthenticationResponse response = openIdProvider.GetResponse();
            if (response == null)
            {
                Identifier id;
                if (Identifier.TryParse(model.UserOpenId, out id))
                {
                    try
                    {
                        return openIdProvider.CreateRequest(model.UserOpenId).RedirectingResponse.AsActionResult();
                    }
                    catch (ProtocolException ex)
                    {
                        Error(ex.Message);
                    }
                }
                else
                {
                    Error(HttpContext.Translate("Messages.InvalidIdentifier",
                                          ResourceHelper.GetControllerScope(this)));
                }
            }
            else
            {
                switch (response.Status)
                {
                    case AuthenticationStatus.Authenticated:
                        {
                            //Session["FriendlyIdentifier"] = response.FriendlyIdentifierForDisplay;
                            var user = userService.FindByEmailOrUsername(response.FriendlyIdentifierForDisplay);
                            if (user == null)
                            {
                                user = new User
                                           {
                                               Username = response.FriendlyIdentifierForDisplay,
                                               Email = response.FriendlyIdentifierForDisplay
                                           };
                                userService.SetPassword(user, Guid.NewGuid().ToString());
                            }
                            authenticationHelper.LoginUser(user, true);
                            FormsAuthentication.SetAuthCookie(response.ClaimedIdentifier, false);
                            Success(response.FriendlyIdentifierForDisplay);
                            break;
                        }
                    case AuthenticationStatus.Canceled:
                        {
                            Error(HttpContext.Translate("Messages.CanceledAtProvider",
                                          ResourceHelper.GetControllerScope(this)));
                            break;
                        }
                    case AuthenticationStatus.Failed:
                        {
                            Error(response.Exception.Message);
                            break;
                        }
                    default:
                        {
                            break;
                        }
                }
                return Redirect(Url.Action("Index", "Home", new { area = "" }));
            }
            //            }

            return PartialView("ViewWidget", model);
        }