-
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add permissions check for file upload
- Loading branch information
Showing
12 changed files
with
148 additions
and
58 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
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,30 @@ | ||
from django import get_version | ||
from django.conf import settings | ||
from django.http import JsonResponse | ||
|
||
if get_version() >= "4.0": | ||
from django.utils.translation import gettext_lazy as _ | ||
else: | ||
from django.utils.translation import ugettext_lazy as _ | ||
|
||
|
||
def check_upload_permission(view_func): | ||
def _wrapped_view(request, *args, **kwargs): | ||
permission = getattr(settings, "CKEDITOR_5_FILE_UPLOAD_PERMISSION", "staff") | ||
if permission == "staff" and not request.user.is_staff: | ||
return JsonResponse( | ||
{ | ||
"error": { | ||
"message": _("You do not have permission to upload files."), | ||
}, | ||
}, | ||
status=403, | ||
) | ||
if permission == "authenticated" and not request.user.is_authenticated: | ||
return JsonResponse( | ||
{"error": {"message": _("You must be logged in to upload files.")}}, | ||
status=403, | ||
) | ||
return view_func(request, *args, **kwargs) | ||
|
||
return _wrapped_view |
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
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
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
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,43 @@ | ||
from django.conf import settings | ||
|
||
from django_ckeditor_5.views import upload_file | ||
|
||
|
||
def test_upload_file_permission_anonymous(factory, anonymous_user, file): | ||
settings.CKEDITOR_5_FILE_UPLOAD_PERMISSION = "authenticated" | ||
request = factory.post("/upload/", {"upload": file}) | ||
request.user = anonymous_user | ||
response = upload_file(request) | ||
assert response.status_code == 403 | ||
|
||
|
||
def test_upload_file_permission_authenticated(factory, authenticated_user, file): | ||
settings.CKEDITOR_5_FILE_UPLOAD_PERMISSION = "authenticated" | ||
request = factory.post("/upload/", {"upload": file}) | ||
request.user = authenticated_user | ||
response = upload_file(request) | ||
assert response.status_code == 200 | ||
|
||
|
||
def test_upload_file_permission_staff(factory, staff_user, file): | ||
settings.CKEDITOR_5_FILE_UPLOAD_PERMISSION = "staff" | ||
request = factory.post("/upload/", {"upload": file}) | ||
request.user = staff_user | ||
response = upload_file(request) | ||
assert response.status_code == 200 | ||
|
||
|
||
def test_upload_file_permission_any(factory, anonymous_user, file): | ||
settings.CKEDITOR_5_FILE_UPLOAD_PERMISSION = "any" | ||
request = factory.post("/upload/", {"upload": file}) | ||
request.user = anonymous_user | ||
response = upload_file(request) | ||
assert response.status_code == 200 | ||
|
||
|
||
def test_upload_file_permission_authenticated_user(factory, authenticated_user, file): | ||
settings.CKEDITOR_5_FILE_UPLOAD_PERMISSION = "any" | ||
request = factory.post("/upload/", {"upload": file}) | ||
request.user = authenticated_user | ||
response = upload_file(request) | ||
assert response.status_code == 200 |
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 |
---|---|---|
|
@@ -82,7 +82,6 @@ exclude = ''' | |
| buck-out | ||
| build | ||
| dist | ||
| django_ckeditor_5 | ||
| migrations | ||
)/ | ||
) | ||
|