kopia lustrzana https://github.com/simonw/datasette
				
				
				
			Respect existing scope["actor"] if set, closes #854
							rodzic
							
								
									d2aef9f7ef
								
							
						
					
					
						commit
						6151c25a5a
					
				|  | @ -908,6 +908,7 @@ class DatasetteRouter(AsgiRouter): | ||||||
|         ): |         ): | ||||||
|             scope_modifications["scheme"] = "https" |             scope_modifications["scheme"] = "https" | ||||||
|         # Handle authentication |         # Handle authentication | ||||||
|  |         default_actor = scope.get("actor") or None | ||||||
|         actor = None |         actor = None | ||||||
|         for actor in pm.hook.actor_from_request( |         for actor in pm.hook.actor_from_request( | ||||||
|             datasette=self.ds, request=Request(scope, receive) |             datasette=self.ds, request=Request(scope, receive) | ||||||
|  | @ -918,7 +919,7 @@ class DatasetteRouter(AsgiRouter): | ||||||
|                 actor = await actor |                 actor = await actor | ||||||
|             if actor: |             if actor: | ||||||
|                 break |                 break | ||||||
|         scope_modifications["actor"] = actor |         scope_modifications["actor"] = actor or default_actor | ||||||
|         return await super().route_path( |         return await super().route_path( | ||||||
|             dict(scope, **scope_modifications), receive, send, path |             dict(scope, **scope_modifications), receive, send, path | ||||||
|         ) |         ) | ||||||
|  |  | ||||||
|  | @ -39,6 +39,7 @@ EXPECTED_PLUGINS = [ | ||||||
|         "version": None, |         "version": None, | ||||||
|         "hooks": [ |         "hooks": [ | ||||||
|             "actor_from_request", |             "actor_from_request", | ||||||
|  |             "asgi_wrapper", | ||||||
|             "extra_body_script", |             "extra_body_script", | ||||||
|             "extra_css_urls", |             "extra_css_urls", | ||||||
|             "extra_js_urls", |             "extra_js_urls", | ||||||
|  |  | ||||||
|  | @ -137,6 +137,20 @@ def actor_from_request(datasette, request): | ||||||
|         return None |         return None | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | @hookimpl | ||||||
|  | def asgi_wrapper(): | ||||||
|  |     def wrap(app): | ||||||
|  |         async def maybe_set_actor_in_scope(scope, recieve, send): | ||||||
|  |             if b"_actor_in_scope" in scope["query_string"]: | ||||||
|  |                 scope = dict(scope, actor={"id": "from-scope"}) | ||||||
|  |                 print(scope) | ||||||
|  |             await app(scope, recieve, send) | ||||||
|  | 
 | ||||||
|  |         return maybe_set_actor_in_scope | ||||||
|  | 
 | ||||||
|  |     return wrap | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
| @hookimpl | @hookimpl | ||||||
| def permission_allowed(actor, action): | def permission_allowed(actor, action): | ||||||
|     if action == "this_is_allowed": |     if action == "this_is_allowed": | ||||||
|  |  | ||||||
|  | @ -534,6 +534,11 @@ def test_actor_from_request_async(app_client): | ||||||
|     assert {"id": "bot2", "1+1": 2} == app_client.ds._last_request.scope["actor"] |     assert {"id": "bot2", "1+1": 2} == app_client.ds._last_request.scope["actor"] | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | def test_existing_scope_actor_respected(app_client): | ||||||
|  |     app_client.get("/?_actor_in_scope=1") | ||||||
|  |     assert {"id": "from-scope"} == app_client.ds._last_request.scope["actor"] | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
| @pytest.mark.asyncio | @pytest.mark.asyncio | ||||||
| @pytest.mark.parametrize( | @pytest.mark.parametrize( | ||||||
|     "action,expected", |     "action,expected", | ||||||
|  |  | ||||||
		Ładowanie…
	
		Reference in New Issue
	
	 Simon Willison
						Simon Willison