using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using Ease.CoreV35; using Ease.CoreV35.Model; using Ease.CoreV35.Caching; using System.Data.Linq.Mapping; using System.Data; using System.Web; namespace Payroll.BO { #region User [Serializable] public class User : BasicBaseObject { #region Cache Store private static Cache _cache = new Cache(typeof(User)); private static User _currentUser; #endregion #region Constructor public User() { _PayrollTypes = null; _UserAccessTypes = null; _Roles = null; _Permission = null; _loginID = string.Empty; _name = string.Empty; _parentID = null; _password = string.Empty; _passwordHints = string.Empty; _sISU = false; _logInpayrollTypeID = null; _logInMode = EnumUserLogInMode.SuperUser; _isForceChangePass = false; _authorizedBy = null; _authorizedDate = DateTime.MinValue; _LockedWorkStation = string.Empty; _GroupNumber = 1; _TempStatus = EnumStatus.Archieved; _ComputerName = ""; _ApprovedComputerName = ""; } #endregion #region Properties private string _ApprovedComputerName; public string ApprovedComputerName { get { return _ApprovedComputerName; } set { base.OnPropertyChange("ApprovedComputerName", _ApprovedComputerName, value); _ApprovedComputerName = value; } } private string _ComputerName; public string ComputerName { get { return _ComputerName; } set { base.OnPropertyChange("ComputerName", _ComputerName, value); _ComputerName = value; } } #region User Type : int private EnumSystemType _userType; public EnumSystemType UserType { get { return _userType; } set { _userType = value; } } #endregion private EnumStatus _TempStatus; public EnumStatus TempStatus { get { return _TempStatus; } set { _TempStatus = value; } } #region _isForceChangePass : bool private bool _isForceChangePass; public bool IsForceChangePass { get { return _isForceChangePass; } set { _isForceChangePass = value; } } #endregion #region loginID : ID private string _loginID; public string LoginID { get { return _loginID; } set { base.OnPropertyChange("loginID", _loginID, value); _loginID = value; } } #endregion #region name : string private string _name; public string Name { get { return _name; } set { base.OnPropertyChange("name", _name, value); _name = value; } } #endregion #region parentID : ID private ID _parentID; public ID ParentID { get { return _parentID; } set { base.OnPropertyChange("parentID", _parentID, value); _parentID = value; } } #endregion #region password : string private string _password; public string Password { get { return _password; } set { base.OnPropertyChange("password", _password, value); _password = value; } } #endregion #region passwordHints : string private string _passwordHints; public string PasswordHints { get { return _passwordHints; } set { base.OnPropertyChange("passwordHints", _passwordHints, value); _passwordHints = value; } } #endregion #region SISU : string private bool _sISU; public bool SISU { get { return _sISU; } set { base.OnPropertyChange("SISU", _sISU, value); _sISU = value; } } #endregion #region Log-In PayrollTypeID : int private ID _logInpayrollTypeID; public ID LogInPayrollTypeID { get { return _logInpayrollTypeID; } set { _logInpayrollTypeID = value; } } #endregion #region User LogIn Mode : int private EnumUserLogInMode _logInMode; public EnumUserLogInMode LogInMode { get { return _logInMode; } set { _logInMode = value; } } #endregion #region AuthorizedBy : ID private ID _authorizedBy; public ID AuthorizedBy { get { return _authorizedBy; } set { base.OnPropertyChange("AuthorizedBy", _authorizedBy, value); _authorizedBy = value; } } #endregion #region AuthorizedDate : DateTime private DateTime? _authorizedDate; public DateTime? AuthorizedDate { get { return _authorizedDate; } set { base.OnPropertyChange("AuthorizedDate", _authorizedDate, value); _authorizedDate = value; } } #endregion #region passwordHints : string private string _Reason; public string Reason { get { return _Reason; } set { base.OnPropertyChange("Reason", _Reason, value); _Reason = value; } } #endregion #endregion #region CurrentUser : User public static Payroll.BO.User CurrentUser { get { if (_currentUser != null && _currentUser.IsWebLogin == true) { return _currentUser.WebLoginUser; } else return _currentUser; } set { _currentUser = value; } } public Payroll.BO.User WebLoginUser { get { if (HttpContext.Current.Session.Contents["LoinUser"] == null) return null; else return (Payroll.BO.User)HttpContext.Current.Session.Contents["LoinUser"]; } set { HttpContext.Current.Session.Contents["LoinUser"] = value; } } private bool _isWebLogin = false; public bool IsWebLogin { get { return _isWebLogin; } set { _isWebLogin = value; } } public static void SetCurrentUser(User ouser) { //_currentUser.Roles = null; //_currentUser = null; _currentUser = ouser; } public static void LogOut() { _currentUser = null; SystemInformation.CurrentSysInfo = null; } #endregion #region MenuPermission private Hashtable _Permission = null; public Hashtable MenuPermission { get { if (_Permission == null && this.ID != null && !this.ID.IsUnassigned) { _Permission = new Hashtable(); if (Roles != null) { foreach (UserRole role in Roles) { if (role.Status == EnumStatus.Active) { foreach (RolePermission key in role.RolePermision) { if (_Permission[key.MenuKey] == null) { _Permission.Add(key.MenuKey, key.MenuKey); } } } } } } return _Permission; } } #endregion #region UserRole ObjectsTemplate _Roles = null; public ObjectsTemplate Roles { get { if (_Roles == null && this.ID != null && !this.ID.IsUnassigned) { _Roles = UserRole.GetByUserID(this.ID); } return _Roles; } set { _Roles = value; } } #endregion #region UserRole ObjectsTemplate _UserAccessTypes = null; public ObjectsTemplate UserAccessTypes { get { if ((_UserAccessTypes == null || _UserAccessTypes.Count <= 0) && this.ID != null && !this.ID.IsUnassigned) { _UserAccessTypes = UserAccessType.Get(this.ID.Integer); } return _UserAccessTypes; } } #endregion #region PayrollTypes ObjectsTemplate _PayrollTypes = null; public ObjectsTemplate PayrollTypes { get { if (_PayrollTypes == null && this.ID != null && !this.ID.IsUnassigned) { if (this.UserAccessTypes != null) { foreach (UserAccessType accessType in this.UserAccessTypes) { _PayrollTypes.Add(accessType.PayrollTypeObj); } } } return _PayrollTypes; } } #endregion #region _GroupNumber : int private int _GroupNumber; public int GroupNumber { get { return _GroupNumber; } set { _GroupNumber = value; } } #endregion private string _LockedWorkStation; public string LockedWorkStation { get { return _LockedWorkStation; } set { base.OnPropertyChange("LockedWorkStation", _LockedWorkStation, value); _LockedWorkStation = value; } } #region LogIN public static void LogIn(string logINID, string password, EnumSystemType eSysType) { _currentUser = null; try { User oUser = User.GetByLogINID(logINID, eSysType); if (oUser != null) { string s = Ease.CoreV35.Utility.Global.CipherFunctions.Encrypt("CeLiMiTeD.AdMIn", password); if (s.Trim() == oUser.Password.Trim()) { if (oUser.Status != EnumStatus.Active) { throw new Exception("User is inactive."); } _currentUser = oUser; //_currentUser.WebLoginUser = null; } else { throw new Exception("Invalid password."); } } else { throw new Exception("Invalid LogIn ID."); } } catch (Exception ex) { throw new Exception(ex.Message); } } public static void ADLogIn(string logINID, EnumSystemType eSysType) { _currentUser = null; try { User oUser = User.GetByLogINID(logINID, eSysType); if (oUser != null) { //string s = Ease.CoreV35.Utility.Global.CipherFunctions.Encrypt("CeLiMiTeD.AdMIn", password); //if (s.Trim() == oUser.Password.Trim()) //{ if (oUser.Status != EnumStatus.Active) { throw new Exception("User is inactive."); } _currentUser = oUser; //_currentUser.WebLoginUser = null; //} //else //{ // throw new Exception("Invalid password."); //} } else { throw new Exception("Invalid LogIn ID."); } } catch (Exception ex) { throw new Exception(ex.Message); } } public static void LogIn(string logINID, string password, bool isWeblogin, EnumSystemType eSysType) { _currentUser = null; try { User oUser = User.GetByLogINID(logINID, eSysType); if (oUser != null) { string s = Ease.CoreV35.Utility.Global.CipherFunctions.Encrypt("CeLiMiTeD.AdMIn", password); if (s.Trim() == oUser.Password.Trim()) { if (oUser.Status != EnumStatus.Active) { throw new Exception("User is inactive."); } _currentUser = oUser; _currentUser.IsWebLogin = isWeblogin; _currentUser.WebLoginUser = isWeblogin ? _currentUser : null; } else { throw new Exception("Invalid password."); } } else { throw new Exception("Invalid LogIn ID."); } } catch (Exception ex) { throw new Exception(ex.Message); } } #endregion #region Service Factory IUserService : IUserService internal static IUserService Service { get { return Services.Factory.CreateService(typeof(IUserService)); } } #endregion #region Functions public static User Get(ID nID) { User oUser = null; #region Cache Header oUser = (User)_cache["Get", nID]; if (oUser != null) return oUser; #endregion oUser = User.Service.Get(nID); #region Cache Footer _cache.Add(oUser, "Get", nID); #endregion return oUser; } public static User Get(string sLogInID, EnumSystemType eSysType) { User oUser = null; #region Cache Header oUser = (User)_cache["Get", sLogInID]; if (oUser != null) return oUser; #endregion oUser = User.Service.GetByLogINID(sLogInID, eSysType); #region Cache Footer _cache.Add(oUser, "Get", sLogInID); #endregion return oUser; } public static User GetByLogINID(string sLogINID, EnumSystemType eSysType) { User oUser = null; #region Cache Header oUser = (User)_cache["GetByLogINID", sLogINID]; if (oUser != null) return oUser; #endregion oUser = User.Service.GetByLogINID(sLogINID, eSysType); #region Cache Footer _cache.Add(oUser, "GetByLogINID", sLogINID); #endregion return oUser; } public static ObjectsTemplate Get(EnumSystemType type) { #region Cache Header ObjectsTemplate users = _cache["Get"] as ObjectsTemplate; if (users != null) return users; #endregion try { users = Service.Get(type); } catch (ServiceException e) { throw new Exception(e.Message, e); } #region Cache Footer _cache.Add(users, "Get"); #endregion return users; } public static ObjectsTemplate GetPayrollTypes(User user) { ObjectsTemplate payrollTypes = null; if (user != null) { if (user.UserAccessTypes != null && !user.SISU) { payrollTypes = new ObjectsTemplate(); foreach (UserAccessType accessType in user.UserAccessTypes) { if (!payrollTypes.Contains(ID.FromInteger(accessType.PayrollTypeID))) { payrollTypes.Add(accessType.PayrollTypeObj); } } } } return payrollTypes; } public ID Save() { this.SetAuditTrailProperties(); return User.Service.Save(this); } public void Update() { if (this.Status == EnumStatus.Active) { User.Service.Update(this, this.Status); } else { User.Service.Update(this, this.Status); } } public void Delete() { User.Service.Delete(ID); } #endregion public static User GetById(int userID) { return User.Service.GetById(userID); } public static void SaveAllUsers(ObjectsTemplate oUsers) { User.Service.SaveAllUsers(oUsers); } public static DataSet GetUsers(EnumSystemType type, DateTime fromDate, DateTime ToDate) { DataSet ds = null; try { ds = Service.GetUsers(type, fromDate, ToDate); } catch (Exception e) { throw new Exception(e.Message, e); } return ds; } } #endregion #region IUser Service public interface IUserService { User Get(ID id); User Get(string sName); User GetByLogINID(string sLogINID, EnumSystemType eSysType); User GetById(int userID); ObjectsTemplate Get(EnumSystemType type); ID Save(User item); void Delete(ID id); void Update(User user, EnumStatus status); void SaveAllUsers(ObjectsTemplate oUsers); DataSet GetUsers(EnumSystemType type, DateTime fromDate, DateTime ToDate); } #endregion }