Skip to main content

django-host-user-override

Overrides current user based on host prefix. For example any request to 5.user.example.com (format can be changed in settings) becomes request as if you were logged in as user with ID 5. This allows you to be logged in as different users in different tabs at the same time without losing your primary authenticated user.

Works only if you're logged in as superuser by default (can be changed in settings).

Also gives you big red banner on top of every page if your user is overridden.

Requirements

  • Your DNS server should resolve subdomains *.user.<your domain> to the same IP address as main domain.
  • Your project should not use absolute link generation or any other technic that can change current subdomain. It is a more inconvenience than requirement though.

Installing django-host-user-override

  1. Install the package from PyPI: pip install django-host-user-override

  2. Add host_user_override to INSTALLED_APPS:

INSTALLED_APPS = [
   ...,
   'host_user_override',
   ...,
]
  1. Add HostUserOverrideMiddleware right after AuthenticationMiddleware:
MIDDLEWARE = [
   ...,
   'django.contrib.auth.middleware.AuthenticationMiddleware',
   'host_user_override.middleware.HostUserOverrideMiddleware',
   ...,
]
  1. Update your settings.py file to support subdomains (don't forget about DNS as well):
ALLOWED_HOSTS = ['.example.com']

SESSION_COOKIE_DOMAIN = '.example.com'
  1. Set new change_form.html template in UserAdmin:
admin.site.unregister(User)

@admin.register(User)
class CustomUserAdmin(UserAdmin):
    change_form_template = 'host_user_override/change_form.html'

Optional settings

  1. Update settings.py if you want host pattern other than <id>.user.<domain>. Example for u<id>.<domain>:
HOSTUSEROVERRIDE_HOST_REGEXP = r'u(\d+)\..+'

HOSTUSEROVERRIDE_HOST_SUB_REGEXP = r'u\d+\.'

HOSTUSEROVERRIDE_REDIRECT_URL_FORMAT = 'http://u{user_id}.{host}/'

HOSTUSEROVERRIDE_PERMANENT_REDIRECT = False
  1. Set HOSTUSEROVERRIDE_PERMISSION_CHECK to customize required permissions. Should be function that takes 2 positional arguments: current user and desired user.

  2. Set HOSTUSEROVERRIDE_FORCE_ACTIVE to force overriden user to be active even when he is actually disabled.

Usage

Open any non-superuser in Django Admin and press 'Login as multiuser' button.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

  • Props to django-debug-toolbar team for HTML injection code
  • Thanks to @dimoha for original idea

Metadata

Release files for django-host-user-override 0.4.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for django-host-user-override 0.4.5
File Interpreter ABI Platform
django_host_user_override-0.4.5-py2.py3-none-any.whl Python 2, Python 3 none any Details

Release files / django_host_user_override-0.4.5-py2.py3-none-any.whl

Download URL django_host_user_override-0.4.5-py2.py3-none-any.whl
Size 11.1 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
970338a7c9ac455cd44e9b1f48f56857ac4a93a9478aec7fda5ab29eae5e6006
BLAKE2b-256 checksum
How to use checksums
891f2a500209987655274a515d28645bed461b08e55fcd77cd8c5c6b67007685
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.15.0 pkginfo/1.4.2 requests/2.20.1 setuptools/46.0.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.5.2

Security for django-host-user-override 0.4.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page