mirror of
https://gitlab.com/allianceauth/allianceauth.git
synced 2026-02-07 15:46:20 +01:00
Enforce unique AuthServicesInfo (#618)
Alter user field to OneToOneField Migration to enforce uniqueness pre-change Migration to ensure all users have an AuthServicesInfo Receiver to automatically create one upon user creation Replace AuthServicesInfo.get_or_create with get Prevent deletion of AuthServicesInfo from admin site Remove add and delete permissions from model. Get character names in chunks on corpstats update to prevent HTTP400 when requesting >350(ish) names Include corpstats docs. Update settings docs.
This commit is contained in:
@@ -26,6 +26,14 @@ class AuthServicesInfoManager(admin.ModelAdmin):
|
||||
pass
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def has_delete_permission(request, obj=None):
|
||||
return False
|
||||
|
||||
@staticmethod
|
||||
def has_add_permission(request, obj=None):
|
||||
return False
|
||||
|
||||
def sync_jabber(self, request, queryset):
|
||||
count = 0
|
||||
for a in queryset: # queryset filtering doesn't work here?
|
||||
|
||||
@@ -17,7 +17,7 @@ class AuthServicesInfoManager:
|
||||
def update_main_char_id(char_id, user):
|
||||
if User.objects.filter(username=user.username).exists():
|
||||
logger.debug("Updating user %s main character to id %s" % (user, char_id))
|
||||
authserviceinfo = AuthServicesInfo.objects.get_or_create(user=user)[0]
|
||||
authserviceinfo = AuthServicesInfo.objects.get(user=user)
|
||||
authserviceinfo.main_char_id = char_id
|
||||
authserviceinfo.save(update_fields=['main_char_id'])
|
||||
logger.info("Updated user %s main character to id %s" % (user, char_id))
|
||||
@@ -28,7 +28,7 @@ class AuthServicesInfoManager:
|
||||
def update_user_forum_info(username, user):
|
||||
if User.objects.filter(username=user.username).exists():
|
||||
logger.debug("Updating user %s forum info: username %s" % (user, username))
|
||||
authserviceinfo = AuthServicesInfo.objects.get_or_create(user=user)[0]
|
||||
authserviceinfo = AuthServicesInfo.objects.get(user=user)
|
||||
authserviceinfo.forum_username = username
|
||||
authserviceinfo.save(update_fields=['forum_username'])
|
||||
logger.info("Updated user %s forum info in authservicesinfo model." % user)
|
||||
@@ -39,7 +39,7 @@ class AuthServicesInfoManager:
|
||||
def update_user_jabber_info(username, user):
|
||||
if User.objects.filter(username=user.username).exists():
|
||||
logger.debug("Updating user %s jabber info: username %s" % (user, username))
|
||||
authserviceinfo = AuthServicesInfo.objects.get_or_create(user=user)[0]
|
||||
authserviceinfo = AuthServicesInfo.objects.get(user=user)
|
||||
authserviceinfo.jabber_username = username
|
||||
authserviceinfo.save(update_fields=['jabber_username'])
|
||||
logger.info("Updated user %s jabber info in authservicesinfo model." % user)
|
||||
@@ -50,7 +50,7 @@ class AuthServicesInfoManager:
|
||||
def update_user_mumble_info(username, user):
|
||||
if User.objects.filter(username=user.username).exists():
|
||||
logger.debug("Updating user %s mumble info: username %s" % (user, username))
|
||||
authserviceinfo = AuthServicesInfo.objects.get_or_create(user=user)[0]
|
||||
authserviceinfo = AuthServicesInfo.objects.get(user=user)
|
||||
authserviceinfo.mumble_username = username
|
||||
authserviceinfo.save(update_fields=['mumble_username'])
|
||||
logger.info("Updated user %s mumble info in authservicesinfo model." % user)
|
||||
@@ -61,7 +61,7 @@ class AuthServicesInfoManager:
|
||||
def update_user_ipboard_info(username, user):
|
||||
if User.objects.filter(username=user.username).exists():
|
||||
logger.debug("Updating user %s ipboard info: uername %s" % (user, username))
|
||||
authserviceinfo = AuthServicesInfo.objects.get_or_create(user=user)[0]
|
||||
authserviceinfo = AuthServicesInfo.objects.get(user=user)
|
||||
authserviceinfo.ipboard_username = username
|
||||
authserviceinfo.save(update_fields=['ipboard_username'])
|
||||
logger.info("Updated user %s ipboard info in authservicesinfo model." % user)
|
||||
@@ -72,7 +72,7 @@ class AuthServicesInfoManager:
|
||||
def update_user_xenforo_info(username, user):
|
||||
if User.objects.filter(username=user.username).exists():
|
||||
logger.debug("Updating user %s xenforo info: uername %s" % (user, username))
|
||||
authserviceinfo = AuthServicesInfo.objects.get_or_create(user=user)[0]
|
||||
authserviceinfo = AuthServicesInfo.objects.get(user=user)
|
||||
authserviceinfo.xenforo_username = username
|
||||
authserviceinfo.save(update_fields=['xenforo_username'])
|
||||
logger.info("Updated user %s xenforo info in authservicesinfo model." % user)
|
||||
@@ -83,7 +83,7 @@ class AuthServicesInfoManager:
|
||||
def update_user_teamspeak3_info(uid, perm_key, user):
|
||||
if User.objects.filter(username=user.username).exists():
|
||||
logger.debug("Updating user %s teamspeak3 info: uid %s" % (user, uid))
|
||||
authserviceinfo = AuthServicesInfo.objects.get_or_create(user=user)[0]
|
||||
authserviceinfo = AuthServicesInfo.objects.get(user=user)
|
||||
authserviceinfo.teamspeak3_uid = uid
|
||||
authserviceinfo.teamspeak3_perm_key = perm_key
|
||||
authserviceinfo.save(update_fields=['teamspeak3_uid', 'teamspeak3_perm_key'])
|
||||
@@ -95,7 +95,7 @@ class AuthServicesInfoManager:
|
||||
def update_is_blue(is_blue, user):
|
||||
if User.objects.filter(username=user.username).exists():
|
||||
logger.debug("Updating user %s blue status: %s" % (user, is_blue))
|
||||
authserviceinfo = AuthServicesInfo.objects.get_or_create(user=user)[0]
|
||||
authserviceinfo = AuthServicesInfo.objects.get(user=user)
|
||||
authserviceinfo.is_blue = is_blue
|
||||
authserviceinfo.save(update_fields=['is_blue'])
|
||||
logger.info("Updated user %s blue status to %s in authservicesinfo model." % (user, is_blue))
|
||||
@@ -104,7 +104,7 @@ class AuthServicesInfoManager:
|
||||
def update_user_discord_info(user_id, user):
|
||||
if User.objects.filter(username=user.username).exists():
|
||||
logger.debug("Updating user %s discord info: user_id %s" % (user, user_id))
|
||||
authserviceinfo = AuthServicesInfo.objects.get_or_create(user=user)[0]
|
||||
authserviceinfo = AuthServicesInfo.objects.get(user=user)
|
||||
authserviceinfo.discord_uid = user_id
|
||||
authserviceinfo.save(update_fields=['discord_uid'])
|
||||
logger.info("Updated user %s discord info in authservicesinfo model." % user)
|
||||
@@ -115,7 +115,7 @@ class AuthServicesInfoManager:
|
||||
def update_user_ips4_info(username, id, user):
|
||||
if User.objects.filter(username=user.username).exists():
|
||||
logger.debug("Updating user %s IPS4 info: username %s" % (user, username))
|
||||
authserviceinfo = AuthServicesInfo.objects.get_or_create(user=user)[0]
|
||||
authserviceinfo = AuthServicesInfo.objects.get(user=user)
|
||||
authserviceinfo.ips4_username = username
|
||||
authserviceinfo.ips4_id = id
|
||||
authserviceinfo.save(update_fields=['ips4_username', 'ips4_id'])
|
||||
@@ -127,7 +127,7 @@ class AuthServicesInfoManager:
|
||||
def update_user_smf_info(username, user):
|
||||
if User.objects.filter(username=user.username).exists():
|
||||
logger.debug("Updating user %s forum info: username %s" % (user, username))
|
||||
authserviceinfo = AuthServicesInfo.objects.get_or_create(user=user)[0]
|
||||
authserviceinfo = AuthServicesInfo.objects.get(user=user)
|
||||
authserviceinfo.smf_username = username
|
||||
authserviceinfo.save(update_fields=['smf_username'])
|
||||
logger.info("Updated user %s smf info in authservicesinfo model." % user)
|
||||
@@ -138,7 +138,7 @@ class AuthServicesInfoManager:
|
||||
def update_user_market_info(username, user):
|
||||
if User.objects.filter(username=user.username).exists():
|
||||
logger.debug("Updating user %s market info: username %s" % (user, username))
|
||||
authserviceinfo = AuthServicesInfo.objects.get_or_create(user=user)[0]
|
||||
authserviceinfo = AuthServicesInfo.objects.get(user=user)
|
||||
authserviceinfo.market_username = username
|
||||
authserviceinfo.save(update_fields=['market_username'])
|
||||
logger.info("Updated user %s market info in authservicesinfo model." % user)
|
||||
@@ -173,7 +173,7 @@ class UserState:
|
||||
@classmethod
|
||||
def get_membership_state(cls, request):
|
||||
if request.user.is_authenticated:
|
||||
auth = AuthServicesInfo.objects.get_or_create(user=request.user)[0]
|
||||
auth = AuthServicesInfo.objects.get(user=request.user)
|
||||
return {'STATE': auth.state}
|
||||
return {'STATE': cls.NONE_STATE}
|
||||
|
||||
@@ -182,6 +182,6 @@ class UserState:
|
||||
if user.is_superuser and settings.SUPERUSER_STATE_BYPASS:
|
||||
return True
|
||||
if user.is_authenticated:
|
||||
auth = AuthServicesInfo.objects.get_or_create(user=user)[0]
|
||||
auth = AuthServicesInfo.objects.get(user=user)
|
||||
return auth.state in states
|
||||
return False
|
||||
|
||||
@@ -24,7 +24,7 @@ def determine_membership_by_character(char, apps):
|
||||
|
||||
def determine_membership_by_user(user, apps):
|
||||
AuthServicesInfo = apps.get_model('authentication', 'AuthServicesInfo')
|
||||
auth, c = AuthServicesInfo.objects.get_or_create(user=user)
|
||||
auth = AuthServicesInfo.objects.get(user=user)
|
||||
if auth.main_char_id:
|
||||
EveCharacter = apps.get_model('eveonline', 'EveCharacter')
|
||||
if EveCharacter.objects.filter(character_id=auth.main_char_id).exists():
|
||||
@@ -41,7 +41,7 @@ def set_state(user, apps):
|
||||
else:
|
||||
state = NONE_STATE
|
||||
AuthServicesInfo = apps.get_model('authentication', 'AuthServicesInfo')
|
||||
auth = AuthServicesInfo.objects.get_or_create(user=user)[0]
|
||||
auth = AuthServicesInfo.objects.get(user=user)
|
||||
if auth.state != state:
|
||||
auth.state = state
|
||||
auth.save()
|
||||
|
||||
40
authentication/migrations/0010_only_one_authservicesinfo.py
Normal file
40
authentication/migrations/0010_only_one_authservicesinfo.py
Normal file
@@ -0,0 +1,40 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.10.1 on 2017-01-07 06:47
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
def count_completed_fields(model):
|
||||
return len([True for key, value in model.__dict__.items() if bool(value)])
|
||||
|
||||
def forward(apps, schema_editor):
|
||||
# this ensures only one model exists per user
|
||||
AuthServicesInfo = apps.get_model('authentication', 'AuthServicesInfo')
|
||||
users = set([a.user for a in AuthServicesInfo.objects.all()])
|
||||
for u in users:
|
||||
auths = AuthServicesInfo.objects.filter(user=u)
|
||||
if auths.count() > 1:
|
||||
pk = auths[0].pk
|
||||
largest = 0
|
||||
for auth in auths:
|
||||
completed = count_completed_fields(auth)
|
||||
if completed > largest:
|
||||
largest = completed
|
||||
pk = auth.pk
|
||||
auths.exclude(pk=pk).delete()
|
||||
|
||||
# ensure all users have a model
|
||||
User = apps.get_model('auth', 'User')
|
||||
for u in User.objects.exclude(pk__in=[user.pk for user in users]):
|
||||
AuthServicesInfo.objects.create(user=u)
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('authentication', '0009_auto_20161021_0228'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RunPython(forward, migrations.RunPython.noop)
|
||||
]
|
||||
@@ -0,0 +1,22 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.10.1 on 2017-01-07 07:11
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('authentication', '0010_only_one_authservicesinfo'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='authservicesinfo',
|
||||
name='user',
|
||||
field=models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,39 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.10.5 on 2017-01-12 00:59
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
def remove_permissions(apps, schema_editor):
|
||||
ContentType = apps.get_model('contenttypes', 'ContentType')
|
||||
Permission = apps.get_model('auth', 'Permission')
|
||||
AuthServicesInfo = apps.get_model('authentication', 'AuthServicesInfo')
|
||||
|
||||
# delete the add and remove permissions for AuthServicesInfo
|
||||
ct = ContentType.objects.get_for_model(AuthServicesInfo)
|
||||
Permission.objects.filter(content_type=ct).filter(codename__in=['add_authservicesinfo', 'delete_authservicesinfo']).delete()
|
||||
|
||||
|
||||
def add_permissions(apps, schema_editor):
|
||||
ContentType = apps.get_model('contenttypes', 'ContentType')
|
||||
Permission = apps.get_model('auth', 'Permission')
|
||||
AuthServicesInfo = apps.get_model('authentication', 'AuthServicesInfo')
|
||||
|
||||
# recreate the add and remove permissions for AuthServicesInfo
|
||||
ct = ContentType.objects.get_for_model(AuthServicesInfo)
|
||||
Permission.objects.create(content_type=ct, codename='add_authservicesinfo', name='Can add auth services info')
|
||||
Permission.objects.create(content_type=ct, codename='delete_authservicesinfo', name='Can delete auth services info')
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('authentication', '0011_authservicesinfo_user_onetoonefield'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterModelOptions(
|
||||
name='authservicesinfo',
|
||||
options={'default_permissions': ('change',)},
|
||||
),
|
||||
migrations.RunPython(remove_permissions, add_permissions),
|
||||
]
|
||||
@@ -7,6 +7,9 @@ from authentication.states import MEMBER_STATE, BLUE_STATE, NONE_STATE
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class AuthServicesInfo(models.Model):
|
||||
class Meta:
|
||||
default_permissions = ('change',)
|
||||
|
||||
STATE_CHOICES = (
|
||||
(NONE_STATE, 'None'),
|
||||
(BLUE_STATE, 'Blue'),
|
||||
@@ -27,7 +30,7 @@ class AuthServicesInfo(models.Model):
|
||||
smf_username = models.CharField(max_length=254, blank=True, default="")
|
||||
market_username = models.CharField(max_length=254, blank=True, default="")
|
||||
main_char_id = models.CharField(max_length=64, blank=True, default="")
|
||||
user = models.ForeignKey(User)
|
||||
user = models.OneToOneField(User)
|
||||
state = models.CharField(blank=True, null=True, choices=STATE_CHOICES, default=NONE_STATE, max_length=10)
|
||||
|
||||
def __str__(self):
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
from __future__ import unicode_literals
|
||||
from django.db.models.signals import pre_save
|
||||
from django.db.models.signals import pre_save, post_save
|
||||
from django.dispatch import receiver
|
||||
from django.contrib.auth.models import User
|
||||
from authentication.models import AuthServicesInfo
|
||||
from authentication.states import MEMBER_STATE, BLUE_STATE
|
||||
from authentication.tasks import make_member, make_blue, disable_member
|
||||
@@ -23,3 +24,11 @@ def pre_save_auth_state(sender, instance, *args, **kwargs):
|
||||
else:
|
||||
disable_member(instance.user)
|
||||
validate_services(instance.user, instance.state)
|
||||
|
||||
|
||||
@receiver(post_save, sender=User)
|
||||
def post_save_user(sender, instance, created, *args, **kwargs):
|
||||
# ensure all users have a model
|
||||
if created:
|
||||
AuthServicesInfo.objects.get_or_create(user=instance)
|
||||
|
||||
|
||||
@@ -85,7 +85,7 @@ def determine_membership_by_character(char):
|
||||
|
||||
def determine_membership_by_user(user):
|
||||
logger.debug("Determining membership of user %s" % user)
|
||||
auth, c = AuthServicesInfo.objects.get_or_create(user=user)
|
||||
auth = AuthServicesInfo.objects.get(user=user)
|
||||
if auth.main_char_id:
|
||||
if EveCharacter.objects.filter(character_id=auth.main_char_id).exists():
|
||||
char = EveCharacter.objects.get(character_id=auth.main_char_id)
|
||||
@@ -105,7 +105,7 @@ def set_state(user):
|
||||
else:
|
||||
state = NONE_STATE
|
||||
logger.debug("Assigning user %s to state %s" % (user, state))
|
||||
auth = AuthServicesInfo.objects.get_or_create(user=user)[0]
|
||||
auth = AuthServicesInfo.objects.get(user=user)
|
||||
if auth.state != state:
|
||||
auth.state = state
|
||||
auth.save()
|
||||
|
||||
Reference in New Issue
Block a user