Projet

Général

Profil

0002-debian-add-InternalIpMiddleware-29149.patch

Benjamin Dauvergne, 13 avril 2020 18:40

Télécharger (3,24 ko)

Voir les différences:

Subject: [PATCH 2/3] debian: add InternalIpMiddleware (#29149)

It sets DEBUG=True when current request IP is in settings.INTERNAL_IPS.
 debian/debian_config_common.py |  2 ++
 hobo/middleware/debug.py       | 49 ++++++++++++++++++++++++++++++++++
 2 files changed, 51 insertions(+)
 create mode 100644 hobo/middleware/debug.py
debian/debian_config_common.py
376 376
    MIDDLEWARE_CLASSES = (
377 377
        'hobo.middleware.utils.StoreRequestMiddleware',
378 378
        'hobo.middleware.xforwardedfor.XForwardedForMiddleware',
379
        'hobo.middleware.debug.InternalIPMiddleware',
379 380
    ) + MIDDLEWARE_CLASSES
380 381

  
381 382
    MIDDLEWARE_CLASSES = MIDDLEWARE_CLASSES + (
......
384 385
    MIDDLEWARE = (
385 386
        'hobo.middleware.utils.StoreRequestMiddleware',
386 387
        'hobo.middleware.xforwardedfor.XForwardedForMiddleware',
388
        'hobo.middleware.debug.InternalIPMiddleware',
387 389
    ) + MIDDLEWARE
388 390

  
389 391
    MIDDLEWARE = MIDDLEWARE + (
hobo/middleware/debug.py
1
# hobo - portal to configure and deploy applications
2
# Copyright (C) 2019  Entr'ouvert
3
#
4
# This program is free software: you can redistribute it and/or modify it
5
# under the terms of the GNU Affero General Public License as published
6
# by the Free Software Foundation, either version 3 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU Affero General Public License for more details.
13
#
14
# You should have received a copy of the GNU Affero General Public License
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16

  
17
from django.conf import settings
18

  
19

  
20
class InternalIPMiddleware(object):
21
    def __init__(self, get_response=None):
22
        self.get_response = get_response
23

  
24
    def process_request(self, request):
25
        internal_ips = getattr(settings, 'INTERNAL_IPS', [])
26
        try:
27
            if request.META['REMOTE_ADDR'] in internal_ips:
28
                self.old_value = settings.DEBUG
29
                settings.DEBUG = True
30
        except TypeError:
31
            pass
32
        return None
33

  
34
    def process_response(self, request, response):
35
        if hasattr(self, 'old_value'):
36
            settings.DEBUG = self.old_value
37
            del self.old_value
38
        return response
39

  
40
    def __call__(self, request):
41
        old_value = settings.DEBUG
42
        internal_ips = getattr(settings, 'INTERNAL_IPS', [])
43
        set_debug = request.META['REMOTE_ADDR'] in internal_ips
44
        try:
45
            if set_debug:
46
                settings.DEBUG = True
47
            return self.get_response(request)
48
        finally:
49
            settings.DEBUG = old_value
0
-