| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- using Dapper;
- using LAPS_XMLQC_Service.Models;
- using LAPS_XMLQC_Service.Models.Configuration;
- using Microsoft.Extensions.Configuration;
- using Newtonsoft.Json;
- using Npgsql;
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.IO;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Security.Principal;
- namespace LAPS_XMLQC_Service.App_Data
- {
- public class CommonRepository
- {
- private readonly string _connectionString;
- public CommonRepository(IConfiguration configuration)
- {
- _connectionString = configuration.GetConnectionString("DbConnection")
- ?? throw new ArgumentNullException("Connection string is not configured.");
- }
-
- public IEnumerable<string> ExecuteSP_ReturnListWithoutMode(string spName, string inputJsonString)
- {
- return ExecuteStoredProcedure<string>(spName, new { inputjsonstr = inputJsonString });
- }
- public IEnumerable<string> ExecuteSP_ReturnList(string spName, string inputJsonString, string mode)
- {
- var parameters = new
- {
- inputjsonstr = inputJsonString,
- mode_p = mode
- };
- return ExecuteStoredProcedure<string>(spName, parameters);
- }
- public IEnumerable<string> FindByID(int id)
- {
- var input = new
- {
- userid_p = id
- };
- return ExecuteSP_ReturnList("spusermasterselect", JsonConvert.SerializeObject(input), "getbyid");
- }
- public string GetServerPath(string transactionId, string mode, bool createDir = false)
- {
- string osType = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "Windows" : "Linux";
- var parameters = new
- {
- p_tranid = Convert.ToInt32(transactionId),
- p_lotid = (int?)null,
- p_option = mode,
- p_ostype = osType
- };
- try
- {
- var serverPaths = ExecuteStoredProcedure<servermasterModel>("public.spusrserverpathinfo", parameters);
- var serverPath = FormatPath(serverPaths.FirstOrDefault().serverpath);
- if (createDir)
- {
- Directory.CreateDirectory(Path.Combine(serverPath, "IN"));
- Directory.CreateDirectory(Path.Combine(serverPath, "OUT"));
- }
- return serverPath;
- }
- catch (Exception ex)
- {
- LogError("Error retrieving server path: " + ex.Message);
- return string.Empty;
- }
- }
-
- public lotmasterModel ExecuteSP_ReturnListForLot(string spName, string inputJsonString, string mode)
- {
- string osType = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "Windows" : "Linux";
- var inputData = JsonConvert.DeserializeObject<lotmasterModel>(inputJsonString);
- var parameters = new DynamicParameters();
- parameters.Add("p_tranid", dbType: DbType.Int32, value: inputData.transactionid, direction: ParameterDirection.Input);
- parameters.Add("p_lotid", dbType: DbType.Int32, value: inputData.lotid, direction: ParameterDirection.Input);
- parameters.Add("p_option", dbType: DbType.String, value: inputData.option, direction: ParameterDirection.Input);
- parameters.Add("p_ostype", dbType: DbType.String, value: osType, direction: ParameterDirection.Input);
- return ExecuteStoredProcedure<lotmasterModel>(spName, parameters).FirstOrDefault();
- }
- private string FormatPath(string inputPath)
- {
- try
- {
- inputPath = inputPath.Replace("/", "\\").Replace("\\\\", "");
- return string.Concat("\\\\", inputPath);
- }
- catch (Exception ex)
- {
- LogError("Error formatting path: " + ex.Message);
- return string.Empty;
- }
- }
- private void LogError(string message)
- {
- var logData = new
- {
- errormessage = message,
- createdby = 0,
- controller = "UploaderApi: Downloader"
- };
- AddLog(logData, "errorlog");
- }
- public int AddLog(object data, string masterName)
- {
- var parameters = new
- {
- inputjsonstr = JsonConvert.SerializeObject(data),
- mode_p = "insert"
- };
- return ExecuteStoredProcedure<int>($"public.sp{masterName}", parameters).First();
- }
- private IEnumerable<T> ExecuteStoredProcedure<T>(string spName, object parameters)
- {
- using var dbConnection = new NpgsqlConnection(_connectionString);
- dbConnection.Open();
- return dbConnection.Query<T>(spName, parameters, commandType: CommandType.StoredProcedure);
- }
- public class Impartunate
- {
- #region Impersonating User
- #region Impersonation
- static IntPtr tokenHandle;
- // static WindowsImpersonationContext impersonatedUser;
- private string impersUsername { get; set; }
- private string impersPwd { get; set; }
- private string impersDomain { get; set; }
- static WindowsIdentity newId;
- #endregion
- public Impartunate(string username, string password, string domain)
- {
- impersUsername = username;
- impersPwd = password;
- impersDomain = domain;
- }
- // public static WindowsImpersonationContext oImpersonatedUser;
- [DllImport("advapi32.dll", SetLastError = true)]
- private static extern bool LogonUser(string sUsername, string sDomain, string sPassword, int iLogonType, int iLogonProvider, ref IntPtr oToken);
- [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
- private static extern bool CloseHandle(IntPtr oHandle);
- //public bool AllowAccesstoServer()
- public WindowsIdentity AllowAccesstoServer()
- {
- try
- {
- tokenHandle = IntPtr.Zero;
- bool Result = LogonUser(impersUsername, impersDomain, impersPwd, 2, 0, ref tokenHandle);
- if (Result)
- {
- newId = new WindowsIdentity(tokenHandle);
- //impersonatedUser = newId.Impersonate();
- //return true;
- return newId;
- }
- else
- {
- //return false;
- return null;
- }
- }
- catch (Exception ex)
- {
- //return false;
- return null;
- }
- }
- public void RemoveServerAccess()
- {
- try
- {
- //if (impersonatedUser != null)
- //{
- // impersonatedUser.Undo();
- //}
- if (tokenHandle != IntPtr.Zero)
- {
- CloseHandle(tokenHandle);
- }
- }
- catch (Exception ex) { }
- }
- private void ImpersonateUser()
- {
- System.IO.FileInfo localFileLastModified;
- System.IO.FileInfo serverFileLastModified;
- if (AllowAccesstoServer() == null)
- {
- RemoveServerAccess();
- }
- }
- #endregion
- }
- }
- }
|