public async Task <ActionResult> CreateNewByLeaveType(LeaveAllocationEditionViewModel leave) { var currentEmployee = await _UnitOfWork.Employees.GetEmployeeAsync(User); if (!await _UserManager.IsCompanyPrivelegedUser(User)) { ModelState.AddModelError("", _Localizer["You're forbidden to create allocation"]); return(Forbid()); } if (!ModelState.IsValid) { return(View(leave)); } var leaveTypeObj = await _UnitOfWork.LeaveTypes.FindAsync(x => x.Id == leave.AllocationLeaveTypeId); var consernedEmployee = await _UnitOfWork.Employees.FindAsync(x => x.Id.Equals(leave.AllocationEmployeeId)); if (currentEmployee.CompanyId != consernedEmployee.CompanyId) { ModelState.AddModelError("", _Localizer["You're forbidden to create allocation because you allocate leave to employee from other company"]); return(Forbid()); } if (leaveTypeObj.CompanyId != consernedEmployee.CompanyId) { ModelState.AddModelError("", _Localizer["You're forbidden to create allocation because it belongs to other company"]); return(Unauthorized()); } int period = leave.Period; var newLeaveAllocation = new LeaveAllocation() { AllocationEmployeeId = leave.AllocationEmployeeId, AllocationLeaveTypeId = leave.AllocationLeaveTypeId, DateCreated = leave.DateCreated, NumberOfDays = leave.NumberOfDays, Period = period, }; bool succeed = await _UnitOfWork.LeaveAllocations.CreateAsync(newLeaveAllocation); succeed &= await _UnitOfWork.Save(); if (succeed) { return(await UserLeaveAllocationsForPeriod(userId : leave.AllocationEmployeeId, period : leave.Period.ToString()));; } else { leave = await InitializeLeaveAllocationEditionViewModel( employee : consernedEmployee, leaveType : leaveTypeObj, period : leave.Period, includeEmployeesDictionary : true ); SetEditViewProperties(allowEditPeriod: true, allowEditEmployee: true, allowEditLeaveType: false); return(View(leave)); } }
public async Task <IActionResult> Create(CreateLeaveRequestVM leaveRequest) { var currentEmployee = await _UnitOfWork.Employees.GetEmployeeAsync(User); if (currentEmployee == null) { ModelState.AddModelError("", _ControllerLocalizer["Your account not belongs to employees"]); return(Forbid()); } if (!ModelState.IsValid) { return(View(leaveRequest)); } leaveRequest.LeaveTypes = _Mapper.Map <IEnumerable <SelectListItem> >(await _UnitOfWork.LeaveTypes.WhereAsync(c => c.CompanyId == currentEmployee.CompanyId)); if (DateTime.Compare(leaveRequest.StartDate, leaveRequest.EndDate) > 1) { ModelState.AddModelError("Date", _ControllerLocalizer["Start date must be earlier that the end date"]); return(View(leaveRequest)); } if (!await ValidateRequestDaysAsync(currentEmployee.Id, (int)leaveRequest.LeaveTypeId, leaveRequest.StartDate, leaveRequest.EndDate)) { ModelState.AddModelError("", _ControllerLocalizer["You have requested more days that you owned"]); return(View(leaveRequest)); } var request = new LeaveRequest() { ActionedDateTime = null, Approuved = null, ApprouvedBy = null, ApprouvedById = null, LeaveTypeId = (int)leaveRequest.LeaveTypeId, RequestedDate = DateTime.Now, StartDate = leaveRequest.StartDate, EndDate = leaveRequest.EndDate, RequestingEmployeeId = currentEmployee.Id, RequestComment = leaveRequest.RequestComment, }; bool result = await _UnitOfWork.LeaveRequest.CreateAsync(request); result &= await _UnitOfWork.Save(); if (!result) { ModelState.AddModelError("", _ControllerLocalizer[ @"Something went wrong when submitting your request. Please wait a moment and retry or contact your system administrator"]); return(View(leaveRequest)); } else { return(RedirectToAction(nameof(EmployeeRequests))); } }
public async Task <ActionResult> Create(LeaveTypeEditionViewModel creationModel) { int?currentEmplCompanyId = await GetCurrentEmployeesCompany(); if (!(await _SignInManager.UserManager?.IsCompanyPrivelegedUser(User)) || currentEmplCompanyId == null) { return(Forbid()); } string newName = string.Empty; if (!ModelState.IsValid) { HomeController.DisplayProblem(_Logger, this, _ControllerLocalizer["Model state invalid"], _ControllerLocalizer[ModelState.ValidationState.ToString()]); return(View(creationModel)); } newName = creationModel.LeaveTypeName; var leaveTypes = (await GetListLeaveTypesForThisCompany()).Where(x => x.LeaveTypeName?.Equals(newName) ?? false); if (leaveTypes?.Count() > 0) { ModelState.AddModelError("", _ControllerLocalizer["Leave type with name {0} already exists", newName]); return(View(creationModel)); } var createdLeaveType = _Mapper.Map <LeaveType>(creationModel); createdLeaveType.DateCreated = DateTime.Now; createdLeaveType.CompanyId = (int)currentEmplCompanyId; bool creationResult = await _UnitOfWork.LeaveTypes.CreateAsync(createdLeaveType); creationResult &= await _UnitOfWork.Save(); if (!creationResult) { ModelState.AddModelError("", _ControllerLocalizer["Failed to save leave type '{0}' in repositary", newName]); return(View(creationModel)); } ; return(RedirectToAction(nameof(Index))); }
public async Task <ActionResult> Create(CompanyVM companyVM) { try { var currentUser = await _UserManager.GetUserAsync(User); UserRoles currentUserRoles = await _UserManager.GetUserRolesAsync(currentUser); bool roleAllowsCreateCompany = (currentUserRoles & UserRoles.AppAdministrator) == UserRoles.AppAdministrator; if (!roleAllowsCreateCompany) { _CompanyControllerLogger.LogWarning($"User {currentUser.UserName} was forbidden to create company record"); ModelState.AddModelError("", _MessageLocalizer["Your not allowed to create the companies"]); return(Forbid(_MessageLocalizer["Your not allowed to create the companies"])); } Company company = _Mapper.Map <Company>(companyVM); await _UnitOfWork.Companies.CreateAsync(company); try { if (!(await _UnitOfWork.Save())) { ModelState.AddModelError("", _MessageLocalizer["Unabled to create the company due the server error"]); _CompanyControllerLogger.LogError("Unabled to create the company due the server error"); } } catch (AggregateException ae) { _CompanyControllerLogger.LogError(ae.Flatten(), ae.Message); ModelState.AddModelError("", _MessageLocalizer["Unabled to create the company due the server error"]); } return(RedirectToAction(nameof(Index))); } catch { ViewData["Action"] = nameof(Create); return(View(EditionViewName, companyVM)); } }
public async Task <IActionResult> OnPostAsync(string returnUrl = null) { returnUrl ??= Url.Content("~/"); if (ModelState.IsValid) { // This doesn't count login failures towards account lockout // To enable password failures to trigger account lockout, set lockoutOnFailure: true Microsoft.AspNetCore.Identity.SignInResult result = null; try { var user = await _userManager.FindByNameAsync(Input.Email); if (user == null) { user = await _userManager.FindByEmailAsync(Input.Email); } result = await _signInManager.PasswordSignInAsync(user, Input.Password, Input.RememberMe, lockoutOnFailure : false); } catch (Exception e) { _logger.LogError(e, e.Message); ModelState.AddModelError(string.Empty, _stringLocalizer["Cant connect sign in manager. Please contact your system administrator"]); return(Page()); } if (result.Succeeded) { _logger.LogInformation("User logged in."); var user = await _userManager.FindByEmailAsync(Input.Email); if (user == null) { return(LocalRedirect(returnUrl)); } var employee = (await _UnitOfWork.Employees.FindAsync(x => x.Id.Equals(user.Id))); if (employee != null) { employee.LastConnectionDate = employee.CurrentConnectionDate; employee.CurrentConnectionDate = DateTime.Now; await _UnitOfWork.Employees.UpdateAsync(employee); try { await _UnitOfWork.Save(); } catch (AggregateException ae) { _logger.LogError(ae.Flatten(), ae.Message); } catch (Exception e) { _logger.LogError(e, e.Message); } } return(LocalRedirect(returnUrl)); } if (result.RequiresTwoFactor) { return(RedirectToPage("./LoginWith2fa", new { ReturnUrl = returnUrl, RememberMe = Input.RememberMe })); } if (result.IsLockedOut) { _logger.LogWarning("User account locked out."); return(RedirectToPage("./Lockout")); } else { ModelState.AddModelError(string.Empty, "Invalid login attempt."); return(Page()); } } // If we got this far, something failed, redisplay form return(Page()); }
public async Task <IActionResult> UpdateEmployee(EmployeeCreationVM employeeCreationVM) { var consernedEmployee = await _UnitOfWork.Employees.FindAsync(x => x.Id.Equals(employeeCreationVM.Id), includes : new System.Linq.Expressions.Expression <Func <Employee, object> >[] { x => x.Company, x => x.Manager } ); if (consernedEmployee == null) { return(NotFound()); } employeeCreationVM.UserName = consernedEmployee.UserName; employeeCreationVM.Email = consernedEmployee.Email; var currentUserData = await GetCurrentUserData(); bool reviewUserRoles = currentUserData.Item3 > UserRoles.Employee; bool canEditUser = reviewUserRoles || employeeCreationVM.Id.Equals(currentUserData.Item1.Id); if (!canEditUser) { ModelState.AddModelError("", _DataLocalizer["You not autorized to edit this profile"]); } var permissions = await GetEditionPermission(consernedEmployee); bool validation = await ValidateInput(consernedEmployee, employeeCreationVM, permissions); if (ModelState.ErrorCount == 0 && validation) { //Reappliying company which was not changed if (!permissions.AllowChangeCompany) { employeeCreationVM.CompanyId = consernedEmployee.CompanyId; employeeCreationVM.Company = consernedEmployee.Company; } if (!permissions.AllowChangeManager) { employeeCreationVM.ManagerId = consernedEmployee.ManagerId; employeeCreationVM.Manager = _Mapper.Map <EmployeeCreationVM>(await _UnitOfWork.Employees.FindAsync(predicate: x => x.Id.Equals(employeeCreationVM.ManagerId))); } consernedEmployee = _Mapper.Map(employeeCreationVM, consernedEmployee); bool result = await _UnitOfWork.Employees.UpdateAsync(consernedEmployee); result &= await _UnitOfWork.Save(); if (!result) { ModelState.AddModelError("", _DataLocalizer["Unable to save user to repository"]); } else { result &= await UpdateUserRoles(consernedEmployee, employeeCreationVM.Roles); } if (currentUserData.Item3 <= UserRoles.Employee) { return(RedirectToAction("Index", "Home")); } else { return(RedirectToAction("Index")); } } else { @ViewBag.Action = nameof(UpdateEmployee); employeeCreationVM = await PrepareEmployeeEditionViewModel(employeeCreationVM : employeeCreationVM, concernedEmployee : consernedEmployee, concernedEmployeesRoles : await _UserManager.GetUserRolesAsync(consernedEmployee), editionPermissions : permissions); return(View(EditEmployeeView, employeeCreationVM)); } }