Skip to content

Commit 927e2bb

Browse files
Log username download requests (#65)
* log which user made file download request * add timestamp to download log
1 parent 9179c5e commit 927e2bb

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

config/settings/base.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,11 @@
179179
"level": "INFO",
180180
"handlers": ["console", "slack_admins"],
181181
},
182+
"downloads": {
183+
"level": "INFO",
184+
"handlers": ["console"],
185+
"propagate": False,
186+
},
182187
},
183188
}
184189

downloads/views.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
from app.authentication import TokenAuthentication as SageTokenAuthentication
3232
import time
3333
from unittest.mock import patch
34+
import logging
35+
36+
37+
logger = logging.getLogger(__name__)
3438

3539

3640
@dataclass
@@ -299,6 +303,19 @@ def get(
299303
node_id,
300304
timestamp_and_filename,
301305
)
306+
307+
# Log the access attempt
308+
username = (
309+
request.user.username if request.user.is_authenticated else "anonymous"
310+
)
311+
302312
if self.file_is_public or has_object_permission(request.user, self.node):
313+
logger.info(
314+
f"{datetime.now(timezone.utc).isoformat()} file download allowed: username={username} path={request.path} is_public={self.file_is_public}"
315+
)
303316
return HttpResponseRedirect(get_redirect_url(item))
317+
318+
logger.warning(
319+
f"{datetime.now(timezone.utc).isoformat()} file download denied: username={username} path={request.path} is_public={self.file_is_public}"
320+
)
304321
return HttpResponse("Permission denied", status=status.HTTP_403_FORBIDDEN)

0 commit comments

Comments
 (0)