You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For example someone reported a crucial bug, then we don't want that screenshot to be accessable by any users except our staffs/superuser.
So, to handle that case I think we can use custom middleware,
importosfromdjango.confimportsettingsfromdjango.contribimportmessagesfromdjango.utils.deprecationimportMiddlewareMixinfromdjango.utils.translationimportugettext_lazyas_fromdjango.core.exceptionsimportPermissionDeniedclassRestrictMediaFoldersMiddleware(MiddlewareMixin):
""" Class Middleware to protect specific media folders to only staff/superuser who have access to them. """defprocess_request(self, request):
protected_folders=getattr(settings, 'PROTECTED_MEDIA_FOLDERS', [])
forfolderinprotected_folders:
# 'tellme' => 'tellme/'# 'tellme/' => 'tellme/'iffolder[:-1] !='/':
folder=f'{folder}/'# folder_path => '/media/tellme/'# request.path => '/media/tellme/screenshots/screenshot_fw9h8D.png'folder_path=os.path.join(settings.MEDIA_URL, folder)
iffolder_pathinrequest.path:
user=request.userifuser.is_authenticatedand (user.is_superuser|user.is_staff):
# that mean user will able to accesspasselse:
message=_('You are not allowed to access this path or file.')
messages.error(request, message)
raisePermissionDenied()
Then in settings.py;
MIDDLEWARE= [
....
'path.to.middleware.RestrictMediaFoldersMiddleware',
]
# Protect specific media folders to only staff/superuser who have access to them.# this variable is related to `RestrictMediaFoldersMiddleware`PROTECTED_MEDIA_FOLDERS= ['tellme', ]
The text was updated successfully, but these errors were encountered:
For example someone reported a crucial bug, then we don't want that screenshot to be accessable by any users except our staffs/superuser.
So, to handle that case I think we can use custom middleware,
Then in
settings.py
;The text was updated successfully, but these errors were encountered: