Skip to content

Commit 4b9e681

Browse files
authored
Code Quality: Removed the Tulpep.ActiveDirectoryObjectPicker reference for AOT compatibility (#18638)
1 parent 0dfde68 commit 4b9e681

7 files changed

Lines changed: 212 additions & 70 deletions

File tree

.github/NOTICE.md

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -583,39 +583,6 @@ Library.
583583
limitations under the License.
584584
```
585585

586-
## Active Directory Object Picker
587-
588-
**Source**: [https://github.com/Tulpep/Active-Directory-Object-Picker](https://github.com/Tulpep/Active-Directory-Object-Picker)
589-
590-
### License
591-
592-
```
593-
Microsoft Public License (MS-PL)
594-
595-
The initial project was originally created by Armand du Plessis <armand@dotnet.org.za> in 2004 and now is extended and maintained by Tulpep.
596-
597-
This license governs use of the accompanying software. If you use the software, you
598-
accept this license. If you do not accept the license, do not use the software.
599-
600-
1. Definitions
601-
The terms "reproduce," "reproduction," "derivative works," and "distribution" have the
602-
same meaning here as under U.S. copyright law.
603-
A "contribution" is the original software, or any additions or changes to the software.
604-
A "contributor" is any person that distributes its contribution under this license.
605-
"Licensed patents" are a contributor's patent claims that read directly on its contribution.
606-
607-
2. Grant of Rights
608-
(A) Copyright Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free copyright license to reproduce its contribution, prepare derivative works of its contribution, and distribute its contribution or any derivative works that you create.
609-
(B) Patent Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free license under its licensed patents to make, have made, use, sell, offer for sale, import, and/or otherwise dispose of its contribution in the software or derivative works of the contribution in the software.
610-
611-
3. Conditions and Limitations
612-
(A) No Trademark License- This license does not grant you rights to use any contributors' name, logo, or trademarks.
613-
(B) If you bring a patent claim against any contributor over patents that you claim are infringed by the software, your patent license from such contributor to the software ends automatically.
614-
(C) If you distribute any portion of the software, you must retain all copyright, patent, trademark, and attribution notices that are present in the software.
615-
(D) If you distribute any portion of the software in source code form, you may do so only under this license by including a complete copy of this license with your distribution. If you distribute any portion of the software in compiled or object code form, you may only do so under a license that complies with this license.
616-
(E) The software is licensed "as-is." You bear the risk of using it. The contributors give no express warranties, guarantees or conditions. You may have additional consumer rights under your local laws which this license cannot change. To the extent permitted under your local laws, the contributors exclude the implied warranties of merchantability, fitness for a particular purpose and non-infringement.
617-
```
618-
619586
## libgit2sharp
620587

621588
**Source**: [https://github.com/libgit2/libgit2sharp](https://github.com/libgit2/libgit2sharp)

Directory.Packages.props

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
<PackageVersion Include="Microsoft.WindowsAppSDK" Version="2.2.0" />
3939
<PackageVersion Include="Microsoft.Graphics.Win2D" Version="1.3.2" />
4040
<PackageVersion Include="TagLibSharp" Version="2.3.0" />
41-
<PackageVersion Include="Tulpep.ActiveDirectoryObjectPicker" Version="3.0.11" />
4241
<PackageVersion Include="UTF.Unknown" Version="2.6.0" />
4342
<PackageVersion Include="WinUIEx" Version="2.9.0" />
4443
<PackageVersion Include="Vanara.Windows.Extensions" Version="4.0.1" />

src/Files.App.CsWin32/NativeMethods.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,3 +301,18 @@ HCERTSTORE
301301
CERT_QUERY_ENCODING_TYPE
302302
CertGetNameString
303303
MessageBox
304+
IDsObjectPicker
305+
CLSID_DsObjectPicker
306+
DSOP_*
307+
DS_SELECTION_LIST
308+
DVASPECT
309+
CFSTR_DSOP_DS_SELECTION_LIST
310+
GlobalLock
311+
GlobalUnlock
312+
RegisterClipboardFormat
313+
ReleaseStgMedium
314+
SafeArrayAccessData
315+
SafeArrayUnaccessData
316+
SafeArrayGetDim
317+
SafeArrayGetLBound
318+
SafeArrayGetUBound
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
// Copyright (c) Files Community
2+
// Licensed under the MIT License.
3+
4+
using Microsoft.Extensions.Logging;
5+
using System.Security.Principal;
6+
using Windows.Win32;
7+
using Windows.Win32.Foundation;
8+
using Windows.Win32.System.Com;
9+
using Windows.Win32.Networking.ActiveDirectory;
10+
using Windows.Win32.System.Variant;
11+
12+
namespace Files.App.Storage
13+
{
14+
public static unsafe class WindowsObjectPicker
15+
{
16+
private const string ObjectSidAttributeName = "objectSid";
17+
18+
public static Task<string?> OpenObjectPickerAsync(nint ownerHWnd, ILogger? logger)
19+
=> STATask.Run(() => OpenObjectPicker((HWND)ownerHWnd), logger);
20+
21+
private static string? OpenObjectPicker(HWND ownerHWnd)
22+
{
23+
Guid CLSID_DsObjectPicker = PInvoke.CLSID_DsObjectPicker;
24+
25+
using ComPtr<IDsObjectPicker> pObjectPicker = default;
26+
HRESULT hr = pObjectPicker.CoCreateInstance(&CLSID_DsObjectPicker, null, CLSCTX.CLSCTX_INPROC_SERVER);
27+
if (hr.ThrowIfFailedOnDebug().Failed)
28+
return null;
29+
30+
DSOP_SCOPE_INIT_INFO* scopeInitInfos = stackalloc DSOP_SCOPE_INIT_INFO[2];
31+
scopeInitInfos[0] = CreateScopeInitInfo(PInvoke.DSOP_SCOPE_TYPE_TARGET_COMPUTER, true);
32+
scopeInitInfos[1] = CreateScopeInitInfo(
33+
PInvoke.DSOP_SCOPE_TYPE_UPLEVEL_JOINED_DOMAIN |
34+
PInvoke.DSOP_SCOPE_TYPE_DOWNLEVEL_JOINED_DOMAIN |
35+
PInvoke.DSOP_SCOPE_TYPE_ENTERPRISE_DOMAIN |
36+
PInvoke.DSOP_SCOPE_TYPE_GLOBAL_CATALOG |
37+
PInvoke.DSOP_SCOPE_TYPE_EXTERNAL_UPLEVEL_DOMAIN |
38+
PInvoke.DSOP_SCOPE_TYPE_EXTERNAL_DOWNLEVEL_DOMAIN |
39+
PInvoke.DSOP_SCOPE_TYPE_WORKGROUP |
40+
PInvoke.DSOP_SCOPE_TYPE_USER_ENTERED_UPLEVEL_SCOPE |
41+
PInvoke.DSOP_SCOPE_TYPE_USER_ENTERED_DOWNLEVEL_SCOPE,
42+
false);
43+
44+
fixed (char* pszObjectSidAttributeName = ObjectSidAttributeName)
45+
{
46+
PCWSTR* attributeNames = stackalloc PCWSTR[1];
47+
attributeNames[0] = pszObjectSidAttributeName;
48+
49+
DSOP_INIT_INFO initInfo = default;
50+
initInfo.cbSize = (uint)sizeof(DSOP_INIT_INFO);
51+
initInfo.cDsScopeInfos = 2;
52+
initInfo.aDsScopeInfos = scopeInitInfos;
53+
initInfo.cAttributesToFetch = 1;
54+
initInfo.apwzAttributeNames = attributeNames;
55+
56+
hr = pObjectPicker.Get()->Initialize(&initInfo);
57+
if (hr.ThrowIfFailedOnDebug().Failed)
58+
return null;
59+
}
60+
61+
using ComPtr<IDataObject> pSelections = default;
62+
hr = pObjectPicker.Get()->InvokeDialog(ownerHWnd, pSelections.GetAddressOf());
63+
if (hr == HRESULT.S_FALSE || hr.ThrowIfFailedOnDebug().Failed || pSelections.IsNull)
64+
return null;
65+
66+
return GetSelectedSid(pSelections.Get());
67+
}
68+
69+
private static DSOP_SCOPE_INIT_INFO CreateScopeInitInfo(uint scopeType, bool startingScope)
70+
{
71+
const uint defaultFilter =
72+
PInvoke.DSOP_SCOPE_FLAG_DEFAULT_FILTER_USERS |
73+
PInvoke.DSOP_SCOPE_FLAG_DEFAULT_FILTER_GROUPS;
74+
75+
const uint upLevelFilter =
76+
PInvoke.DSOP_FILTER_INCLUDE_ADVANCED_VIEW |
77+
PInvoke.DSOP_FILTER_USERS |
78+
PInvoke.DSOP_FILTER_BUILTIN_GROUPS |
79+
PInvoke.DSOP_FILTER_WELL_KNOWN_PRINCIPALS |
80+
PInvoke.DSOP_FILTER_UNIVERSAL_GROUPS_DL |
81+
PInvoke.DSOP_FILTER_UNIVERSAL_GROUPS_SE |
82+
PInvoke.DSOP_FILTER_GLOBAL_GROUPS_DL |
83+
PInvoke.DSOP_FILTER_GLOBAL_GROUPS_SE |
84+
PInvoke.DSOP_FILTER_DOMAIN_LOCAL_GROUPS_DL |
85+
PInvoke.DSOP_FILTER_DOMAIN_LOCAL_GROUPS_SE |
86+
PInvoke.DSOP_FILTER_CONTACTS |
87+
PInvoke.DSOP_FILTER_COMPUTERS |
88+
PInvoke.DSOP_FILTER_SERVICE_ACCOUNTS;
89+
90+
const uint downLevelFilter =
91+
PInvoke.DSOP_DOWNLEVEL_FILTER_USERS |
92+
PInvoke.DSOP_DOWNLEVEL_FILTER_LOCAL_GROUPS |
93+
PInvoke.DSOP_DOWNLEVEL_FILTER_GLOBAL_GROUPS |
94+
PInvoke.DSOP_DOWNLEVEL_FILTER_COMPUTERS |
95+
PInvoke.DSOP_DOWNLEVEL_FILTER_ALL_WELLKNOWN_SIDS;
96+
97+
DSOP_SCOPE_INIT_INFO info = default;
98+
info.cbSize = (uint)sizeof(DSOP_SCOPE_INIT_INFO);
99+
info.flType = scopeType;
100+
info.flScope = startingScope
101+
? PInvoke.DSOP_SCOPE_FLAG_STARTING_SCOPE | defaultFilter
102+
: defaultFilter;
103+
info.FilterFlags.Uplevel.flBothModes = upLevelFilter;
104+
info.FilterFlags.flDownlevel = downLevelFilter;
105+
106+
return info;
107+
}
108+
109+
private static string? GetSelectedSid(IDataObject* pSelections)
110+
{
111+
uint clipboardFormat;
112+
fixed (char* pszSelectionListFormatName = PInvoke.CFSTR_DSOP_DS_SELECTION_LIST)
113+
clipboardFormat = PInvoke.RegisterClipboardFormat(pszSelectionListFormatName);
114+
115+
if (clipboardFormat is 0 || clipboardFormat > ushort.MaxValue)
116+
return null;
117+
118+
FORMATETC format = default;
119+
format.cfFormat = (ushort)clipboardFormat;
120+
format.dwAspect = (uint)DVASPECT.DVASPECT_CONTENT;
121+
format.lindex = -1;
122+
format.tymed = (uint)TYMED.TYMED_HGLOBAL;
123+
124+
STGMEDIUM medium = default;
125+
medium.tymed = TYMED.TYMED_HGLOBAL;
126+
127+
HRESULT hr = pSelections->GetData(&format, &medium);
128+
if (hr.ThrowIfFailedOnDebug().Failed)
129+
return null;
130+
131+
void* pvSelectionList = PInvoke.GlobalLock(medium.u.hGlobal);
132+
if (pvSelectionList is null)
133+
{
134+
PInvoke.ReleaseStgMedium(&medium);
135+
return null;
136+
}
137+
138+
try
139+
{
140+
DS_SELECTION_LIST* pSelectionList = (DS_SELECTION_LIST*)pvSelectionList;
141+
if (pSelectionList->cItems is 0 || pSelectionList->cFetchedAttributes is 0)
142+
return null;
143+
144+
var selectionsAsSpan = pSelectionList->aDsSelection.AsSpan((int)pSelectionList->cItems);
145+
fixed (DS_SELECTION* pSelection = selectionsAsSpan)
146+
{
147+
if (pSelection->pvarFetchedAttributes is null)
148+
return null;
149+
150+
return GetSidString(pSelection->pvarFetchedAttributes);
151+
}
152+
}
153+
finally
154+
{
155+
PInvoke.GlobalUnlock(medium.u.hGlobal);
156+
PInvoke.ReleaseStgMedium(&medium);
157+
}
158+
}
159+
160+
private static string? GetSidString(VARIANT* objectSidVariant)
161+
{
162+
VARENUM variantType = objectSidVariant->Anonymous.Anonymous.vt;
163+
if ((variantType & VARENUM.VT_ARRAY) is 0 || (variantType & VARENUM.VT_TYPEMASK) is not VARENUM.VT_UI1)
164+
return null;
165+
166+
SAFEARRAY* pSafeArray = (variantType & VARENUM.VT_BYREF) is not 0
167+
? *objectSidVariant->Anonymous.Anonymous.Anonymous.pparray
168+
: objectSidVariant->Anonymous.Anonymous.Anonymous.parray;
169+
170+
if (pSafeArray is null || PInvoke.SafeArrayGetDim(pSafeArray) is not 1)
171+
return null;
172+
173+
int lowerBound = 0, upperBound = 0;
174+
if (PInvoke.SafeArrayGetLBound(pSafeArray, 1, &lowerBound).ThrowIfFailedOnDebug().Failed ||
175+
PInvoke.SafeArrayGetUBound(pSafeArray, 1, &upperBound).ThrowIfFailedOnDebug().Failed ||
176+
upperBound < lowerBound)
177+
return null;
178+
179+
void* pSidBytes;
180+
if (PInvoke.SafeArrayAccessData(pSafeArray, &pSidBytes).ThrowIfFailedOnDebug().Failed || pSidBytes is null)
181+
return null;
182+
183+
try
184+
{
185+
int length = upperBound - lowerBound + 1;
186+
byte[] sidBytes = new ReadOnlySpan<byte>((byte*)pSidBytes, length).ToArray();
187+
188+
return new SecurityIdentifier(sidBytes, 0).Value;
189+
}
190+
finally
191+
{
192+
PInvoke.SafeArrayUnaccessData(pSafeArray);
193+
}
194+
}
195+
}
196+
}

src/Files.App/Files.App.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@
100100
<PackageReference Include="Microsoft.WindowsAppSDK" />
101101
<PackageReference Include="Microsoft.Graphics.Win2D" />
102102
<PackageReference Include="TagLibSharp" />
103-
<PackageReference Include="Tulpep.ActiveDirectoryObjectPicker" />
104103
<PackageReference Include="UTF.Unknown" />
105104
<PackageReference Include="WinUIEx" />
106105
<PackageReference Include="Vanara.Windows.Extensions" />

src/Files.App/Utils/Storage/Operations/FileOperationsHelpers.cs

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
using Microsoft.Extensions.Logging;
77
using System.Collections.Concurrent;
88
using System.IO;
9-
using System.Security.Principal;
10-
using Tulpep.ActiveDirectoryObjectPicker;
119
using Vanara.PInvoke;
1210
using Vanara.Windows.Shell;
1311
using Windows.ApplicationModel.DataTransfer;
@@ -941,38 +939,7 @@ private static bool TrySetLnkShortcutIcon(string filePath, string iconFile, int
941939
}
942940

943941
public static Task<string?> OpenObjectPickerAsync(long hWnd)
944-
{
945-
return STATask.Run(() =>
946-
{
947-
var picker = new DirectoryObjectPickerDialog()
948-
{
949-
AllowedObjectTypes = ObjectTypes.All,
950-
DefaultObjectTypes = ObjectTypes.Users | ObjectTypes.Groups,
951-
AllowedLocations = Locations.All,
952-
DefaultLocations = Locations.LocalComputer,
953-
MultiSelect = false,
954-
ShowAdvancedView = true
955-
};
956-
957-
picker.AttributesToFetch.Add("objectSid");
958-
959-
using (picker)
960-
{
961-
if (picker.ShowDialog(Win32Helper.Win32Window.FromLong(hWnd)) == System.Windows.Forms.DialogResult.OK)
962-
{
963-
try
964-
{
965-
var attribs = picker.SelectedObject.FetchedAttributes;
966-
if (attribs.Any() && attribs[0] is byte[] objectSid)
967-
return new SecurityIdentifier(objectSid, 0).Value;
968-
}
969-
catch { }
970-
}
971-
}
972-
973-
return null;
974-
}, App.Logger);
975-
}
942+
=> WindowsObjectPicker.OpenObjectPickerAsync((nint)hWnd, App.Logger);
976943

977944
private static ShellItem? GetFirstFile(ShellItem shi)
978945
{

src/Files.App/ViewModels/Settings/AboutViewModel.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ public AboutViewModel()
7373
new ("https://github.com/microsoft/Win2D", "Win2D"),
7474
new ("https://github.com/CommunityToolkit/Windows", "Windows Community Toolkit"),
7575
new ("https://github.com/mono/taglib-sharp", "TagLibSharp"),
76-
new ("https://github.com/Tulpep/Active-Directory-Object-Picker", "ActiveDirectoryObjectPicker"),
7776
new ("https://github.com/PowerShell/MMI", "MMI"),
7877
new ("https://github.com/microsoft/CsWin32", "CsWin32"),
7978
new ("https://github.com/microsoft/CsWinRT", "CsWinRT"),

0 commit comments

Comments
 (0)