Skip to content

Commit 409c2e6

Browse files
authored
Add get_permissions() method to list all permissions on server (#376)
* Add get_permissions() method to list all permissions on server Fix README param * Fix intermittent test
1 parent 4ec468c commit 409c2e6

File tree

6 files changed

+53
-0
lines changed

6 files changed

+53
-0
lines changed

README.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,19 @@ The objects permissions can be specified or modified by passing a ``permissions`
196196
client.update_record(data=record)
197197
198198
199+
In order to obtain a list of all the permissions, on every object, use the ``get_permissions()`` method:
200+
201+
.. code-block:: python
202+
203+
all_perms = client.get_permissions(exclude_resource_names=("record",))
204+
205+
has_collection_perms = any(
206+
p for p in all_perms
207+
if p["collection_id"] == "my-collection"
208+
and "write" in p["permissions"]
209+
)
210+
211+
199212
Get or create
200213
-------------
201214

src/kinto_http/client.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,16 @@ def get_paginated_records(self, *, collection=None, bucket=None, **kwargs) -> Li
667667

668668
return self._paginated_generator(endpoint, **kwargs)
669669

670+
@retry_timeout
671+
def get_permissions(self, exclude_resource_names=None, **kwargs):
672+
endpoint = self._get_endpoint("permissions")
673+
params = kwargs.setdefault("params", {})
674+
params.setdefault("_sort", "id")
675+
if exclude_resource_names:
676+
params["exclude_resource_name"] = ",".join(exclude_resource_names)
677+
body, _ = self.session.request("get", endpoint, **kwargs)
678+
return body["data"]
679+
670680
def _paginated_generator(self, endpoint, *, if_none_match=None, **kwargs):
671681
headers = {}
672682
if if_none_match is not None:

src/kinto_http/endpoints.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class Endpoints(object):
1111
endpoints = {
1212
"root": "{root}/",
1313
"batch": "{root}/batch",
14+
"permissions": "{root}/permissions",
1415
"buckets": "{root}/buckets",
1516
"bucket": "{root}/buckets/{bucket}",
1617
"history": "{root}/buckets/{bucket}/history",

tests/config/kinto.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ kinto.bucket_create_principals = account:user
1616

1717
kinto.attachment.base_path = /tmp
1818

19+
kinto.experimental_permissions_endpoint = true
20+
1921
[server:main]
2022
use = egg:waitress#main
2123
host = 0.0.0.0

tests/test_client.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1414,3 +1414,22 @@ def test_add_attachment_guesses_mimetype(record_setup: Client, tmp_path):
14141414
permissions=None,
14151415
files=[("attachment", ("file.txt", b"hello", "text/plain"))],
14161416
)
1417+
1418+
1419+
def test_get_permissions(client_setup: Client):
1420+
client = client_setup
1421+
mock_response(client.session)
1422+
1423+
client.get_permissions()
1424+
url = "/permissions"
1425+
client.session.request.assert_called_with("get", url, params={"_sort": "id"})
1426+
1427+
client.get_permissions(exclude_resource_names=("record", "group"))
1428+
client.session.request.assert_called_with(
1429+
"get",
1430+
url,
1431+
params={
1432+
"exclude_resource_name": "record,group",
1433+
"_sort": "id",
1434+
},
1435+
)

tests/test_functional.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,3 +582,11 @@ def test_removing_an_attachment(functional_setup, tmp_path):
582582

583583
record = client.get_record(id="abc")
584584
assert record["data"]["attachment"] is None
585+
586+
587+
def test_get_permissions(functional_setup):
588+
client = functional_setup
589+
perms = client.get_permissions()
590+
591+
perms_by_uri = {p["uri"]: p for p in perms}
592+
assert set(perms_by_uri["/accounts/user"]["permissions"]) == {"read", "write"}

0 commit comments

Comments
 (0)