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

Sanitize copying a survey to the client. #692

Merged
merged 4 commits into from
Jan 26, 2024
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
83 changes: 30 additions & 53 deletions src/euphorie/client/browser/publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,12 @@

Copy and publish Surveys from the admin to the client database.
"""
from AccessControl.SecurityManagement import getSecurityManager
from AccessControl.SecurityManagement import newSecurityManager
from AccessControl.SecurityManagement import setSecurityManager
from Acquisition import aq_inner
from Acquisition import aq_parent
from euphorie.client import MessageFactory as _
from euphorie.content.interfaces import ICustomRisksModule
from euphorie.content.interfaces import ObjectPublishedEvent
from euphorie.content.utils import IToolTypesInfo
from OFS.event import ObjectWillBeRemovedEvent
from plone import api
from plone.scale.storage import AnnotationStorage
from plonetheme.nuplone.utils import getPortal
Expand Down Expand Up @@ -92,30 +88,21 @@ def CopyToClient(survey, preview=False):
# Clear any scaled logos
AnnotationStorage(target).storage.clear()

copy = source._getCopy(target)
if preview:
copy.id = "preview"
new_id = "preview"
else:
copy.id = surveygroup.id
new_id = surveygroup.id
if new_id in target:
api.content.delete(obj=target[new_id])

copy = api.content.copy(source, target, id=new_id)
copy.title = surveygroup.title
copy.obsolete = surveygroup.obsolete
copy.evaluation_algorithm = surveygroup.evaluation_algorithm
copy.version = source.id
copy.published = datetime.datetime.now()
copy.preview = preview

if copy.id in target:
# We must suppress events to prevent the can-not-delete-published-
# content check from blocking us.
# XXX: We need however the ObjectWillBeRemovedEvent event to be called
# otherwise the removed objects are not uncatalogged.
to_delete = target._getOb(copy.id)
notify(ObjectWillBeRemovedEvent(to_delete, target, copy.id))
target._delObject(copy.id, suppress_events=True)

target._setObject(copy.id, copy, suppress_events=True)
copy = target[copy.id]
copy._postCopy(target, op=0)
copy.reindexObject()

notify(ObjectPublishedEvent(source))
return copy
Expand Down Expand Up @@ -170,22 +157,17 @@ def PublishToClient(survey, preview=False):
the currently active Zope user to make sure content can be created in the
client.
"""
pas = getToolByName(survey, "acl_users")
clientuser = pas.getUserById("client")
sm = getSecurityManager()
tti = getUtility(IToolTypesInfo)
tool_types_info = tti()
tool_type_data = tool_types_info.get(
survey.tool_type, tool_types_info.get(tti.default_tool_type)
)
try:
newSecurityManager(None, clientuser)
survey = CopyToClient(survey, preview)
if tool_type_data.get("use_omega_risks", True):
EnableCustomRisks(survey)
survey.published = (survey.id, survey.title, datetime.datetime.now())
finally:
setSecurityManager(sm)
with api.env.adopt_user("client"):
with api.env.adopt_roles(["Manager"]):
survey = CopyToClient(survey, preview)
if tool_type_data.get("use_omega_risks", True):
EnableCustomRisks(survey)
survey.published = (survey.id, survey.title, datetime.datetime.now())
return survey


Expand All @@ -206,28 +188,23 @@ def handleSurveyUnpublish(survey, event):
sector = aq_parent(surveygroup)
country = aq_parent(sector)

pas = getToolByName(survey, "acl_users")
clientuser = pas.getUserById("client")
sm = getSecurityManager()
try:
newSecurityManager(None, clientuser)
client = getPortal(survey).client
try:
clientcountry = client[country.id]
clientsector = clientcountry[sector.id]
clientsector[surveygroup.id]
except KeyError:
log.info(
"Trying to unpublish unpublished survey %s",
"/".join(survey.getPhysicalPath()),
)
return

clientsector.manage_delObjects([surveygroup.id])
if not clientsector.keys():
clientcountry.manage_delObjects([clientsector.id])
finally:
setSecurityManager(sm)
with api.env.adopt_user("client"):
with api.env.adopt_roles(["Manager"]):
client = getPortal(survey).client
try:
clientcountry = client[country.id]
clientsector = clientcountry[sector.id]
clientsector[surveygroup.id]
except KeyError:
log.info(
"Trying to unpublish unpublished survey %s",
"/".join(survey.getPhysicalPath()),
)
return

clientsector.manage_delObjects([surveygroup.id])
if not clientsector.keys():
clientcountry.manage_delObjects([clientsector.id])


class PublishSurvey(form.Form):
Expand Down
2 changes: 1 addition & 1 deletion src/euphorie/client/tests/test_publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def testPublish(self):
self.assertEqual(survey.objectIds(), copy.objectIds())

brains = survey.portal_catalog(portal_type="euphorie.risk")
self.assertEqual(len(brains), 1)
self.assertEqual(len(brains), 2)

self.survey.manage_delObjects(["1"])
copy = publish.CopyToClient(survey)
Expand Down
4 changes: 0 additions & 4 deletions src/euphorie/content/survey.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,6 @@ class Survey(Container):

dirty = False

def _canCopy(self, op=0):
"""Tell Zope2 that this object can not be copied."""
return op

def _get_id(self, orig_id):
"""Pick an id for pasted content."""
frame = sys._getframe(1)
Expand Down
5 changes: 0 additions & 5 deletions src/euphorie/content/tests/test_functional_survey.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,3 @@ def testCanDeleteItemsWhenNotPublished(self):
newSecurityManager(None, sector)
manager = getSecurityManager()
self.assertTrue(manager.checkPermission("Delete objects", survey))

def testCanNotBeCopied(self):
self.loginAsPortalOwner()
survey = self.createSurvey()
self.assertFalse(survey.cb_isCopyable())
Loading