Esempio n. 1
0
        public ActionResult Edit(string id)
        {
            ApplicationUser user = _db.Users.Find(id);
            SelectList roles = new SelectList(_db.Roles, "Id", "Name");
            ViewBag.Roles = roles;

            EditViewModel regWm = new EditViewModel();
            regWm.Name = user.Name;
            regWm.Password = user.Password;
            regWm.Email = user.Email;
            regWm.Id = user.Id;

            return View(regWm);
        }
Esempio n. 2
0
        public ActionResult Edit(EditViewModel user)
        {
            if (ModelState.IsValid)
            {
                var userCurrent = _db.Users.Find(user.Id);

                string pass = user.Password;

                userCurrent.Name = user.Name;
                userCurrent.UserName = user.Email;
                userCurrent.Email = user.Email;
                userCurrent.Password = user.Password;
                userCurrent.PasswordHash = UserManager.PasswordHasher.HashPassword(pass);

                // Теперь разберемся с изменением роли 
#warning TODO Крайне кривая конструкция
                var allRoles = new List<IdentityRole>();
                var userRoles = userCurrent.Roles;

                // Соберем все роли
                foreach(var ur in userRoles)
                {
                    allRoles.Add(_db.Roles.First(x => x.Id == ur.RoleId));
                }
                // Удалим роли
                foreach(var r in allRoles)
                {
                    UserManager.RemoveFromRole(user.Id, r.Name);
                }
                // Добавим новую роль
                var newRole = _db.Roles.First(x => x.Id == user.RoleId).Name;
                UserManager.AddToRole(user.Id, newRole);

                // Сохраним изменения
                _db.Entry(userCurrent).State = EntityState.Modified;

                using (var transaction = _db.Database.BeginTransaction())
                {
                    try
                    {
                        _db.SaveChanges();

                        transaction.Commit();
                    }
                    catch (Exception error)
                    {
                        transaction.Rollback();
                        return PartialView("_Content", error.Message);
                    }
                }

                return RedirectToAction("Index");
            }
            
            SelectList roles = new SelectList(_db.Roles, "Id", "Name");
            ViewBag.Roles = roles;

            return View(user);
        }
        public ActionResult Edit(EditViewModel user)
        {
            if (ModelState.IsValid)
            {
                var userCurrent = db.Users.Find(user.Id);

                string pass = user.Password;

                userCurrent.Name = user.Name;
                userCurrent.UserName = user.Email;
                userCurrent.Email = user.Email;
                userCurrent.Password = user.Password;
                userCurrent.PasswordHash = UserManager.PasswordHasher.HashPassword(pass);

                // Теперь разберемся с изменением роли 
                #warning TODO Крайне кривая конструкция
                var allRoles = new List<IdentityRole>();
                var userRoles = userCurrent.Roles;

                userCurrent.Roles.Clear();
                
                // Добавим новую роль
                var newRole = db.Roles.First(x => x.Id == user.RoleId).Name;
                var result = UserManager.AddToRole(user.Id, newRole);

                // Сохраним изменения
                db.Entry(userCurrent).State = EntityState.Modified;

                db.SaveChanges();

                return RedirectToAction("Index");
            }
            
            SelectList roles = new SelectList(db.Roles, "Id", "Name");
            ViewBag.Roles = roles;

            return View(user);
        }