public ActionResult ChangeEmployeeRoles(string userID) { ViewBag.Username = UserManager.FindById(userID).UserName; ViewBag.ErrorMessage = ""; var employeeRoles = UserManager.GetRoles(userID); var allRoles = RoleManager.Roles.Where(r => r.Name != "SuperUser").ToList(); List <string> availableRoles = new List <string>(); List <string> allRolesText = new List <string>(); foreach (var i in allRoles) { availableRoles.Add(i.Name); allRolesText.Add(i.Name); } // filter for available roles foreach (var er in employeeRoles) { foreach (var ar in allRolesText) { if (er.Equals(ar)) { availableRoles.Remove(ar); } } } ViewBag.EmployeeRoles = (List <string>)employeeRoles; ViewBag.AvailableRoles = availableRoles; var roles = new EmployeeRoleViewModel() { UserID = userID }; return(View(roles)); }
/// <summary> /// /// </summary> /// <param name="employeeId"></param> /// <returns></returns> public async Task <IResult> GetEmployeeById(int employeeId) { var result = new Result { Operation = Operation.Read, Status = Status.Success, StatusCode = HttpStatusCode.OK }; try { if (employeeId > 0) { var employeeVm = new EmployeeViewModel(); var employeeModel = await _employeeRepository.GetEmployeeById(employeeId); if (employeeModel != null) { employeeVm.MapFromModel(employeeModel); var roles = new List <EmployeeRoleViewModel>(); roles = employeeModel.EmployeeRoles.Select(t => { var role = new EmployeeRoleViewModel(); role.Id = t.Id; role.RoleId = t.RoleId; role.RoleName = t.Role.Name; role.EmployeeId = t.EmployeeId; return(role); }).ToList(); employeeVm.Roles = roles; result.Body = employeeVm; } else { result.Body = null; result.Message = "Employee Id does not exists"; result.Status = Status.Fail; result.StatusCode = HttpStatusCode.NotFound; } return(result); } result.Status = Status.Fail; result.StatusCode = HttpStatusCode.BadRequest; return(result); } catch (Exception e) { result.Message = e.Message; result.Status = Status.Error; result.StatusCode = HttpStatusCode.InternalServerError; return(result); } }
public IActionResult UpdateEmployee(EmployeeRoleViewModel model) { try { return(Ok(ep.UpdateEmployee(model))); } catch (Exception ex) { Exceptions(ex); return(StatusCode(500)); } }
public int UpdateEmployee(EmployeeRoleViewModel model) { try { return(employee.UpdateEmployee(model)); } catch (Exception ex) { StaticHelper.LogException(path: up.GetLogFilePath(), errorMessage: ex.Message, methodName: $"Class Name: {nameof(EmployeePresenter)} - Method name: {nameof(UpdateEmployee)}", stackTrace: ex.StackTrace); return(0); } }
public ActionResult ChangeEmployeeRoles(EmployeeRoleViewModel model) { var result = UserManager.AddToRole(model.UserID, model.Role); if (result.Succeeded) { return(RedirectToAction("ManageEmployee", new { userID = model.UserID })); } else { ViewBag.ErrorMessage = "Unable to assign role. Please try again."; return(View(model)); } }
/// <summary> /// Returns employees /// </summary> /// <returns></returns> public IResult GetEmployeeList(SearchSortModel search) { var employeeViewModels = new List <EmployeeViewModel>(); var result = new Result { Operation = Operation.Read, Status = Status.Success, StatusCode = HttpStatusCode.OK }; try { var employees = _employeeRepository.GetAllEmployees(search); if (employees.Any()) { employeeViewModels = employees.Select(t => { var employeeViewModel = new EmployeeViewModel(); employeeViewModel.MapFromModel(t); var roles = new List <EmployeeRoleViewModel>(); roles = t.EmployeeRoles.Select(r => { var role = new EmployeeRoleViewModel(); role.Id = r.Id; role.RoleId = r.RoleId; role.RoleName = r.Role.Name; role.EmployeeId = r.EmployeeId; return(role); }).ToList(); employeeViewModel.Roles = roles; return(employeeViewModel); }).ToList(); } else { result.Message = "No records found"; } search.SearchResult = employeeViewModels; result.Body = search; } catch (Exception ex) { result.Status = Status.Error; result.Message = ex.Message; result.StatusCode = HttpStatusCode.InternalServerError; } return(result); }
public int UpdateEmployee(EmployeeRoleViewModel model) { try { var recordToUpdateInDb = dbContext.TblEmployee.Where(x => x.Id == model.EmployeeId).FirstOrDefault(); if (recordToUpdateInDb != null) { var duplicateNameRecord = dbContext.TblEmployee.Where(x => x.Id != model.EmployeeId && x.FullName == model.FullName && x.UserName == model.Username && x.PhoneNo == model.PhoneNo).FirstOrDefault(); if (duplicateNameRecord != null) { return(0); } else { recordToUpdateInDb.FullName = model.FullName; recordToUpdateInDb.UserName = model.Username; recordToUpdateInDb.PhoneNo = model.PhoneNo; recordToUpdateInDb.Password = model.Password; recordToUpdateInDb.RoleId = model.RoleId; return(dbContext.SaveChanges()); } } else { return(0); } } catch (Exception ex) { StaticHelper.LogException(path: up.GetLogFilePath(), errorMessage: ex.Message, methodName: $"Repository name: {nameof(EmployeeRepository)} - Method name: {nameof(UpdateEmployee)}", stackTrace: ex.StackTrace); return(0); } }
/// <summary> /// /// </summary> /// <param name="loginModel"></param> /// <returns></returns> public async Task <IResult> GetEmployee(UserLoginViewModel loginModel) { var result = new Result { Operation = Operation.Read, Status = Status.Success, StatusCode = HttpStatusCode.OK }; try { if (loginModel != null) { var employeeModel = await _employeeRepository.GetEmployeeByEmail(loginModel.Email); if (employeeModel == null) { result.Body = null; result.Message = "Account with " + loginModel.Email + " does not exists."; result.Status = Status.Fail; result.StatusCode = HttpStatusCode.NotFound; return(result); } else { if (!BC.Verify(loginModel.Password, employeeModel.Password)) { result.Body = null; result.Message = "Password is incorrect"; result.Status = Status.Fail; result.StatusCode = HttpStatusCode.Forbidden; return(result); } // check if employee is confirmed or not if (employeeModel.Status == EntityStatus.Accept) { var roles = new List <EmployeeRoleViewModel>(); roles = employeeModel.EmployeeRoles.Select(t => { var role = new EmployeeRoleViewModel(); role.Id = t.Id; role.RoleId = t.RoleId; role.RoleName = t.Role.Name; role.EmployeeId = t.EmployeeId; return(role); }).ToList(); UserViewModel userView = new UserViewModel() { Email = employeeModel.Email, EmployeeCode = employeeModel.EmployeeCode, Name = employeeModel.Name, UserId = employeeModel.Id, CurrentResidentialAddress = employeeModel.CurrentResidentialAddress, PermanentResidentialAddress = employeeModel.PermanentResidentialAddress, Token = GenerateToken(employeeModel.Name, employeeModel.Email), Roles = roles }; result.Body = userView; } else if (employeeModel.Status == EntityStatus.Deny) { result.Body = null; result.Message = "Your account has declined by the HR"; result.Status = Status.Fail; result.StatusCode = HttpStatusCode.Forbidden; } else { result.Body = null; result.Message = "Your account has not confirmed yet by the HR"; result.Status = Status.Fail; result.StatusCode = HttpStatusCode.Unauthorized; } } return(result); } else { result.Status = Status.Fail; result.StatusCode = HttpStatusCode.BadRequest; return(result); } } catch (Exception e) { result.Message = e.Message; result.Status = Status.Error; return(result); } }
public async Task <ResponseViewModel <EmployeeRoleViewModel> > DeleteEmployeeRole([FromBody] EmployeeRoleViewModel employeeRoleView) { return(await _adminService.DeleteEmployeeRole(employeeRoleView)); }