diff options
| author | Romain Derie <rde@odoo.com> | 2021-04-16 15:21:33 +0000 |
|---|---|---|
| committer | Romain Derie <rde@odoo.com> | 2021-05-03 16:16:12 +0000 |
| commit | 0490828de135406675b8c6949cf31cdccd697b02 (patch) | |
| tree | 6d049da8da93ba29ff0dd776a97e1318aa394bdd /addons/website | |
| parent | 3470a3c94e06d92afcd8a8129f80e4dfea14efe4 (diff) | |
[FIX] http_routing, website: prevent crash when using `fw` in url
Before this commit, the routing map generated and used would be the one from
the website the request is performed, instead of the one from the `fw` website
ID which will be the one we redirect the user to.
This issue was introduced with the routing map by website, be8fc2296b38 and is
restricted to a single case: a publisher using the website switcher, and it
won't happen on next page naviguation/refresh as the `fw` website id will be
the same as the current website's ID. Thus there won't be any routing map
mismatch.
Step to reproduce:
- Create a page on website 2, set it as homepage
- Naviguate to website 1 on '/' url
- Naviguate to website 2 on '/' url
This will raise a werkzeug error about `EndPoint not iterable`.
----- Technical analysis ------
This is the current flow:
1. `_dispatch()` is setting `website_routing` to `get_current_website()` -> 2
2. `_dispatch()` is calling `_match()`
3. `_match()` is calling `routing_map()` with key = `website_routing`, which
was set to 2 in step 1.
4. `routing_map()` is calling `_generate_routing_rules()` which generate the
rules based on `website_routing`, which was set to 2 in step 1.
5. `_dispatch()` authenticate the user by calling `_authenticate()`
6. `_dispatch()` is calling `_add_dispatch_parameter()`, where URL param `fw`
is forced in session, so `get_current_website()` now return the correct
`website_id` -> 1
The issue: in order to handle the `fw` URL parameter (step 6.), we need to
check the rights to ensure we can allow the website switch.
To check rights, user need to be authenticated (step 5.), which is done after
generating the routing map (2. & 3. & 4.).
The routing map is generated based on the current website (step 1.)
Step 6 depends of steps 5 which depends of steps 2/3/4 which depend of step 1,
but step 1 should depend of step 6, which is an impossible cycle.
closes odoo/odoo#70278
X-original-commit: 878e28f93a65756fa2002ef2af5b02a3e2c59b1b
Signed-off-by: Jérémy Kersten (jke) <jke@openerp.com>
Signed-off-by: Romain Derie <rdeodoo@users.noreply.github.com>
Diffstat (limited to 'addons/website')
| -rw-r--r-- | addons/website/controllers/main.py | 36 | ||||
| -rw-r--r-- | addons/website/models/ir_http.py | 9 | ||||
| -rw-r--r-- | addons/website/static/src/js/backend/dashboard.js | 2 | ||||
| -rw-r--r-- | addons/website/static/src/js/content/website_root.js | 8 | ||||
| -rw-r--r-- | addons/website/tests/test_ui.py | 4 | ||||
| -rw-r--r-- | addons/website/tests/test_views.py | 16 |
6 files changed, 48 insertions, 27 deletions
diff --git a/addons/website/controllers/main.py b/addons/website/controllers/main.py index 86de3cf7414..e89f9375173 100644 --- a/addons/website/controllers/main.py +++ b/addons/website/controllers/main.py @@ -12,6 +12,7 @@ import werkzeug.utils import werkzeug.wrappers from itertools import islice +from werkzeug import urls from xml.etree import ElementTree as ET import odoo @@ -85,10 +86,37 @@ class Website(Home): raise request.not_found() - @http.route('/website/force_website', type='json', auth="user") - def force_website(self, website_id): - request.env['website']._force_website(website_id) - return True + @http.route('/website/force/<int:website_id>', type='http', auth="user", website=True, sitemap=False, multilang=False) + def website_force(self, website_id, path='/', isredir=False, **kw): + """ To switch from a website to another, we need to force the website in + session, AFTER landing on that website domain (if set) as this will be a + different session. + """ + parse = werkzeug.urls.url_parse + safe_path = parse(path).path + + if not (request.env.user.has_group('website.group_multi_website') + and request.env.user.has_group('website.group_website_publisher')): + # The user might not be logged in on the forced website, so he won't + # have rights. We just redirect to the path as the user is already + # on the domain (basically a no-op as it won't change domain or + # force website). + # Website 1 : 127.0.0.1 (admin) + # Website 2 : 127.0.0.2 (not logged in) + # Click on "Website 2" from Website 1 + return request.redirect(safe_path) + + website = request.env['website'].browse(website_id) + + if not isredir and website.domain: + domain_from = request.httprequest.environ.get('HTTP_HOST', '') + domain_to = parse(website._get_http_domain()).netloc + if domain_from != domain_to: + # redirect to correct domain for a correct routing map + url_to = urls.url_join(website._get_http_domain(), '/website/force/%s?isredir=1&path=%s' % (website.id, safe_path)) + return request.redirect(url_to) + website._force() + return request.redirect(safe_path) # ------------------------------------------------------ # Login - overwrite of the web login so that regular users are redirected to the backend diff --git a/addons/website/models/ir_http.py b/addons/website/models/ir_http.py index a5b78da22ff..c35a65fce85 100644 --- a/addons/website/models/ir_http.py +++ b/addons/website/models/ir_http.py @@ -189,11 +189,12 @@ class Http(models.AbstractModel): @classmethod def _add_dispatch_parameters(cls, func): - # Force website with query string paramater, typically set from website selector in frontend navbar + # DEPRECATED for /website/force/<website_id> - remove me in master~saas-14.4 + # Force website with query string paramater, typically set from website selector in frontend navbar and inside tests force_website_id = request.httprequest.args.get('fw') - if (force_website_id and request.session.get('force_website_id') != force_website_id and - request.env.user.has_group('website.group_multi_website') and - request.env.user.has_group('website.group_website_publisher')): + if (force_website_id and request.session.get('force_website_id') != force_website_id + and request.env.user.has_group('website.group_multi_website') + and request.env.user.has_group('website.group_website_publisher')): request.env['website']._force_website(request.httprequest.args.get('fw')) context = {} diff --git a/addons/website/static/src/js/backend/dashboard.js b/addons/website/static/src/js/backend/dashboard.js index d9daa81b45a..b705067543c 100644 --- a/addons/website/static/src/js/backend/dashboard.js +++ b/addons/website/static/src/js/backend/dashboard.js @@ -137,7 +137,7 @@ var Dashboard = AbstractAction.extend({ on_go_to_website: function (ev) { ev.preventDefault(); var website = _.findWhere(this.websites, {selected: true}); - window.location.href = $.param.querystring(website.domain + '/', {'fw': website.id}); + window.location.href = `/website/force/${website.id}`; }, on_save_ga_client_id: function(ga_client_id, ga_analytics_key) { diff --git a/addons/website/static/src/js/content/website_root.js b/addons/website/static/src/js/content/website_root.js index 5d52902272c..c2844a49176 100644 --- a/addons/website/static/src/js/content/website_root.js +++ b/addons/website/static/src/js/content/website_root.js @@ -313,12 +313,12 @@ var WebsiteRoot = publicRootData.PublicRoot.extend(KeyboardNavigationMixin, { _onWebsiteSwitch: function (ev) { var websiteId = ev.currentTarget.getAttribute('website-id'); var websiteDomain = ev.currentTarget.getAttribute('domain'); - var url = window.location.href; + let url = `/website/force/${websiteId}`; if (websiteDomain && window.location.hostname !== websiteDomain) { - var path = window.location.pathname + window.location.search + window.location.hash; - url = websiteDomain + path; + url = websiteDomain + url; } - window.location.href = $.param.querystring(url, {'fw': websiteId}); + const path = window.location.pathname + window.location.search + window.location.hash; + window.location.href = $.param.querystring(url, {'path': path}); }, /** * @private diff --git a/addons/website/tests/test_ui.py b/addons/website/tests/test_ui.py index d76bf22cf81..1a629bc416b 100644 --- a/addons/website/tests/test_ui.py +++ b/addons/website/tests/test_ui.py @@ -154,8 +154,8 @@ class TestUi(odoo.tests.HttpCase): </xpath> """, }) - self.start_tour("/?fw=%s" % website_default.id, "generic_website_editor", login='admin') - self.start_tour("/?fw=%s" % new_website.id, "specific_website_editor", login='admin') + self.start_tour("/website/force/%s" % website_default.id, "generic_website_editor", login='admin') + self.start_tour("/website/force/%s" % new_website.id, "specific_website_editor", login='admin') def test_06_public_user_editor(self): website_default = self.env['website'].search([], limit=1) diff --git a/addons/website/tests/test_views.py b/addons/website/tests/test_views.py index d1bfea11166..39c2ded1388 100644 --- a/addons/website/tests/test_views.py +++ b/addons/website/tests/test_views.py @@ -1130,9 +1130,7 @@ class Crawler(HttpCase): base_url = self.env['ir.config_parameter'].sudo().get_param('web.base.url') # Simulate website 2 (that use only generic views) - url = base_url + '/website/force_website' - json = {'params': {'website_id': website_2.id}} - self.opener.post(url=url, json=json) + self.url_open(base_url + '/website/force/%s' % website_2.id) # Test controller url = base_url + '/website/get_switchable_related_views' @@ -1170,9 +1168,7 @@ class Crawler(HttpCase): # | Filter By Country # Simulate website 1 (that has specific views) - url = base_url + '/website/force_website' - json = {'params': {'website_id': website_1.id}} - self.opener.post(url=url, json=json) + self.url_open(base_url + '/website/force/%s' % website_1.id) # Test controller url = base_url + '/website/get_switchable_related_views' @@ -1345,9 +1341,7 @@ class Crawler(HttpCase): base_url = self.env['ir.config_parameter'].sudo().get_param('web.base.url') # Simulate website 2 - url = base_url + '/website/force_website' - json = {'params': {'website_id': website_2.id}} - self.opener.post(url=url, json=json) + self.url_open(base_url + '/website/force/%s' % website_2.id) # Test controller url = base_url + '/website/get_switchable_related_views' @@ -1358,9 +1352,7 @@ class Crawler(HttpCase): self.assertEqual(response.json()['result'][0]['key'], '_theme_kea_sale.products', "Only '_theme_kea_sale.products' should be returned") # Simulate website 1 - url = base_url + '/website/force_website' - json = {'params': {'website_id': website_1.id}} - self.opener.post(url=url, json=json) + self.url_open(base_url + '/website/force/%s' % website_1.id) # Test controller url = base_url + '/website/get_switchable_related_views' |
