Skip to content

Commit e31e954

Browse files
committed
Require execute_sql permission to edit SQL in dashboard through admin, closes #94
1 parent cae1050 commit e31e954

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

django_sql_dashboard/admin.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@ class DashboardQueryInline(admin.TabularInline):
1010
model = DashboardQuery
1111
extra = 1
1212

13+
def has_change_permission(self, request, obj=None):
14+
if obj is None:
15+
return True
16+
return obj.user_can_edit(request.user)
17+
18+
def get_readonly_fields(self, request, obj=None):
19+
if not request.user.has_perm("django_sql_dashboard.execute_sql"):
20+
return ("sql",)
21+
else:
22+
return tuple()
23+
1324

1425
@admin.register(Dashboard)
1526
class DashboardAdmin(admin.ModelAdmin):

test_project/test_dashboard_permissions.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from enum import Enum
22

33
import pytest
4+
from bs4 import BeautifulSoup
45
from django.contrib.auth.models import Group, User
56

67
from django_sql_dashboard.models import Dashboard
@@ -314,11 +315,21 @@ def test_user_can_edit(
314315
slug="owned_by_other_superuser", owned_by=other, edit_policy="superuser"
315316
)
316317
dashboard_obj = Dashboard.objects.get(slug=dashboard)
318+
dashboard_obj.queries.create(sql="select 1 + 1")
317319
assert dashboard_obj.user_can_edit(user) == expected
318320
if dashboard != "owned_by_other_staff":
319321
# This test doesn't make sense for the 'staff' one, they cannot access admin
320322
# https://github.com/simonw/django-sql-dashboard/issues/44#issuecomment-835653787
321-
assert can_user_edit_using_admin(client, user, dashboard_obj) == expected
323+
can_edit_using_admin = can_user_edit_using_admin(client, user, dashboard_obj)
324+
assert can_edit_using_admin == expected
325+
if can_edit_using_admin:
326+
# Check that they cannot edit the SQL queries, because they do not
327+
# have the execute_sql permisssion
328+
assert not user.has_perm("django_sql_dashboard.execute_sql")
329+
html = get_admin_change_form_html(client, user, dashboard_obj)
330+
soup = BeautifulSoup(html, "html5lib")
331+
assert soup.select("td.field-sql p")[0].text == "select 1 + 1"
332+
322333
user.is_staff = True
323334
user.save()
324335
assert dashboard_obj.user_can_edit(user) == expected_if_staff
@@ -329,15 +340,23 @@ def test_user_can_edit(
329340
assert can_user_edit_using_admin(client, user, dashboard_obj)
330341

331342

332-
def can_user_edit_using_admin(client, user, dashboard):
343+
def get_admin_change_form_html(client, user, dashboard):
333344
# Only staff can access the admin:
345+
original_is_staff = user.is_staff
334346
user.is_staff = True
335347
user.save()
336348
client.force_login(user)
337349
response = client.get(dashboard.get_edit_url())
350+
if not original_is_staff:
351+
user.is_staff = False
352+
user.save()
353+
return response.content.decode("utf-8")
354+
355+
356+
def can_user_edit_using_admin(client, user, dashboard):
338357
return (
339-
b'<input type="text" name="title" class="vTextField" maxlength="128" id="id_title">'
340-
in response.content
358+
'<input type="text" name="title" class="vTextField" maxlength="128" id="id_title">'
359+
in get_admin_change_form_html(client, user, dashboard)
341360
)
342361

343362

0 commit comments

Comments
 (0)