Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lower restriction of vocabularies endpoint #1258

Merged
merged 8 commits into from
Nov 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/1258.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Adjust restrictions of vocabularies endpoint [ksuess]
4 changes: 2 additions & 2 deletions src/plone/restapi/services/vocabularies/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
accept="application/json"
factory=".get.VocabulariesGet"
for="Products.CMFPlone.interfaces.IPloneSiteRoot"
permission="plone.restapi.vocabularies"
permission="zope2.View"
name="@vocabularies"
/>

Expand All @@ -17,7 +17,7 @@
accept="application/json"
factory=".get.VocabulariesGet"
for="Products.CMFCore.interfaces.IContentish"
permission="plone.restapi.vocabularies"
permission="zope2.View"
name="@vocabularies"
/>

Expand Down
40 changes: 36 additions & 4 deletions src/plone/restapi/services/vocabularies/get.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
from AccessControl import getSecurityManager


from plone.app.content.browser.vocabulary import DEFAULT_PERMISSION
from plone.app.content.browser.vocabulary import PERMISSIONS

from plone.restapi.interfaces import ISerializeToJson
from plone.restapi.services import Service
from zope.component import ComponentLookupError
Expand All @@ -24,7 +30,23 @@ def _error(self, status, type, message):
self.request.response.setStatus(status)
return {"error": {"type": type, "message": message}}

def _has_permission_to_access_vocabulary(self, vocabulary_name):
"""Check if user is authorized to access the vocabulary.

The endpoint using this method is supposed to have no further protection (`zope.View` permission).
A vocabulary with no further protection follows the `plone.app.vocabularies.DEFAULT_PERMISSION` (usually `zope2.View`).
For further protection the dictionary `plone.app.vocabularies.PERMISSION` is used.
It is a mapping from vocabulary name to permission.
If a vocabulary is mapped there, the permission from the map is taken.
Thus vocabularies can be protected stronger than the default.
"""
sm = getSecurityManager()
return sm.checkPermission(
PERMISSIONS.get(vocabulary_name, DEFAULT_PERMISSION), self.context
)

def reply(self):
# return list of all vocabularies
if len(self.params) == 0:
return [
{
Expand All @@ -36,16 +58,26 @@ def reply(self):
for vocab in getUtilitiesFor(IVocabularyFactory)
]

name = self.params[0]
# return single vocabulary by name
vocabulary_name = self.params[0]
if not self._has_permission_to_access_vocabulary(vocabulary_name):
return self._error(
403,
"Not authorized",
(
f"You are not authorized to access "
f"the vocabulary '{vocabulary_name}'."
),
)

try:
factory = getUtility(IVocabularyFactory, name=name)
factory = getUtility(IVocabularyFactory, name=vocabulary_name)
except ComponentLookupError:
return self._error(
404, "Not Found", f"The vocabulary '{name}' does not exist"
404, "Not Found", f"The vocabulary '{vocabulary_name}' does not exist"
)

vocabulary = factory(self.context)
vocabulary_name = self.params[0]
serializer = getMultiAdapter(
(vocabulary, self.request), interface=ISerializeToJson
)
Expand Down
34 changes: 34 additions & 0 deletions src/plone/restapi/tests/test_services_vocabularies.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from plone.app.testing import SITE_OWNER_NAME
from plone.app.testing import SITE_OWNER_PASSWORD
from plone.app.testing import TEST_USER_ID
from plone.app.testing import TEST_USER_NAME
from plone.app.testing import TEST_USER_PASSWORD
from plone.restapi.testing import PLONE_RESTAPI_DX_FUNCTIONAL_TESTING
from plone.restapi.testing import RelativeSession
from zope.component import getGlobalSiteManager
Expand Down Expand Up @@ -110,6 +112,38 @@ def test_get_vocabulary(self):
},
)

def test_get_builtin_vocabulary(self):
"""Check if built-in vocabularies are protected.

See plone.app.vocabularies.PERMISSIONS
"""
self.api_session.auth = (TEST_USER_NAME, TEST_USER_PASSWORD)

# test editor
setRoles(self.portal, TEST_USER_ID, ["Member", "Contributor", "Editor"])
transaction.commit()
response = self.api_session.get(
"/@vocabularies/plone.app.vocabularies.Keywords"
)
self.assertEqual(200, response.status_code)
response = response.json()
self.assertEqual(
response,
{
"@id": self.portal_url
+ "/@vocabularies/plone.app.vocabularies.Keywords", # noqa
"items": [],
"items_total": 0,
},
)
# test Anonymous
setRoles(self.portal, TEST_USER_ID, ["Anonymous"])
transaction.commit()
response = self.api_session.get(
"/@vocabularies/plone.app.vocabularies.Keywords"
)
self.assertEqual(403, response.status_code)

def test_get_vocabulary_batched(self):
response = self.api_session.get(
"/@vocabularies/plone.restapi.tests.test_vocabulary?b_size=1"
Expand Down