Skip to content

Commit 845de41

Browse files
author
Dean Troyer
committed
Return current user/project for user/project show commands
If non-admin user attempts 'project show' or 'user show' on the currently authenticated project or user return the information that is already in the service catalog rather than throwing a Forbidden error. Change-Id: Ieeb6eacf71a471e410fbd3c09e7871740547e890
1 parent ae957b1 commit 845de41

File tree

2 files changed

+53
-17
lines changed

2 files changed

+53
-17
lines changed

openstackclient/identity/v2_0/project.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from cliff import lister
2323
from cliff import show
2424

25+
from keystoneclient.openstack.common.apiclient import exceptions as ksc_exc
2526
from openstackclient.common import parseractions
2627
from openstackclient.common import utils
2728

@@ -238,11 +239,28 @@ def get_parser(self, prog_name):
238239
def take_action(self, parsed_args):
239240
self.log.debug('take_action(%s)', parsed_args)
240241
identity_client = self.app.client_manager.identity
241-
project = utils.find_resource(
242-
identity_client.tenants,
243-
parsed_args.project,
244-
)
245242

246243
info = {}
247-
info.update(project._info)
244+
try:
245+
project = utils.find_resource(
246+
identity_client.tenants,
247+
parsed_args.project,
248+
)
249+
info.update(project._info)
250+
except ksc_exc.Forbidden as e:
251+
auth_ref = self.app.client_manager.auth_ref
252+
if (
253+
parsed_args.project == auth_ref.project_id or
254+
parsed_args.project == auth_ref.project_name
255+
):
256+
# Ask for currently auth'ed project so return it
257+
info = {
258+
'id': auth_ref.project_id,
259+
'name': auth_ref.project_name,
260+
# True because we don't get this far if it is disabled
261+
'enabled': True,
262+
}
263+
else:
264+
raise e
265+
248266
return zip(*sorted(six.iteritems(info)))

openstackclient/identity/v2_0/user.py

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from cliff import lister
2323
from cliff import show
2424

25+
from keystoneclient.openstack.common.apiclient import exceptions as ksc_exc
2526
from openstackclient.common import utils
2627

2728

@@ -347,20 +348,37 @@ def take_action(self, parsed_args):
347348
self.log.debug('take_action(%s)', parsed_args)
348349
identity_client = self.app.client_manager.identity
349350

350-
user = utils.find_resource(
351-
identity_client.users,
352-
parsed_args.user,
353-
)
351+
info = {}
352+
try:
353+
user = utils.find_resource(
354+
identity_client.users,
355+
parsed_args.user,
356+
)
357+
info.update(user._info)
358+
except ksc_exc.Forbidden as e:
359+
auth_ref = self.app.client_manager.auth_ref
360+
if (
361+
parsed_args.user == auth_ref.user_id or
362+
parsed_args.user == auth_ref.username
363+
):
364+
# Ask for currently auth'ed project so return it
365+
info = {
366+
'id': auth_ref.user_id,
367+
'name': auth_ref.username,
368+
'project_id': auth_ref.project_id,
369+
# True because we don't get this far if it is disabled
370+
'enabled': True,
371+
}
372+
else:
373+
raise e
354374

355-
if 'tenantId' in user._info:
356-
user._info.update(
357-
{'project_id': user._info.pop('tenantId')}
375+
if 'tenantId' in info:
376+
info.update(
377+
{'project_id': info.pop('tenantId')}
358378
)
359-
if 'tenant_id' in user._info:
360-
user._info.update(
361-
{'project_id': user._info.pop('tenant_id')}
379+
if 'tenant_id' in info:
380+
info.update(
381+
{'project_id': info.pop('tenant_id')}
362382
)
363383

364-
info = {}
365-
info.update(user._info)
366384
return zip(*sorted(six.iteritems(info)))

0 commit comments

Comments
 (0)