This file is indexed.

/usr/lib/python3/dist-packages/impersonate/helpers.py is in python3-django-impersonate 1.3-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# -*- coding: utf-8 -*-
import re
from importlib import import_module

from django.utils.safestring import mark_safe
from django.core.paginator import Paginator, EmptyPage

from .settings import User, settings


def get_redir_path(request=None):
    nextval = None
    redirect_field_name = settings.REDIRECT_FIELD_NAME
    if request and redirect_field_name:
        nextval = request.GET.get(redirect_field_name, None)
    return nextval or settings.REDIRECT_URL


def get_redir_arg(request):
    redirect_field_name = settings.REDIRECT_FIELD_NAME
    if redirect_field_name:
        nextval = request.GET.get(redirect_field_name, None)
        if nextval:
            return u'?{0}={1}'.format(redirect_field_name, nextval)
    return u''


def get_redir_field(request):
    redirect_field_name = settings.REDIRECT_FIELD_NAME
    if redirect_field_name:
        nextval = request.GET.get(redirect_field_name, None)
        if nextval:
            return mark_safe(
                u'<input type="hidden" name="{0}" value="{1}"/>'.format(
                    redirect_field_name,
                    nextval,
                )
            )
    return u''


def get_paginator(request, qs):
    try:
        page_number = int(request.GET.get('page', 1))
    except ValueError:
        page_number = 1

    paginator = Paginator(
        qs,
        int(settings.PAGINATE_COUNT),
    )
    try:
        page = paginator.page(page_number)
    except EmptyPage:
        page = None

    return (paginator, page, page_number)


def check_allow_staff():
    return (not settings.REQUIRE_SUPERUSER)


def users_impersonable(request):
    ''' Returns a QuerySet of users that this user can impersonate.
        Uses the CUSTOM_USER_QUERYSET if set, else, it
        returns all users
    '''
    if settings.CUSTOM_USER_QUERYSET is not None:
        custom_queryset_func = import_func_from_string(
            settings.CUSTOM_USER_QUERYSET
        )
        return custom_queryset_func(request)
    else:
        qs = User.objects.all()
        if not User._meta.ordering:
            qs = qs.order_by('pk')
        return qs


def check_allow_for_user(request, end_user):
    ''' Return True if some request can impersonate end_user
    '''
    if check_allow_impersonate(request):
        # start user can impersonate
        # Can impersonate superusers if ALLOW_SUPERUSER is True
        # Can impersonate anyone who is in your queryset of 'who i can impersonate'.
        allow_superusers = settings.ALLOW_SUPERUSER
        upk = end_user.pk
        return (
            ((request.user.is_superuser and allow_superusers) or
                not end_user.is_superuser) and
            users_impersonable(request).filter(pk=upk).exists()
        )

    # start user not allowed impersonate at all
    return False


def import_func_from_string(string_name):
    ''' Given a string like 'mod.mod2.funcname' which refers to a function,
        return that function so it can be called
    '''
    mod_name, func_name = string_name.rsplit('.', 1)
    mod = import_module(mod_name)
    return getattr(mod, func_name)


def check_allow_impersonate(request):
    ''' Returns True if this request is allowed to do any impersonation.
        Uses the CUSTOM_ALLOW function if required, else
        looks at superuser/staff status and REQUIRE_SUPERUSER
    '''
    if settings.CUSTOM_ALLOW is not None:
        custom_allow_func = \
            import_func_from_string(settings.CUSTOM_ALLOW)

        return custom_allow_func(request)
    else:
        # default allow checking:
        if not request.user.is_superuser:
            if not request.user.is_staff or not check_allow_staff():
                return False

        return True


def check_allow_for_uri(uri):
    uri = uri.lstrip('/')

    exclusions = settings.URI_EXCLUSIONS
    if not isinstance(exclusions, (list, tuple)):
        exclusions = (exclusions,)

    for exclusion in exclusions:
        if re.search(exclusion, uri):
            return False

    return True


def is_authenticated(user):
    ''' Helper to check if a user is authenticated or not.
        Added because in Django 2.0, the method compatibility is
        being removed.
        https://docs.djangoproject.com/en/1.11/ref/contrib/auth/#django.contrib.auth.models.User.is_authenticated
    '''
    if not hasattr(user, 'is_authenticated'):
        return False

    if callable(user.is_authenticated):
        return user.is_authenticated()
    else:
        return user.is_authenticated


try:
    from django.utils.duration import duration_string
except ImportError:
    # Django < 1.8
    def duration_string(duration):
        ''' Taken straight from Django 1.8 - django/utils/duration.py
        '''
        days = duration.days
        seconds = duration.seconds
        microseconds = duration.microseconds

        minutes = seconds // 60
        seconds = seconds % 60

        hours = minutes // 60
        minutes = minutes % 60

        string = '{:02d}:{:02d}:{:02d}'.format(hours, minutes, seconds)
        if days:
            string = '{} '.format(days) + string
        if microseconds:
            string += '.{:06d}'.format(microseconds)

        return string