From 39777c9039e5005d899bc61d49fcb26852abed2f Mon Sep 17 00:00:00 2001 From: Bruno Boi Date: Wed, 20 Oct 2021 07:57:26 +0000 Subject: [PATCH] [FIX] web: always add usercontext to legacy action In the new web client architecture, the user context is added to server requests through the ORM service and is therefore not needed in the action context. Thus the commit 7354d16 has removed it from the action. Before the new webclient architecture, the user context was always prepended to the action context. So this commit reintroduces it for the legacy views and client actions through their proper adapters. closes odoo/odoo#78685 X-original-commit: 7975674166a3d8fa9cd8714adab769a7fd681368 Signed-off-by: Aaron Bohy (aab) Signed-off-by: Bruno Boi --- .../src/legacy/legacy_client_actions.js | 10 +++++- addons/web/static/src/legacy/legacy_views.js | 8 ++++- .../tests/webclient/actions/legacy_tests.js | 33 ++++++++++++++++++- 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/addons/web/static/src/legacy/legacy_client_actions.js b/addons/web/static/src/legacy/legacy_client_actions.js index b649b537653..37334e64bb7 100644 --- a/addons/web/static/src/legacy/legacy_client_actions.js +++ b/addons/web/static/src/legacy/legacy_client_actions.js @@ -1,6 +1,7 @@ /** @odoo-module **/ import { registry } from "@web/core/registry"; +import { useService } from "@web/core/utils/hooks"; import { action_registry as legacyActionRegistry } from "web.core"; import Widget from "web.Widget"; import { setScrollPosition } from "../core/utils/scrolling"; @@ -36,7 +37,14 @@ function registerClientAction(name, action) { options[key] = this.props[key]; } } - this.widgetArgs = [this.props.action, options]; + + // always add user context to the action context + this.user = useService("user"); + const clientAction = Object.assign({}, this.props.action, { + context: Object.assign({}, this.user.context, this.props.action.context), + }); + + this.widgetArgs = [clientAction, options]; this.widget = this.props.state && this.props.state.__legacy_widget__; this.onReverseBreadcrumb = this.props.state && this.props.state.__on_reverse_breadcrumb__; diff --git a/addons/web/static/src/legacy/legacy_views.js b/addons/web/static/src/legacy/legacy_views.js index 48876eed3e2..02f04fdb50d 100644 --- a/addons/web/static/src/legacy/legacy_views.js +++ b/addons/web/static/src/legacy/legacy_views.js @@ -51,9 +51,15 @@ function registerView(name, LegacyView) { searchPanel = globalState.searchPanel; } + // always add user context to the action context + this.user = useService("user"); + const action = Object.assign({}, this.props.action, { + context: Object.assign({}, this.user.context, this.props.action.context), + }); + const { actionFlags, breadcrumbs = [] } = this.env.config; this.viewParams = Object.assign({}, actionFlags, { - action: this.props.action, + action, // legacy views automatically add the last part of the breadcrumbs breadcrumbs: breadcrumbsToLegacy(breadcrumbs), modelName: this.props.resModel, diff --git a/addons/web/static/tests/webclient/actions/legacy_tests.js b/addons/web/static/tests/webclient/actions/legacy_tests.js index bf6aa011919..6dafcca704d 100644 --- a/addons/web/static/tests/webclient/actions/legacy_tests.js +++ b/addons/web/static/tests/webclient/actions/legacy_tests.js @@ -10,7 +10,7 @@ import { makeTestEnv } from "../../helpers/mock_env"; import { createWebClient, doAction, getActionManagerServerData } from "./../helpers"; import makeTestEnvironment from "web.test_env"; -import { ClientActionAdapter } from "@web/legacy/action_adapters"; +import { ClientActionAdapter, ViewAdapter } from "@web/legacy/action_adapters"; import { makeLegacyCrashManagerService } from "@web/legacy/utils"; import { useDebugCategory } from "@web/core/debug/debug_context"; import { ErrorDialog } from "@web/core/errors/error_dialogs"; @@ -409,4 +409,35 @@ QUnit.module("ActionManager", (hooks) => { assert.verifySteps(["do action"]); delete SystrayMenu.Items.FakeSystrayItemWidget; }); + + QUnit.test("usercontext always added to legacy actions", async (assert) => { + assert.expect(8); + core.action_registry.add("testClientAction", AbstractAction); + registerCleanup(() => delete core.action_registry.map.testClientAction); + patchWithCleanup(ClientActionAdapter.prototype, { + setup() { + assert.step("ClientActionAdapter"); + const action = { ...this.props.widgetArgs[0] }; + const originalAction = JSON.parse(action._originalAction); + assert.deepEqual(originalAction.context, undefined); + assert.deepEqual(action.context, this.env.services.user.context); + this._super(); + }, + }); + patchWithCleanup(ViewAdapter.prototype, { + setup() { + assert.step("ViewAdapter"); + const action = { ...this.props.viewParams.action }; + const originalAction = JSON.parse(action._originalAction); + assert.deepEqual(originalAction.context, undefined); + assert.deepEqual(action.context, this.env.services.user.context); + this._super(); + }, + }); + const webClient = await createWebClient({ serverData }); + await doAction(webClient, "testClientAction"); + assert.verifySteps(["ClientActionAdapter"]); + await doAction(webClient, 1); + assert.verifySteps(["ViewAdapter"]); + }); });