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,37 @@ 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
+ {
80
+ // Get PSID object from string sid
81
+ PInvoke . ConvertStringSidToSid ( new PCWSTR ( cSid ) , & lpSid ) ;
82
+ }
83
+
84
+ PWSTR lpName = default ;
85
+ PWSTR lpDomain = default ;
86
+ uint cchName = 0 , cchDomainName = 0 ;
79
87
80
88
// Get size of account name and domain name
81
- bool bResult = LookupAccountSid ( null , lpSid , lpName , ref cchName , lpDomain , ref cchDomainName , out _ ) ;
89
+ bool bResult = PInvoke . LookupAccountSid ( new PCWSTR ( ) , lpSid , lpName , & cchName , lpDomain , & cchDomainName , null ) ;
82
90
83
91
// Ensure requested capacity
84
- lpName . EnsureCapacity ( cchName ) ;
85
- lpDomain . EnsureCapacity ( cchDomainName ) ;
92
+ fixed ( char * cName = new char [ cchName ] )
93
+ lpName = new ( cName ) ;
94
+
95
+ fixed ( char * cDomain = new char [ cchDomainName ] )
96
+ lpDomain = new ( cDomain ) ;
86
97
87
98
// Get account name and domain
88
- bResult = LookupAccountSid ( null , lpSid , lpName , ref cchName , lpDomain , ref cchDomainName , out var snu ) ;
99
+ bResult = PInvoke . LookupAccountSid ( new PCWSTR ( ) , lpSid , lpName , & cchName , lpDomain , & cchDomainName , & snu ) ;
89
100
if ( ! bResult )
90
101
return ;
91
102
@@ -96,24 +107,24 @@ var x when
96
107
( x == SID_NAME_USE . SidTypeAlias ||
97
108
x == SID_NAME_USE . SidTypeGroup ||
98
109
x == SID_NAME_USE . SidTypeWellKnownGroup )
99
- => PrincipalType . Group ,
110
+ => AccessControlPrincipalType . Group ,
100
111
101
112
// User
102
113
SID_NAME_USE . SidTypeUser
103
- => PrincipalType . User ,
114
+ => AccessControlPrincipalType . User ,
104
115
105
116
// Unknown
106
- _ => PrincipalType . Unknown
117
+ _ => AccessControlPrincipalType . Unknown
107
118
} ;
108
119
109
- lpDomain . Clear ( ) ;
110
-
111
120
// Replace domain name with computer name if the account type is user or alias type
112
121
if ( snu == SID_NAME_USE . SidTypeUser || snu == SID_NAME_USE . SidTypeAlias )
113
122
{
114
- lpDomain = new ( 256 , 256 ) ;
115
- uint size = ( uint ) lpDomain . Capacity ;
116
- bResult = Kernel32 . GetComputerName ( lpDomain , ref size ) ;
123
+ uint size = 256 ;
124
+ fixed ( char * cDomain = new char [ size ] )
125
+ lpDomain = new ( cDomain ) ;
126
+
127
+ bResult = PInvoke . GetComputerName ( lpDomain , ref size ) ;
117
128
if ( ! bResult )
118
129
return ;
119
130
}
0 commit comments