This commit is contained in:
Mohaimen Hasan 2026-11-03 18:32:24 +06:00
parent b4e3bf9f21
commit 1724b5cf52
5 changed files with 112 additions and 36 deletions

View File

@ -23,7 +23,7 @@ export class User extends BaseObject {
public employeeID?: number; public employeeID?: number;
public changePasswordAtNextLogon: boolean; public changePasswordAtNextLogon: boolean;
public userID: number; public userID: number;
public payrollTypeID?: number; public PayrollTypeID?: number;
reason: string; reason: string;
authorizedBy: number; authorizedBy: number;
authorizedDate: Date; authorizedDate: Date;

View File

@ -284,4 +284,7 @@ export class AuthService {
}) })
); );
} }
getIsFromUser(): boolean {
return this.isFromUserSubject.value;
}
} }

View File

@ -97,15 +97,23 @@ export class AppTopBarComponent implements OnInit, DoCheck, OnDestroy {
this.loginName = user.UserName; this.loginName = user.UserName;
this.isSuperUser = true; this.isSuperUser = true;
var spItem = localStorage.getItem('switchUserID'); var spItem = localStorage.getItem('switchUserID');
if (spItem != undefined && spItem != null) { if (spItem != undefined && spItem != null && spItem != "undefined") {
this.isSuperUser = false; this.isSuperUser = false;
this.swithtypeName = "Switch to Super-User"; this.swithtypeName = "Switch to Super-User";
this.switchUserID = parseInt(localStorage.getItem('switchUserID')); this.switchUserID = parseInt(localStorage.getItem('switchUserID'));
} }
else if (user.UserType == EnumUserType.SuperUser && user.EmployeeID != undefined) { else if (user.UserType == EnumUserType.SuperUser && (!user.EmployeeID || user.EmployeeID === 0 || user.EmployeeID === "0")) {
this.swithtypeName = undefined;
}
else if (user.UserType == EnumUserType.SuperUser && user.EmployeeID != undefined && user.EmployeeID != 0 && user.EmployeeID != "0") {
if(this.authService.getIsFromUser()){
this.swithtypeName = "Switch to Admin";
}
else{
this.swithtypeName = "Switch to ESS"; this.swithtypeName = "Switch to ESS";
} }
} }
}
} }
@ -227,10 +235,15 @@ export class AppTopBarComponent implements OnInit, DoCheck, OnDestroy {
this.switchUserID = undefined; this.switchUserID = undefined;
var userid = undefined; var userid = undefined;
this.authService.getSwitchUserId(EnumUserType.SuperUser).subscribe(next => { this.authService.getSwitchUserId(EnumUserType.SuperUser).subscribe(next => {
if(next == 0){
userid = undefined;
this.swithtypeName = undefined;
}
else{
userid = next; userid = next;
}
}, error => { }, error => {
this.swithtypeName = undefined; this.swithtypeName = undefined;
console.log(error);
}, () => { }, () => {
if (userid != 0){ if (userid != 0){
this.switchUserID = userid; this.switchUserID = userid;
@ -238,11 +251,20 @@ export class AppTopBarComponent implements OnInit, DoCheck, OnDestroy {
} }
}); });
} }
else if (user.UserType == EnumUserType.SuperUser && user.EmployeeID != undefined) else if (user.UserType == EnumUserType.SuperUser && (!user.EmployeeID || user.EmployeeID === 0 || user.EmployeeID === "0"))
{ {
this.swithtypeName = undefined;
}
else if (user.UserType == EnumUserType.SuperUser && user.EmployeeID != undefined && user.EmployeeID != 0 && user.EmployeeID != "0")
{
if(this.authService.getIsFromUser()){
this.swithtypeName = "Switch to Admin";
}
else{
this.swithtypeName = "Switch to ESS"; this.swithtypeName = "Switch to ESS";
} }
} }
}
//ngOnInit() { //ngOnInit() {
// this.employee = new Employee(); // this.employee = new Employee();
@ -308,8 +330,9 @@ export class AppTopBarComponent implements OnInit, DoCheck, OnDestroy {
switchTheUser(oUser: User) { switchTheUser(oUser: User) {
const data = { const data = {
user: oUser, user: oUser,
payrollTypeId: oUser.payrollTypeID, payrollTypeId: oUser.PayrollTypeID,
}; };
debugger;
this.loadingPanel.ShowLoadingPanel = true; this.loadingPanel.ShowLoadingPanel = true;
this.authService.SwtichUserMode(data).subscribe( this.authService.SwtichUserMode(data).subscribe(
(resp: any) => { (resp: any) => {

View File

@ -280,7 +280,7 @@ export class LandingPageComponent implements OnInit {
this.currentUser.userType = EnumUserType.Employee this.currentUser.userType = EnumUserType.Employee
const data = { const data = {
user: this.currentUser, user: this.currentUser,
payrollTypeId: this.currentUser.payrollTypeID, payrollTypeId: this.currentUser.PayrollTypeID,
}; };
this.authService.SwtichUserMode(data).subscribe( this.authService.SwtichUserMode(data).subscribe(
(resp: any) => { (resp: any) => {

View File

@ -365,6 +365,36 @@ namespace HRM.UI.Controllers
userForLoginDto.payrollTypeid = payrollTypes[0].ID; userForLoginDto.payrollTypeid = payrollTypes[0].ID;
} }
} }
if (isMFA)
{
int ID = userFromRepo.ID;
mfaCode = this.processMFA();
var mfaSessionId = Guid.NewGuid().ToString();
decimal minutes = mFATimer > 0 ? mFATimer : 2.0m;
TimeSpan otpValidity = TimeSpan.FromSeconds((double)(minutes * 60));
_cache.Remove($"MFA_{mfaSessionId}");
var challenge = new MfaChallenge
{
UserId = ID,
OTP = mfaCode,
CreatedAt = DateTime.Now,
ExpiredAt = DateTime.Now.Add(otpValidity),
AttemptCount = 0
};
var cacheOptions = new MemoryCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = otpValidity,
Priority = CacheItemPriority.High
};
_cache.Set($"MFA_{mfaSessionId}", challenge, cacheOptions);
WriteToFile("Code ready");
this.SendMfaCode(mfaCode, time, userFromRepo);
return Ok(new
{
mfaRequired = true,
mfaSessionId = mfaSessionId,
});
}
} }
} }
//userFromRepo = _userService.GetByLoginIDAndPassword(userForLoginDto.Username.ToLower(), userForLoginDto.Password); //userFromRepo = _userService.GetByLoginIDAndPassword(userForLoginDto.Username.ToLower(), userForLoginDto.Password);
@ -600,13 +630,11 @@ namespace HRM.UI.Controllers
otpBodyMail = "<p>Your login verification code is: <b>" + mfaCode + "</b></p>"; otpBodyMail = "<p>Your login verification code is: <b>" + mfaCode + "</b></p>";
//otpBodyMail += "<p>This code is valid till " + time + ".</p>"; //otpBodyMail += "<p>This code is valid till " + time + ".</p>";
otpBodyMail += "<p>This code will expire in next 2 minutes.</p>"; otpBodyMail += "<p>This code will expire in next 2 minutes.</p>";
otpBodySMS = "Your login verification code is: " + mfaCode + ". This code will expire in next 2 minutes."; otpBodySMS = "Your login verification code is: " + mfaCode + ". This code will expire in next 2 minutes.";
#endregion #endregion
#region Mail #region Mail
string notificationtypeMail = new SystemConfigarationService().GetconfigStringValue(EnumConfigurationType.Logic, "mfa", "notificationtypeMail"); string notificationtypeMail = new SystemConfigarationService().GetconfigStringValue(EnumConfigurationType.Logic, "mfa", "notificationtypeMail");
if (notificationtypeMail.ToLower() == "yes") if (notificationtypeMail.ToLower() == "yes")
{ {
if (!string.IsNullOrEmpty(email) && !string.IsNullOrEmpty(name)) if (!string.IsNullOrEmpty(email) && !string.IsNullOrEmpty(name))
@ -623,30 +651,27 @@ namespace HRM.UI.Controllers
WriteToFile($"Mail is sending: {email}, {name}, {mobile}"); WriteToFile($"Mail is sending: {email}, {name}, {mobile}");
mailSender.ThreadSendMail(emailSettings); mailSender.ThreadSendMail(emailSettings);
} }
else throw new Exception("Mail Address not found."); else throw new Exception("Mail Address not found.");
WriteToFile($"Mail has been sent: {email}, {name}, {mobile}"); WriteToFile($"Mail has been sent: {email}, {name}, {mobile}");
} }
#endregion #endregion
#region SMS #region SMS
string notificationtypeSMS = new SystemConfigarationService().GetconfigStringValue(EnumConfigurationType.Logic, "mfa", "notificationtypeSMS"); string notificationtypeSMS = new SystemConfigarationService().GetconfigStringValue(EnumConfigurationType.Logic, "mfa", "notificationtypeSMS");
if (notificationtypeSMS.ToLower() == "yes")
{
WriteToFile($"SMS init"); WriteToFile($"SMS init");
if (!string.IsNullOrEmpty(mobile)) if (!string.IsNullOrEmpty(mobile))
{ {
string mobileNo = new string(mobile.Where(char.IsDigit).ToArray()); string mobileNo = new string(mobile.Where(char.IsDigit).ToArray());
if (mobileNo.Length == 11) mobileNo = "880" + mobileNo; if (mobileNo.Length == 11) mobileNo = "880" + mobileNo;
WriteToFile($"SMS is sending: {email}, {name}, {mobile}"); WriteToFile($"SMS is sending: {email}, {name}, {mobile}");
await _smsService.SendLoginOtp(mobileNo, otpBodySMS); await _smsService.SendLoginOtp(mobileNo, otpBodySMS);
WriteToFile($"SMS has been sent: {email}, {name}, {mobile}"); WriteToFile($"SMS has been sent: {email}, {name}, {mobile}");
} }
else throw new Exception("Mobile Number not found."); else throw new Exception("Mobile Number not found.");
}
#endregion #endregion
} }
private void WriteToFile(string text) private void WriteToFile(string text)
@ -829,7 +854,8 @@ namespace HRM.UI.Controllers
} }
#endregion #endregion
token = this.CreateToten(oempUser, omp.PayrollTypeID, null); //token = this.CreateToten(oempUser, omp.PayrollTypeID, null);
token = this.CreateTokenNew(oempUser, omp.PayrollTypeID, null, null);
} }
else if (ouser.UserType == EnumUserType.User) else if (ouser.UserType == EnumUserType.User)
{ {
@ -840,7 +866,15 @@ namespace HRM.UI.Controllers
} }
else payrollTypeId = (int)item["payrollTypeId"].ToObject<int>(); else payrollTypeId = (int)item["payrollTypeId"].ToObject<int>();
PayrollType optype = new PayrollTypeService().Get(payrollTypeId); PayrollType optype = new PayrollTypeService().Get(payrollTypeId);
User guser = this._userService.Get((int)currentUser.EmployeeID, EnumUserType.User); User guser = null;
if (currentUser.UserType == EnumUserType.SuperUser && (currentUser.EmployeeID == null || currentUser.EmployeeID == 0))
{
return BadRequest("Admin access is restricted.");
}
else
{
guser = this._userService.Get((int)currentUser.EmployeeID, EnumUserType.User);
}
if (guser == null) if (guser == null)
{ {
return BadRequest("Employee is not a Admin user."); return BadRequest("Employee is not a Admin user.");
@ -857,11 +891,26 @@ namespace HRM.UI.Controllers
} }
#endregion #endregion
token = this.CreateToten(guser, optype.ID, optype.NextPayProcessDate); //token = this.CreateToten(guser, optype.ID, optype.NextPayProcessDate);
token = this.CreateTokenNew(guser, optype.ID, optype.NextPayProcessDate, null);
} }
else if (ouser.UserType == EnumUserType.SuperUser) else if (ouser.UserType == EnumUserType.SuperUser)
{ {
User guser = this._userService.Get((int)currentUser.EmployeeID, EnumUserType.SuperUser); if (item["payrollTypeId"] == null)
{
}
payrollTypeId = (int)item["payrollTypeId"].ToObject<int>();
PayrollType optype = new PayrollTypeService().Get(payrollTypeId);
User guser = null;
if (currentUser.EmployeeID == null || currentUser.EmployeeID == 0)
{
return BadRequest("You do not have permission to access this feature.");
}
else
{
guser = this._userService.Get((int)currentUser.EmployeeID, EnumUserType.SuperUser);
}
if (guser == null) if (guser == null)
{ {
return BadRequest("Employee is not a Super-User."); return BadRequest("Employee is not a Super-User.");
@ -878,7 +927,8 @@ namespace HRM.UI.Controllers
} }
#endregion #endregion
token = this.CreateToten(guser, null, null); //token = this.CreateToten(guser, null, null);
token = this.CreateTokenNew(guser, optype.ID, optype.NextPayProcessDate, null);
} }
} }