CommonRepository.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. using Dapper;
  2. using LAPS_XMLQC_Service.Models;
  3. using LAPS_XMLQC_Service.Models.Configuration;
  4. using Microsoft.Extensions.Configuration;
  5. using Newtonsoft.Json;
  6. using Npgsql;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Data;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Runtime.InteropServices;
  13. using System.Security.Principal;
  14. namespace LAPS_XMLQC_Service.App_Data
  15. {
  16. public class CommonRepository
  17. {
  18. private readonly string _connectionString;
  19. public CommonRepository(IConfiguration configuration)
  20. {
  21. _connectionString = configuration.GetConnectionString("DbConnection")
  22. ?? throw new ArgumentNullException("Connection string is not configured.");
  23. }
  24. public IEnumerable<string> ExecuteSP_ReturnListWithoutMode(string spName, string inputJsonString)
  25. {
  26. return ExecuteStoredProcedure<string>(spName, new { inputjsonstr = inputJsonString });
  27. }
  28. public IEnumerable<string> ExecuteSP_ReturnList(string spName, string inputJsonString, string mode)
  29. {
  30. var parameters = new
  31. {
  32. inputjsonstr = inputJsonString,
  33. mode_p = mode
  34. };
  35. return ExecuteStoredProcedure<string>(spName, parameters);
  36. }
  37. public IEnumerable<string> FindByID(int id)
  38. {
  39. var input = new
  40. {
  41. userid_p = id
  42. };
  43. return ExecuteSP_ReturnList("spusermasterselect", JsonConvert.SerializeObject(input), "getbyid");
  44. }
  45. public string GetServerPath(string transactionId, string mode, bool createDir = false)
  46. {
  47. string osType = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "Windows" : "Linux";
  48. var parameters = new
  49. {
  50. p_tranid = Convert.ToInt32(transactionId),
  51. p_lotid = (int?)null,
  52. p_option = mode,
  53. p_ostype = osType
  54. };
  55. try
  56. {
  57. var serverPaths = ExecuteStoredProcedure<servermasterModel>("public.spusrserverpathinfo", parameters);
  58. var serverPath = FormatPath(serverPaths.FirstOrDefault().serverpath);
  59. if (createDir)
  60. {
  61. Directory.CreateDirectory(Path.Combine(serverPath, "IN"));
  62. Directory.CreateDirectory(Path.Combine(serverPath, "OUT"));
  63. }
  64. return serverPath;
  65. }
  66. catch (Exception ex)
  67. {
  68. LogError("Error retrieving server path: " + ex.Message);
  69. return string.Empty;
  70. }
  71. }
  72. public lotmasterModel ExecuteSP_ReturnListForLot(string spName, string inputJsonString, string mode)
  73. {
  74. string osType = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "Windows" : "Linux";
  75. var inputData = JsonConvert.DeserializeObject<lotmasterModel>(inputJsonString);
  76. var parameters = new DynamicParameters();
  77. parameters.Add("p_tranid", dbType: DbType.Int32, value: inputData.transactionid, direction: ParameterDirection.Input);
  78. parameters.Add("p_lotid", dbType: DbType.Int32, value: inputData.lotid, direction: ParameterDirection.Input);
  79. parameters.Add("p_option", dbType: DbType.String, value: inputData.option, direction: ParameterDirection.Input);
  80. parameters.Add("p_ostype", dbType: DbType.String, value: osType, direction: ParameterDirection.Input);
  81. return ExecuteStoredProcedure<lotmasterModel>(spName, parameters).FirstOrDefault();
  82. }
  83. private string FormatPath(string inputPath)
  84. {
  85. try
  86. {
  87. inputPath = inputPath.Replace("/", "\\").Replace("\\\\", "");
  88. return string.Concat("\\\\", inputPath);
  89. }
  90. catch (Exception ex)
  91. {
  92. LogError("Error formatting path: " + ex.Message);
  93. return string.Empty;
  94. }
  95. }
  96. private void LogError(string message)
  97. {
  98. var logData = new
  99. {
  100. errormessage = message,
  101. createdby = 0,
  102. controller = "UploaderApi: Downloader"
  103. };
  104. AddLog(logData, "errorlog");
  105. }
  106. public int AddLog(object data, string masterName)
  107. {
  108. var parameters = new
  109. {
  110. inputjsonstr = JsonConvert.SerializeObject(data),
  111. mode_p = "insert"
  112. };
  113. return ExecuteStoredProcedure<int>($"public.sp{masterName}", parameters).First();
  114. }
  115. private IEnumerable<T> ExecuteStoredProcedure<T>(string spName, object parameters)
  116. {
  117. using var dbConnection = new NpgsqlConnection(_connectionString);
  118. dbConnection.Open();
  119. return dbConnection.Query<T>(spName, parameters, commandType: CommandType.StoredProcedure);
  120. }
  121. public class Impartunate
  122. {
  123. #region Impersonating User
  124. #region Impersonation
  125. static IntPtr tokenHandle;
  126. // static WindowsImpersonationContext impersonatedUser;
  127. private string impersUsername { get; set; }
  128. private string impersPwd { get; set; }
  129. private string impersDomain { get; set; }
  130. static WindowsIdentity newId;
  131. #endregion
  132. public Impartunate(string username, string password, string domain)
  133. {
  134. impersUsername = username;
  135. impersPwd = password;
  136. impersDomain = domain;
  137. }
  138. // public static WindowsImpersonationContext oImpersonatedUser;
  139. [DllImport("advapi32.dll", SetLastError = true)]
  140. private static extern bool LogonUser(string sUsername, string sDomain, string sPassword, int iLogonType, int iLogonProvider, ref IntPtr oToken);
  141. [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  142. private static extern bool CloseHandle(IntPtr oHandle);
  143. //public bool AllowAccesstoServer()
  144. public WindowsIdentity AllowAccesstoServer()
  145. {
  146. try
  147. {
  148. tokenHandle = IntPtr.Zero;
  149. bool Result = LogonUser(impersUsername, impersDomain, impersPwd, 2, 0, ref tokenHandle);
  150. if (Result)
  151. {
  152. newId = new WindowsIdentity(tokenHandle);
  153. //impersonatedUser = newId.Impersonate();
  154. //return true;
  155. return newId;
  156. }
  157. else
  158. {
  159. //return false;
  160. return null;
  161. }
  162. }
  163. catch (Exception ex)
  164. {
  165. //return false;
  166. return null;
  167. }
  168. }
  169. public void RemoveServerAccess()
  170. {
  171. try
  172. {
  173. //if (impersonatedUser != null)
  174. //{
  175. // impersonatedUser.Undo();
  176. //}
  177. if (tokenHandle != IntPtr.Zero)
  178. {
  179. CloseHandle(tokenHandle);
  180. }
  181. }
  182. catch (Exception ex) { }
  183. }
  184. private void ImpersonateUser()
  185. {
  186. System.IO.FileInfo localFileLastModified;
  187. System.IO.FileInfo serverFileLastModified;
  188. if (AllowAccesstoServer() == null)
  189. {
  190. RemoveServerAccess();
  191. }
  192. }
  193. #endregion
  194. }
  195. }
  196. }