@@ -73,6 +73,15 @@ public PSRoleDefinition GetRoleDefinition(string roleId)
73
73
return AuthorizationManagementClient . RoleDefinitions . GetById ( roleId ) . ToPSRoleDefinition ( ) ;
74
74
}
75
75
76
+ /// <summary>
77
+ /// Gets a single role definition by the role Id guid.
78
+ /// </summary>
79
+ /// <param name="roleId">RoleId guid</param>
80
+ public PSRoleDefinition GetRoleDefinition ( string roleId , string scope )
81
+ {
82
+ return AuthorizationManagementClient . RoleDefinitions . Get ( scope , roleId ) . ToPSRoleDefinition ( ) ;
83
+ }
84
+
76
85
/// <summary>
77
86
/// Gets a single role definition by the role Id guid.
78
87
/// </summary>
@@ -90,22 +99,51 @@ public PSRoleDefinition GetRoleDefinition(Guid roleId)
90
99
/// </summary>
91
100
/// <param name="name">The role name</param>
92
101
/// <returns>The matched role Definitions</returns>
93
- public List < PSRoleDefinition > FilterRoleDefinitions ( string name )
102
+ public List < PSRoleDefinition > FilterRoleDefinitions ( string name , string scope , bool scopeAndBelow = false )
94
103
{
95
104
List < PSRoleDefinition > result = new List < PSRoleDefinition > ( ) ;
105
+
106
+ ODataQuery < RoleDefinitionFilter > odataFilter = null ;
107
+
108
+ if ( scopeAndBelow )
109
+ {
110
+ odataFilter = new ODataQuery < RoleDefinitionFilter > ( item => item . AtScopeAndBelow ( ) && item . RoleName == name ) ;
111
+ }
112
+ else
113
+ {
114
+ odataFilter = new ODataQuery < RoleDefinitionFilter > ( item => item . RoleName == name ) ;
115
+ }
116
+
96
117
result . AddRange ( AuthorizationManagementClient . RoleDefinitions . List (
97
- "subscriptions/" + AuthorizationManagementClient . SubscriptionId ,
98
- new ODataQuery < RoleDefinition > ( item => item . Name == name ) )
118
+ scope ,
119
+ odataFilter )
99
120
. Select ( r => r . ToPSRoleDefinition ( ) ) ) ;
100
121
101
122
return result ;
102
123
}
124
+ public List < PSRoleDefinition > FilterRoleDefinitions ( FilterRoleDefinitionOptions options )
125
+ {
126
+ if ( options . RoleDefinitionId != Guid . Empty )
127
+ {
128
+ return new List < PSRoleDefinition > { GetRoleDefinition ( options . RoleDefinitionId . ToString ( ) , options . Scope ) } ;
129
+ }
130
+ else if ( options . CustomOnly )
131
+ {
132
+ // Special case - if custom only flag is specified then you don't need to lookup on a specific id or name since it will be a bit redundant
133
+ return FilterRoleDefinitionsByCustom ( options . Scope , options . ScopeAndBelow ) ;
134
+ }
135
+ else
136
+ {
137
+ // If RoleDefinition name is not specified (null/empty), service will handle it and return all roles
138
+ return FilterRoleDefinitions ( options . RoleDefinitionName , options . Scope , options . ScopeAndBelow ) ;
139
+ }
140
+ }
103
141
104
142
/// <summary>
105
143
/// Fetches all existing role Definitions.
106
144
/// </summary>
107
145
/// <returns>role Definitions</returns>
108
- public List < PSRoleDefinition > GetRoleDefinitions ( )
146
+ public List < PSRoleDefinition > GetRoleDefinitionsAtScopeAndBelow ( )
109
147
{
110
148
List < PSRoleDefinition > result = new List < PSRoleDefinition > ( ) ;
111
149
result . AddRange ( AuthorizationManagementClient . RoleDefinitions . List (
@@ -118,11 +156,13 @@ public List<PSRoleDefinition> GetRoleDefinitions()
118
156
/// Filters the existing role Definitions by CustomRole.
119
157
/// </summary>
120
158
/// <returns>The custom role Definitions</returns>
121
- public List < PSRoleDefinition > FilterRoleDefinitionsByCustom ( )
159
+ public List < PSRoleDefinition > FilterRoleDefinitionsByCustom ( string scope , bool scopeAndBelow )
122
160
{
123
161
List < PSRoleDefinition > result = new List < PSRoleDefinition > ( ) ;
162
+
124
163
result . AddRange ( AuthorizationManagementClient . RoleDefinitions . List (
125
- "subscriptions/" + AuthorizationManagementClient . SubscriptionId )
164
+ scope ,
165
+ scopeAndBelow ? new ODataQuery < RoleDefinitionFilter > ( filter => filter . AtScopeAndBelow ( ) ) : null )
126
166
. Where ( r => r . Properties . Type == AuthorizationClientExtensions . CustomRole )
127
167
. Select ( r => r . ToPSRoleDefinition ( ) ) ) ;
128
168
return result ;
@@ -138,7 +178,7 @@ public PSRoleAssignment CreateRoleAssignment(FilterRoleAssignmentsOptions parame
138
178
Guid principalId = ActiveDirectoryClient . GetObjectId ( parameters . ADObjectFilter ) ;
139
179
Guid roleAssignmentId = RoleAssignmentNames . Count == 0 ? Guid . NewGuid ( ) : RoleAssignmentNames . Dequeue ( ) ;
140
180
string roleDefinitionId = ! string . IsNullOrEmpty ( parameters . RoleDefinitionName )
141
- ? AuthorizationHelper . GetRoleDefinitionFullyQualifiedId ( subscriptionId , GetRoleRoleDefinition ( parameters . RoleDefinitionName ) . Id )
181
+ ? AuthorizationHelper . GetRoleDefinitionFullyQualifiedId ( subscriptionId , GetSingleRoleDefinitionByName ( parameters . RoleDefinitionName , parameters . Scope ) . Id )
142
182
: AuthorizationHelper . GetRoleDefinitionFullyQualifiedId ( subscriptionId , parameters . RoleDefinitionId ) ;
143
183
144
184
var createParameters = new RoleAssignmentProperties
@@ -318,9 +358,9 @@ public IEnumerable<PSRoleAssignment> RemoveRoleAssignment(FilterRoleAssignmentsO
318
358
return roleAssignments ;
319
359
}
320
360
321
- public PSRoleDefinition GetRoleRoleDefinition ( string name )
361
+ public PSRoleDefinition GetSingleRoleDefinitionByName ( string name , string scope )
322
362
{
323
- List < PSRoleDefinition > roles = FilterRoleDefinitions ( name ) ;
363
+ List < PSRoleDefinition > roles = FilterRoleDefinitions ( name , scope ) ;
324
364
325
365
if ( roles == null || ! roles . Any ( ) )
326
366
{
@@ -334,38 +374,52 @@ public PSRoleDefinition GetRoleRoleDefinition(string name)
334
374
return roles . First ( ) ;
335
375
}
336
376
377
+ public PSRoleDefinition RemoveRoleDefinition ( FilterRoleDefinitionOptions options )
378
+ {
379
+ if ( options . RoleDefinitionId != Guid . Empty )
380
+ {
381
+ return this . RemoveRoleDefinition ( options . RoleDefinitionId , options . Scope ) ;
382
+ }
383
+ else if ( ! string . IsNullOrEmpty ( options . RoleDefinitionName ) )
384
+ {
385
+ return this . RemoveRoleDefinition ( options . RoleDefinitionName , options . Scope ) ;
386
+ }
387
+ else
388
+ {
389
+ throw new InvalidOperationException ( "RoleDefinition Name or Id should be specified." ) ;
390
+ }
391
+ }
337
392
/// <summary>
338
393
/// Deletes a role definition based on the id.
339
394
/// </summary>
340
395
/// <param name="roleDefinitionId">The role definition id to delete</param>
341
396
/// <param name="subscriptionId">Current subscription id</param>
342
397
/// <returns>The deleted role definition.</returns>
343
- public PSRoleDefinition RemoveRoleDefinition ( Guid roleDefinitionId , string subscriptionId )
398
+ public PSRoleDefinition RemoveRoleDefinition ( Guid roleDefinitionId , string scope )
344
399
{
345
400
string id = roleDefinitionId . ToString ( ) ;
346
401
347
- PSRoleDefinition roleDefinition = this . GetRoleDefinition ( roleDefinitionId ) ;
402
+ PSRoleDefinition roleDefinition = this . GetRoleDefinition ( id , scope ) ;
348
403
if ( roleDefinition != null )
349
404
{
350
- return AuthorizationManagementClient . RoleDefinitions
351
- . Delete ( roleDefinition . AssignableScopes . First ( ) , roleDefinitionId . ToString ( ) ) . ToPSRoleDefinition ( ) ;
405
+ return AuthorizationManagementClient . RoleDefinitions . Delete ( scope , id ) . ToPSRoleDefinition ( ) ;
352
406
}
353
407
else
354
408
{
355
- throw new KeyNotFoundException ( string . Format ( ProjectResources . RoleDefinitionWithIdNotFound , id ) ) ;
409
+ throw new KeyNotFoundException ( string . Format ( ProjectResources . RoleDefinitionWithIdNotFound , roleDefinitionId ) ) ;
356
410
}
357
411
}
358
-
412
+
359
413
/// <summary>
360
414
/// Deletes a role definition based on the name.
361
415
/// </summary>
362
416
/// <param name="roleDefinitionName">The role definition name.</param>
363
417
/// <returns>The deleted role definition.</returns>
364
- public PSRoleDefinition RemoveRoleDefinition ( string roleDefinitionName , string subscriptionId )
418
+ public PSRoleDefinition RemoveRoleDefinition ( string roleDefinitionName , string scope )
365
419
{
366
- PSRoleDefinition roleDefinition = this . GetRoleRoleDefinition ( roleDefinitionName ) ;
420
+ PSRoleDefinition roleDefinition = this . GetSingleRoleDefinitionByName ( roleDefinitionName , scope ) ;
367
421
return AuthorizationManagementClient . RoleDefinitions
368
- . Delete ( roleDefinition . AssignableScopes . First ( ) , roleDefinition . Id ) . ToPSRoleDefinition ( ) ;
422
+ . Delete ( scope , roleDefinition . Id ) . ToPSRoleDefinition ( ) ;
369
423
}
370
424
371
425
/// <summary>
@@ -381,34 +435,26 @@ public PSRoleDefinition UpdateRoleDefinition(PSRoleDefinition role, string subsc
381
435
throw new InvalidOperationException ( ProjectResources . RoleDefinitionIdShouldBeAGuid ) ;
382
436
}
383
437
384
- PSRoleDefinition roleDefinition = this . GetRoleDefinition ( roleDefinitionId ) ;
438
+ PSRoleDefinition roleDefinition = this . GetRoleDefinition ( roleDefinitionId . ToString ( ) , role . AssignableScopes . First ( ) ) ;
385
439
if ( roleDefinition == null )
386
440
{
387
441
throw new KeyNotFoundException ( string . Format ( ProjectResources . RoleDefinitionWithIdNotFound , role . Id ) ) ;
388
442
}
389
-
390
- string roleDefinitionFullyQualifiedId = AuthorizationHelper . GetRoleDefinitionFullyQualifiedId ( subscriptionId , role . Id ) ;
391
-
392
- roleDefinition . Name = role . Name ?? roleDefinition . Name ;
393
- roleDefinition . Actions = role . Actions ?? roleDefinition . Actions ;
394
- roleDefinition . NotActions = role . NotActions ?? roleDefinition . NotActions ;
395
- roleDefinition . AssignableScopes = role . AssignableScopes ?? roleDefinition . AssignableScopes ;
396
- roleDefinition . Description = role . Description ?? roleDefinition . Description ;
397
-
443
+
398
444
ValidateRoleDefinition ( roleDefinition ) ;
399
445
400
446
return
401
447
AuthorizationManagementClient . RoleDefinitions . CreateOrUpdate (
402
- roleDefinitionId . ToString ( ) ,
403
448
roleDefinition . AssignableScopes . First ( ) ,
449
+ roleDefinitionId . ToString ( ) ,
404
450
new RoleDefinition ( )
405
451
{
406
- Id = roleDefinitionFullyQualifiedId ,
407
452
Name = roleDefinitionId . ToString ( ) ,
408
453
Properties =
409
454
new RoleDefinitionProperties ( )
410
455
{
411
- RoleName = roleDefinition . Name ,
456
+ AssignableScopes = roleDefinition . AssignableScopes ,
457
+ Description = roleDefinition . Description ,
412
458
Permissions =
413
459
new List < Permission > ( )
414
460
{
@@ -418,8 +464,7 @@ public PSRoleDefinition UpdateRoleDefinition(PSRoleDefinition role, string subsc
418
464
NotActions = roleDefinition . NotActions
419
465
}
420
466
} ,
421
- AssignableScopes = roleDefinition . AssignableScopes ,
422
- Description = roleDefinition . Description
467
+ RoleName = roleDefinition . Name
423
468
}
424
469
}
425
470
) . ToPSRoleDefinition ( ) ;
0 commit comments