Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复 is_ajax() 判断失效的问题 #1896

Merged
merged 2 commits into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions common/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,3 +541,25 @@ def test_init_user(self):
# init 需要是无状态的, 可以重复执行, 执行一次和执行n次结果一样
init_user(self.u1)
self.assertEqual(self.u1, self.resource_group1.users_set.get(pk=self.u1.pk))


class PermissionTest(TestCase):
def setUp(self) -> None:
self.user = User.objects.create(
username="test_user", display="中文显示", is_active=True, email="XXX@xxx.com"
)
self.client.force_login(self.user)

def tearDown(self) -> None:
self.user.delete()

def test_superuser_required_false(self):
"""测试超管权限校验"""
r = self.client.get("/config/")
self.assertContains(r, "您无权操作,请联系管理员")

def test_superuser_required_true(self):
"""测试超管权限校验"""
User.objects.filter(username=self.user.username).update(is_superuser=1)
r = self.client.get("/config/")
self.assertNotContains(r, "您无权操作,请联系管理员")
6 changes: 4 additions & 2 deletions common/utils/permission.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ def wrapper(request, *args, **kw):
user = request.user

if user.is_superuser is False:
if request.is_ajax():
is_ajax = request.META.get("HTTP_X_REQUESTED_WITH") == "XMLHttpRequest"
if is_ajax:
result = {"status": 1, "msg": "您无权操作,请联系管理员", "data": []}
return HttpResponse(json.dumps(result), content_type="application/json")
else:
Expand All @@ -30,7 +31,8 @@ def wrapper(request, *args, **kw):
# 获取用户信息,权限验证
user = request.user
if user.role not in roles and user.is_superuser is False:
if request.is_ajax():
is_ajax = request.META.get("HTTP_X_REQUESTED_WITH") == "XMLHttpRequest"
if is_ajax:
result = {"status": 1, "msg": "您无权操作,请联系管理员", "data": []}
return HttpResponse(
json.dumps(result), content_type="application/json"
Expand Down