public async Task <ActionResult> EditUser(EditUserViewModel model) { var Roleslist = context.Roles.OrderBy(r => r.Name).ToList().Select(rr => new SelectListItem { Value = rr.Name.ToString(), Text = rr.Name }).ToList(); ViewBag.Roles = Roleslist; List <UserEntity> RoleManagers = UserServices.GetUserByRoleName("Manager"); List <UserEntity> RoleAdmins = UserServices.GetUserByRoleName("Admin"); RoleManagers.AddRange(RoleAdmins); ViewBag.Manager = RoleManagers; ViewBag.Designations = UserServices.GetAllDesignations(); if (ModelState.IsValid) { UserEntity UserObj = UserServices.GetUserByID(model.UserId); if (UserObj != null) { try { ApplicationUser User = await UserManager.FindByNameAsync(UserObj.UserName); IList <string> UserRoles = await UserManager.GetRolesAsync(User.Id); foreach (var role in UserRoles) { await UserManager.RemoveFromRoleAsync(User.Id, role); } await UserManager.AddToRoleAsync(User.Id, model.Role); if (model.Password != null) { UserStore <ApplicationUser> store = new UserStore <ApplicationUser>(context); String hashedNewPassword = UserManager.PasswordHasher.HashPassword(model.Password); ApplicationUser cUser = await store.FindByIdAsync(User.Id); await store.SetPasswordHashAsync(cUser, hashedNewPassword); await store.UpdateAsync(cUser); } UserObj.Name = model.Name; UserObj.Email = model.Email; UserObj.PersonalEmail = model.PersonalEmail; UserObj.ManagerId = model.ManagerId; UserObj.DesignationId = model.DesignationId; UserObj.ContactNo = model.ContactNo; UserObj.Address = model.Address; UserObj.Salary = model.Salary; UserObj.DOJ = model.DOJ; UserObj.DOR = model.DOR; UserObj.DOB = model.DOB; //Speial Case For Admin by UserName if (UserObj.UserName == "Admin") { UserObj.Status = true; } else { UserObj.Status = model.Status; } int x = UserServices.InsertUpdateUser(UserObj); if (x > 0) { TempData[HRMWeb.Helpers.AlertStyles.Success] = UserObj.UserName + " is Updated Successfully."; } try { if (Request.Files.Count > 0) { var file = Request.Files[0]; if (file != null && file.ContentLength > 0) { string FileExtention = Path.GetExtension(file.FileName); string NewFileName = UserObj.UserId.ToString() + FileExtention; string path = HRMHelper.GetUserDirectory(); path = Path.Combine(path, NewFileName); file.SaveAs(path); path = path.Replace(Server.MapPath("~"), ""); path = path.Replace("\\", "/"); UserObj.Image = path; int y = UserServices.InsertUpdateUser(UserObj); if (y > 0) { model.Image = path; } } } } catch { } } catch { TempData[HRMWeb.Helpers.AlertStyles.Warning] = "Updation Fails"; } } } if (model.Image == null) { model.Image = string.Empty; } return(View(model)); }
public async Task <ActionResult> Register(RegisterViewModel model) { var Roleslist = context.Roles.OrderBy(r => r.Name).ToList().Select(rr => new SelectListItem { Value = rr.Name.ToString(), Text = rr.Name }).ToList(); ViewBag.Roles = Roleslist; List <UserEntity> RoleManagers = UserServices.GetUserByRoleName("Manager"); List <UserEntity> RoleAdmins = UserServices.GetUserByRoleName("Admin"); RoleManagers.AddRange(RoleAdmins); ViewBag.Manager = RoleManagers; ViewBag.Designations = UserServices.GetAllDesignations(); if (ModelState.IsValid) { var user = new ApplicationUser() { UserName = model.UserName }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { UserEntity ob = new UserEntity(); ob.UserName = user.UserName; ob.Name = model.Name; ob.Status = model.Status; ob.Guid = user.Id; ob.Email = model.Email; ob.PersonalEmail = model.PersonalEmail; ob.DesignationId = model.DesignationId; ob.ContactNo = model.ContactNo; ob.Address = model.Address; ob.Salary = model.Salary; ob.ManagerId = model.ManagerId; ob.DOB = model.DOB; ob.DOJ = model.DOJ; ob.DOR = model.DOR; ob.UserId = UserServices.InsertUpdateUser(ob); if (ob.UserId > 0) { TempData[HRMWeb.Helpers.AlertStyles.Success] = model.UserName + " is Successfully added"; UserManager.AddToRole(user.Id, model.Role); } else { var tempUser = UserManager.FindByName(model.UserName); await UserManager.DeleteAsync(tempUser); } //await SignInAsync(user, isPersistent: false); //return RedirectToAction("Index", "Home"); try { if (Request.Files.Count > 0) { var file = Request.Files[0]; if (file != null && file.ContentLength > 0) { string FileExtention = Path.GetExtension(file.FileName); string NewFileName = ob.UserId.ToString() + FileExtention; string path = HRMHelper.GetUserDirectory(); path = Path.Combine(path, NewFileName); file.SaveAs(path); path = path.Replace(Server.MapPath("~"), ""); path = path.Replace("\\", "/"); ob.Image = path; UserServices.InsertUpdateUser(ob); } } } catch { } model = new RegisterViewModel(); } else { string error = ""; foreach (string item in result.Errors) { error += item + "\n"; } TempData[HRMWeb.Helpers.AlertStyles.Danger] = error; //AddErrors(result); } } // If we got this far, something failed, redisplay form return(View(model)); }