Commit 2524c09e authored by Grizzly's avatar Grizzly

Exclusion d'users, anonymisation machine et domaines

parent 3ce8815c
from django.core.management.base import BaseCommand from django.core.management.base import BaseCommand
from users.models import User, School, Adherent, Club from users.models import User, School, Adherent, Club
from machines.models import Domain, Machine
from django.db.models import F, Value from django.db.models import F, Value
from django.db.models import Q
from django.db.models.functions import Concat from django.db.models.functions import Concat
from re2o.login import hashNT, makeSecret from re2o.login import hashNT, makeSecret
import os, random, string import os, random, string
from random import randint
class Command(BaseCommand): class Command(BaseCommand):
help="Anonymize the data in the database in order to use them on critical servers (dev, personnal...). Every information will be overwritten using non-personnal informations. This script must follow any modification of the database." help="Anonymize the data in the database in order to use them on critical servers (dev, personnal...). Every information will be overwritten using non-personnal informations. This script must follow any modification of the database.\nOptionnal argument: {id|id|id|...} to exclude users from anonymisation"
def handle(self, *args, **kwargs): def add_arguments(self, parser):
parser.add_argument('user_id', nargs='+', type=int, help='User ID')
total = Adherent.objects.count() def handle(self, *args, **kwargs):
self.stdout.write("Starting anonymizing the {} users data.".format(total)) users_ids = kwargs['user_id']
for user_id in users_ids:
u = User.objects.all() self.stdout.write("User: {} will not be anonymised".format(User.objects.filter(id=user_id).get().name))
a = Adherent.objects.all()
c = Club.objects.all() self.stdout.write(self.style.WARNING('\nDISCLAIMER\nThis function will make your database unusable for production. Are you sure you want to run this ?(doit): '))
if(input()=="doit"):
self.stdout.write('Supression de l\'école...')
# Create a fake School to put everyone in it. total = Adherent.objects.count()
ecole = School(name="Ecole des Ninja") self.stdout.write("Starting anonymizing the {} users data.".format(total))
ecole.save()
u.update(school=ecole) u = User.objects.filter(~Q(id__in=users_ids))
self.stdout.write(self.style.SUCCESS('done ...')) a = Adherent.objects.filter(~Q(id__in=users_ids))
c = Club.objects.filter(~Q(id__in=users_ids))
self.stdout.write('Supression des chambres...') d = Domain.objects.all()
a.update(room=None) m = Machine.objects.filter(~Q(user_id__in=users_ids))
c.update(room=None)
self.stdout.write(self.style.SUCCESS('done ...')) self.stdout.write('Supression de l\'école...')
# Create a fake School to put everyone in it.
self.stdout.write('Supression des mails...') ecole = School(name="Ecole des Ninja")
u.update(email='example@example.org', ecole.save()
local_email_redirect = False, u.update(school=ecole)
local_email_enabled=False) self.stdout.write(self.style.SUCCESS('done ...'))
self.stdout.write(self.style.SUCCESS('done ...'))
self.stdout.write('Supression des chambres...')
self.stdout.write('Supression des noms, prenoms, pseudo, telephone, commentaire...') a.update(room=None)
a.update(name=Concat(Value('name of '), 'id')) c.update(room=None)
self.stdout.write(self.style.SUCCESS('done name')) self.stdout.write(self.style.SUCCESS('done ...'))
a.update(surname=Concat(Value('surname of '), 'id')) self.stdout.write('Supression des mails...')
self.stdout.write(self.style.SUCCESS('done surname')) u.update(email='example@example.org',
local_email_redirect = False,
u.update(pseudo=F('id')) local_email_enabled=False)
self.stdout.write(self.style.SUCCESS('done pseudo')) self.stdout.write(self.style.SUCCESS('done ...'))
a.update(telephone=Concat(Value('phone of '), 'id')) self.stdout.write('Supression des noms, prenoms, pseudo, telephone, commentaire...')
self.stdout.write(self.style.SUCCESS('done phone')) a.update(name=Concat(Value('name of '), 'id'))
self.stdout.write(self.style.SUCCESS('done name'))
a.update(comment=Concat(Value('commentaire of '), 'id'))
self.stdout.write(self.style.SUCCESS('done ...')) a.update(surname=Concat(Value('surname of '), 'id'))
self.stdout.write(self.style.SUCCESS('done surname'))
self.stdout.write('Unification du mot de passe...')
# Define the password u.update(pseudo=F('id'))
chars = string.ascii_letters + string.digits + '!@#$%^&*()' self.stdout.write(self.style.SUCCESS('done pseudo'))
taille = 20
random.seed = (os.urandom(1024)) a.update(telephone=Concat(Value('phone of '), 'id'))
password = "" self.stdout.write(self.style.SUCCESS('done phone'))
for i in range(taille):
password+=random.choice(chars) a.update(comment=Concat(Value('commentaire of '), 'id'))
self.stdout.write(self.style.SUCCESS('done ...'))
self.stdout.write(self.style.HTTP_NOT_MODIFIED('The password will be: {}'.format(password)))
self.stdout.write('Renommage des machines...')
u.update(pwd_ntlm = hashNT(password)) m.update(name=Concat(Value('Machine '),F('id'),Value(' of '),F('user_id')))
u.update(password = makeSecret(password)) d.update(name=Concat(Value('Domaine id '),F('id')))
self.stdout.write(self.style.SUCCESS('done...')) self.stdout.write(self.style.SUCCESS('done ...'))
self.stdout.write("Data anonymized!") self.stdout.write('Unification du mot de passe...')
# Define the password
chars = string.ascii_letters + string.digits + '!@#$%^&*()'
taille = 20
random.seed = (os.urandom(1024))
password = ""
for i in range(taille):
password+=random.choice(chars)
self.stdout.write(self.style.HTTP_NOT_MODIFIED('The password will be: {}'.format(password)))
u.update(pwd_ntlm = hashNT(password))
u.update(password = makeSecret(password))
self.stdout.write(self.style.SUCCESS('done...'))
self.stdout.write("Data anonymized!")
else:
self.stdout.write("Anonymisation aborted")
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment