diff --git a/ATXSerializables/ATXCommon.csproj b/ATXSerializables/ATXCommon.csproj index 6437a4c75bcc5019d812e019a18c40a5763ac969..4cd375356fa634ab845c837c39163f2b6636afa7 100644 --- a/ATXSerializables/ATXCommon.csproj +++ b/ATXSerializables/ATXCommon.csproj @@ -37,6 +37,8 @@ <Reference Include="System" /> <Reference Include="System.Configuration" /> <Reference Include="System.Core" /> + <Reference Include="System.DirectoryServices.AccountManagement" /> + <Reference Include="System.Management" /> <Reference Include="System.Xml.Linq" /> <Reference Include="System.Data.DataSetExtensions" /> <Reference Include="Microsoft.CSharp" /> @@ -44,6 +46,7 @@ <Reference Include="System.Xml" /> </ItemGroup> <ItemGroup> + <Compile Include="ActiveDirectory.cs" /> <Compile Include="FsUtils.cs" /> <Compile Include="Serializables\DriveToCheck.cs" /> <Compile Include="Serializables\ServiceConfig.cs" /> diff --git a/ATXSerializables/ActiveDirectory.cs b/ATXSerializables/ActiveDirectory.cs new file mode 100644 index 0000000000000000000000000000000000000000..98b99bf45195b0ec18c3701c805dd65902804787 --- /dev/null +++ b/ATXSerializables/ActiveDirectory.cs @@ -0,0 +1,78 @@ +using System; +using System.DirectoryServices.AccountManagement; +using System.Linq; +using System.Management; +using NLog; + +namespace ATXCommon +{ + public static class ActiveDirectory + { + private static readonly Logger Log = LogManager.GetCurrentClassLogger(); + + /// <summary> + /// Check if a user is currently logged into Windows. + /// + /// WARNING: this DOES NOT ACCOUNT for users logged in via RDP!! + /// </summary> + /// See https://stackoverflow.com/questions/5218778/ for the RDP problem. + public static bool NoUserIsLoggedOn() { + var username = ""; + try { + var searcher = new ManagementObjectSearcher("SELECT UserName " + + "FROM Win32_ComputerSystem"); + var collection = searcher.Get(); + username = (string)collection.Cast<ManagementBaseObject>().First()["UserName"]; + } + catch (Exception ex) { + // TODO / FIXME: combine log and admin-email! + var msg = string.Format("Error in getCurrentUsername(): {0}", ex.Message); + Log.Error(msg); + // TODO: FIXME! + // SendAdminEmail(msg); + } + return username == ""; + } + + /// <summary> + /// Get the user email address from ActiveDirectory. + /// </summary> + /// <param name="username">The username.</param> + /// <returns>Email address of AD user, an empty string if not found.</returns> + public static string GetEmailAddress(string username) { + try { + using (var pctx = new PrincipalContext(ContextType.Domain)) { + using (var up = UserPrincipal.FindByIdentity(pctx, username)) { + if (up != null && !string.IsNullOrWhiteSpace(up.EmailAddress)) { + return up.EmailAddress; + } + } + } + } + catch (Exception ex) { + Log.Warn("Can't find email address for {0}: {1}", username, ex.Message); + } + return ""; + } + + /// <summary> + /// Get the full user name (human-friendly) from ActiveDirectory. + /// </summary> + /// <param name="username">The username.</param> + /// <returns>A human-friendly string representation of the user principal.</returns> + public static string GetFullUserName(string username) { + try { + using (var pctx = new PrincipalContext(ContextType.Domain)) { + using (var up = UserPrincipal.FindByIdentity(pctx, username)) { + if (up != null) + return up.GivenName + " " + up.Surname; + } + } + } + catch (Exception ex) { + Log.Warn("Can't find full name for {0}: {1}", username, ex.Message); + } + return ""; + } + } +} diff --git a/AutoTx/AutoTx.cs b/AutoTx/AutoTx.cs index 0167d2c168737d7fbcc4f15a4d13006b2740918c..7263467f85c6821fd73996008c8dba2bf1fc7eb9 100644 --- a/AutoTx/AutoTx.cs +++ b/AutoTx/AutoTx.cs @@ -6,9 +6,6 @@ using System.Linq; using System.ServiceProcess; using System.IO; using System.Timers; -using System.DirectoryServices.AccountManagement; -using System.Globalization; -using System.Management; using NLog; using NLog.Config; using NLog.Targets; @@ -469,7 +466,7 @@ namespace AutoTx } // set state to "Running" if no-one is logged on: - if (NoUserIsLoggedOn()) { + if (ActiveDirectory.NoUserIsLoggedOn()) { _status.ServiceSuspended = false; if (!string.IsNullOrEmpty(_status.LimitReason)) { _status.LimitReason = ""; // reset to force a message on next service suspend @@ -512,72 +509,6 @@ namespace AutoTx #endregion - #region ActiveDirectory, email address, user name, ... - - /// <summary> - /// Check if a user is currently logged into Windows. - /// - /// WARNING: this DOES NOT ACCOUNT for users logged in via RDP!! - /// </summary> - /// See https://stackoverflow.com/questions/5218778/ for the RDP problem. - private bool NoUserIsLoggedOn() { - var username = ""; - try { - var searcher = new ManagementObjectSearcher("SELECT UserName " + - "FROM Win32_ComputerSystem"); - var collection = searcher.Get(); - username = (string) collection.Cast<ManagementBaseObject>().First()["UserName"]; - } - catch (Exception ex) { - // TODO / FIXME: combine log and admin-email! - var msg = string.Format("Error in getCurrentUsername(): {0}", ex.Message); - Log.Error(msg); - SendAdminEmail(msg); - } - return username == ""; - } - - /// <summary> - /// Get the user email address from ActiveDirectory. - /// </summary> - /// <param name="username">The username.</param> - /// <returns>Email address of AD user, an empty string if not found.</returns> - public string GetEmailAddress(string username) { - try { - using (var pctx = new PrincipalContext(ContextType.Domain)) { - using (var up = UserPrincipal.FindByIdentity(pctx, username)) { - if (up != null && !String.IsNullOrEmpty(up.EmailAddress)) { - return up.EmailAddress; - } - } - } - } - catch (Exception ex) { - Log.Warn("Can't find email address for {0}: {1}", username, ex.Message); - } - return ""; - } - - /// <summary> - /// Get the full user name (human-friendly) from ActiveDirectory. - /// </summary> - /// <param name="username">The username.</param> - /// <returns>A human-friendly string representation of the user principal.</returns> - public string GetFullUserName(string username) { - try { - using (var pctx = new PrincipalContext(ContextType.Domain)) { - using (var up = UserPrincipal.FindByIdentity(pctx, username)) { - if (up != null) return up.GivenName + " " + up.Surname; - } - } - } - catch (Exception ex) { - Log.Warn("Can't find full name for {0}: {1}", username, ex.Message); - } - return ""; - } - - #endregion #region transfer tasks diff --git a/AutoTx/Email.cs b/AutoTx/Email.cs index e036f4b925eb35c40d2331f62f9a0e55550fde61..eefb9b28de853da62d23164b58fe41f744691df5 100644 --- a/AutoTx/Email.cs +++ b/AutoTx/Email.cs @@ -4,6 +4,7 @@ using System.IO; using System.Linq; using System.Net.Mail; using System.Text; +using ATXCommon; namespace AutoTx { @@ -23,7 +24,7 @@ namespace AutoTx } if (!recipient.Contains(@"@")) { Log.Debug("Invalid recipient, trying to resolve via AD: {0}", recipient); - recipient = GetEmailAddress(recipient); + recipient = ActiveDirectory.GetEmailAddress(recipient); } if (string.IsNullOrWhiteSpace(recipient)) { Log.Info("Invalid or empty recipient given, NOT sending email!"); @@ -149,7 +150,7 @@ namespace AutoTx } var substitutions = new List<Tuple<string, string>> { - Tuple.Create("FACILITY_USER", GetFullUserName(userDir)), + Tuple.Create("FACILITY_USER", ActiveDirectory.GetFullUserName(userDir)), Tuple.Create("HOST_ALIAS", _config.HostAlias), Tuple.Create("HOST_NAME", Environment.MachineName), Tuple.Create("DESTINATION_ALIAS", _config.DestinationAlias), @@ -181,7 +182,7 @@ namespace AutoTx var userDir = new DirectoryInfo(_status.CurrentTransferSrc).Name; var substitutions = new List<Tuple<string, string>> { - Tuple.Create("FACILITY_USER", GetFullUserName(userDir)), + Tuple.Create("FACILITY_USER", ActiveDirectory.GetFullUserName(userDir)), Tuple.Create("HOST_ALIAS", _config.HostAlias), Tuple.Create("HOST_NAME", Environment.MachineName), Tuple.Create("EMAIL_FROM", _config.EmailFrom)