mirror of
https://github.com/aphrody-labs/PowerShell.git
synced 2026-10-17 04:36:39 +00:00
Consolidate GetUserName code
Once .NET Core 1.1 becomes available, we can likely remove the entirety of CorePsExtensions.cs.
This commit is contained in:
1 parent
3f9889d6da
commit
a8416d7b30
5 files changed
+28
-33
No files matched your search
@@ -1026,11 +1026,20 @@ namespace System.Management.Automation
|
||||
{
|
||||
return Platform.NonWindowsGetDomainName();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
internal static string WinGetUserName()
|
||||
/// <summary>
|
||||
/// UserName
|
||||
/// </summary>
|
||||
public static string UserName
|
||||
{
|
||||
get
|
||||
{
|
||||
#if UNIX
|
||||
return Platform.Unix.UserName;
|
||||
#else
|
||||
StringBuilder domainName = new StringBuilder(1024);
|
||||
uint domainNameLen = (uint)domainName.Capacity;
|
||||
|
||||
@@ -1045,23 +1054,7 @@ namespace System.Management.Automation
|
||||
}
|
||||
}
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// UserName
|
||||
/// </summary>
|
||||
public static string UserName
|
||||
{
|
||||
get
|
||||
{
|
||||
if (Platform.IsWindows)
|
||||
{
|
||||
return WinGetUserName();
|
||||
}
|
||||
else
|
||||
{
|
||||
return Platform.NonWindowsGetUserName();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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()
|
||||
{
|
||||
|
||||
@@ -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
|
||||
{
|
||||
/// <summary>
|
||||
@@ -38,15 +42,12 @@ namespace System.Management.Automation
|
||||
|
||||
this.TimeGenerated = DateTime.Now;
|
||||
this.Tags = new List<string>();
|
||||
if (Platform.IsWindows)
|
||||
{
|
||||
// domain\user on Windows, just user on Unix
|
||||
#if UNIX
|
||||
this.User = Platform.Unix.UserName;
|
||||
#else
|
||||
this.User = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.User = Platform.NonWindowsGetUserName();
|
||||
}
|
||||
// Porting note: PsUtils.GetHostName() already handles platform specifics
|
||||
#endif
|
||||
this.Computer = PsUtils.GetHostName();
|
||||
this.ProcessId = (uint)System.Diagnostics.Process.GetCurrentProcess().Id;
|
||||
this.NativeThreadId = PsUtils.GetNativeThreadId();
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user