Skip to content

Commit e16596e

Browse files
geidoclaude
andcommitted
Merge branch 'geido/extension-context-user' into geido/canvas-draft-events
The extension context helper this branch carried now lands once, on geido/extension-context-user, shaped as #45894 shapes the extension context and passing the user the call runs for. Draft listeners, step handlers and commit participants keep running in their extension's context, for the caller's user. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 parents 701852c + ace7f5e commit e16596e

2 files changed

Lines changed: 55 additions & 5 deletions

File tree

‎superset/extensions/context.py‎

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,13 @@ def persistent(self) -> Any:
5757
class ConcreteExtensionContext:
5858
"""Concrete implementation of ExtensionContext for the host."""
5959

60-
def __init__(self, manifest: Manifest):
60+
def __init__(
61+
self,
62+
manifest: Manifest,
63+
user_id: int | None | Callable[[], int | None] = None,
64+
) -> None:
6165
self._manifest = manifest
66+
self._user_id = user_id
6267
self._storage = ExtensionStorage()
6368

6469
@property
@@ -75,6 +80,11 @@ def manifest(self) -> Manifest:
7580
def storage(self) -> ExtensionStorage:
7681
return self._storage
7782

83+
@property
84+
def user_id(self) -> int | None:
85+
"""Resolve request principals after authentication, or use a fixed worker ID."""
86+
return self._user_id() if callable(self._user_id) else self._user_id
87+
7888

7989
# Context variable for ambient extension context pattern.
8090
# Thread-safe and async-safe via Python's contextvars.
@@ -127,17 +137,21 @@ def use_context(ctx: ConcreteExtensionContext) -> Iterator[None]:
127137

128138

129139
@contextmanager
130-
def extension_context(manifest: Manifest) -> Iterator[ConcreteExtensionContext]:
140+
def extension_context(
141+
manifest: Manifest,
142+
user_id: int | None | Callable[[], int | None] = None,
143+
) -> Iterator[ConcreteExtensionContext]:
131144
"""
132145
Context manager for setting extension context during loading.
133146
134147
Creates a new ExtensionContext for the given manifest and sets it as
135148
the current context. Supports nested contexts via ContextVar tokens.
136149
137150
:param manifest: The extension manifest
151+
:param user_id: A fixed principal or a resolver called when the ID is accessed
138152
:yields: The created ExtensionContext
139153
"""
140-
ctx = ConcreteExtensionContext(manifest)
154+
ctx = ConcreteExtensionContext(manifest, user_id=user_id)
141155
with use_context(ctx):
142156
yield ctx
143157

@@ -155,12 +169,16 @@ def restore_context(ctx: ConcreteExtensionContext | None) -> Iterator[None]:
155169
The host calls what an extension registered while loading long after it
156170
loaded, when no context is set, so ``get_context()`` and extension storage
157171
would not find the extension. Each call gets a context of its own, made
158-
from the extension's manifest.
172+
from the extension's manifest, whose user is whoever the call runs for:
173+
the request's user, resolved when the context is asked, or a task's.
159174
"""
160175
if ctx is None:
161176
yield
162177
return
163-
with extension_context(ctx.manifest):
178+
# Imported here, as importing superset.utils.core imports this module.
179+
from superset.utils.core import get_user_id
180+
181+
with extension_context(ctx.manifest, user_id=get_user_id):
164182
yield
165183

166184

‎tests/unit_tests/extensions/storage/test_context.py‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,35 @@ def whoami(greeting: str) -> str:
158158
assert bound.__name__ == "whoami"
159159
assert get_current_extension_context() is None
160160
assert bind_context(whoami, None) is whoami
161+
162+
163+
def test_a_context_names_a_fixed_user_or_resolves_it_when_asked():
164+
"""As apache/superset#45894 shapes it: a worker's fixed principal, or a
165+
request's, resolved after authentication."""
166+
manifest = create_manifest("acme", "tools")
167+
users = iter([7, 8])
168+
169+
with extension_context(manifest, user_id=42) as fixed:
170+
assert fixed.user_id == 42
171+
with extension_context(manifest, user_id=lambda: next(users)) as resolved:
172+
assert (resolved.user_id, resolved.user_id) == (7, 8)
173+
with extension_context(manifest) as nobody:
174+
assert nobody.user_id is None
175+
assert ConcreteExtensionContext(manifest).manifest is manifest
176+
177+
178+
def test_a_restored_context_runs_for_whoever_the_call_runs_for(mocker):
179+
"""The user is the caller's when the host calls the extension, not the
180+
user who was signed in while it loaded."""
181+
manifest = create_manifest("acme", "tools")
182+
with extension_context(manifest, user_id=1):
183+
captured = get_current_extension_context()
184+
caller = mocker.patch("superset.utils.core.get_user_id", return_value=5)
185+
186+
with restore_context(captured):
187+
assert get_context().user_id == 5
188+
caller.return_value = 6
189+
assert get_context().user_id == 6
190+
191+
bound = bind_context(lambda: get_context().user_id, captured)
192+
assert bound() == 6

0 commit comments

Comments
 (0)