Skip to content

Commit 2f1ea9b

Browse files
committed
Raise Unauthorized when the user has no permission to create the content
1 parent 03e4279 commit 2f1ea9b

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

src/plone/api/content.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Module that provides functionality for content manipulation."""
22

3+
from AccessControl import Unauthorized
34
from copy import copy as _copy
45
from plone.api import portal
56
from plone.api.exc import InvalidParameterError
@@ -69,7 +70,7 @@ def create(
6970
# For dexterity objects we want to not use the invokeFactory
7071
# method because we want to have the id generated by the name chooser
7172
if not fti.isConstructionAllowed(container):
72-
raise ValueError(f"Cannot create {type}")
73+
raise Unauthorized(f"Cannot create {type}")
7374

7475
container_fti = container.getTypeInfo()
7576
if container_fti is not None and not container_fti.allowType(type):

src/plone/api/tests/test_content.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,17 @@ def test_create_with_not_lowercase_id(self):
387387
)
388388
self.assertEqual(obj.id, "Doc1")
389389

390+
def test_create_anonymous_unauthorized(self):
391+
from AccessControl import Unauthorized
392+
from plone.app.testing import logout
393+
logout()
394+
with self.assertRaises(Unauthorized):
395+
obj = api.content.create(
396+
container=self.portal,
397+
type="Dexterity Item",
398+
id="foo",
399+
)
400+
390401
def test_create_raises_unicodedecodeerror(self):
391402
"""Test that the create method raises UnicodeDecodeErrors correctly."""
392403
site = getGlobalSiteManager()

0 commit comments

Comments
 (0)