Skip to content

Commit f9a64a2

Browse files
committed
Raise Unauthorized when the user has no permission to create the content
1 parent 6a1f39b commit f9a64a2

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

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
@@ -76,7 +77,7 @@ def create(
7677
# For dexterity objects we want to not use the invokeFactory
7778
# method because we want to have the id generated by the name chooser
7879
if not fti.isConstructionAllowed(container):
79-
raise ValueError(f"Cannot create {type}")
80+
raise Unauthorized(f"Cannot create {type}")
8081

8182
container_fti = container.getTypeInfo()
8283
if container_fti is not None and not container_fti.allowType(type):

src/plone/api/tests/test_content.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,18 @@ 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+
394+
logout()
395+
with self.assertRaises(Unauthorized):
396+
api.content.create(
397+
container=self.portal,
398+
type="Dexterity Item",
399+
id="foo",
400+
)
401+
390402
def test_create_raises_unicodedecodeerror(self):
391403
"""Test that the create method raises UnicodeDecodeErrors correctly."""
392404
site = getGlobalSiteManager()

0 commit comments

Comments
 (0)