Skip to content

Commit

Permalink
Removed check_permission() from BaseView, closes #1677
Browse files Browse the repository at this point in the history
Refs #1660
  • Loading branch information
simonw committed Mar 21, 2022
1 parent dfafce6 commit 194e4f6
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 22 deletions.
1 change: 1 addition & 0 deletions datasette/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,7 @@ async def ensure_permissions(
Raises datasette.Forbidden() if any of the checks fail
"""
assert actor is None or isinstance(actor, dict)
for permission in permissions:
if isinstance(permission, str):
action = permission
Expand Down
10 changes: 0 additions & 10 deletions datasette/views/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,6 @@ async def head(self, *args, **kwargs):
response.body = b""
return response

async def check_permission(self, request, action, resource=None):
ok = await self.ds.permission_allowed(
request.actor,
action,
resource=resource,
default=True,
)
if not ok:
raise Forbidden(action)

def database_color(self, database):
return "ff0000"

Expand Down
2 changes: 1 addition & 1 deletion datasette/views/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ async def data(
None, "view-query", (database, canned_query), default=True
)
else:
await self.check_permission(request, "execute-sql", database)
await self.ds.ensure_permissions(request.actor, [("execute-sql", database)])

# Extract any :named parameters
named_parameters = named_parameters or await derive_named_parameters(
Expand Down
2 changes: 1 addition & 1 deletion datasette/views/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class IndexView(BaseView):

async def get(self, request):
as_format = request.url_vars["format"]
await self.check_permission(request, "view-instance")
await self.ds.ensure_permissions(request.actor, ["view-instance"])
databases = []
for name, db in self.ds.databases.items():
visible, database_private = await check_visibility(
Expand Down
10 changes: 5 additions & 5 deletions datasette/views/special.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def __init__(self, datasette, filename, data_callback, needs_request=False):

async def get(self, request):
as_format = request.url_vars["format"]
await self.check_permission(request, "view-instance")
await self.ds.ensure_permissions(request.actor, ["view-instance"])
if self.needs_request:
data = self.data_callback(request)
else:
Expand Down Expand Up @@ -47,7 +47,7 @@ class PatternPortfolioView(BaseView):
has_json_alternate = False

async def get(self, request):
await self.check_permission(request, "view-instance")
await self.ds.ensure_permissions(request.actor, ["view-instance"])
return await self.render(["patterns.html"], request=request)


Expand Down Expand Up @@ -95,7 +95,7 @@ class PermissionsDebugView(BaseView):
has_json_alternate = False

async def get(self, request):
await self.check_permission(request, "view-instance")
await self.ds.ensure_permissions(request.actor, ["view-instance"])
if not await self.ds.permission_allowed(request.actor, "permissions-debug"):
raise Forbidden("Permission denied")
return await self.render(
Expand Down Expand Up @@ -146,11 +146,11 @@ class MessagesDebugView(BaseView):
has_json_alternate = False

async def get(self, request):
await self.check_permission(request, "view-instance")
await self.ds.ensure_permissions(request.actor, ["view-instance"])
return await self.render(["messages_debug.html"], request)

async def post(self, request):
await self.check_permission(request, "view-instance")
await self.ds.ensure_permissions(request.actor, ["view-instance"])
post = await request.post_vars()
message = post.get("message", "")
message_type = post.get("message_type") or "INFO"
Expand Down
13 changes: 8 additions & 5 deletions tests/test_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,17 +321,20 @@ def test_permissions_debug(app_client):
checks = [
{
"action": div.select_one(".check-action").text,
"result": bool(div.select(".check-result-true")),
# True = green tick, False = red cross, None = gray None
"result": None
if div.select(".check-result-no-opinion")
else bool(div.select(".check-result-true")),
"used_default": bool(div.select(".check-used-default")),
}
for div in check_divs
]
assert [
assert checks == [
{"action": "permissions-debug", "result": True, "used_default": False},
{"action": "view-instance", "result": True, "used_default": True},
{"action": "view-instance", "result": None, "used_default": True},
{"action": "permissions-debug", "result": False, "used_default": True},
{"action": "view-instance", "result": True, "used_default": True},
] == checks
{"action": "view-instance", "result": None, "used_default": True},
]


@pytest.mark.parametrize(
Expand Down

0 comments on commit 194e4f6

Please sign in to comment.