Override current user based on subdomain
Project description
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``:
.. code:: python
INSTALLED_APPS = [
...,
'host_user_override',
...,
]
3. Add ``HostUserOverrideMiddleware`` right after
``AuthenticationMiddleware``:
.. code:: python
MIDDLEWARE = [
...,
'django.contrib.auth.middleware.AuthenticationMiddleware',
'host_user_override.middleware.HostUserOverrideMiddleware',
...,
]
4. Update your ``settings.py`` file to support subdomains (don't forget
about DNS as well): \`\`\`python ALLOWED\_HOSTS = ['.example.com']
SESSION\_COOKIE\_DOMAIN = '.example.com' \`\`\`
5. Set new ``change_form.html`` template in ``UserAdmin``: \`\`\`python
admin.site.unregister(User)
@admin.register(User) class CustomUserAdmin(UserAdmin):
change\_form\_template = 'host\_user\_override/change\_form.html' \`\`\`
Optional settings
~~~~~~~~~~~~~~~~~
6. Update ``settings.py`` if you want host pattern other than
``<id>.user.<domain>``. Example for ``u<id>.<domain>``: \`\`\`python
HOSTUSEROVERRIDE\_HOST\_REGEXP = r'u(:raw-latex:`\d`+)..+'
HOSTUSEROVERRIDE\_HOST\_SUB\_REGEXP = r'u:raw-latex:`\d`+.'
HOSTUSEROVERRIDE\_REDIRECT\_URL\_FORMAT = 'http://u{user\_id}.{host}/'
HOSTUSEROVERRIDE\_PERMANENT\_REDIRECT = False \`\`\`
7. Set ``HOSTUSEROVERRIDE_PERMISSION_CHECK`` to customize required
permissions. Should be function that takes 2 positional arguments:
current user and desired user.
8. 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 <LICENSE>`__ file for details.
Acknowledgments
---------------
- Props to django-debug-toolbar team for HTML injection code
- Thanks to @dimoha for original idea
=========================
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``:
.. code:: python
INSTALLED_APPS = [
...,
'host_user_override',
...,
]
3. Add ``HostUserOverrideMiddleware`` right after
``AuthenticationMiddleware``:
.. code:: python
MIDDLEWARE = [
...,
'django.contrib.auth.middleware.AuthenticationMiddleware',
'host_user_override.middleware.HostUserOverrideMiddleware',
...,
]
4. Update your ``settings.py`` file to support subdomains (don't forget
about DNS as well): \`\`\`python ALLOWED\_HOSTS = ['.example.com']
SESSION\_COOKIE\_DOMAIN = '.example.com' \`\`\`
5. Set new ``change_form.html`` template in ``UserAdmin``: \`\`\`python
admin.site.unregister(User)
@admin.register(User) class CustomUserAdmin(UserAdmin):
change\_form\_template = 'host\_user\_override/change\_form.html' \`\`\`
Optional settings
~~~~~~~~~~~~~~~~~
6. Update ``settings.py`` if you want host pattern other than
``<id>.user.<domain>``. Example for ``u<id>.<domain>``: \`\`\`python
HOSTUSEROVERRIDE\_HOST\_REGEXP = r'u(:raw-latex:`\d`+)..+'
HOSTUSEROVERRIDE\_HOST\_SUB\_REGEXP = r'u:raw-latex:`\d`+.'
HOSTUSEROVERRIDE\_REDIRECT\_URL\_FORMAT = 'http://u{user\_id}.{host}/'
HOSTUSEROVERRIDE\_PERMANENT\_REDIRECT = False \`\`\`
7. Set ``HOSTUSEROVERRIDE_PERMISSION_CHECK`` to customize required
permissions. Should be function that takes 2 positional arguments:
current user and desired user.
8. 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 <LICENSE>`__ file for details.
Acknowledgments
---------------
- Props to django-debug-toolbar team for HTML injection code
- Thanks to @dimoha for original idea
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
No source distribution files available for this release.See tutorial on generating distribution archives.
Built Distribution
Close
Hashes for django_host_user_override-0.4.2-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8fb6beb69a93337f9a9fd120413cd32d91765a95118acf39d197de9862247aac |
|
MD5 | 2ebc627cff49b7d47c8e2ef263e5d4e3 |
|
BLAKE2b-256 | 2600bb661ad0717ba4404084668ead222a5400560fa81124c65ca691a59111e2 |