@@ -84,7 +84,7 @@ private static string Normalize(string s)
84
84
85
85
public IEnumerable < PSADServicePrincipal > FilterServicePrincipals ( Rest . Azure . OData . ODataQuery < ServicePrincipal > odataQuery , ulong first = ulong . MaxValue , ulong skip = 0 )
86
86
{
87
- return new PageEnumerable < ServicePrincipal > (
87
+ return new GenericPageEnumerable < ServicePrincipal > (
88
88
delegate ( )
89
89
{
90
90
return GraphClient . ServicePrincipals . List ( odataQuery ) ;
@@ -146,8 +146,18 @@ public IEnumerable<PSADUser> FilterUsers(ADObjectFilterOptions options, ulong fi
146
146
}
147
147
else
148
148
{
149
- var odataQuery = new Rest . Azure . OData . ODataQuery < User > ( u => u . DisplayName . StartsWith ( options . SearchString ) ) ;
150
- return new PageEnumerable < User > (
149
+ Rest . Azure . OData . ODataQuery < User > odataQuery = null ;
150
+ if ( ! string . IsNullOrEmpty ( options . SearchString ) && options . SearchString . EndsWith ( "*" ) )
151
+ {
152
+ options . SearchString = options . SearchString . TrimEnd ( '*' ) ;
153
+ odataQuery = new Rest . Azure . OData . ODataQuery < User > ( u => u . DisplayName . StartsWith ( options . SearchString ) ) ;
154
+ }
155
+ else
156
+ {
157
+ odataQuery = new Rest . Azure . OData . ODataQuery < User > ( u => u . DisplayName == options . SearchString ) ;
158
+ }
159
+
160
+ return new GenericPageEnumerable < User > (
151
161
delegate ( )
152
162
{
153
163
return GraphClient . Users . List ( odataQuery . ToString ( ) ) ;
@@ -197,6 +207,22 @@ public List<PSADObject> GetObjectsByObjectId(List<string> objectIds)
197
207
return result ;
198
208
}
199
209
210
+ public PSADGroup GetGroupByDisplayName ( string displayName )
211
+ {
212
+ var group = FilterGroups ( new ADObjectFilterOptions ( ) { SearchString = displayName } ) ;
213
+ if ( group . Count ( ) > 1 )
214
+ {
215
+ throw new InvalidOperationException ( string . Format ( ProjectResources . MultipleGroupsWithDisplayNameFound , displayName ) ) ;
216
+ }
217
+
218
+ if ( group . Count ( ) == 0 )
219
+ {
220
+ throw new InvalidOperationException ( string . Format ( ProjectResources . GroupWithDisplayNameDoesntExist , displayName ) ) ;
221
+ }
222
+
223
+ return group . FirstOrDefault ( ) ;
224
+ }
225
+
200
226
public IEnumerable < PSADGroup > FilterGroups ( ADObjectFilterOptions options , ulong first = ulong . MaxValue , ulong skip = 0 )
201
227
{
202
228
if ( ! string . IsNullOrEmpty ( options . Id ) )
@@ -221,10 +247,18 @@ public IEnumerable<PSADGroup> FilterGroups(ADObjectFilterOptions options, ulong
221
247
}
222
248
else
223
249
{
224
- odataQuery = new Rest . Azure . OData . ODataQuery < ADGroup > ( g => g . DisplayName . StartsWith ( options . SearchString ) ) ;
250
+ if ( ! string . IsNullOrEmpty ( options . SearchString ) && options . SearchString . EndsWith ( "*" ) )
251
+ {
252
+ options . SearchString = options . SearchString . TrimEnd ( '*' ) ;
253
+ odataQuery = new Rest . Azure . OData . ODataQuery < ADGroup > ( g => g . DisplayName . StartsWith ( options . SearchString ) ) ;
254
+ }
255
+ else
256
+ {
257
+ odataQuery = new Rest . Azure . OData . ODataQuery < ADGroup > ( g => g . DisplayName == options . SearchString ) ;
258
+ }
225
259
}
226
260
227
- return new PageEnumerable < ADGroup > (
261
+ return new GenericPageEnumerable < ADGroup > (
228
262
delegate ( )
229
263
{
230
264
return GraphClient . Groups . List ( odataQuery ) ;
@@ -251,7 +285,7 @@ public void RemoveGroup(string groupObjectId)
251
285
252
286
public IEnumerable < PSADObject > GetGroupMembers ( ADObjectFilterOptions options , ulong first = ulong . MaxValue , ulong skip = 0 )
253
287
{
254
- return new PageEnumerable < AADObject > (
288
+ return new GenericPageEnumerable < AADObject > (
255
289
delegate ( )
256
290
{
257
291
return GraphClient . Groups . GetGroupMembers ( options . Id ) ;
@@ -458,16 +492,50 @@ public void RemoveAllAppCredentials(Guid appObjectId)
458
492
459
493
public Guid GetAppObjectIdFromApplicationId ( Guid applicationId )
460
494
{
461
- var appId = applicationId . ToString ( ) ;
462
- var odataQueryFilter = new Rest . Azure . OData . ODataQuery < Application > ( a => a . AppId == appId ) ;
495
+ var applicationIdString = applicationId . ToString ( ) ;
496
+ var odataQueryFilter = new Rest . Azure . OData . ODataQuery < Application > ( a => a . AppId == applicationIdString ) ;
463
497
var app = GetApplicationWithFilters ( odataQueryFilter ) . SingleOrDefault ( ) ;
464
498
if ( app == null )
465
499
{
466
- throw new InvalidOperationException ( String . Format ( ProjectResources . ApplicationWithAppIdDoesntExist , applicationId ) ) ;
500
+ throw new InvalidOperationException ( string . Format ( ProjectResources . ApplicationWithAppIdDoesntExist , applicationId ) ) ;
467
501
}
468
502
return app . ObjectId ;
469
503
}
470
504
505
+ public Guid GetAppObjectIdFromDisplayName ( string displayName )
506
+ {
507
+ var odataQueryFilter = new Rest . Azure . OData . ODataQuery < Application > ( a => a . DisplayName == displayName ) ;
508
+ var app = GetApplicationWithFilters ( odataQueryFilter ) ;
509
+ if ( app == null || app . FirstOrDefault ( ) == null )
510
+ {
511
+ throw new InvalidOperationException ( string . Format ( ProjectResources . ApplicationWithDisplayNameDoesntExist , displayName ) ) ;
512
+ }
513
+
514
+ if ( app . Count ( ) > 1 )
515
+ {
516
+ throw new InvalidOperationException ( string . Format ( ProjectResources . MultipleApplicationsWithDisplayNameFound , displayName ) ) ;
517
+ }
518
+
519
+ return app . FirstOrDefault ( ) . ObjectId ;
520
+ }
521
+
522
+ public Guid GetUserObjectIdFromDisplayName ( string displayName )
523
+ {
524
+ var odataQueryFilter = new Rest . Azure . OData . ODataQuery < User > ( u => u . DisplayName == displayName ) ;
525
+ var user = GraphClient . Users . List ( odataQueryFilter . ToString ( ) ) ;
526
+ if ( user == null || user . FirstOrDefault ( ) == null )
527
+ {
528
+ throw new InvalidOperationException ( string . Format ( ProjectResources . UserWithDisplayNameDoesntExist , displayName ) ) ;
529
+ }
530
+
531
+ if ( user . Count ( ) > 1 )
532
+ {
533
+ throw new InvalidOperationException ( string . Format ( ProjectResources . MultipleUsersWithDisplayNameFound , displayName ) ) ;
534
+ }
535
+
536
+ return new Guid ( user . FirstOrDefault ( ) . ObjectId ) ;
537
+ }
538
+
471
539
private List < KeyCredential > GetSpKeyCredentials ( Guid spObjectId )
472
540
{
473
541
return GraphClient . ServicePrincipals . ListKeyCredentials ( spObjectId . ToString ( ) ) . ToList ( ) ;
@@ -594,6 +662,23 @@ public Guid GetObjectIdFromSPN(string spn)
594
662
return new Guid ( sp . ObjectId ) ;
595
663
}
596
664
665
+ public Guid GetObjectIdFromServicePrincipalDisplayName ( string displayName )
666
+ {
667
+ var odataQueryFilter = new Rest . Azure . OData . ODataQuery < ServicePrincipal > ( s => s . DisplayName == displayName ) ;
668
+ var sp = GraphClient . ServicePrincipals . List ( odataQueryFilter . ToString ( ) ) ;
669
+ if ( sp == null || sp . FirstOrDefault ( ) == null )
670
+ {
671
+ throw new InvalidOperationException ( string . Format ( ProjectResources . ServicePrincipalWithDisplayNameDoesntExist , displayName ) ) ;
672
+ }
673
+
674
+ if ( sp . Count ( ) > 1 )
675
+ {
676
+ throw new InvalidOperationException ( string . Format ( ProjectResources . MultipleServicePrincipalsWithDisplayNameFound , displayName ) ) ;
677
+ }
678
+
679
+ return new Guid ( sp . FirstOrDefault ( ) . ObjectId ) ;
680
+ }
681
+
597
682
public void RemoveApplication ( Guid applicationObjectId )
598
683
{
599
684
GraphClient . Applications . Delete ( applicationObjectId . ToString ( ) ) ;
@@ -606,7 +691,7 @@ public PSADApplication GetApplication(Guid applicationObjectId)
606
691
607
692
public IEnumerable < PSADApplication > GetApplicationWithFilters ( Rest . Azure . OData . ODataQuery < Application > odataQueryFilter , ulong first = ulong . MaxValue , ulong skip = 0 )
608
693
{
609
- return new PageEnumerable < Application > (
694
+ return new GenericPageEnumerable < Application > (
610
695
delegate ( )
611
696
{
612
697
return GraphClient . Applications . List ( odataQueryFilter ) ;
@@ -652,12 +737,12 @@ public PSADServicePrincipal CreateServicePrincipal(CreatePSServicePrincipalParam
652
737
653
738
public PSADServicePrincipal RemoveServicePrincipal ( Guid objectId )
654
739
{
655
- var objId = objectId . ToString ( ) ;
656
- Rest . Azure . OData . ODataQuery < ServicePrincipal > odataQuery = new Rest . Azure . OData . ODataQuery < ServicePrincipal > ( s => s . ObjectId == objId ) ;
740
+ var objectIdString = objectId . ToString ( ) ;
741
+ Rest . Azure . OData . ODataQuery < ServicePrincipal > odataQuery = new Rest . Azure . OData . ODataQuery < ServicePrincipal > ( s => s . ObjectId == objectIdString ) ;
657
742
PSADServicePrincipal servicePrincipal = FilterServicePrincipals ( odataQuery ) . FirstOrDefault ( ) ;
658
743
if ( servicePrincipal != null )
659
744
{
660
- GraphClient . ServicePrincipals . Delete ( objId ) ;
745
+ GraphClient . ServicePrincipals . Delete ( objectIdString ) ;
661
746
}
662
747
else
663
748
{
0 commit comments