public ActionResult EditUserRoles(List <UserRoleViewModel> users) { //UserRolesHelper uRoleHelper = new UserRolesHelper(); var helper = new UserRolesHelper(); var usersInAdminRole = helper.UsersInRoles("Admin"); var adminCount = usersInAdminRole.Count(); foreach (var c in users) { if (c.admin) { helper.AddUserRole(c.userId, "Admin"); } else { if (adminCount > 0) { helper.RemoveUserRole(c.userId, "Admin"); adminCount--; } } if (c.projectManager) { helper.AddUserRole(c.userId, "Project Manager"); } else { helper.RemoveUserRole(c.userId, "Project Manager"); } if (c.developer) { helper.AddUserRole(c.userId, "Developer"); } else { helper.RemoveUserRole(c.userId, "Developer"); } if (c.submitter) { helper.AddUserRole(c.userId, "Submitter"); } else { helper.RemoveUserRole(c.userId, "Submitter"); } } return(RedirectToAction("ListUsers")); }
public async Task <ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { // creates new user with constructor var user = new ApplicationUser { UserName = model.Email, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, DisplayName = model.DisplayName, PhoneNumber = model.PhoneNumber }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); ApplicationDbContext UserDb = new ApplicationDbContext(); var userId = UserDb.Users.Single(u => u.UserName == model.Email).Id; db.Users.FirstOrDefault(u => u.Id == userId).DisplayName = model.FirstName + " " + model.LastName; // IList<IdentityRole> userRoles = UserDb.Roles.ToList(); var helper = new UserRolesHelper(); helper.AddUserRole(userId, "Submitter"); //Lowest Priority by Default // db.Entry(User).State = EntityState.Modified; db.SaveChanges(); // BugTrackerEntities BugTrackerUser = new BugTrackerEntities(); //var BugTrackerUserInstance = new BTUser //{ // FirstName = model.FirstName, // LastName = model.LastName, // DisplayName = model.DisplayName, // AspNetUserId = UserDb.Users.FirstOrDefault(u => u.Email == model.Email).Id //}; //BugTrackerUser.BTUsers.Add(BugTrackerUserInstance); //await BugTrackerUser.SaveChangesAsync(); // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 // Send an email with this link // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); return(RedirectToAction("Index", "Home")); } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }
public ActionResult Edit(string AdminSelected, string DeveloperSelected, string ProjectManagerSelected, string SubmitterSelected, string UserId) { var testRole = UserId; var helper = new UserRolesHelper(); if (AdminSelected == "on") { helper.AddUserRole(UserId, "Admin"); } else { helper.RemoveUserRole(UserId, "Admin"); } if (DeveloperSelected == "on") { helper.AddUserRole(UserId, "Developer"); } else { helper.RemoveUserRole(UserId, "Developer"); } if (ProjectManagerSelected == "on") { helper.AddUserRole(UserId, "Project Manager"); } else { helper.RemoveUserRole(UserId, "Project Manager"); } if (SubmitterSelected == "on") { helper.AddUserRole(UserId, "Submitter"); } else { helper.RemoveUserRole(UserId, "Submitter"); } return(RedirectToAction("ListUsers")); // return View("ListUsers"); }