Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
nk20
Manage
Activity
Members
Labels
Plan
Issues
32
Issue boards
Milestones
Wiki
Code
Merge requests
6
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
BDE
nk20
Commits
20e2d415
Commit
20e2d415
authored
5 years ago
by
ynerant
Browse files
Options
Downloads
Patches
Plain Diff
Use a middleware rather than inspect the stack to get current user and IP
parent
fffd674c
No related branches found
Branches containing commit
No related tags found
Tags containing commit
Loading
Pipeline
#7893
passed with warnings with stages
in 3 minutes and 59 seconds
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
apps/logs/middlewares.py
+55
-0
55 additions, 0 deletions
apps/logs/middlewares.py
apps/logs/signals.py
+5
-40
5 additions, 40 deletions
apps/logs/signals.py
note_kfet/settings/__init__.py
+5
-1
5 additions, 1 deletion
note_kfet/settings/__init__.py
with
65 additions
and
41 deletions
apps/logs/middlewares.py
0 → 100644
+
55
−
0
View file @
20e2d415
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later
from
django.conf
import
settings
from
django.contrib.auth.models
import
AnonymousUser
from
threading
import
local
USER_ATTR_NAME
=
getattr
(
settings
,
'
LOCAL_USER_ATTR_NAME
'
,
'
_current_user
'
)
IP_ATTR_NAME
=
getattr
(
settings
,
'
LOCAL_IP_ATTR_NAME
'
,
'
_current_ip
'
)
_thread_locals
=
local
()
def
_set_current_user_and_ip
(
user
=
None
,
ip
=
None
):
"""
Sets current user in local thread.
Can be used as a hook e.g. for shell jobs (when request object is not
available).
"""
setattr
(
_thread_locals
,
USER_ATTR_NAME
,
user
)
setattr
(
_thread_locals
,
IP_ATTR_NAME
,
ip
)
def
get_current_user
():
return
getattr
(
_thread_locals
,
USER_ATTR_NAME
,
None
)
def
get_current_ip
():
return
getattr
(
_thread_locals
,
IP_ATTR_NAME
,
None
)
def
get_current_authenticated_user
():
current_user
=
get_current_user
()
if
isinstance
(
current_user
,
AnonymousUser
):
return
None
return
current_user
class
LogsMiddleware
(
object
):
def
__init__
(
self
,
get_response
):
self
.
get_response
=
get_response
def
__call__
(
self
,
request
):
user
=
request
.
user
if
'
HTTP_X_FORWARDED_FOR
'
in
request
.
META
:
ip
=
request
.
META
.
get
(
'
HTTP_X_FORWARDED_FOR
'
)
else
:
ip
=
request
.
META
.
get
(
'
REMOTE_ADDR
'
)
_set_current_user_and_ip
(
user
,
ip
)
response
=
self
.
get_response
(
request
)
return
response
This diff is collapsed.
Click to expand it.
apps/logs/signals.py
+
5
−
40
View file @
20e2d415
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later
import
inspect
from
django.contrib.contenttypes.models
import
ContentType
from
django.core
import
serializers
from
django.db.models.signals
import
pre_save
,
post_save
,
post_delete
from
django.dispatch
import
receiver
from
.middlewares
import
get_current_authenticated_user
,
get_current_ip
from
.models
import
Changelog
def
get_request_in_signal
(
sender
):
req
=
None
for
entry
in
reversed
(
inspect
.
stack
()):
try
:
req
=
entry
[
0
].
f_locals
[
'
request
'
]
# Check if there is a user
# noinspection PyStatementEffect
req
.
user
break
except
:
pass
if
not
req
:
print
(
"
WARNING: Attempt to save
"
+
str
(
sender
)
+
"
with no user
"
)
return
req
def
get_user_and_ip
(
sender
):
req
=
get_request_in_signal
(
sender
)
try
:
user
=
req
.
user
if
'
HTTP_X_FORWARDED_FOR
'
in
req
.
META
:
ip
=
req
.
META
.
get
(
'
HTTP_X_FORWARDED_FOR
'
)
else
:
ip
=
req
.
META
.
get
(
'
REMOTE_ADDR
'
)
except
:
user
=
None
ip
=
None
return
user
,
ip
EXCLUDED
=
[
'
admin.logentry
'
,
'
authtoken.token
'
,
...
...
@@ -75,13 +42,11 @@ def save_object(sender, instance, **kwargs):
if
instance
.
_meta
.
label_lower
in
EXCLUDED
:
return
pr
evious
=
instance
.
_previous
pr
int
(
"
LOGGING SOMETHING
"
)
user
,
ip
=
get_user_and_ip
(
sender
)
previous
=
instance
.
_previous
from
django.contrib.auth.models
import
AnonymousUser
if
isinstance
(
user
,
AnonymousUser
):
user
=
None
user
,
ip
=
get_current_authenticated_user
(),
get_current_ip
()
if
user
is
not
None
and
instance
.
_meta
.
label_lower
==
"
auth.user
"
and
previous
:
# Don't save last login modifications
...
...
@@ -111,7 +76,7 @@ def delete_object(sender, instance, **kwargs):
if
instance
.
_meta
.
label_lower
in
EXCLUDED
:
return
user
,
ip
=
get_
user_and_ip
(
sender
)
user
,
ip
=
get_
current_authenticated_user
(),
get_current_ip
(
)
instance_json
=
serializers
.
serialize
(
'
json
'
,
[
instance
,
])[
1
:
-
1
]
Changelog
.
objects
.
create
(
user
=
user
,
...
...
This diff is collapsed.
Click to expand it.
note_kfet/settings/__init__.py
+
5
−
1
View file @
20e2d415
...
...
@@ -73,7 +73,11 @@ if "cas" in INSTALLED_APPS:
'
cas_explained
'
,
]
AUTHENTICATION_BACKENDS
+=
(
'
cas.backends.CASBackend
'
,)
if
"
logs
"
in
INSTALLED_APPS
:
MIDDLEWARE
+=
(
'
logs.middlewares.LogsMiddleware
'
,)
if
"
debug_toolbar
"
in
INSTALLED_APPS
:
MIDDLEWARE
.
insert
(
1
,
"
debug_toolbar.middleware.DebugToolbarMiddleware
"
)
INTERNAL_IPS
=
[
'
127.0.0.1
'
]
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment