HRM-10/HRM.DA/Service/Users/UserService.cs

1177 lines
37 KiB
C#
Raw Normal View History

2026-11-03 13:06:00 +06:00
using Azure.Core;
using Ease.Core;
using Ease.Core.DataAccess;
using Ease.Core.DataAccess.SQL;
using Ease.Core.Model;
using Ease.Core.Utility;
using HRM.BO;
using Microsoft.AspNetCore.Http;
using Org.BouncyCastle.Asn1.Ocsp;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Net;
using System.Threading;
namespace HRM.DA
{
#region User Service
public class UserService : ServiceTemplate, IUserService
{
private string _lastPasswords;
public UserService() { }
private void MapObject(User oUser, DataReader oReader)
{
base.SetObjectID(oUser, oReader.GetInt32("UserID").Value);
oUser.LoginID = oReader.GetString("loginID");
oUser.UserName = oReader.GetString("userName");
oUser.ParentID = oReader.GetInt32("ownerID", 0);
oUser.Password = oReader.GetString("password");
oUser.SISU = oReader.GetBoolean("SISU").Value;
oUser.IPAddress = oReader.GetString("IPAddress",true, string.Empty);
oUser.UserRole = oReader.GetString("UserRole", true, string.Empty);
oUser.Reason = oReader.GetString("Reason", true, string.Empty);
oUser.CreatedBy = oReader.GetInt32("ownerID") == null ? -9: oReader.GetInt32("ownerID").Value;
oUser.CreatedDate = oReader.GetDateTime("CreatedDate") == null ? DateTime.Today : oReader.GetDateTime("CreatedDate").Value;
oUser.AuthorizedBy = oReader.GetInt32("AUTHORIZEDBY");
oUser.AuthorizedDate = oReader.GetDateTime("AUTHORIZEDDATE");
oUser.ModifiedBy = oReader.GetInt32("AUTHORIZEDBY");
oUser.ModifiedDate = oReader.GetDateTime("AUTHORIZEDDATE");
oUser.PasswordHints = oReader.GetString("PasswordHint");
oUser.UserStatus = (EnumAuthStatus)Convert.ToInt32(oReader.GetInt32("Status"));
oUser.UserType = (EnumUserType)Convert.ToInt32(oReader.GetInt32("UserType"));
oUser.ApprovedBy = oReader.GetInt32("ApprovedBy");
oUser.ApprovedDate = oReader.GetDateTime("ApprovedDate");
//oUser.LastPasswordChangedDate = oReader.GetDateTime("LastPasswordChangeDate").Value;
//oUser.LockedWorkStation = oReader.GetString("LockedWorkStation");
oUser.ComputerName = oReader.GetString("ComputerName", true, string.Empty);
oUser.ApprovedComputerName = oReader.GetString("ApprovedComputerName", true, string.Empty);
oUser.ChangePasswordAtNextLogon = oReader.GetBoolean("ChangePasswordAtNextLogon", false);
oUser.EmployeeID = oReader.GetInt32("EmployeeID");
oUser.Email = oReader.GetString("Email", true, string.Empty);
2026-11-11 12:09:05 +06:00
// oUser.EmailAddress = oReader.GetString("EmailAddress", true, null);
2026-11-03 13:06:00 +06:00
// oUser.LogInPayrollTypeID = oReader.GetInt32("PAYROLLTYPEID", 0);
// oUser.ChangePasswordAtNextLogon = oReader.GetBoolean("ChangePasswordAtNextLogon").Value;
//oUser.NeverExpire = oReader.GetBoolean("NeverExpire").Value;
//oUser.ExpireDate = oReader.GetDateTime("ExpireDate");
//oUser.ResetPassword = oReader.GetBoolean("ResetPassword").Value;
//oUser.MacAddress = oReader.GetString("MacAddress");
//oUser.CreatedBy = oReader.GetInt32("CreatedBy").Value;
//oUser.CreatedDate = oReader.GetDateTime("CreatedDate").Value;
//oUser.ModifiedBy = oReader.GetInt32("ModifiedBy");
//oUser.ModifiedDate = oReader.GetDateTime("ModifiedDate");
//||||||| .r85
// oUser.LogInPayrollTypeID = oReader.GetInt32("PAYROLLTYPEID", 0);
// oUser.ChangePasswordAtNextLogon = oReader.GetBoolean("ChangePasswordAtNextLogon").Value;
// oUser.NeverExpire = oReader.GetBoolean("NeverExpire").Value;
// oUser.LastPasswords = oReader.GetString("LastPasswords");
// oUser.ExpireDate = oReader.GetDateTime("ExpireDate");
// oUser.ResetPassword = oReader.GetBoolean("ResetPassword").Value;
// oUser.ConfirmedPassword = oReader.GetString("ConfirmedPassword");
// oUser.MacAddress = oReader.GetString("MacAddress");
// oUser.CreatedBy = oReader.GetInt32("CreatedBy").Value;
// oUser.CreatedDate = oReader.GetDateTime("CreatedDate").Value;
// oUser.ModifiedBy = oReader.GetInt32("ModifiedBy");
// oUser.ModifiedDate = oReader.GetDateTime("ModifiedDate");
//=======
//oUser.LogInPayrollTypeID = oReader.GetInt32("PAYROLLTYPEID", 0);
//oUser.ChangePasswordAtNextLogon = oReader.GetBoolean("ChangePasswordAtNextLogon").Value;
//oUser.NeverExpire = oReader.GetBoolean("NeverExpire").Value;
//oUser.LastPasswords = oReader.GetString("LastPasswords");
//oUser.ExpireDate = oReader.GetDateTime("ExpireDate");
//oUser.ResetPassword = oReader.GetBoolean("ResetPassword").Value;
//oUser.ConfirmedPassword = oReader.GetString("ConfirmedPassword");
//oUser.MacAddress = oReader.GetString("MacAddress");
//oUser.CreatedBy = oReader.GetInt32("CreatedBy").Value;
//oUser.CreatedDate = oReader.GetDateTime("CreatedDate").Value;
//oUser.ModifiedBy = oReader.GetInt32("ModifiedBy");
//oUser.ModifiedDate = oReader.GetDateTime("ModifiedDate");
this.SetObjectState(oUser, Ease.Core.ObjectState.Saved);
}
protected override T CreateObject<T>(DataReader oReader)
{
User oUser = new User();
MapObject(oUser, oReader);
return oUser as T;
}
#region Service implementation
public List<User> Get(string LoginID, string Name, EnumUserType type)
{
TransactionContext tc = null;
List<User> users = new List<User>();
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(UserDA.Get(tc, LoginID, Name, type));
users = this.CreateObjects<User>(dr);
dr.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return users;
}
public List<User> GetByPayrollType(EnumUserType type, int payrollTypeID)
{
TransactionContext tc = null;
List<User> users = new List<User>();
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(UserDA.GetByPayrollType(tc, type, payrollTypeID));
users = this.CreateObjects<User>(dr);
dr.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return users;
}
public List<User> GetAll()
{
TransactionContext tc = null;
List<User> users = new List<User>();
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(UserDA.GetAll(tc));
users = this.CreateObjects<User>(dr);
dr.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return users;
}
public User Get(int userid)
{
User oUser = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(UserDA.Get(tc, userid));
if (oreader.Read())
{
oUser = this.CreateObject<User>(oreader);
}
oreader.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return oUser;
}
public User Get(TransactionContext tc, int userid)
{
User oUser = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(UserDA.Get(tc, userid));
if (oreader.Read())
{
oUser = this.CreateObject<User>(oreader);
}
oreader.Close();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return oUser;
}
public User GetByEmail(string email)
{
User oUser = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(UserDA.GetByEmail(tc, email));
if (oreader.Read())
{
oUser = this.CreateObject<User>(oreader);
}
oreader.Close();
if (oUser == null)
{
oreader = new DataReader(UserDA.GetUserByEmail(tc, email));
if (oreader.Read())
{
oUser = this.CreateObject<User>(oreader);
}
oreader.Close();
}
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return oUser;
}
#region PPIC SPECIFIC
public User Get(string loginID)
{
User oUser = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(UserDA.Get(tc, loginID));
if (oreader.Read())
{
oUser = this.CreateObject<User>(oreader);
}
oreader.Close();
tc.End();
}
catch (Exception e)
{
throw new Exception(e.Message, e);
}
return oUser;
}
#region PasswordHistories
public string CheckPasswordHistory(string password, string lastPasswords, bool checkPassword, bool esp, bool dalp, short ndap)
{
string value = string.Empty;
if (checkPassword)
{
_lastPasswords = lastPasswords;
if (esp && !Global.StringFuncions.IsStrongPassword(password))
value = "Password must be at least 8 letters and have uppper & lower case letters and numbers.";
password = Global.CipherFunctions.EncryptByTDS(password);
if (dalp && actualPasswordHistories(dalp, ndap).Contains(password))
value = string.Format("You cannot use this password, because it was used in last {0} passwords.", ndap);
}
return value;
}
void CheckPasswordHistory(string password, bool esp, bool dalp, short ndap)
{
try
{
string tmpPassword = Global.CipherFunctions.DecryptByTDS(password);
if (esp && !Global.StringFuncions.IsStrongPassword(tmpPassword))
throw new Exception("Password must be at least 8 letters and have uppper & lower case letters and numbers.");
if (dalp && actualPasswordHistories(dalp, ndap).Contains(password))
throw new Exception(string.Format("You cannot use this password, because it was used in last {0} passwords.", ndap));
if (passwordHistories.Count >= 5)
passwordHistories.Dequeue();
passwordHistories.Enqueue(password);
}
catch (Exception e)
{
throw new Exception(e.Message, e);
}
}
void makePasswordHistory()
{
string[] phs = passwordHistories.ToArray();
if (phs.Length > 0)
_lastPasswords = string.Join(",", phs);
}
private Queue<string> _phs;
Queue<string> passwordHistories
{
get
{
if (_phs == null)
{
_phs = new Queue<string>();
if (!string.IsNullOrWhiteSpace(_lastPasswords))
{
string[] passwords = _lastPasswords.Split(',');
foreach (string item in passwords)
{
if (!_phs.Contains(item))
_phs.Enqueue(item);
}
}
}
return _phs;
}
}
List<string> actualPasswordHistories(bool dalp, short ndap)
{
List<string> pwds = new List<string>();
string[] pwdsHists = passwordHistories.ToArray();
if (dalp && ndap > 0 && pwdsHists.Length > 0)
{
for (int idx = pwdsHists.Length - 1; idx >= 0; idx--)
{
if (!pwds.Contains(pwdsHists[idx]) && pwds.Count < ndap)
pwds.Add(pwdsHists[idx]);
}
}
return pwds;
}
#endregion
#endregion
public User GetByLogINID(string sName, EnumSystemType eSysType)
{
User oUser = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(UserDA.GetByLogInID(tc, sName, eSysType));
if (oreader.Read())
{
oUser = this.CreateObject<User>(oreader);
}
oreader.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return oUser;
}
#region Save User Login History
public void SaveUserLoginHistory(int userID)
{
UserLogInInfo login = new UserLogInInfo();
login.UserID = userID;
login.LoginTime = DateTime.Now;
2026-11-12 14:58:04 +06:00
UserLogInInfoService.CaptureRequestInfo(login); // on the request thread, before the save thread
2026-11-03 13:06:00 +06:00
Thread myNewThread = new Thread(() => SaveLoginfo(login));
myNewThread.Start();
}
#endregion
public User Get(int employeeid, EnumUserType type)
{
User oUser = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(UserDA.Get(tc, employeeid, type));
if (oreader.Read())
{
oUser = this.CreateObject<User>(oreader);
}
oreader.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return oUser;
}
public User GetByLoginIDAndPassword(string sLoginID, string sPassword)
{
User oUser = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(UserDA.GetByLoginIDAndPassword(tc, sLoginID, sPassword));
if (oreader.Read())
{
oUser = this.CreateObject<User>(oreader);
}
oreader.Close();
tc.End();
if (oUser !=null)
{
UserLogInInfo login = new UserLogInInfo();
login.UserID = oUser.ID;
login.LoginTime = DateTime.Now;
2026-11-12 14:58:04 +06:00
UserLogInInfoService.CaptureRequestInfo(login); // on the request thread, before the save thread
2026-11-03 13:06:00 +06:00
Thread myNewThread = new Thread(() => SaveLoginfo(login));
myNewThread.Start();
}
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return oUser;
}
public User GetByLoginIDAndPasswordNew(string sLoginID, string sPassword)
{
User oUser = null;
List<User> users = new List<User>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(UserDA.GetByLoginIDAndPassword(tc, sLoginID, sPassword));
users = this.CreateObjects<User>(dr);
dr.Close();
tc.End();
if (users.Count == 1)
{
oUser = users[0];
}
else if (users.Count > 1)
{
oUser = users.Find(x => x.UserType == EnumUserType.Employee);
if (oUser == null)
{
oUser = users.Find(x => x.UserType == EnumUserType.User);
if (oUser == null)
{
oUser = users.Find(x => x.UserType == EnumUserType.SuperUser);
}
}
}
if (oUser != null)
{
UserLogInInfo login = new UserLogInInfo();
login.UserID = oUser.ID;
login.LoginTime = DateTime.Now;
2026-11-12 14:58:04 +06:00
UserLogInInfoService.CaptureRequestInfo(login); // on the request thread, before the save thread
2026-11-03 13:06:00 +06:00
Thread myNewThread = new Thread(() => SaveLoginfo(login));
myNewThread.Start();
}
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return oUser;
}
public User GetByLoginIDAndPasswordAndHostName(string sLoginID, string sPassword, string hostName)
{
User oUser = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(UserDA.GetByLoginIDAndPassword(tc, sLoginID, sPassword));
if (oreader.Read())
{
oUser = this.CreateObject<User>(oreader);
}
oreader.Close();
tc.End();
if (oUser != null)
{
UserLogInInfo login = new UserLogInInfo();
login.UserID = oUser.ID;
login.LoginTime = DateTime.Now;
login.PCNumber = hostName;
2026-11-12 14:58:04 +06:00
UserLogInInfoService.CaptureRequestInfo(login); // on the request thread, before the save thread
2026-11-03 13:06:00 +06:00
Thread myNewThread = new Thread(() => SaveLoginfo(login));
myNewThread.Start();
}
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return oUser;
}
public User GetByLoginIDbyEmail(string sEmail)
{
User oUser = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(UserDA.GetByLoginIDByEmail(tc, sEmail));
if (oreader.Read())
{
oUser = this.CreateObject<User>(oreader);
}
oreader.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return oUser;
}
public void SaveLoginfo(UserLogInInfo oTran)
{
2026-11-12 14:58:04 +06:00
// PCNumber used to be Environment.MachineName / Dns.GetHostName() - the web
// SERVER's name, the same for every login. UserLogInInfoService.Save now fills it
// with the client's computer name (reverse DNS of its IP) unless the caller set it
// (GetByLoginIDAndPasswordAndHostName passes the host name it was given).
2026-11-03 13:06:00 +06:00
new UserLogInInfoService().Save(oTran);
//if(GlobalFunctions.defaultConfigVal == null)
//{
// int roundofDigit = new SystemConfigarationService().GetconfigIntValue(EnumConfigurationType.Logic, "root", "roundofdegit");
// string DaysInmonth = Convert.ToString( new SystemConfigarationService().GetconfigValue(EnumConfigurationType.Logic, "root", "monthfraction"));
// GlobalFunctions.defaultConfigVal = new DefaultConfigurationValue(roundofDigit, DaysInmonth, false);
//}
}
public DateTime GetEndofContractDate(string loginID)
{
DateTime endofContractDate = DateTime.MinValue;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
endofContractDate = UserDA.GetEndofContractDate(tc, loginID);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return endofContractDate;
}
public bool IsSuperUser(string LoginID)
{
bool returnvalue = false;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
returnvalue = UserDA.IsSuperUSer(tc, LoginID);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return returnvalue;
}
public User ADLogIn(string emailAddress, EnumSystemType eSysType)
{
User oUser = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(UserDA.ADLogIn(tc, emailAddress, eSysType));
if (oreader.Read())
{
oUser = this.CreateObject<User>(oreader);
}
oreader.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return oUser;
}
public DataSet GetUsers(EnumSystemType type, DateTime fromDate, DateTime ToDate)
{
DataSet role = null;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
role = UserDA.GetUsers(tc, type, fromDate, ToDate);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return role;
}
public List<User> Get(EnumSystemType type)
{
List<User> users = new List<User>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader dr = new DataReader(UserDA.Get(tc, type));
users = this.CreateObjects<User>(dr);
dr.Close();
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return users;
}
public int Save(User oUser)
{
TransactionContext tc = null;
int id = 0;
try
{
tc = TransactionContext.Begin(true);
id = this.Save(tc, oUser);
tc.End();
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
return id;
}
public void DoActiveAndIntacive(User oUser)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
UserDA.DoActiveAndIntacive(tc, oUser);
tc.End();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
public void ChangePasswordAdmin(User oUser)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
UserDA.passwordChange(tc, oUser);
#region Notification
if (oUser.EmployeeID.HasValue)
{
var notification = new EmployeeNotification
{
EmployeeID = oUser.EmployeeID.Value,
Header = "Password Changed by Administrator",
Description = $"Your password has been changed by the administrator. " +
$"Please log in using your new password.",
Link = "",
CreatedDate = DateTime.Now,
CreatedBy = oUser.PasswordChangedBy,
ReferenceID = oUser.ID,
IsRead = 0,
ReadDate = null,
ReadBy = null
};
new EmployeeNotificationService().Insert(tc, notification);
}
#endregion
tc.End();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
public void ChangePasswordEss(User oUser)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
UserDA.passwordEss(tc, oUser);
#region Notification
if (oUser.EmployeeID.HasValue)
{
var notification = new EmployeeNotification
{
EmployeeID = oUser.EmployeeID.Value,
Header = "Password Changed Successfully",
Description = $"Your password has been changed successfully. " +
$"If you did not make this change, please contact your administrator immediately.",
Link = "",
CreatedDate = DateTime.Now,
CreatedBy = oUser.ID,
ReferenceID = oUser.ID,
IsRead = 0,
ReadDate = null,
ReadBy = null
};
new EmployeeNotificationService().Insert(tc, notification);
}
#endregion
tc.End();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
//public int Save(TransactionContext tc, User oUser)
//{
// try
// {
// oUser.Password = Ease.Core.Utility.Global.CipherFunctions.Encrypt("CeLiMiTeD.AdMIn", oUser.Password);
// if (oUser.IsNew)
// {
// int id = tc.GenerateID("Users", "UserID");
// base.SetObjectID(oUser, id);
// UserDA.Insert(tc, oUser);
// }
// else
// {
// UserDA.Update(tc, oUser);
// }
// return oUser.ID;
// }
// catch (Exception e)
// {
// #region Handle Exception
// if (tc != null)
// tc.HandleError();
// ExceptionLog.Write(e);
// throw new ServiceException(e.Message, e);
// #endregion
// }
//}
public int Save(TransactionContext tc, User oUser)
{
try
{
oUser.Password = Ease.Core.Utility.Global.CipherFunctions.Encrypt("CeLiMiTeD.AdMIn", oUser.Password);
if (oUser.IsNew)
{
int id = tc.GenerateID("Users", "UserID");
base.SetObjectID(oUser, id);
UserDA.Insert(tc, oUser);
if (oUser.EmployeeID.HasValue)
{
var notification = new EmployeeNotification
{
EmployeeID = oUser.EmployeeID.Value,
Header = "Welcome to the System!",
Description = $"Hi, your account has been created successfully.\n" +
$"Login ID: {oUser.LoginID} | Created: {DateTime.Now:dd MMM yyyy, hh:mm tt}",
Link = "",
CreatedDate = DateTime.Now,
CreatedBy = oUser.CreatedBy,
ReferenceID = oUser.ID,
IsRead = 0,
ReadDate = null,
ReadBy = null
};
new EmployeeNotificationService().Insert(tc, notification);
}
}
else
{
UserDA.Update(tc, oUser);
if (oUser.EmployeeID.HasValue)
{
var notification = new EmployeeNotification
{
EmployeeID = oUser.EmployeeID.Value,
Header = "User Account Updated",
Description = $"Your user account has been updated by the administrator.\n" +
$"Login ID: {oUser.LoginID} | Updated: {DateTime.Now:dd MMM yyyy, hh:mm tt}",
Link = "",
CreatedDate = DateTime.Now,
CreatedBy = oUser.ModifiedBy ?? 0,
ReferenceID = oUser.ID,
IsRead = 0,
ReadDate = null,
ReadBy = null
};
new EmployeeNotificationService().Insert(tc, notification);
}
}
return oUser.ID;
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
2026-11-12 14:58:04 +06:00
// Unique index Unique_EmpNo on dbo.USERS: this employee already has a user.
for (Exception inner = e; inner != null; inner = inner.InnerException)
{
if (inner.Message.IndexOf("Cannot insert duplicate key row", StringComparison.OrdinalIgnoreCase) >= 0
&& inner.Message.IndexOf("Unique_EmpNo", StringComparison.OrdinalIgnoreCase) >= 0)
throw new ServiceException("User already exist in the system", e);
}
throw new ServiceException("Failed to Create/Update user.", e);
2026-11-03 13:06:00 +06:00
#endregion
}
}
public int GetUserId(int employeeid, EnumUserType type)
{
int userId;
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
userId = UserDA.GetUserId(tc, employeeid, type);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return userId;
}
public void Update(User oUser, EnumStatus status)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
UserDA.Update(tc, oUser, status);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
public void Approve(User oUser)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
UserDA.Approve(tc, oUser);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
public void Delete(int id)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin(true);
UserDA.Delete(tc, id);
UserRoleDA.DeleteByUserID(tc, id);
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
public void SaveAllUsers(List<User> oUsers)
{
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
foreach (User usr in oUsers)
{
int id = tc.GenerateID("Users", "UserID");
usr.SetObjectID(id);
UserDA.Insert(tc, usr);
//UserDA.Update(tc, usr, usr.Status);
int i;
//will be working after porting roles
//for (i = 0; i < usr.Roles.Count; i++)
//{
// UserRole orole = usr.Roles[i];
// orole.UserID = usr.ID;
// UserRole.SaveSingleUserRole(orole);
//}
}
tc.End();
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
}
#endregion
public User GetByAdLoginID(string adLoginID)
{
User oUser = null;
List<User> users = new List<User>();
TransactionContext tc = null;
try
{
tc = TransactionContext.Begin();
DataReader oreader = new DataReader(UserDA.GetByEmail(tc, adLoginID));
if (oreader.Read())
{
oUser = this.CreateObject<User>(oreader);
}
oreader.Close();
if (oUser == null)
{
oreader = new DataReader(UserDA.GetUserByEmail(tc, adLoginID));
if (oreader.Read())
{
oUser = this.CreateObject<User>(oreader);
}
oreader.Close();
}
tc.End();
if (oUser != null)
{
UserLogInInfo login = new UserLogInInfo();
login.UserID = oUser.ID;
login.LoginTime = DateTime.Now;
2026-11-12 14:58:04 +06:00
UserLogInInfoService.CaptureRequestInfo(login); // on the request thread, before the save thread
2026-11-03 13:06:00 +06:00
Thread myNewThread = new Thread(() => SaveLoginfo(login));
myNewThread.Start();
}
}
catch (Exception e)
{
#region Handle Exception
if (tc != null)
tc.HandleError();
ExceptionLog.Write(e);
throw new ServiceException(e.Message, e);
#endregion
}
return oUser;
}
}
#endregion
}