public virtual ActionResult Edit(string id, Web100Tango.ManageController.ManageMessageId? Message = null) { var Db = new PortalDbContext(); var user = Db.Users.First(u => u.UserName == id); var model = new EditUserViewModel(user); ViewBag.MessageId = Message; // Get connected logins var usr = UserManager.FindById(User.Identity.GetUserId()); if (usr == null) { return View("Error"); } var userLogins = UserManager.GetLogins(user.Id); var fbLogin = userLogins.SingleOrDefault(l => l.LoginProvider == "Facebook"); var gLogin = userLogins.SingleOrDefault(l => l.LoginProvider == "Google"); model.FacebookId = fbLogin != null ? fbLogin.ProviderKey : null; model.GoogleId = gLogin != null ? gLogin.ProviderKey : null; return View(model); }
public virtual async Task<ActionResult> Edit(EditUserViewModel model) { if (ModelState.IsValid) { string userid = null; string username = model.OldUserName ?? model.UserName; using (var Db = new PortalDbContext()) { var user = Db.Users.First(u => u.UserName == username); // Update the user data: user.UserName = model.UserName; user.FirstName = model.FirstName; user.LastName = model.LastName; user.Email = model.Email; Db.Entry(user).State = System.Data.Entity.EntityState.Modified; userid = user.Id; await Db.SaveChangesAsync(); } // Update Facebook and Google id var usr = await UserManager.FindByIdAsync(userid); if (usr == null) { return View("Error"); } var userLogins = await UserManager.GetLoginsAsync(userid); // Facebook var fbLogin = userLogins.SingleOrDefault(l => l.LoginProvider == "Facebook"); if (fbLogin != null) { if (model.FacebookId.Empty()) { var res = await UserManager.RemoveLoginAsync(userid, fbLogin); if (!res.Succeeded) DependencyResolver.Current.GetService<IAlertsProvider>().Add(string.Format("Failed to remove Facebook account: {0}", string.Concat(res.Errors)), AlertType.Danger, true); } else { if (fbLogin.ProviderKey != model.FacebookId) { await UserManager.RemoveLoginAsync(userid, fbLogin); await UserManager.AddLoginAsync(usr.Id, new UserLoginInfo("Facebook", model.FacebookId)); } } } else { if (model.FacebookId.NotEmpty()) { var res = await UserManager.AddLoginAsync(usr.Id, new UserLoginInfo("Facebook", model.FacebookId)); if (!res.Succeeded) DependencyResolver.Current.GetService<IAlertsProvider>().Add(string.Format("Failed to add Facebook account: {0}", string.Concat(res.Errors)), AlertType.Danger, true); } } // Google var gLogin = userLogins.SingleOrDefault(l => l.LoginProvider == "Google"); if (gLogin != null) { if (model.GoogleId.Empty()) { var res = await UserManager.RemoveLoginAsync(userid, gLogin); if (!res.Succeeded) DependencyResolver.Current.GetService<IAlertsProvider>().Add(string.Format("Failed to remove Google account: {0}", string.Concat(res.Errors)), AlertType.Danger, true); } else { if (gLogin.ProviderKey != model.GoogleId) { await UserManager.RemoveLoginAsync(userid, fbLogin); await UserManager.AddLoginAsync(usr.Id, new UserLoginInfo("Facebook", model.FacebookId)); } } } else { if (model.GoogleId.NotEmpty()) { var res = await UserManager.AddLoginAsync(usr.Id, new UserLoginInfo("Google", model.GoogleId)); if (!res.Succeeded) DependencyResolver.Current.GetService<IAlertsProvider>().Add(string.Format("Failed to add Google account: {0}", string.Concat(res.Errors)), AlertType.Danger, true); } } return RedirectToAction("Index"); } // If we got this far, something failed, redisplay form return View(model); }
public virtual ActionResult Index() { var Db = new PortalDbContext(); var users = Db.Users; var model = new List<EditUserViewModel>(); foreach (var user in users) { var u = new EditUserViewModel(user); model.Add(u); } return View(model); }
public virtual ActionResult Delete(string id) { var Db = new PortalDbContext(); var user = Db.Users.First(u => u.UserName == id); var model = new EditUserViewModel(user); if (user == null) { return HttpNotFound(); } return View(model); }