-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
106 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,60 @@ | ||
from datasette import hookimpl | ||
from datasette.database import Database | ||
import asyncio | ||
import threading | ||
|
||
# Adds a current_actor() function to SQLite that shows the current actor's | ||
# ID. | ||
|
||
original_execute_fn = Database.execute_fn | ||
actor = threading.local() | ||
|
||
async def patched_execute_fn(self, fn): | ||
task = asyncio.current_task() | ||
|
||
scope = None if not hasattr(task, '_dux_request') else task._dux_request.scope | ||
|
||
def wrapped_fn(conn): | ||
if scope and 'actor' in scope and scope['actor'] and 'id' in scope['actor']: | ||
actor.actor = scope['actor']['id'] | ||
else: | ||
actor.actor = None | ||
rv = fn(conn) | ||
actor.actor = None | ||
return rv | ||
|
||
return await original_execute_fn(self, wrapped_fn) | ||
|
||
Database.execute_fn = patched_execute_fn | ||
@hookimpl | ||
def prepare_connection(conn): | ||
try: | ||
if getattr(conn, 'engine') == 'duckdb': | ||
return | ||
except AttributeError: | ||
pass | ||
|
||
def fn(): | ||
return actor.actor | ||
|
||
conn.create_function( | ||
"current_actor", 0, fn | ||
) | ||
|
||
# We always register an actor_from_request hook so that our | ||
# actor_from_request hookwrapper is guaranteed to fire | ||
# on every request. | ||
@hookimpl | ||
def actor_from_request(datasette, request): | ||
return None | ||
_id = None | ||
if 'x-me' in request.headers: | ||
_id = request.headers['x-me'] | ||
return {'id': _id} | ||
|
||
@hookimpl(specname='actor_from_request', hookwrapper=True) | ||
def sniff_actor_from_request(datasette, request): | ||
asyncio.current_task()._dux_request = request | ||
|
||
# all corresponding hookimpls are invoked here | ||
outcome = yield |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from datasette import hookimpl | ||
|
||
@hookimpl | ||
def actor_from_request(request): | ||
return {'id': 'someuser'} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters