Skip to content

Commit

Permalink
Raise Unauthorized when the user has no permission to create the content
Browse files Browse the repository at this point in the history
  • Loading branch information
ale-rt committed Jan 30, 2025
1 parent 6a1f39b commit f9a64a2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/plone/api/content.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Module that provides functionality for content manipulation."""

from AccessControl import Unauthorized
from copy import copy as _copy
from plone.api import portal
from plone.api.exc import InvalidParameterError
Expand Down Expand Up @@ -76,7 +77,7 @@ def create(
# For dexterity objects we want to not use the invokeFactory
# method because we want to have the id generated by the name chooser
if not fti.isConstructionAllowed(container):
raise ValueError(f"Cannot create {type}")
raise Unauthorized(f"Cannot create {type}")

container_fti = container.getTypeInfo()
if container_fti is not None and not container_fti.allowType(type):
Expand Down
12 changes: 12 additions & 0 deletions src/plone/api/tests/test_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,18 @@ def test_create_with_not_lowercase_id(self):
)
self.assertEqual(obj.id, "Doc1")

def test_create_anonymous_unauthorized(self):
from AccessControl import Unauthorized
from plone.app.testing import logout

logout()
with self.assertRaises(Unauthorized):
api.content.create(
container=self.portal,
type="Dexterity Item",
id="foo",
)

def test_create_raises_unicodedecodeerror(self):
"""Test that the create method raises UnicodeDecodeErrors correctly."""
site = getGlobalSiteManager()
Expand Down

0 comments on commit f9a64a2

Please sign in to comment.