2020-06-01 05:00:36 +00:00
|
|
|
from datasette import hookimpl
|
2020-06-07 21:23:16 +00:00
|
|
|
from datasette.utils import actor_matches_allow
|
2020-06-01 05:00:36 +00:00
|
|
|
|
|
|
|
|
2020-06-08 22:09:57 +00:00
|
|
|
@hookimpl(tryfirst=True)
|
2020-06-08 18:59:11 +00:00
|
|
|
def permission_allowed(datasette, actor, action, resource):
|
2020-06-07 21:23:16 +00:00
|
|
|
if action == "permissions-debug":
|
|
|
|
if actor and actor.get("id") == "root":
|
|
|
|
return True
|
2020-06-07 21:30:39 +00:00
|
|
|
elif action == "view-instance":
|
|
|
|
allow = datasette.metadata("allow")
|
|
|
|
if allow is not None:
|
|
|
|
return actor_matches_allow(actor, allow)
|
2020-06-08 03:50:37 +00:00
|
|
|
elif action == "view-database":
|
2020-06-08 18:59:11 +00:00
|
|
|
database_allow = datasette.metadata("allow", database=resource)
|
2020-06-08 03:50:37 +00:00
|
|
|
if database_allow is None:
|
|
|
|
return True
|
|
|
|
return actor_matches_allow(actor, database_allow)
|
2020-06-08 04:47:22 +00:00
|
|
|
elif action == "view-table":
|
2020-06-08 18:59:11 +00:00
|
|
|
database, table = resource
|
2020-06-08 04:47:22 +00:00
|
|
|
tables = datasette.metadata("tables", database=database) or {}
|
|
|
|
table_allow = (tables.get(table) or {}).get("allow")
|
|
|
|
if table_allow is None:
|
|
|
|
return True
|
|
|
|
return actor_matches_allow(actor, table_allow)
|
2020-06-07 21:23:16 +00:00
|
|
|
elif action == "view-query":
|
|
|
|
# Check if this query has a "allow" block in metadata
|
2020-06-08 18:59:11 +00:00
|
|
|
database, query_name = resource
|
2020-06-07 21:23:16 +00:00
|
|
|
queries_metadata = datasette.metadata("queries", database=database)
|
|
|
|
assert query_name in queries_metadata
|
|
|
|
if isinstance(queries_metadata[query_name], str):
|
|
|
|
return True
|
|
|
|
allow = queries_metadata[query_name].get("allow")
|
|
|
|
if allow is None:
|
|
|
|
return True
|
|
|
|
return actor_matches_allow(actor, allow)
|
2020-06-09 00:05:44 +00:00
|
|
|
elif action == "execute-sql":
|
|
|
|
# Use allow_sql block from database block, or from top-level
|
|
|
|
database_allow_sql = datasette.metadata("allow_sql", database=resource)
|
|
|
|
if database_allow_sql is None:
|
|
|
|
database_allow_sql = datasette.metadata("allow_sql")
|
|
|
|
if database_allow_sql is None:
|
|
|
|
return True
|
|
|
|
return actor_matches_allow(actor, database_allow_sql)
|