using System; using System.Collections.Generic; using System.Configuration; using System.Diagnostics; using System.Linq; using System.Web; using System.Web.Security; using System.IO; using System.Net.Mail; using System.DirectoryServices; using System.DirectoryServices.AccountManagement; namespace Fab2ApprovalSystem.Misc { public static class Functions { /// /// Writes to the Application Event Log and sends an email notification if appropriate /// /// /// public static void WriteEvent(string logtext, System.Diagnostics.EventLogEntryType eventType) { //#if(!DEBUG) EmailNotification en = new EmailNotification(); EventLog ev = new EventLog("Application"); ev.Source = "Fab Approval System"; try { //Write to the Event Log ev.WriteEntry(logtext, eventType); ////Send an email notification if appropriate ////Don't attempt to send an email if the error is pertaining to an email problem if (!logtext.Contains("SendEmailNotification()")) { //Only send email notifications for Error and Warning level events if (eventType == System.Diagnostics.EventLogEntryType.Error) en.SendNotificationEmailToAdmin(ev.Source + " - Error Notification", logtext, MailPriority.High); //else if (eventType == System.Diagnostics.EventLogEntryType.Warning) // SendEmailNotification(ErrorRecipient(), ev.Source + " Warning Event Logged", logtext, NORMAL_PRI); } } catch { //throw; } finally { ev = null; } //#endif } /// /// Returns the FTP Server Name /// /// public static string FTPServer() { ConfigurationManager.RefreshSection("appSettings"); return ConfigurationManager.AppSettings["FTP Server"]; } /// /// Returns the FTP User Name /// /// public static string FTPUser() { ConfigurationManager.RefreshSection("appSettings"); return ConfigurationManager.AppSettings["FTP User"]; } /// /// Returns the FTP Password /// /// public static string FTPPassword() { ConfigurationManager.RefreshSection("appSettings"); return ConfigurationManager.AppSettings["FTP Password"]; } /// /// /// /// public static string GetAttachmentFolder() { ConfigurationManager.RefreshSection("appSettings"); if (GlobalVars.DBConnection.ToUpper() == "TEST" || GlobalVars.DBConnection.ToUpper() == "QUALITY") { return ConfigurationManager.AppSettings["AttachmentFolderTest"]; } else return ConfigurationManager.AppSettings["AttachmentFolder"]; } /// /// /// /// /// public static void CopyAttachments(int oldECNNumber, int newECNNumber) { // The Name of the Upload component is "files" string oldFolderPath = Functions.GetAttachmentFolder() + "ECN\\" + oldECNNumber.ToString(); string newFolderPath = Functions.GetAttachmentFolder() + "ECN\\" + newECNNumber.ToString() ; DirectoryInfo newdi = new DirectoryInfo(newFolderPath); if (!newdi.Exists) newdi.Create(); FileInfo[] existingFiles = new DirectoryInfo(oldFolderPath ).GetFiles(); foreach (FileInfo file in existingFiles) { if (!file.Name.Contains("ECNApprovalLog_" + oldECNNumber.ToString()) && !file.Name.Contains("ECNForm_" + oldECNNumber.ToString())) //var fileName = Path.GetFileName(file.FullName); file.CopyTo(Path.Combine(newFolderPath, file.Name)); } } /// /// /// /// /// public static bool CheckITARAccess(string userID) { MembershipProvider domainProvider = Membership.Providers["ADMembershipProvider"]; MembershipUser mu = domainProvider.GetUser(userID, false); if (mu == null) return false; else return true; } /// /// /// /// /// public static bool NA_HasITARAccess(string userID, string pwd) { string ECDomain = ConfigurationManager.AppSettings["ECDomain"]; string ECADGroup = ConfigurationManager.AppSettings["ECADGroup"]; string naContainer = ConfigurationManager.AppSettings["NAContainer"]; string naDomain = ConfigurationManager.AppSettings["NADomain"]; //WriteEvent("NA - Before PrincipalContext for EC for " + userID, System.Diagnostics.EventLogEntryType.Information); PrincipalContext contextGroup = new PrincipalContext(ContextType.Domain, ECDomain); //WriteEvent("NA - After PrincipalContext for EC for " + userID, System.Diagnostics.EventLogEntryType.Information); //WriteEvent("NA - Before PrincipalContext for NA for " + userID, System.Diagnostics.EventLogEntryType.Information); PrincipalContext contextUser = new PrincipalContext(ContextType.Domain, naDomain, naContainer, ContextOptions.Negotiate, userID, pwd); //WriteEvent("NA - After PrincipalContext for NA for " + userID, System.Diagnostics.EventLogEntryType.Information); //WriteEvent("NA - Before check user in EC group for " + userID, System.Diagnostics.EventLogEntryType.Information); GroupPrincipal gp = GroupPrincipal.FindByIdentity(contextGroup, ECADGroup); //WriteEvent("NA - After check user in EC group for " + userID, System.Diagnostics.EventLogEntryType.Information); //WriteEvent("NA - Before check user in NA group for " + userID, System.Diagnostics.EventLogEntryType.Information); UserPrincipal up = UserPrincipal.FindByIdentity(contextUser, userID); //WriteEvent("NA - After check user in NA group for " + userID, System.Diagnostics.EventLogEntryType.Information); if (null == up) { //WriteEvent("NA - User not in NA for " + userID, System.Diagnostics.EventLogEntryType.Information); return false; } else { //WriteEvent("NA - Member of EC group is " + up.IsMemberOf(gp).ToString() + " for " + userID, System.Diagnostics.EventLogEntryType.Information); return up.IsMemberOf(gp); } } //public static bool NA_GetUsers() //{ // //string ECDomain = ConfigurationManager.AppSettings["ECDomain"]; // //string ECADGroup = ConfigurationManager.AppSettings["ECADGroup"]; // string naContainer = ConfigurationManager.AppSettings["NAContainer"]; // string naDomain = ConfigurationManager.AppSettings["NADomain"]; // //WriteEvent("NA - Before PrincipalContext for EC for " + userID, System.Diagnostics.EventLogEntryType.Information); // //PrincipalContext contextGroup = new PrincipalContext(ContextType.Domain, ECDomain); // //WriteEvent("NA - After PrincipalContext for EC for " + userID, System.Diagnostics.EventLogEntryType.Information); // //WriteEvent("NA - Before PrincipalContext for NA for " + userID, System.Diagnostics.EventLogEntryType.Information); // PrincipalContext contextUser = new PrincipalContext(ContextType.Domain, // naDomain, // naContainer, // ContextOptions.Negotiate, userID, pwd); // //WriteEvent("NA - After PrincipalContext for NA for " + userID, System.Diagnostics.EventLogEntryType.Information); // //WriteEvent("NA - Before check user in EC group for " + userID, System.Diagnostics.EventLogEntryType.Information); // GroupPrincipal gp = GroupPrincipal.FindByIdentity(contextGroup, ECADGroup); // //WriteEvent("NA - After check user in EC group for " + userID, System.Diagnostics.EventLogEntryType.Information); // //WriteEvent("NA - Before check user in NA group for " + userID, System.Diagnostics.EventLogEntryType.Information); // UserPrincipal up = UserPrincipal.FindByIdentity(contextUser, userID); // //WriteEvent("NA - After check user in NA group for " + userID, System.Diagnostics.EventLogEntryType.Information); // if (null == up) // { // //WriteEvent("NA - User not in NA for " + userID, System.Diagnostics.EventLogEntryType.Information); // return false; // } // else // { // //WriteEvent("NA - Member of EC group is " + up.IsMemberOf(gp).ToString() + " for " + userID, System.Diagnostics.EventLogEntryType.Information); // return up.IsMemberOf(gp); // } //} public static bool IFX_HasITARAccess(string userID, string pwd) { string ECDomain = ConfigurationManager.AppSettings["ECDomain"]; string ECADGroup = ConfigurationManager.AppSettings["ECADGroup"]; string ifxcontainer = ConfigurationManager.AppSettings["IFXContainer"]; string ifxdomain = ConfigurationManager.AppSettings["IFXDomain"]; //WriteEvent("IFX - Before PrincipalContext for EC for user " + userID, System.Diagnostics.EventLogEntryType.Information); PrincipalContext contextGroup = new PrincipalContext(ContextType.Domain, ECDomain); //WriteEvent("IFX - After PrincipalContext for EC for user " + userID, System.Diagnostics.EventLogEntryType.Information); //WriteEvent("IFX - Before PrincipalContext for IFX for user " + userID, System.Diagnostics.EventLogEntryType.Information); PrincipalContext contextUser = new PrincipalContext(ContextType.Domain, ifxdomain, ifxcontainer, ContextOptions.Negotiate, userID, pwd); //WriteEvent("IFX - After PrincipalContext for IFX for user " + userID, System.Diagnostics.EventLogEntryType.Information); //WriteEvent("IFX - Before check user in EC group for " + userID, System.Diagnostics.EventLogEntryType.Information); GroupPrincipal gp = GroupPrincipal.FindByIdentity(contextGroup, ECADGroup); //WriteEvent("IFX - After check user in EC group for " + userID, System.Diagnostics.EventLogEntryType.Information); //WriteEvent("IFX - Before check user in IFX for " + userID, System.Diagnostics.EventLogEntryType.Information); UserPrincipal up = UserPrincipal.FindByIdentity(contextUser, userID); //WriteEvent("IFX - After check user in IFX for " + userID, System.Diagnostics.EventLogEntryType.Information); if (null == up) { //WriteEvent("IFX - not a member of IFX for " + userID, System.Diagnostics.EventLogEntryType.Information); return false; } else { //WriteEvent("IFX - Member of EC group is " + up.IsMemberOf(gp).ToString() + " for " + userID, System.Diagnostics.EventLogEntryType.Information); return up.IsMemberOf(gp); } } /// /// /// /// /// /// public static bool NA_ADAuthenticate(string userID, string pwd) { string naContainer = ConfigurationManager.AppSettings["NAContainer"]; string naDomain = ConfigurationManager.AppSettings["NADomain"]; try { PrincipalContext contextUser = new PrincipalContext(ContextType.Domain, naDomain, naContainer, ContextOptions.Negotiate, userID, pwd); UserPrincipal up = UserPrincipal.FindByIdentity(contextUser, userID); if (null == up) return false; else return true; } catch { return false; } } /// /// /// /// /// /// public static bool IFX_ADAuthenticate(string userID, string pwd) { string container = ConfigurationManager.AppSettings["IFXContainer"]; string domain = ConfigurationManager.AppSettings["IFXDomain"]; try { PrincipalContext contextUser = new PrincipalContext(ContextType.Domain, domain, container, ContextOptions.Negotiate, userID, pwd); UserPrincipal up = UserPrincipal.FindByIdentity(contextUser, userID); if (null == up) return false; else return true; } catch { return false; } } public static string FTPSPNBatch() { ConfigurationManager.RefreshSection("appSettings"); return ConfigurationManager.AppSettings["FTPSPNBatchFileName"]; } /// /// /// /// public static string FTPSPNBatch_Test() { ConfigurationManager.RefreshSection("appSettings"); return ConfigurationManager.AppSettings["FTPSPNBatchFileName_Test"]; } /// /// /// /// /// public static string CASectionMapper(GlobalVars.CASection casection) { switch (casection) { case GlobalVars.CASection.D1: return "D1"; case GlobalVars.CASection.D2: return "D2"; case GlobalVars.CASection.D3: return "D3"; case GlobalVars.CASection.D4: return "D4"; case GlobalVars.CASection.D5: return "D5"; case GlobalVars.CASection.D6: return "D6"; case GlobalVars.CASection.D7: return "D7"; case GlobalVars.CASection.D8: return "D8"; case GlobalVars.CASection.CF: // CA Findings return "CF"; } return ""; } public static string DocumentTypeMapper(GlobalVars.DocumentType docType) { switch (docType) { case GlobalVars.DocumentType.Audit: return "Audit"; case GlobalVars.DocumentType.ChangeControl: return "ChangeControl"; case GlobalVars.DocumentType.CorrectiveAction: return "CorrectiveAction"; case GlobalVars.DocumentType.ECN: return "ECN"; case GlobalVars.DocumentType.EECN: return "EECN"; case GlobalVars.DocumentType.LotDisposition: return "LotDisposition"; case GlobalVars.DocumentType.MRB: return "MRB"; case GlobalVars.DocumentType.TECNCancelledExpired: return "TECNCancelledExpired"; } return ""; } /// /// Converts the CA No to the C00000 format /// /// /// public static string ReturnCANoStringFormat(int caNo) { string caNoString = ""; if(caNo == 0) return ""; caNoString = "C" + caNo.ToString().PadLeft(5, '0'); return caNoString; } public static string ReturnAuditNoStringFormat(int auditNo) { string auditNoString = ""; if (auditNo == 0) return ""; auditNoString = "A" + auditNo.ToString().PadLeft(5, '0'); return auditNoString; } public static string ReturnPartsRequestNoStringFormat(int PRNumber) { if (PRNumber == 0) return ""; return String.Format("PR{0:000000}", PRNumber); } } }