Skip to content

Commit 9a1efd4

Browse files
Jenkinsopenstack-gerrit
authored andcommitted
Merge "Non-Admin can't list own projects"
2 parents 97a4ac4 + 49f6032 commit 9a1efd4

4 files changed

Lines changed: 69 additions & 1 deletion

File tree

doc/source/command-objects/project.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ List projects
9595
openstack project list
9696
[--domain <domain>]
9797
[--user <user>]
98+
[--my-projects]
9899
[--long]
99100
[--sort <key>[:<direction>,<key>:<direction>,..]]
100101
@@ -110,6 +111,12 @@ List projects
110111
111112
.. versionadded:: 3
112113
114+
.. option:: --my-projects
115+
116+
List projects for the authenticated user. Supersedes other filters.
117+
118+
.. versionadded:: 3
119+
113120
.. option:: --long
114121
115122
List additional fields in output

openstackclient/identity/v3/project.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,12 @@ def get_parser(self, prog_name):
188188
metavar='<user>',
189189
help=_('Filter projects by <user> (name or ID)'),
190190
)
191+
parser.add_argument(
192+
'--my-projects',
193+
action='store_true',
194+
help=_('List projects for the authenticated user. '
195+
'Supersedes other filters.'),
196+
)
191197
parser.add_argument(
192198
'--long',
193199
action='store_true',
@@ -228,9 +234,25 @@ def take_action(self, parsed_args):
228234

229235
kwargs['user'] = user_id
230236

231-
data = identity_client.projects.list(**kwargs)
237+
if parsed_args.my_projects:
238+
# NOTE(adriant): my-projects supersedes all the other filters.
239+
kwargs = {'user': self.app.client_manager.auth_ref.user_id}
240+
241+
try:
242+
data = identity_client.projects.list(**kwargs)
243+
except ks_exc.Forbidden:
244+
# NOTE(adriant): if no filters, assume a forbidden is non-admin
245+
# wanting their own project list.
246+
if not kwargs:
247+
user = self.app.client_manager.auth_ref.user_id
248+
data = identity_client.projects.list(
249+
user=user)
250+
else:
251+
raise
252+
232253
if parsed_args.sort:
233254
data = utils.sort_items(data, parsed_args.sort)
255+
234256
return (columns,
235257
(utils.get_item_properties(
236258
s, columns,

openstackclient/tests/unit/identity/v3/test_project.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,36 @@ def test_project_list_sort(self):
617617

618618
self.assertEqual(datalists, tuple(data))
619619

620+
def test_project_list_my_projects(self):
621+
auth_ref = identity_fakes.fake_auth_ref(
622+
identity_fakes.TOKEN_WITH_PROJECT_ID,
623+
)
624+
ar_mock = mock.PropertyMock(return_value=auth_ref)
625+
type(self.app.client_manager).auth_ref = ar_mock
626+
627+
arglist = [
628+
'--my-projects',
629+
]
630+
verifylist = [
631+
('my_projects', True),
632+
]
633+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
634+
635+
# In base command class Lister in cliff, abstract method take_action()
636+
# returns a tuple containing the column names and an iterable
637+
# containing the data to be listed.
638+
columns, data = self.cmd.take_action(parsed_args)
639+
self.projects_mock.list.assert_called_with(
640+
user=self.app.client_manager.auth_ref.user_id)
641+
642+
collist = ('ID', 'Name')
643+
self.assertEqual(collist, columns)
644+
datalist = ((
645+
self.project.id,
646+
self.project.name,
647+
), )
648+
self.assertEqual(datalist, tuple(data))
649+
620650

621651
class TestProjectSet(TestProject):
622652

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
features:
3+
- |
4+
The ``project list`` command lists all projects when called by an
5+
admin user. For non-admin users it will now list projects for the
6+
authenticated user instead of exiting with an authorization failure.
7+
The ``--my-projects`` option has also been added to the ``project list``
8+
command to allow admin users to list their own projects.
9+
[Bug `1627555 <https://bugs.launchpad.net/bugs/1627555>`_]

0 commit comments

Comments
 (0)