Skip to content

Commit 0088515

Browse files
committed
assignee_object_id
1 parent 28ce46f commit 0088515

2 files changed

Lines changed: 28 additions & 20 deletions

File tree

src/azure-cli/azure/cli/command_modules/role/_params.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -327,10 +327,11 @@ def load_arguments(self, _):
327327
c.argument('include_inherited', action='store_true', help='include assignments applied on parent scopes')
328328
c.argument('can_delegate', action='store_true', help='when set, the assignee will be able to create further role assignments to the same role')
329329
c.argument('assignee', help='represent a user, group, or service principal. supported format: object id, user sign-in name, or service principal name')
330-
c.argument('assignee_object_id', help="Use this parameter instead of '--assignee' to bypass Graph API invocation in case of insufficient privileges. "
331-
"This parameter only works with object ids for users, groups, service principals, and "
332-
"managed identities. For managed identities use the principal id. For service principals, "
333-
"use the object id and not the app id.")
330+
c.argument('assignee_object_id',
331+
help="The assignee's object ID (also known as principal ID). "
332+
"Use this argument instead of '--assignee' to bypass Microsoft Graph invocation in case "
333+
"the logged-in account has no permission or the machine has no network access to query "
334+
"Microsoft Graph.")
334335
c.argument('ids', nargs='+', help='space-separated role assignment ids')
335336
c.argument('include_classic_administrators', arg_type=get_three_state_flag(),
336337
help='list default role assignments for subscription classic administrators, aka co-admins')

src/azure-cli/azure/cli/command_modules/role/custom.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ def create_role_assignment(cmd, role, scope,
209209
assignment_name=assignment_name)
210210
except Exception as ex: # pylint: disable=broad-except
211211
if _error_caused_by_role_assignment_exists(ex): # for idempotent
212-
return list_role_assignments(cmd, assignee=assignee, role=role, scope=scope)[0]
212+
return list_role_assignments(cmd, assignee_object_id=object_id, role=role, scope=scope)[0]
213213
raise
214214

215215

@@ -232,13 +232,17 @@ def _create_role_assignment(cli_ctx, role, assignee, resource_group_name=None, s
232232
condition=condition, condition_version=condition_version)
233233

234234

235-
def list_role_assignments(cmd, assignee=None, role=None, resource_group_name=None,
235+
def list_role_assignments(cmd,
236+
assignee=None, assignee_object_id=None,
237+
role=None, resource_group_name=None,
236238
scope=None, include_inherited=False,
237239
show_all=False, include_groups=False, include_classic_administrators=False):
238-
'''
239-
:param include_groups: include extra assignments to the groups of which the user is a
240-
member(transitively).
241-
'''
240+
# include_groups: include extra assignments to the groups of which the user is a member(transitively).
241+
if assignee and assignee_object_id:
242+
raise CLIError('Usage error: Provide only one of --assignee or --assignee-object-id.')
243+
if assignee and not assignee_object_id:
244+
assignee_object_id = _resolve_object_id(cmd.cli_ctx, assignee, fallback_to_object_id=True)
245+
242246
if include_classic_administrators:
243247
logger.warning(CLASSIC_ADMINISTRATOR_WARNING)
244248

@@ -256,7 +260,7 @@ def list_role_assignments(cmd, assignee=None, role=None, resource_group_name=Non
256260
definitions_client._config.subscription_id)
257261

258262
assignments = _search_role_assignments(cmd.cli_ctx, assignments_client, definitions_client,
259-
scope, assignee, role,
263+
scope, assignee_object_id, role,
260264
include_inherited, include_groups)
261265

262266
results = todict(assignments) if assignments else []
@@ -520,13 +524,20 @@ def _get_displayable_name(graph_object):
520524
return graph_object['displayName'] or ''
521525

522526

523-
def delete_role_assignments(cmd, ids=None, assignee=None, role=None, resource_group_name=None,
527+
def delete_role_assignments(cmd, ids=None,
528+
assignee=None, assignee_object_id=None,
529+
role=None, resource_group_name=None,
524530
scope=None, include_inherited=False,
525531
yes=None): # pylint: disable=unused-argument
526532
# yes is currently a no-op
527-
if not any((ids, assignee, role, resource_group_name, scope)):
533+
if not any((ids, assignee, assignee_object_id, role, resource_group_name, scope)):
528534
raise ArgumentUsageError('Please provide at least one of these arguments: '
529-
'--ids, --assignee, --role, --resource-group, --scope')
535+
'--ids, --assignee, --assignee-object-id, --role, --resource-group, --scope')
536+
537+
if assignee and assignee_object_id:
538+
raise CLIError('Usage error: Provide only one of --assignee or --assignee-object-id.')
539+
if assignee and not assignee_object_id:
540+
assignee_object_id = _resolve_object_id(cmd.cli_ctx, assignee, fallback_to_object_id=True)
530541

531542
factory = _auth_client_factory(cmd.cli_ctx, scope)
532543
assignments_client = factory.role_assignments
@@ -558,7 +569,7 @@ def delete_role_assignments(cmd, ids=None, assignee=None, role=None, resource_gr
558569
scope = _build_role_scope(resource_group_name, scope,
559570
assignments_client._config.subscription_id)
560571
assignments = _search_role_assignments(cmd.cli_ctx, assignments_client, definitions_client,
561-
scope, assignee, role, include_inherited,
572+
scope, assignee_object_id, role, include_inherited,
562573
include_groups=False)
563574

564575
if assignments:
@@ -569,11 +580,7 @@ def delete_role_assignments(cmd, ids=None, assignee=None, role=None, resource_gr
569580

570581

571582
def _search_role_assignments(cli_ctx, assignments_client, definitions_client,
572-
scope, assignee, role, include_inherited, include_groups):
573-
assignee_object_id = None
574-
if assignee:
575-
assignee_object_id = _resolve_object_id(cli_ctx, assignee, fallback_to_object_id=True)
576-
583+
scope, assignee_object_id, role, include_inherited, include_groups):
577584
# https://learn.microsoft.com/en-us/azure/role-based-access-control/role-assignments-list-rest
578585
# "atScope()" and "principalId eq '{value}'" query cannot be used together (API limitation).
579586
# always use "scope" if provided, so we can get assignments beyond subscription e.g. management groups

0 commit comments

Comments
 (0)