public IActionResult EditUserInRole(List<UserRoleModel> model, int id) { var role = new ManageRoles().GetRoleById(id); var result = new ManageRoles(); if (role.RoleName == null) { ViewBag.ErrorMessage = $"Role with Id = {id} cannot be found"; return View("Error"); } for (int i = 0; i < model.Count; i++) { var user = new UserManager().GetUserById(model[i].UserId); if (!string.IsNullOrEmpty(user.Role)) { if (model[i].IsSelected && (user.Role != role.RoleName)) { result.RemoveUserFromRole(user); result.AddUserToRole(user, role); continue; } else if (!model[i].IsSelected && (user.Role == role.RoleName)) { result.RemoveUserFromRole(user); continue; } else { continue; } } else { result.AddUserToRole(user, role); continue; } } return RedirectToAction("EditRole", new { id = id }); }
public void EditUserBySSN(UserViewModel user) { //Instance of the ManageRoles class to get the user's role ManageRoles man = new ManageRoles(); //Find the right user var model = _db.Users.Where(x => x.SSN == user.SSN).FirstOrDefault(); //If the user dosn't exist throw an exception otherwise change the user information if (model == null) { //TODO: kasta villu throw new ArgumentNullException(); } else { model.FullName = user.FullName; model.SSN = user.SSN; model.Email = user.Email; if (man.GetUserRole(model.Email) != null) { man.ClearUserRoles(model.Id); } man.AddUserToRole(model.Id, user.UserRole); } try { _db.SaveChanges(); } catch (DbEntityValidationException ex) { foreach (var error in ex.EntityValidationErrors) { Console.WriteLine("===================="); Console.WriteLine("Entity {0} in state {1} has validation errors:", error.Entry.Entity.GetType().Name, error.Entry.State); foreach (var ve in error.ValidationErrors) { Console.WriteLine("\tProperty: {0}, Error: {1}", ve.PropertyName, ve.ErrorMessage); } Console.WriteLine(); } throw; } }
public async Task <ActionResult> CreateUser(RegisterViewModel model) { if (ModelState.IsValid) { ApplicationUser existingUser = UserManager.FindByEmail(model.Email); if (existingUser != null) { TempData["NewUser"] = model.Email; return(RedirectToAction("UserAlreadyExists", "Security")); } var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; // Does email already exist? var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { ManageRoles.AddUserToRole(model.Email, App_Role.Users); if (model.MakeSuperUser) { ManageRoles.AddUserToRole(model.Email, App_Role.SuperUsers); } // Don't sign them in. //await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false); // 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>"); TempData["NewUser"] = model.Email; return(RedirectToAction("UserCreated", "Security")); } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }
public async Task <ActionResult> Register(RegisterViewModel model, string returnUrl) { if (ModelState.IsValid) { var UserExist = _service.CheckIfUserExist(model.SSN); if (!UserExist) { var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; user.FullName = model.FullName; user.SSN = model.SSN; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { //await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false); ManageRoles addTo = new ManageRoles(); if (model.Role == "Administrator") { addTo.AddUserToRole(user.Id, "Administrator"); } if (model.Role == "Teacher") { addTo.AddUserToRole(user.Id, "Teacher"); } if (model.Role == "Student") { addTo.AddUserToRole(user.Id, "Student"); } if (model.CourseID.HasValue) { if (model.Role == "Student") { UserService userService = new UserService(); userService.AddStudentToCourse(model.CourseID.Value, user.Id); } if (model.Role == "Teacher") { CourseService courseService = new CourseService(); var course = courseService.GetCourseByID(model.CourseID.Value); course.TeacherID = user.Id; courseService.EditCourseById(course); } } // 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", "User")); } AddErrors(result); } else { return(View("Error")); } } // If we got this far, something failed, redisplay form return(View(model)); }