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

Add Plone Site: do a transaction commit before profiles #2410

Merged

Conversation

mauritsvanrees
Copy link
Member

@mauritsvanrees mauritsvanrees commented May 14, 2018

When adding a Plone Site, do a transaction commit before applying extra profiles. This avoids errors in some cases when selecting add-ons in the advanced Plone Site add form, where installing them right after site creation in the add-ons control panel goes fine.

It should certainly fix some CSRF autoprotection errors raised by the first view after creating a Plone Site.

Fixes #2316.
And I think there was a similar issue raised recently, but I cannot find it.

Note for tests: since this does a transaction commit, any test code that calls addPloneSite should use a functional test layer, to avoid leakage into other tests. Case in point is an initial test failure in CMFPlone itself:

Error in test testPlonesiteWithoutUnicodeTitle
(Products.CMFPlone.tests.test_factory.TestFactoryPloneSite)
Traceback (most recent call last):
  File ".../unittest/case.py", line 329, in run
    testMethod()
  File ".../Products/CMFPlone/tests/test_factory.py", line 29,
 in testPlonesiteWithoutUnicodeTitle
    self.app, 'ploneFoo', title=TITLE, setup_content=False)
  File ".../Products/CMFPlone/factory.py", line 132, in addPloneSite
    context._setObject(site_id, PloneSite(site_id))
  File ".../OFS/ObjectManager.py", line 324, in _setObject
    v = self._checkId(id)
  File ".../OFS/ObjectManager.py", line 127, in checkValidId
    'The id "%s" is invalid - it is already in use.' % id)
BadRequest: The id "ploneFoo" is invalid - it is already in use.

I fixed that in the PR by adding a condition: when no extra profiles are selected, don't do a commit.
Only two test files in CMFPlone call addPloneSite directly, plus of course plone.app.testing in test layer setup, so this should not normally cause problems.

This PR is for master (5.2). I can port it to other versions once approved. I would say: only 5.1.

…ra profiles.

This avoids errors in some cases when selecting add-ons in the advanced Plone Site add form,
where installing them right after site creation in the add-ons control panel goes fine.

Fixes #2316
@mauritsvanrees mauritsvanrees force-pushed the add-plonesite-commit-before-applying-profiles-master branch from 5c8f597 to 4be00d0 Compare May 14, 2018 06:52
@jensens
Copy link
Member

jensens commented May 14, 2018

LGTM

@jensens jensens merged commit 491e0eb into master May 14, 2018
@jensens
Copy link
Member

jensens commented May 14, 2018

+1 for backporting this to Plone 5.1

@jensens jensens deleted the add-plonesite-commit-before-applying-profiles-master branch May 14, 2018 09:18
@zopyx
Copy link
Member

zopyx commented May 14, 2018

Is it appropriate to commit here? addPloneSite() is supposed to be transactional. An error at some point of the setup procedure should not leave any traces...so what happens if an error occurs after the commit?

@mauritsvanrees
Copy link
Member Author

Then you have a Plone Site without add-ons. That is probably acceptable here.

The only thing that happens after applying the extra profiles, is a redirect to the Plone Site url:
https://github.com/plone/Products.CMFPlone/blob/5.1.2/Products/CMFPlone/browser/admin.py#L265-L276

And... actually it should return an empty string after setting the redirect. Currently it also calls self.index(), and the redirect only really happens after the rendering is done. Rendering the index/template is unneeded, and might be a source of CSRF errors. Is that something you could try instead in a project where you see this problem?

@zopyx
Copy link
Member

zopyx commented May 14, 2018

I don't think that it is acceptable...everything can happen during during the extensions profile after the commit. This will of course produce a naked Plone site but it would not produce what you expect. There is an unwritten contract that you should not commit yourself in Zope and Plone because this is handled by the ZPublisher. In that sense: addPloneSite() is no longer transactional. Also consider using addPloneSite() as part of a provisioning API (e.g. in plone.api). A remote API client can expect that a site is created according to its specs or not...but nothing in between...so I assume that committing here is a workaround at best...I would not consider this as a suitable solution.

@mauritsvanrees
Copy link
Member Author

Ah, I found the other issue that I was looking for: #2228
Not exactly the same, but also a problem that only happens during initial site creation and not when using the add-ons control panel. Or at least that is the case for @ida. Maybe not for @zopyx who added that issue.
That issue is where I originally suggested a transaction commit, which apparently helped for Ida.

@mauritsvanrees
Copy link
Member Author

mauritsvanrees commented May 14, 2018

I had another look and I see that the transaction commit doesn't really help. It only avoids printing several tracebacks, so that you are more likely to notice the original traceback that was always visible if you cared to scroll up far enough.

I will revert this PR.

@mauritsvanrees
Copy link
Member Author

Directly reverted on master in 4e67724.

mister-roboto pushed a commit to plone/buildout.coredev that referenced this pull request May 14, 2018
Branch: refs/heads/master
Date: 2018-05-14T14:49:18+02:00
Author: Maurits van Rees (mauritsvanrees) <[email protected]>
Commit: plone/Products.CMFPlone@7fddf58

After site creation, do not render the add-site template: we redirect anyway.

See also plone/Products.CMFPlone#2410 (comment)

Files changed:
M CHANGES.rst
M Products/CMFPlone/browser/admin.py
Repository: Products.CMFPlone

Branch: refs/heads/master
Date: 2018-05-14T17:00:10+02:00
Author: Jens W. Klein (jensens) <[email protected]>
Commit: plone/Products.CMFPlone@3936c29

Merge pull request #2413 from plone/add-plonesite-no-useless-render-master

After site creation, do not render a template: we redirect anyway [5.2]

Files changed:
M CHANGES.rst
M Products/CMFPlone/browser/admin.py
mister-roboto pushed a commit to plone/buildout.coredev that referenced this pull request May 14, 2018
Branch: refs/heads/5.1.x
Date: 2018-05-14T14:59:34+02:00
Author: Maurits van Rees (mauritsvanrees) <[email protected]>
Commit: plone/Products.CMFPlone@2b021c5

After site creation, do not render the add-site template: we redirect anyway.

See also plone/Products.CMFPlone#2410 (comment)

Files changed:
M CHANGES.rst
M Products/CMFPlone/browser/admin.py
Repository: Products.CMFPlone

Branch: refs/heads/5.1.x
Date: 2018-05-14T17:00:44+02:00
Author: Jens W. Klein (jensens) <[email protected]>
Commit: plone/Products.CMFPlone@dc27a82

Merge pull request #2414 from plone/add-plonesite-no-useless-render-51

After site creation, do not render a template: we redirect anyway [5.1]

Files changed:
M CHANGES.rst
M Products/CMFPlone/browser/admin.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Creating a new 5.1-pending site with policy package -> aborting transaction due to no CSRF protection
3 participants