EchoTex_Payroll/HRM.DA/Service/Users/UserService.cs

889 lines
28 KiB
C#
Raw Normal View History

2024-10-14 10:01:49 +06:00
using HRM.BO;
using Ease.Core;
using Ease.Core.DataAccess;
using Ease.Core.DataAccess.SQL;
using Ease.Core.Model;
using Ease.Core.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Net;
using System.Threading;
using Azure.Core;
using Microsoft.AspNetCore.Http;
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);
// 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> 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 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;
}
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;
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;
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)
{
oTran.PCNumber = Environment.MachineName + " ," +
System.Net.Dns.GetHostName() + " ," +
Environment.GetEnvironmentVariable("COMPUTERNAME");
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);
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);
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 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
}
#endregion
}