@if (showModal) {
-
- Access Types
-
-
}
@if (showAttachment) {
diff --git a/HRM.UI/Controllers/Auth/AuthController.cs b/HRM.UI/Controllers/Auth/AuthController.cs
index 5af03ad..cf8ca05 100644
--- a/HRM.UI/Controllers/Auth/AuthController.cs
+++ b/HRM.UI/Controllers/Auth/AuthController.cs
@@ -424,6 +424,12 @@ namespace HRM.UI.Controllers
{
return BadRequest("Access Type and process date is not valid.");
}
+ else if (userFromRepo.UserType == EnumUserType.User
+ && !this._payrollTypeService.GetPayrollTypes(userFromRepo.LoginID).Any(p => p.ID == userForLoginDto.payrollTypeid))
+ {
+ // Only an approved (live) access type may be used to sign in; a requested one is not live yet.
+ return BadRequest("You do not have approved access to the selected Access Type.");
+ }
else if (userFromRepo.UserType == EnumUserType.Employee)
{
DateTime eocd= _userService.GetEndofContractDate(userForLoginDto.Username);
@@ -847,6 +853,11 @@ namespace HRM.UI.Controllers
{
PayrollType optype = new PayrollTypeService().Get(payrollTypeId);
User oempUser = new UserService().Get(ouser.ID);
+ // Only an approved (live) access type may be switched to.
+ if (oempUser == null || !this._payrollTypeService.GetPayrollTypes(oempUser.LoginID).Any(p => p.ID == payrollTypeId))
+ {
+ return BadRequest("You do not have approved access to this Access Type.");
+ }
token = this.CreateToten(oempUser, optype.ID, optype.NextPayProcessDate);
}
catch (Exception e)
@@ -917,6 +928,21 @@ namespace HRM.UI.Controllers
return BadRequest("Your account is now locked. Please contact with your Line Manager.");
}
+ // Only an approved (live) access type may be switched to: the same list the ESS
+ // switch shows (getAdminPayrollTypes).
+ List
liveAccessTypes = this._payrollTypeService.GetUsersAdminPayrollTypes((int)currentUser.EmployeeID);
+ if (!liveAccessTypes.Any(p => p.ID == payrollTypeId))
+ {
+ if (item["payrollTypeId"] != null || liveAccessTypes.Count == 0)
+ {
+ return BadRequest("You do not have approved access to this Access Type.");
+ }
+ // No type was chosen and the employee's own one is not an approved access:
+ // use the first approved one instead.
+ payrollTypeId = liveAccessTypes[0].ID;
+ optype = new PayrollTypeService().Get(payrollTypeId);
+ }
+
#region Save User Login History
if (guser != null)
{
@@ -1526,20 +1552,129 @@ namespace HRM.UI.Controllers
return Ok(items);
}
+ // Maker-checker for User Access Type. The page is in the super-user menu only
+ // (WebMenu.AdminMenus, given to SuperUser / SISU users), so only they may request or decide.
+ private bool CanManageUserAccessType(CurrentUser currentUser)
+ {
+ User actingUser = this._userService.Get(currentUser.UserID);
+ return actingUser != null && (actingUser.UserType == EnumUserType.SuperUser || actingUser.SISU == true);
+ }
+
+ private static bool IsPendingAccess(UserAccessType x)
+ {
+ return x.AccessTypeStatus == EnumAuthStatus.NewNotYetApprove
+ || x.AccessTypeStatus == EnumAuthStatus.DeletedNotYetApprove;
+ }
+
+ // Who made a pending row: the requester of a new access, or of a removal.
+ private static int AccessMakerID(UserAccessType x)
+ {
+ return x.AccessTypeStatus == EnumAuthStatus.DeletedNotYetApprove ? (x.ModifiedBy ?? 0) : x.CreatedBy;
+ }
+
+ // Maker: the ticked payroll types become pending changes; nothing is live until approved.
[HttpPost("SaveUserAccessType")]
public ActionResult SaveUserAccessType(List userAccessTypes)
{
try
{
CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
- userAccessTypes.ForEach(x =>
+ if (!CanManageUserAccessType(currentUser))
{
- x.CreatedBy = currentUser.UserID;
- x.CreatedDate = DateTime.Today;
- x.ApprovedBy = currentUser.UserID;
- x.ApprovedDate = DateTime.Today;
- });
- this._userAccessTypeService.Save(userAccessTypes);
+ return StatusCode(StatusCodes.Status403Forbidden, "You are not allowed to change user access types.");
+ }
+ if (userAccessTypes == null || userAccessTypes.Count == 0)
+ {
+ return BadRequest("Select at least one payroll type.");
+ }
+
+ int userID = userAccessTypes[0].UserID;
+ if (userID <= 0 || userAccessTypes.Any(x => x.UserID != userID))
+ {
+ return BadRequest("Select a user.");
+ }
+
+ List payrollTypeIDs = userAccessTypes.Select(x => x.PayrollTypeID).Distinct().ToList();
+ List allPayrollTypes = this._payrollTypeService.Get();
+ if (payrollTypeIDs.Any(id => !allPayrollTypes.Any(p => p.ID == id)))
+ {
+ return BadRequest("Invalid payroll type.");
+ }
+
+ if (this._userAccessTypeService.GetByUserID(userID).Any(IsPendingAccess))
+ {
+ return BadRequest("This user already has an access change waiting for approval. Approve or reject it first.");
+ }
+
+ int changed = this._userAccessTypeService.RequestChange(userID, payrollTypeIDs, currentUser.UserID);
+ if (changed == 0)
+ {
+ return BadRequest("Nothing changed.");
+ }
+ }
+ catch (Exception e)
+ {
+ return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
+ }
+
+ return Ok(true);
+ }
+
+ // Checker: makes the user's pending access changes live. The maker cannot approve.
+ [HttpPost("ApproveUserAccessType/{userID}")]
+ public ActionResult ApproveUserAccessType(int userID)
+ {
+ try
+ {
+ CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
+ if (!CanManageUserAccessType(currentUser))
+ {
+ return StatusCode(StatusCodes.Status403Forbidden, "You are not allowed to approve user access types.");
+ }
+
+ List pending = this._userAccessTypeService.GetByUserID(userID).Where(IsPendingAccess).ToList();
+ if (pending.Count == 0)
+ {
+ return BadRequest("This user has no access change waiting for approval.");
+ }
+ if (pending.Any(x => AccessMakerID(x) == currentUser.UserID))
+ {
+ return BadRequest("You cannot approve a change you made yourself. Another user must approve it.");
+ }
+
+ this._userAccessTypeService.Approve(userID, currentUser.UserID);
+ }
+ catch (Exception e)
+ {
+ return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
+ }
+
+ return Ok(true);
+ }
+
+ // Checker: drops the user's pending access changes; access stays as it was. The maker cannot reject.
+ [HttpPost("RejectUserAccessType/{userID}")]
+ public ActionResult RejectUserAccessType(int userID)
+ {
+ try
+ {
+ CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
+ if (!CanManageUserAccessType(currentUser))
+ {
+ return StatusCode(StatusCodes.Status403Forbidden, "You are not allowed to reject user access types.");
+ }
+
+ List pending = this._userAccessTypeService.GetByUserID(userID).Where(IsPendingAccess).ToList();
+ if (pending.Count == 0)
+ {
+ return BadRequest("This user has no access change waiting for approval.");
+ }
+ if (pending.Any(x => AccessMakerID(x) == currentUser.UserID))
+ {
+ return BadRequest("You cannot reject a change you made yourself. Another user must reject it.");
+ }
+
+ this._userAccessTypeService.Reject(userID);
}
catch (Exception e)
{