diff --git a/EpisodesCommunity/settings.py b/EpisodesCommunity/settings.py index 8b9743e..63e691d 100644 --- a/EpisodesCommunity/settings.py +++ b/EpisodesCommunity/settings.py @@ -42,6 +42,13 @@ ALLOWED_HOSTS = [] # Application definition +AUTHENTICATION_BACKENDS = ( + 'LandingPage.backends.OAuthBackend', + 'django.contrib.auth.backends.ModelBackend', +) + +AUTH_USER_MODEL = 'LandingPage.User' + INSTALLED_APPS = [ 'LandingPage.apps.LandingpageConfig', 'Show.apps.ShowConfig', @@ -65,6 +72,7 @@ MIDDLEWARE = [ ] ROOT_URLCONF = 'EpisodesCommunity.urls' +LOGIN_URL = '/login' TEMPLATES = [ { diff --git a/LandingPage/admin.py b/LandingPage/admin.py index 77fe1f1..44f4c55 100644 --- a/LandingPage/admin.py +++ b/LandingPage/admin.py @@ -1,10 +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(User) -admin.site.register(Admin) +admin.site.register(User, SpecialUserAdmin) admin.site.register(Ban) admin.site.register(ShowModerator) admin.site.register(Report) @@ -18,4 +26,3 @@ admin.site.register(Watch) admin.site.register(DiscussionBoard) admin.site.register(DiscussionReply) admin.site.register(DiscussionVote) - diff --git a/LandingPage/backends.py b/LandingPage/backends.py new file mode 100644 index 0000000..27673ee --- /dev/null +++ b/LandingPage/backends.py @@ -0,0 +1,60 @@ +import requests +import hashlib +import json +import logging +from django.conf import settings +from django.contrib.auth import get_user_model +from django.contrib.auth.backends import ModelBackend + +class OAuthBackend(ModelBackend): + def authenticate(self, code=None): + resp = requests.post( + settings.AUTH_TOKEN_ENDPOINT+"token", + data={ + 'grant_type':'authorization_code', + 'code':code, + 'redirect_uri':settings.AUTH_REDIRECT_URL, + 'client_id':settings.AUTH_CLIENT_ID + }, + headers = { + 'Authorization':'Basic %s'%settings.AUTH_B64 + } + ) + resp_json = resp.json() + if 'error' in resp_json: + logging.warn('OAuth server returned an error: %s'%json.dumps(resp_json)) + else: + user_info = requests.get( + settings.AUTH_TOKEN_ENDPOINT+"user", + headers = { + 'Authorization': 'Bearer ' + resp_json['access_token'] + } + ).json() + + usermodel = get_user_model() + 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: + priv = user_info['privilege'] + user.is_superuser = (priv == 5) + user.is_staff = (priv > 0) + + user.save() + + match = user + else: + match = matches[0] + + match.access_token = resp_json['access_token'] + + return match + return None diff --git a/LandingPage/forms.py b/LandingPage/forms.py new file mode 100644 index 0000000..773d760 --- /dev/null +++ b/LandingPage/forms.py @@ -0,0 +1,6 @@ +from django.contrib.auth.forms import UserChangeForm +from .models import User + +class SpecialUserChangeForm(UserChangeForm): + class Meta(UserChangeForm.Meta): + model = User diff --git a/LandingPage/models.py b/LandingPage/models.py index be27211..9295e45 100644 --- a/LandingPage/models.py +++ b/LandingPage/models.py @@ -1,4 +1,5 @@ from django.db import models +from django.contrib.auth.models import AbstractUser from django.core.files.storage import FileSystemStorage from django.conf import settings import os @@ -71,14 +72,11 @@ class Show(TimestampedModel): def __str__(self): return '%s [%s]'%(self.name,self.abbr) -class User(TimestampedModel): - user_id = models.CharField( +class User(AbstractUser): + icy_id = models.CharField( max_length=36, help_text='The UUID assigned to this user by IcyNet\'s auth servers' ) - email = models.EmailField( - help_text='This user\'s email address' - ) display_name=models.CharField( max_length=20, help_text="The name shown to other users", @@ -94,11 +92,6 @@ class User(TimestampedModel): related_name='watched_by', through='Watch' ) - def __str__(self): - return self.email - -class Admin(User): - pass class Ban(TimestampedModel): user = models.OneToOneField( @@ -108,7 +101,7 @@ class Ban(TimestampedModel): verbose_name="Banned User" ) admin = models.ForeignKey( - Admin, + User, on_delete=models.SET_NULL, null=True, help_text='The admin which banned this user', @@ -286,7 +279,7 @@ class Submission(TimestampedModel): verbose_name='Submitted For' ) user = models.ForeignKey( - 'User', + User, on_delete=models.SET_NULL, null=True, related_name='submissions', @@ -310,7 +303,7 @@ class SubmissionVote(TimestampedModel): help_text='What this submission was cast on' ) user = models.ForeignKey( - 'User', + User, on_delete=models.CASCADE, related_name='votes', help_text='The user who cast this vote' diff --git a/LandingPage/templates/base.html b/LandingPage/templates/base.html index 9d3499a..44580b1 100644 --- a/LandingPage/templates/base.html +++ b/LandingPage/templates/base.html @@ -20,8 +20,8 @@