Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django 1.8, Multiple Custom User Types

Tags:

I am creating a Django site that will have 4 types of users;

  • Super Admin: Essentially manages all types of users
  • UserTypeA: Can login to site and basically will have a CRUD interface for some arbitrary model. Also has extra attributes (specific info that only pertains to TypeA users)
  • UserTypeB: Can login to site and has extra information that pertains specifically to TypeB users, also can create and manage TypeC user accounts
  • UserTypeC: Can login to site and has extra information that pertains only to TypeC users.

Noting that I plan on creating a custom interface and not utilizing the built-in admin interface. What would be the best method for implementing this user structure? I would prefer to use Django's built-in authentication system (which manages password salting and hashing, along with session management etc), but I would be open to using some other authentication system if it is secure and production ready.

EDIT: Also, my plan is to use 1 log-in screen to access the site and utilize the permissions and user type to determine what will be available on each user's dashboard.

EDIT: Would it be possible to accomplish this by using an app for each user type, and if so how would this be done with a single log-in screen.

like image 248
OzzyGreyman Avatar asked May 28 '15 01:05

OzzyGreyman


1 Answers

First of all, you cannot make multiple authentication user base for a project. So you have to use the Django user authentication provided and fork it for multiple types of users. The Django user has some default values you need to provide during registration (try creating a user in Django Admin). What you can do is create a model called 'CustomUser' and inherit from AbstractUser. This will make your 'CustomUser' model the default for the project users. Because you inherit from AbstractUser this 'CustomUser' model will have every field from the original Users model, and then you can add some field on your own. You also need to specify in the settings.py file of the project that the original Users model is not your default authentication model anymore, it is your new 'CustomUser' model that will be used for authentication. See if the following code helps.

from django.db import models from django.contrib.auth.models import AbstractUser  class CustomUser(AbstractUser):     type_choices = (         ('SU', 'Super User'),         ('A', 'User Type A'),         ('B', 'User Type B'),         ('C', 'User Type C'),     )     user_type = models.CharField(max_length=2,                                  choices=type_choices,                                  default='C')  class UserDetails(model.Model):     type = models.OneToOneField('CustomUser')     extra_info = models.CharField(max_length=200) 

In the above code you have created the CustomUser model where users can provide the basic info like username, password, etc. which is the default in Django. And then you select which user type it is and save your additional information on UserDetails model which also has OneToOne relation to your new authentication model. One last thing you need to do is in the settings.py file.

AUTH_USER_MODEL = 'index.CustomUser'

Over here index is the app that my CustomUser model is created in.

Hope this helps.

like image 108
MiniGunnR Avatar answered Sep 21 '22 13:09

MiniGunnR