Example #1
0
        public ActionResult EditProfile(UserProfileModel profile, List<string> role)
        {
            if (ModelState.IsValid)
            {
                if (profile == null)
                    throw new HttpException(404, "Not Found");

                var provider = new UserMembershipProvider(userService, avatarService, roleService);
                bool isNameChanged = false;

                try
                {
                    if (User.IsInRole("admin"))
                    {
                        isNameChanged = ChangeUsername(provider, profile);
                        provider.ChangeEmail(profile.Username, profile.Email);
                        SetRoles(profile, role);
                    }
                    else
                    {
                        profile.Username = profile.PreviousUsername;
                    }

                    ChangeAvatar(provider, profile);
                    ChangePassword(provider, profile);


                    if (ModelState.IsValid)
                    {
                        TempData["MessageType"] = MessageType.success;
                        TempData["StrongResultMessage"] = "Изменения сохранены";

                        if (isNameChanged && User.Identity.Name == profile.PreviousUsername)
                        {
                            return RedirectToAction("Logout", "Account");
                        }
                        return RedirectToAction("Index", "Home");
                    }
                }
                catch (Exception)
                {
                    TempData["MessageType"] = MessageType.error;
                    TempData["StrongResultMessage"] = "Произошла ошибка во время сохранения изменений!";
                    TempData["ResultMessage"] = "Некоторые данные могут быть не сохранены";
                    return RedirectToAction("Index", "Home");
                }
            }
            return View(profile);
        }
Example #2
0
        private void SetRoles(UserProfileModel profile, List<string> roles)
        {
            var userRoles = Roles.GetRolesForUser(profile.Username).ToList();
            userRoles.Remove("USER");

            var newRoles = new List<string>();
            newRoles.Add("USER");
            
            if (roles != null)
            {
                foreach (var newRole in roles)
                {
                    if (userRoles.Contains(newRole))
                    {
                        userRoles.Remove(newRole);
                    }
                    else
                    {
                        Roles.AddUserToRole(profile.Username, newRole);
                    }
                }
                newRoles.AddRange(roles);
            }
            foreach (var removedRole in userRoles)
            {
                Roles.RemoveUserFromRole(profile.Username, removedRole);
            }
            profile.Roles = newRoles;
        }
Example #3
0
 private void ChangeAvatar(UserMembershipProvider provider, UserProfileModel profile)
 {
     if (profile.AvatarFile != null)
     {
         if (profile.AvatarFile.ContentLength < RegisterUserModel.maxAvatarSize)
         {
             provider.ChangeUserAvatar(profile.AvatarFile, profile.UserId);
             Session["Avatar"] = null;
         }
         else
         {
             ModelState.AddModelError("AvatarFile", 
                 String.Format("Размер аватара не может превышать {0} МБ", 
                     RegisterUserModel.maxAvatarSize / (1024 * 1024)));
         }
     }
 }
Example #4
0
 private void ChangePassword(UserMembershipProvider provider, UserProfileModel profile)
 {
     if (profile.Password != null &&
             !provider.ChangePassword(profile.Username, profile.PreviousPassword, profile.Password))
     {
         ModelState.AddModelError("PreviousPassword", "Неправильный пароль");
     }
 }
Example #5
0
 private bool ChangeUsername(UserMembershipProvider provider, UserProfileModel profile)
 {
     if (profile.PreviousUsername != profile.Username && !string.IsNullOrEmpty(profile.Username))
     {
         if (!provider.IsUsernameExist(profile.Username))
         {
             provider.ChangeUsername(profile.PreviousUsername, profile.Username);
             return true;
         }
         else
         {
             ModelState.AddModelError("Username", "Такое имя пользователя уже существует");
         }
     }
     return false;
 }