using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Security.AccessControl; using System.Security.Principal; using System.Text; using System.Threading.Tasks; using C4IT.Logging; using static C4IT.Logging.cLogManager; using System.Reflection; namespace C4IT_IAM_Engine { public class DataArea { public List IAM_Folders; public string rootUID; public static string GetRelativePath(string childFolder, string rootFolder) { LogMethodBegin(MethodBase.GetCurrentMethod()); try { // Folders must end in a slash if (!childFolder.EndsWith(Path.DirectorySeparatorChar.ToString())) { childFolder += Path.DirectorySeparatorChar; } Uri childUri = new Uri(childFolder); // Folders must end in a slash if (!rootFolder.EndsWith(Path.DirectorySeparatorChar.ToString())) { rootFolder += Path.DirectorySeparatorChar; } Uri folderUri = new Uri(rootFolder); return Uri.UnescapeDataString(folderUri.MakeRelativeUri(childUri).ToString().Replace('/', Path.DirectorySeparatorChar)); } catch (Exception E) { cLogManager.DefaultLogger.LogException(E); throw; } finally { LogMethodEnd(MethodBase.GetCurrentMethod()); } } public static string GetUniqueDataAreaID(string name) { LogMethodBegin(MethodBase.GetCurrentMethod()); try { var md5 = new System.Security.Cryptography.MD5CryptoServiceProvider(); var utf8 = new System.Text.UTF8Encoding(); var hash = BitConverter.ToString(md5.ComputeHash(utf8.GetBytes(name))); hash = hash.ToLower().Replace("-", ""); return hash; } catch (Exception E) { cLogManager.DefaultLogger.LogException(E); throw; } finally { LogMethodEnd(MethodBase.GetCurrentMethod()); } } public DataArea() { IAM_Folders = new List(); } public static void AddDirectorySecurity(string baseFolderTechnicalName, string newFolderTechnicalName, SecurityIdentifier Account, FileSystemRights Rights, AccessControlType ControlType) { LogMethodBegin(MethodBase.GetCurrentMethod()); try { // Create a new DirectoryInfo object. DirectoryInfo dInfo = new DirectoryInfo(newFolderTechnicalName); //DirectoryInfo dInfoBaseFolder = new DirectoryInfo(baseFolderTechnicalName); // Get a DirectorySecurity object that represents the // current security settings. DirectorySecurity dSecurity = dInfo.GetAccessControl(); // Add the FileSystemAccessRule to the security settings. dSecurity.AddAccessRule(new FileSystemAccessRule(Account, Rights, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow)); DefaultLogger.LogEntry(LogLevels.Debug, $"Set ACL for folder: {newFolderTechnicalName} for { Account }"); // Set the new access settings. dInfo.SetAccessControl(dSecurity); } catch (Exception E) { cLogManager.DefaultLogger.LogException(E); throw; } finally { LogMethodEnd(MethodBase.GetCurrentMethod()); } } // Removes an ACL entry on the specified directory for the specified account. public static void RemoveDirectorySecurity(string FileName, string Account, FileSystemRights Rights, AccessControlType ControlType) { LogMethodBegin(MethodBase.GetCurrentMethod()); try { // Create a new DirectoryInfo object. DirectoryInfo dInfo = new DirectoryInfo(FileName); // Get a DirectorySecurity object that represents the // current security settings. DirectorySecurity dSecurity = dInfo.GetAccessControl(); // Add the FileSystemAccessRule to the security settings. dSecurity.RemoveAccessRule(new FileSystemAccessRule(Account, Rights, ControlType)); // Set the new access settings. dInfo.SetAccessControl(dSecurity); } catch (Exception E) { cLogManager.DefaultLogger.LogException(E); throw; } finally { LogMethodEnd(MethodBase.GetCurrentMethod()); } } } public class IAM_Folder { public string Name = String.Empty; public string technicalName = String.Empty; public string UID = String.Empty; public string Parent = String.Empty; public string ParentUID = String.Empty; public string Owner = String.Empty; public string Write = String.Empty; public string Read = String.Empty; public string Traverse = String.Empty; public string CreatedDate = String.Empty; public int Level = 0; public int targetType; public string configurationID = String.Empty; public string baseFolder = String.Empty; } }