Exemplo n.º 1
0
        // GET: ManageUsers
        public ActionResult Index()
        {
            var allusers = _unitOfWork.Users.GetUsersAndRoles();

            var editUsersVM = new EditUsersViewModel
            {
                AllRoles = _unitOfWork.Roles.GetAll(),
                Users    = new List <UserViewModel>()
            };

            foreach (var user in allusers)
            {
                var userVM = new UserViewModel
                {
                    UserRoles = user.AspNetRoles,
                    Id        = user.Id,
                    UserName  = user.UserName
                };

                editUsersVM.Users.Add(userVM);
            }


            return(View(editUsersVM));
        }
        public ActionResult Edit([Bind(Include = "UserName, FirstName, LastName, Email, Password, ConfirmPassword")] EditUsersViewModel userModel)
        {
            if (ModelState.IsValid)
            {
                var db      = new ApplicationDbContext();
                var newUser = db.Users.First(u => u.UserName == userModel.UserName);

                if (newUser != null)
                {
                    newUser.FirstName = userModel.FirstName;
                    newUser.LastName  = userModel.LastName;
                    newUser.Email     = userModel.Email;
                    if (userModel.Password != null)
                    {
                        PasswordHasher ph = new PasswordHasher();
                        newUser.PasswordHash = ph.HashPassword(userModel.Password);
                    }
                }


                db.Entry(newUser).State = EntityState.Modified;
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(userModel));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> EditUser(string id)
        {
            var user = await userManager.FindByIdAsync(id);

            if (user == null)
            {
                ViewBag.ErrorMessage = $"User with ID = {id} cannot be found";
                return(View("NotFound"));
            }

            var userRoles = await userManager.GetRolesAsync(user);

            var userClaims = await userManager.GetClaimsAsync(user);

            var model = new EditUsersViewModel
            {
                Id       = user.Id,
                Email    = user.Email,
                UserName = user.UserName,
                Claims   = userClaims.Select(c => c.Value).ToList(),
                Roles    = userRoles.ToList()
            };

            return(View(model));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> EditUser(EditUsersViewModel model)
        {
            var user = await userManager.FindByIdAsync(model.Id);

            if (user == null)
            {
                ViewBag.ErrorMessage = $"User with ID = {model.Id} cannot be found";
                return(View("NotFound"));
            }
            else
            {
                user.Email    = model.Email;
                user.UserName = model.UserName;

                var result = await userManager.UpdateAsync(user);

                if (result.Succeeded)
                {
                    return(RedirectToAction("ListUsers"));
                }

                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError("", error.Description);
                }

                return(View(model));
            }
        }
Exemplo n.º 5
0
        public IActionResult EditUsers()
        {
            EditUsersViewModel model = new EditUsersViewModel();

            model.Users = _userManager.Users;

            return(View(model));
        }
        public ActionResult Delete(string userName = null)
        {
            var db    = new ApplicationDbContext();
            var user  = db.Users.FirstOrDefault(u => u.UserName == userName);
            var model = new EditUsersViewModel(user);

            if (user == null)
            {
                return(HttpNotFound());
            }

            return(View(model));
        }
        public ActionResult Index()
        {
            var db    = new ApplicationDbContext();
            var users = db.Users;
            var model = new List <EditUsersViewModel>();

            foreach (var user in users)
            {
                var u = new EditUsersViewModel(user);
                model.Add(u);
            }

            return(View(model));
        }
        public ActionResult Edit(string userName = null)
        {
            if (userName == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            var db    = new ApplicationDbContext();
            var user  = db.Users.First(u => u.UserName == userName);
            var model = new EditUsersViewModel(user);

            if (User == null)
            {
                return(HttpNotFound());
            }

            return(View(model));
        }
Exemplo n.º 9
0
 // GET: Account/Delete/{guid}
 public ActionResult Delete(string Id)
 {
     if (string.IsNullOrEmpty(Id))
     {
         return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
     }
     using (ApplicationDbContext db = new ApplicationDbContext())
     {
         ApplicationUser user = db.Users.Find(Id);
         if (user != null)
         {
             EditUsersViewModel var = new EditUsersViewModel
             {
                 Name    = user.Name,
                 Surname = user.Surname,
                 Email   = user.Email
             };
             return(View(var));
         }
     }
     return(HttpNotFound());
 }
Exemplo n.º 10
0
 public ActionResult Edit([Bind(Include = "Id,Name,Surname,Email,Active")] EditUsersViewModel view)
 {
     if (ModelState.IsValid)
     {
         using (ApplicationDbContext db = new ApplicationDbContext())
         {
             ApplicationUser user = db.Users.Find(view.Id);
             if (user != null)
             {
                 ApplicationUser newUser = user;
                 newUser.Name            = view.Name;
                 newUser.Surname         = view.Surname;
                 newUser.Email           = view.Email;
                 newUser.Active          = view.Active;
                 db.Entry(newUser).State = EntityState.Modified;
                 db.SaveChanges();
             }
         }
         return(RedirectToAction("ManageLogins"));
     }
     return(View(view));
 }
Exemplo n.º 11
0
 public EditUsersWindowAdmin(EditUsersViewModel viewModel)
 {
     InitializeComponent();
     ViewModel = viewModel;
 }
Exemplo n.º 12
0
 public EditUsersPage()
 {
     InitializeComponent();
     BindingContext = viewModel = new EditUsersViewModel();
 }