mirror of
https://gitlab.com/allianceauth/allianceauth.git
synced 2026-02-09 08:36:23 +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:
@@ -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),
|
||||
]
|
||||
Reference in New Issue
Block a user