Authentication - Made Django use a custom User model

WARNING! This commit requires you to re-migrate the database from the beginning, because AUTH_USER_MODEL can only be set before the initial migration.
This commit is contained in:
Evert Prants 2017-11-14 14:56:44 +02:00
parent 20d3ac7aec
commit 9f725b4c46
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
6 changed files with 27 additions and 23 deletions

View File

@ -47,6 +47,8 @@ AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
)
AUTH_USER_MODEL = 'LandingPage.User'
INSTALLED_APPS = [
'LandingPage.apps.LandingpageConfig',
'Show.apps.ShowConfig',

View File

@ -1,9 +1,18 @@
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .models import *
from .forms import SpecialUserChangeForm
class SpecialUserAdmin(UserAdmin):
form = SpecialUserChangeForm
fieldsets = UserAdmin.fieldsets + (
(None, {'fields': ('display_name',)}),
)
# Register your models here.
admin.site.register(Show)
admin.site.register(ExternalUser)
admin.site.register(User, SpecialUserAdmin)
admin.site.register(Admin)
admin.site.register(Ban)
admin.site.register(ShowModerator)
@ -18,4 +27,3 @@ admin.site.register(Watch)
admin.site.register(DiscussionBoard)
admin.site.register(DiscussionReply)
admin.site.register(DiscussionVote)

View File

@ -5,7 +5,6 @@ import logging
from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.auth.backends import ModelBackend
from .models import ExternalUser
class OAuthBackend(ModelBackend):
def authenticate(self, code=None):
@ -33,13 +32,15 @@ class OAuthBackend(ModelBackend):
).json()
usermodel = get_user_model()
matches = usermodel.objects.filter(externaluser__icy_id=user_info['uuid'])
matches = usermodel.objects.filter(icy_id=user_info['uuid'])
match = None
if not len(matches):
user = usermodel.objects.create_user(
username = user_info['username'],
email = user_info['email'],
icy_id = user_info['uuid'],
display_name = user_info['display_name']
)
if 'privilege' in user_info:
@ -48,12 +49,7 @@ class OAuthBackend(ModelBackend):
user.is_staff = (priv > 0)
user.save()
user.externaluser = ExternalUser(
user = user,
icy_id = user_info['uuid'],
display_name = user_info['display_name']
)
user.externaluser.save()
match = user
else:
match = matches[0]

6
LandingPage/forms.py Normal file
View File

@ -0,0 +1,6 @@
from django.contrib.auth.forms import UserChangeForm
from .models import User
class SpecialUserChangeForm(UserChangeForm):
class Meta(UserChangeForm.Meta):
model = User

View File

@ -1,5 +1,5 @@
from django.db import models
from django.contrib.auth.models import User
from django.contrib.auth.models import AbstractUser
from django.core.files.storage import FileSystemStorage
from django.conf import settings
import os
@ -72,13 +72,7 @@ class Show(TimestampedModel):
def __str__(self):
return '%s [%s]'%(self.name,self.abbr)
class ExternalUser(TimestampedModel):
user = models.OneToOneField(
User,
on_delete=models.CASCADE,
help_text='The internal Django user',
verbose_name="User"
)
class User(AbstractUser):
icy_id = models.CharField(
max_length=36,
help_text='The UUID assigned to this user by IcyNet\'s auth servers'
@ -98,8 +92,6 @@ class ExternalUser(TimestampedModel):
related_name='watched_by',
through='Watch'
)
def __str__(self):
return 'External for %s (%s)'%(self.user.email, self.display_name)
class Admin(User):
pass
@ -326,7 +318,7 @@ class SubmissionVote(TimestampedModel):
class Favorite(TimestampedModel):
user = models.ForeignKey(
ExternalUser,
User,
on_delete=models.CASCADE
)
episode = models.ForeignKey(
@ -338,7 +330,7 @@ class Favorite(TimestampedModel):
class Watch(TimestampedModel):
user = models.ForeignKey(
ExternalUser,
User,
on_delete=models.CASCADE
)
episode = models.ForeignKey(

View File

@ -21,7 +21,7 @@
<span class="logo">Episodes<span class="period">.</span>Community</span>
<div class="userdata">
{% if user.is_authenticated %}
{{ user.externaluser.display_name }}
{{ user.display_name }}
{% else %}
<a href="/login">Log in</a>
{% endif %}