Skip to content

Commit 3465dac

Browse files
author
Mikhail Pyrev
committed
Initial
0 parents  commit 3465dac

File tree

27 files changed

+544
-0
lines changed

27 files changed

+544
-0
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
*.sqlite3
2+
__pycache__/
3+
*.pyc
4+
.idea/
5+
dist/
6+
*.egg-info/

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Copyright (c) 2017 Mikhail Pyrev <[email protected]>
2+
3+
Permission is hereby granted, free of charge, to any person obtaining
4+
a copy of this software and associated documentation files (the
5+
"Software"), to deal in the Software without restriction, including
6+
without limitation the rights to use, copy, modify, merge, publish,
7+
distribute, sublicense, and/or sell copies of the Software, and to
8+
permit persons to whom the Software is furnished to do so, subject to
9+
the following conditions:
10+
11+
The above copyright notice and this permission notice shall be
12+
included in all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

MANIFEST.in

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
include LICENSE
2+
include README.md
3+
recursive-include host_user_override/locale *
4+
recursive-include host_user_override/static *
5+
recursive-include host_user_override/templates *

README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# django-host-user-override
2+
3+
Overrides current user based on host prefix. For example any request to
4+
```5.user.example.com``` becomes request as if you were logged in as
5+
user with ID 5. Works only if you're actually logged in as superuser.
6+
7+
Also gives you big red banner on top of every page if your user is
8+
overridden.
9+
10+
### Installing django-host-user-override
11+
12+
1. Install the package from PyPI: ```pip install django-host-user-override```
13+
14+
2. Add ```host_user_override``` to ```INSTALLED_APPS```:
15+
```python
16+
INSTALLED_APPS = [
17+
...,
18+
'host_user_override',
19+
...,
20+
]
21+
```
22+
23+
3. Add ```HostUserOverrideMiddleware``` right after ```AuthenticationMiddleware```:
24+
```python
25+
MIDDLEWARE = [
26+
...,
27+
'django.contrib.auth.middleware.AuthenticationMiddleware',
28+
'host_user_override.middleware.HostUserOverrideMiddleware',
29+
...,
30+
]
31+
```
32+
33+
4. Update your ```settings.py``` file to support subdomains (don't forget about DNS as well):
34+
```python
35+
ALLOWED_HOSTS = ['.example.com']
36+
37+
SESSION_COOKIE_DOMAIN = '.example.com'
38+
```
39+
40+
5. Set new ```change_form.html``` template in ```UserAdmin```:
41+
```python
42+
admin.site.unregister(User)
43+
44+
@admin.register(User)
45+
class CustomUserAdmin(UserAdmin):
46+
change_form_template = 'host_user_override/change_form.html'
47+
```
48+
49+
## Usage
50+
51+
Open any non-superuser in Django Admin and press 'Login as multiuser' button.
52+
53+
## License
54+
55+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
56+
57+
## Acknowledgments
58+
59+
* Props to django-debug-toolbar team for HTML injection code
60+
* Thanks to @dimoha for original idea

example/app/__init__.py

Whitespace-only changes.

example/app/admin.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# coding: utf-8
2+
from __future__ import unicode_literals
3+
4+
from django.contrib import admin
5+
from django.contrib.auth import get_user_model
6+
from django.contrib.auth.admin import UserAdmin
7+
8+
User = get_user_model()
9+
10+
admin.site.unregister(User)
11+
12+
13+
@admin.register(User)
14+
class CustomUserAdmin(UserAdmin):
15+
change_form_template = 'host_user_override/change_form.html'

example/app/apps.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.apps import AppConfig
2+
3+
4+
class AppConfig(AppConfig):
5+
name = 'app'
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Title</title>
6+
</head>
7+
<body>{{ request.user.pk }}:{{ request.user }}</body>
8+
</html>

example/app/tests.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.test import TestCase
2+
3+
# Create your tests here.

example/app/views.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# coding: utf-8
2+
from __future__ import unicode_literals
3+
4+
from django.views.generic import TemplateView
5+
6+
7+
class IndexView(TemplateView):
8+
template_name = 'app/index.html'

0 commit comments

Comments
 (0)