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:
parent
20d3ac7aec
commit
9f725b4c46
@ -47,6 +47,8 @@ AUTHENTICATION_BACKENDS = (
|
|||||||
'django.contrib.auth.backends.ModelBackend',
|
'django.contrib.auth.backends.ModelBackend',
|
||||||
)
|
)
|
||||||
|
|
||||||
|
AUTH_USER_MODEL = 'LandingPage.User'
|
||||||
|
|
||||||
INSTALLED_APPS = [
|
INSTALLED_APPS = [
|
||||||
'LandingPage.apps.LandingpageConfig',
|
'LandingPage.apps.LandingpageConfig',
|
||||||
'Show.apps.ShowConfig',
|
'Show.apps.ShowConfig',
|
||||||
|
@ -1,9 +1,18 @@
|
|||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
|
from django.contrib.auth.admin import UserAdmin
|
||||||
from .models import *
|
from .models import *
|
||||||
|
from .forms import SpecialUserChangeForm
|
||||||
|
|
||||||
|
class SpecialUserAdmin(UserAdmin):
|
||||||
|
form = SpecialUserChangeForm
|
||||||
|
|
||||||
|
fieldsets = UserAdmin.fieldsets + (
|
||||||
|
(None, {'fields': ('display_name',)}),
|
||||||
|
)
|
||||||
|
|
||||||
# Register your models here.
|
# Register your models here.
|
||||||
admin.site.register(Show)
|
admin.site.register(Show)
|
||||||
admin.site.register(ExternalUser)
|
admin.site.register(User, SpecialUserAdmin)
|
||||||
admin.site.register(Admin)
|
admin.site.register(Admin)
|
||||||
admin.site.register(Ban)
|
admin.site.register(Ban)
|
||||||
admin.site.register(ShowModerator)
|
admin.site.register(ShowModerator)
|
||||||
@ -18,4 +27,3 @@ admin.site.register(Watch)
|
|||||||
admin.site.register(DiscussionBoard)
|
admin.site.register(DiscussionBoard)
|
||||||
admin.site.register(DiscussionReply)
|
admin.site.register(DiscussionReply)
|
||||||
admin.site.register(DiscussionVote)
|
admin.site.register(DiscussionVote)
|
||||||
|
|
||||||
|
@ -5,7 +5,6 @@ import logging
|
|||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.contrib.auth import get_user_model
|
from django.contrib.auth import get_user_model
|
||||||
from django.contrib.auth.backends import ModelBackend
|
from django.contrib.auth.backends import ModelBackend
|
||||||
from .models import ExternalUser
|
|
||||||
|
|
||||||
class OAuthBackend(ModelBackend):
|
class OAuthBackend(ModelBackend):
|
||||||
def authenticate(self, code=None):
|
def authenticate(self, code=None):
|
||||||
@ -33,13 +32,15 @@ class OAuthBackend(ModelBackend):
|
|||||||
).json()
|
).json()
|
||||||
|
|
||||||
usermodel = get_user_model()
|
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
|
match = None
|
||||||
|
|
||||||
if not len(matches):
|
if not len(matches):
|
||||||
user = usermodel.objects.create_user(
|
user = usermodel.objects.create_user(
|
||||||
username = user_info['username'],
|
username = user_info['username'],
|
||||||
email = user_info['email'],
|
email = user_info['email'],
|
||||||
|
icy_id = user_info['uuid'],
|
||||||
|
display_name = user_info['display_name']
|
||||||
)
|
)
|
||||||
|
|
||||||
if 'privilege' in user_info:
|
if 'privilege' in user_info:
|
||||||
@ -48,12 +49,7 @@ class OAuthBackend(ModelBackend):
|
|||||||
user.is_staff = (priv > 0)
|
user.is_staff = (priv > 0)
|
||||||
|
|
||||||
user.save()
|
user.save()
|
||||||
user.externaluser = ExternalUser(
|
|
||||||
user = user,
|
|
||||||
icy_id = user_info['uuid'],
|
|
||||||
display_name = user_info['display_name']
|
|
||||||
)
|
|
||||||
user.externaluser.save()
|
|
||||||
match = user
|
match = user
|
||||||
else:
|
else:
|
||||||
match = matches[0]
|
match = matches[0]
|
||||||
|
6
LandingPage/forms.py
Normal file
6
LandingPage/forms.py
Normal 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
|
@ -1,5 +1,5 @@
|
|||||||
from django.db import models
|
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.core.files.storage import FileSystemStorage
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
import os
|
import os
|
||||||
@ -72,13 +72,7 @@ class Show(TimestampedModel):
|
|||||||
def __str__(self):
|
def __str__(self):
|
||||||
return '%s [%s]'%(self.name,self.abbr)
|
return '%s [%s]'%(self.name,self.abbr)
|
||||||
|
|
||||||
class ExternalUser(TimestampedModel):
|
class User(AbstractUser):
|
||||||
user = models.OneToOneField(
|
|
||||||
User,
|
|
||||||
on_delete=models.CASCADE,
|
|
||||||
help_text='The internal Django user',
|
|
||||||
verbose_name="User"
|
|
||||||
)
|
|
||||||
icy_id = models.CharField(
|
icy_id = models.CharField(
|
||||||
max_length=36,
|
max_length=36,
|
||||||
help_text='The UUID assigned to this user by IcyNet\'s auth servers'
|
help_text='The UUID assigned to this user by IcyNet\'s auth servers'
|
||||||
@ -98,8 +92,6 @@ class ExternalUser(TimestampedModel):
|
|||||||
related_name='watched_by',
|
related_name='watched_by',
|
||||||
through='Watch'
|
through='Watch'
|
||||||
)
|
)
|
||||||
def __str__(self):
|
|
||||||
return 'External for %s (%s)'%(self.user.email, self.display_name)
|
|
||||||
|
|
||||||
class Admin(User):
|
class Admin(User):
|
||||||
pass
|
pass
|
||||||
@ -326,7 +318,7 @@ class SubmissionVote(TimestampedModel):
|
|||||||
|
|
||||||
class Favorite(TimestampedModel):
|
class Favorite(TimestampedModel):
|
||||||
user = models.ForeignKey(
|
user = models.ForeignKey(
|
||||||
ExternalUser,
|
User,
|
||||||
on_delete=models.CASCADE
|
on_delete=models.CASCADE
|
||||||
)
|
)
|
||||||
episode = models.ForeignKey(
|
episode = models.ForeignKey(
|
||||||
@ -338,7 +330,7 @@ class Favorite(TimestampedModel):
|
|||||||
|
|
||||||
class Watch(TimestampedModel):
|
class Watch(TimestampedModel):
|
||||||
user = models.ForeignKey(
|
user = models.ForeignKey(
|
||||||
ExternalUser,
|
User,
|
||||||
on_delete=models.CASCADE
|
on_delete=models.CASCADE
|
||||||
)
|
)
|
||||||
episode = models.ForeignKey(
|
episode = models.ForeignKey(
|
||||||
|
@ -21,7 +21,7 @@
|
|||||||
<span class="logo">Episodes<span class="period">.</span>Community</span>
|
<span class="logo">Episodes<span class="period">.</span>Community</span>
|
||||||
<div class="userdata">
|
<div class="userdata">
|
||||||
{% if user.is_authenticated %}
|
{% if user.is_authenticated %}
|
||||||
{{ user.externaluser.display_name }}
|
{{ user.display_name }}
|
||||||
{% else %}
|
{% else %}
|
||||||
<a href="/login">Log in</a>
|
<a href="/login">Log in</a>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
Reference in New Issue
Block a user