1
1
// Copyright (c) 2024 Files Community
2
2
// Licensed under the MIT License. See the LICENSE.
3
3
4
- using System . Text ;
5
- using Vanara . PInvoke ;
6
- using static Vanara . PInvoke . AdvApi32 ;
4
+ using Windows . Win32 ;
5
+ using Windows . Win32 . Foundation ;
6
+ using Windows . Win32 . Security ;
7
7
8
- namespace Files . App . Utils . Storage
8
+ namespace Files . App . Data . Items
9
9
{
10
10
/// <summary>
11
11
/// Represents a principal of an ACE or an owner of an ACL.
12
12
/// </summary>
13
- public sealed class Principal : ObservableObject
13
+ public sealed class AccessControlPrincipal : ObservableObject
14
14
{
15
15
/// <summary>
16
16
/// Account type.
17
17
/// </summary>
18
- public PrincipalType PrincipalType { get ; private set ; }
18
+ public AccessControlPrincipalType PrincipalType { get ; private set ; }
19
19
20
20
/// <summary>
21
- /// Acount security identifier (SID).
21
+ /// Account security identifier (SID).
22
22
/// </summary>
23
23
public string ? Sid { get ; private set ; }
24
24
@@ -43,8 +43,8 @@ public sealed class Principal : ObservableObject
43
43
public string Glyph
44
44
=> PrincipalType switch
45
45
{
46
- PrincipalType . User => "\xE77B " ,
47
- PrincipalType . Group => "\xE902 " ,
46
+ AccessControlPrincipalType . User => "\xE77B " ,
47
+ AccessControlPrincipalType . Group => "\xE902 " ,
48
48
_ => "\xE716 " ,
49
49
} ;
50
50
@@ -66,26 +66,34 @@ public string? FullNameHumanized
66
66
public string FullNameHumanizedWithBrackes
67
67
=> string . IsNullOrEmpty ( Domain ) ? string . Empty : $ "({ Domain } \\ { Name } )";
68
68
69
- public Principal ( string sid )
69
+ public unsafe AccessControlPrincipal ( string sid )
70
70
{
71
71
if ( string . IsNullOrEmpty ( sid ) )
72
72
return ;
73
73
74
74
Sid = sid ;
75
- var lpSid = ConvertStringSidToSid ( sid ) ;
75
+ PSID lpSid = default ;
76
+ SID_NAME_USE snu = default ;
76
77
77
- StringBuilder lpName = new ( ) , lpDomain = new ( ) ;
78
- int cchName = 0 , cchDomainName = 0 ;
78
+ fixed ( char * cSid = sid )
79
+ PInvoke . ConvertStringSidToSid ( new PCWSTR ( cSid ) , & lpSid ) ;
80
+
81
+ PWSTR lpName = default ;
82
+ PWSTR lpDomain = default ;
83
+ uint cchName = 0 , cchDomainName = 0 ;
79
84
80
85
// Get size of account name and domain name
81
- bool bResult = LookupAccountSid ( null , lpSid , lpName , ref cchName , lpDomain , ref cchDomainName , out _ ) ;
86
+ bool bResult = PInvoke . LookupAccountSid ( new PCWSTR ( ) , lpSid , lpName , & cchName , lpDomain , & cchDomainName , null ) ;
82
87
83
88
// Ensure requested capacity
84
- lpName . EnsureCapacity ( cchName ) ;
85
- lpDomain . EnsureCapacity ( cchDomainName ) ;
89
+ fixed ( char * cName = new char [ cchName ] )
90
+ lpName = new ( cName ) ;
91
+
92
+ fixed ( char * cDomain = new char [ cchDomainName ] )
93
+ lpDomain = new ( cDomain ) ;
86
94
87
95
// Get account name and domain
88
- bResult = LookupAccountSid ( null , lpSid , lpName , ref cchName , lpDomain , ref cchDomainName , out var snu ) ;
96
+ bResult = PInvoke . LookupAccountSid ( new PCWSTR ( ) , lpSid , lpName , & cchName , lpDomain , & cchDomainName , & snu ) ;
89
97
if ( ! bResult )
90
98
return ;
91
99
@@ -96,24 +104,24 @@ var x when
96
104
( x == SID_NAME_USE . SidTypeAlias ||
97
105
x == SID_NAME_USE . SidTypeGroup ||
98
106
x == SID_NAME_USE . SidTypeWellKnownGroup )
99
- => PrincipalType . Group ,
107
+ => AccessControlPrincipalType . Group ,
100
108
101
109
// User
102
110
SID_NAME_USE . SidTypeUser
103
- => PrincipalType . User ,
111
+ => AccessControlPrincipalType . User ,
104
112
105
113
// Unknown
106
- _ => PrincipalType . Unknown
114
+ _ => AccessControlPrincipalType . Unknown
107
115
} ;
108
116
109
- lpDomain . Clear ( ) ;
110
-
111
117
// Replace domain name with computer name if the account type is user or alias type
112
118
if ( snu == SID_NAME_USE . SidTypeUser || snu == SID_NAME_USE . SidTypeAlias )
113
119
{
114
- lpDomain = new ( 256 , 256 ) ;
115
- uint size = ( uint ) lpDomain . Capacity ;
116
- bResult = Kernel32 . GetComputerName ( lpDomain , ref size ) ;
120
+ uint size = 256 ;
121
+ fixed ( char * cDomain = new char [ size ] )
122
+ lpDomain = new ( cDomain ) ;
123
+
124
+ bResult = PInvoke . GetComputerName ( lpDomain , ref size ) ;
117
125
if ( ! bResult )
118
126
return ;
119
127
}
0 commit comments