@@ -356,6 +356,9 @@ private ResourcePathAuthorizationResult ProcessAuthorizationRequestForResourcePa
356
356
SubordinateResourcePathsAuthorizationResults = [ ]
357
357
} ;
358
358
359
+ var mustCheckOptionalRole = authorizationRequest . RoleName ? . EndsWith ( '!' ) ?? false ;
360
+ var optionalRole = authorizationRequest . RoleName ? . TrimEnd ( '!' ) ?? string . Empty ;
361
+
359
362
// Combine the principal id and security group ids into one list.
360
363
var securityPrincipalIds = new List < string > { authorizationRequest . UserContext . SecurityPrincipalId } ;
361
364
if ( authorizationRequest . UserContext . SecurityGroupIds != null )
@@ -388,20 +391,22 @@ private ResourcePathAuthorizationResult ProcessAuthorizationRequestForResourcePa
388
391
// Check if the actions of the role definition include the requested action.
389
392
if ( resourcePath . IncludesResourcePath ( roleAssignment . ScopeResourcePath ! ) )
390
393
{
391
- if ( roleAssignment . RoleDefinition ! . Name == authorizationRequest . RoleName )
394
+ if ( roleAssignment . RoleDefinition ! . Name == optionalRole )
392
395
result . HasRequiredRole = true ;
393
396
394
397
if ( roleAssignment . AllowedActions . Contains ( authorizationRequest . Action ) )
395
398
{
396
399
result . Authorized = true ;
397
400
398
- // If we are not asked to include roles or actions and not asked to expand resource paths,
401
+ // If we are not asked to include roles or actions, not asked to expand resource paths,
402
+ // and checking the assignment of the optional role is not mandatory,
399
403
// we can return immediately (this is the most common case).
400
404
// Otherwise, we need to go through the entire list of security principals and their role assignments,
401
- // to include collect all the roles/actions and/or all the subordinate authorized resource paths.
405
+ // to include all the roles/actions and/or all the subordinate authorized resource paths.
402
406
if ( ! authorizationRequest . IncludeRoles
403
407
&& ! authorizationRequest . IncludeActions
404
- && ! authorizationRequest . ExpandResourceTypePaths )
408
+ && ! authorizationRequest . ExpandResourceTypePaths
409
+ && ! mustCheckOptionalRole )
405
410
return result ;
406
411
407
412
allSecurableActions . UnionWith ( roleAssignment . AllowedActions ) ;
0 commit comments