• Stars
    star
    184
  • Rank 207,999 (Top 5 %)
  • Language
    Python
  • License
    MIT License
  • Created about 13 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Login as any user from the Django admin interface, then switch back when done

django-su

Login as any user from the Django admin interface, then switch back when done

Authored by Adam Charnock, and some great contributors.

https://coveralls.io/repos/adamcharnock/django-su/badge.svg?branch=develop

Installation

  1. Either checkout django_su from GitHub, or install using pip :

    pip install django-su
  2. Add django_su to your INSTALLED_APPS. Make sure you put it before django.contrib.admin :

    INSTALLED_APPS = (
        ...
        'django_su',  # must be before ``django.contrib.admin``
        'django.contrib.admin',
    )
  3. Add SuBackend to AUTHENTICATION_BACKENDS :

    AUTHENTICATION_BACKENDS = (
        ...
        'django_su.backends.SuBackend',
    )
  4. Update your urls.py file :

    urlpatterns = [
        url(r'^su/', include('django_su.urls')),
        ...
    ]

And that should be it!

Please see example application. This application is used to manually test the functionalities of this package. This also serves as a good example.

django-su is tested on Django 2.2 or above, lower versions may work, but are considered unsupported.

External dependencies (optional, but recommended)

The following apps are optional but will enhance the user experience:

Note that django-ajax-selects requires the following settings:

AJAX_LOOKUP_CHANNELS = {'django_su':  dict(model='auth.user', search_field='username')}

Configuration (optional)

There are various optional configuration options you can set in your settings.py

# URL to redirect after the login.
# Default: "/"
SU_LOGIN_REDIRECT_URL = "/"

# URL to redirect after the logout.
# Default: "/"
SU_LOGOUT_REDIRECT_URL = "/"

# A function specifying the permissions a user requires in order
# to use the django-su functionality.
# Default: None
SU_LOGIN_CALLBACK = "example.utils.su_login_callback"

# A function to override the django.contrib.auth.login(request, user)
# view, thereby allowing one to set session data, etc.
# Default: None
SU_CUSTOM_LOGIN_ACTION = "example.utils.custom_login"

Usage

Go and view a user in the admin interface and look for a new "Login as" button in the top right.

Once you have su'ed into a user, you can get exit back into your original user by navigating to /su/ in your browser.

How to

How to Notify superuser when connected with another user

This option warns the superuser when working with another user as initially logged in. To activate this option perform:

  1. Add django_su.context_processors.is_su to TEMPLATE_CONTEXT_PROCESSORS :

    TEMPLATE_CONTEXT_PROCESSORS = (
        ...
        'django_su.context_processors.is_su',
    )
  2. In your base.html include su/is_su.html snippet :

    {% include "su/is_su.html" %}

How to use django-su with a custom user model (AUTH_USER_MODEL)

Django-su should function normally with a custom user model. However, your ModelAdmin in your admin.py file will need tweaking as follows:

# Within your admin.py file
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin

from . import models

 @admin.register(models.CustomUser)
 class CustomUserAdmin(UserAdmin):
     # The following two lines are needed:
     change_form_template = "admin/auth/user/change_form.html"
     change_list_template = "admin/auth/user/change_list.html"

This ensures the Django admin will use the correct template customisations for your custom user model.

Credits

This app was put together by Adam Charnock, but was largely based on ideas, code and comments at:

django-su is packaged using seed.

More Repositories

1

django-hordak

Double entry accounting in Django
Python
237
star
2

django-tz-detect

Easy user timezone detection for Django
Python
215
star
3

django-adminlte2

Reusable AdminLTE templates and admin theme
CSS
195
star
4

lightbus

RPC & event framework for Python 3
Python
189
star
5

dokku-client

Heroku-style command line interface for Dokku
Python
50
star
6

seed

A utility for easily creating and releasing Python packages
Python
50
star
7

python-srtm

Python API for reading NASA's SRTM `.hgt` or `.hgt.zip` altitude files.
Python
22
star
8

django-smalluuid

Short-form UUID model & form fields for Django 1.8 and above
Python
20
star
9

battlecat

General purpose accounting interface
Python
14
star
10

django-pipeline-forgiving

An extension of the django-pipeline storage backend which forgives missing files
Python
13
star
11

swiftwind

User-friendly billing for communal households
Python
11
star
12

mate3

Python library & CLI for your Outback/Mate3s solar power system
Python
9
star
13

smalluuid

Extending Python's builtin UUID to use short UUIDs by default
Python
9
star
14

swiftwind-heroku

Swiftwind for Heroku Deployment
Python
8
star
15

python-hue-client

Python
8
star
16

factorio-status-ui

Status web interface for your factorio dedicated server. View users & download mods
Python
7
star
17

django-skeleton

CSS
4
star
18

repose

Quickly create restful API clients in Python
Python
3
star
19

django-decision-matrix

Django implementation of Weighted Attribute Matrix. For making complex decisions between multiple options. Bootstrap UI.
Python
3
star
20

i-hate-papers

Create easily readable versions of academic papers via OpenAI
Python
2
star
21

dokku-selenium

Dokku plugin for a Selenium server
Shell
2
star
22

django-encrypt

2
star
23

django-live-server

The live server test case implementation due out in Django 1.4
Python
2
star
24

flynn-utils

Python
2
star
25

django-hashed-url

Python
2
star
26

colly

Util for quick comparisons of columns in CSV files, mostly just a wrapper for 'set' built-in.
Python
1
star
27

jshumandate

A JavaScript human readable (fuzzy) date parser
1
star
28

spineapi

An experiment with Spine and Tastypie
JavaScript
1
star
29

redisoverhead

A library for calculating the overhead of Redis data
1
star