From a8416d7b3049b7650f726460ed345e93f9ce3089 Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Fri, 5 Aug 2016 14:55:17 -0700 Subject: [PATCH] Consolidate GetUserName code Once .NET Core 1.1 becomes available, we can likely remove the entirety of CorePsExtensions.cs. --- .../CoreCLR/CorePsExtensions.cs | 39 ++++++++----------- .../CoreCLR/CorePsPlatform.cs | 5 --- .../engine/InformationRecord.cs | 19 ++++----- .../engine/InitialSessionState.cs | 8 +++- test/csharp/test_CorePsPlatform.cs | 2 +- 5 files changed, 34 insertions(+), 39 deletions(-) diff --git a/src/System.Management.Automation/CoreCLR/CorePsExtensions.cs b/src/System.Management.Automation/CoreCLR/CorePsExtensions.cs index 6b0c774325..657475c068 100644 --- a/src/System.Management.Automation/CoreCLR/CorePsExtensions.cs +++ b/src/System.Management.Automation/CoreCLR/CorePsExtensions.cs @@ -1026,25 +1026,8 @@ namespace System.Management.Automation { return Platform.NonWindowsGetDomainName(); } - } - } - internal static string WinGetUserName() - { - StringBuilder domainName = new StringBuilder(1024); - uint domainNameLen = (uint)domainName.Capacity; - - byte ret = Win32Native.GetUserNameEx(Win32Native.NameSamCompatible, domainName, ref domainNameLen); - if (ret == 1) - { - string samName = domainName.ToString(); - int index = samName.IndexOf('\\'); - if (index != -1) - { - return samName.Substring(index + 1); - } } - return string.Empty; } /// @@ -1054,14 +1037,24 @@ namespace System.Management.Automation { get { - if (Platform.IsWindows) + #if UNIX + return Platform.Unix.UserName; + #else + StringBuilder domainName = new StringBuilder(1024); + uint domainNameLen = (uint)domainName.Capacity; + + byte ret = Win32Native.GetUserNameEx(Win32Native.NameSamCompatible, domainName, ref domainNameLen); + if (ret == 1) { - return WinGetUserName(); - } - else - { - return Platform.NonWindowsGetUserName(); + string samName = domainName.ToString(); + int index = samName.IndexOf('\\'); + if (index != -1) + { + return samName.Substring(index + 1); + } } + return string.Empty; + #endif } } diff --git a/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs b/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs index dff618743f..53b72fcbfb 100644 --- a/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs +++ b/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs @@ -314,11 +314,6 @@ namespace System.Management.Automation return string.Empty; } - internal static string NonWindowsGetUserName() - { - return Unix.UserName; - } - // Hostname in this context seems to be the FQDN internal static string NonWindowsGetHostName() { diff --git a/src/System.Management.Automation/engine/InformationRecord.cs b/src/System.Management.Automation/engine/InformationRecord.cs index 68df207a93..9bc8f98a51 100644 --- a/src/System.Management.Automation/engine/InformationRecord.cs +++ b/src/System.Management.Automation/engine/InformationRecord.cs @@ -6,6 +6,10 @@ using Dbg = System.Management.Automation.Diagnostics; using System.Runtime.Serialization; using System.Collections.Generic; +#if CORECLR +using Environment = System.Management.Automation.Environment; +#endif + namespace System.Management.Automation { /// @@ -38,15 +42,12 @@ namespace System.Management.Automation this.TimeGenerated = DateTime.Now; this.Tags = new List(); - if (Platform.IsWindows) - { - this.User = System.Security.Principal.WindowsIdentity.GetCurrent().Name; - } - else - { - this.User = Platform.NonWindowsGetUserName(); - } - // Porting note: PsUtils.GetHostName() already handles platform specifics + // domain\user on Windows, just user on Unix + #if UNIX + this.User = Platform.Unix.UserName; + #else + this.User = System.Security.Principal.WindowsIdentity.GetCurrent().Name; + #endif this.Computer = PsUtils.GetHostName(); this.ProcessId = (uint)System.Diagnostics.Process.GetCurrentProcess().Id; this.NativeThreadId = PsUtils.GetNativeThreadId(); diff --git a/src/System.Management.Automation/engine/InitialSessionState.cs b/src/System.Management.Automation/engine/InitialSessionState.cs index d3a7a1a13c..16f6a05715 100644 --- a/src/System.Management.Automation/engine/InitialSessionState.cs +++ b/src/System.Management.Automation/engine/InitialSessionState.cs @@ -3148,7 +3148,13 @@ namespace System.Management.Automation.Runspaces // otherwise use the current user name. var userName = (!string.IsNullOrEmpty(this.UserDriveUserName)) ? this.UserDriveUserName : - System.Security.Principal.WindowsIdentity.GetCurrent().Name; + // domain\user on Windows, just user on Unix + #if UNIX + Platform.Unix.UserName + #else + System.Security.Principal.WindowsIdentity.GetCurrent().Name + #endif + ; // Ensure that user name contains no invalid path characters. // MSDN indicates that logon names cannot contain any of these invalid characters, diff --git a/test/csharp/test_CorePsPlatform.cs b/test/csharp/test_CorePsPlatform.cs index 7937d539b7..8974a51b44 100644 --- a/test/csharp/test_CorePsPlatform.cs +++ b/test/csharp/test_CorePsPlatform.cs @@ -34,7 +34,7 @@ namespace PSTests // The process should return an exit code of 0 on success Assert.Equal(0, process.ExitCode); // It should be the same as what our platform code returns - Assert.Equal(username, Platform.NonWindowsGetUserName()); + Assert.Equal(username, Platform.Unix.UserName()); } }