2018-11-04 16:26:04 +00:00
from django . core . management . base import BaseCommand
2018-11-05 19:08:33 +00:00
from users . models import User , School , Adherent , Club
2018-12-10 20:50:48 +00:00
from machines . models import Domain , Machine
2019-01-09 18:14:06 +00:00
from reversion . models import Revision
2018-11-04 16:26:04 +00:00
from django . db . models import F , Value
2018-12-10 20:50:48 +00:00
from django . db . models import Q
2018-11-04 16:26:04 +00:00
from django . db . models . functions import Concat
2018-11-04 17:09:24 +00:00
from re2o . login import hashNT , makeSecret
import os , random , string
2018-12-10 20:50:48 +00:00
from random import randint
2018-11-04 17:09:24 +00:00
2019-11-04 16:55:03 +00:00
2018-11-04 16:26:04 +00:00
class Command ( BaseCommand ) :
2019-11-04 16:55:03 +00:00
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. \n Optionnal argument: { id|id|id|...} to exclude users from anonymisation "
2018-11-04 16:26:04 +00:00
2018-12-10 20:50:48 +00:00
def add_arguments ( self , parser ) :
2019-11-04 16:55:03 +00:00
parser . add_argument ( " user_id " , nargs = " + " , type = int , help = " User ID " )
2018-11-04 16:26:04 +00:00
2018-12-10 20:50:48 +00:00
def handle ( self , * args , * * kwargs ) :
2019-11-04 16:55:03 +00:00
users_ids = kwargs [ " user_id " ]
2018-12-10 20:50:48 +00:00
for user_id in users_ids :
2019-11-04 16:55:03 +00:00
self . stdout . write (
" User: {} will not be anonymised " . format (
User . objects . filter ( id = user_id ) . get ( ) . name
)
)
self . stdout . write (
self . style . WARNING (
" \n DISCLAIMER \n This function will make your database unusable for production. Are you sure you want to run this ?(doit): "
)
)
if input ( ) == " doit " :
2018-12-10 20:50:48 +00:00
total = Adherent . objects . count ( )
self . stdout . write ( " Starting anonymizing the {} users data. " . format ( total ) )
2019-11-04 16:55:03 +00:00
2018-12-10 20:50:48 +00:00
u = User . objects . filter ( ~ Q ( id__in = users_ids ) )
a = Adherent . objects . filter ( ~ Q ( id__in = users_ids ) )
c = Club . objects . filter ( ~ Q ( id__in = users_ids ) )
d = Domain . objects . all ( )
m = Machine . objects . filter ( ~ Q ( user_id__in = users_ids ) )
2019-11-04 16:55:03 +00:00
self . stdout . write ( " Supression de l ' école... " )
2018-12-10 20:50:48 +00:00
# Create a fake School to put everyone in it.
ecole = School ( name = " Ecole des Ninja " )
ecole . save ( )
u . update ( school = ecole )
2019-11-04 16:55:03 +00:00
self . stdout . write ( self . style . SUCCESS ( " done ... " ) )
2018-12-10 20:50:48 +00:00
2019-11-04 16:55:03 +00:00
self . stdout . write ( " Supression des chambres... " )
2018-12-10 20:50:48 +00:00
a . update ( room = None )
c . update ( room = None )
2019-11-04 16:55:03 +00:00
self . stdout . write ( self . style . SUCCESS ( " done ... " ) )
self . stdout . write ( " Supression des mails... " )
u . update (
email = " example@example.org " ,
local_email_redirect = False ,
local_email_enabled = False ,
)
self . stdout . write ( self . style . SUCCESS ( " done ... " ) )
self . stdout . write (
" Supression des noms, prenoms, pseudo, telephone, commentaire... "
)
a . update ( name = Concat ( Value ( " name of " ) , " id " ) )
self . stdout . write ( self . style . SUCCESS ( " done name " ) )
a . update ( surname = Concat ( Value ( " surname of " ) , " id " ) )
self . stdout . write ( self . style . SUCCESS ( " done surname " ) )
u . update ( pseudo = F ( " id " ) )
self . stdout . write ( self . style . SUCCESS ( " done pseudo " ) )
a . update ( telephone = Concat ( Value ( " phone of " ) , " id " ) )
self . stdout . write ( self . style . SUCCESS ( " done phone " ) )
a . update ( comment = Concat ( Value ( " commentaire of " ) , " id " ) )
self . stdout . write ( self . style . SUCCESS ( " done ... " ) )
self . stdout . write ( " Renommage des machines... " )
m . update (
name = Concat ( Value ( " Machine " ) , F ( " id " ) , Value ( " of " ) , F ( " user_id " ) )
)
d . update ( name = Concat ( Value ( " Domaine id " ) , F ( " id " ) ) )
self . stdout . write ( self . style . SUCCESS ( " done ... " ) )
self . stdout . write ( " Unification du mot de passe... " )
2018-12-10 20:50:48 +00:00
# Define the password
2019-11-04 16:55:03 +00:00
chars = string . ascii_letters + string . digits + " !@#$ % ^&*() "
2018-12-10 20:50:48 +00:00
taille = 20
2019-11-04 16:55:03 +00:00
random . seed = os . urandom ( 1024 )
2018-12-10 20:50:48 +00:00
password = " "
for i in range ( taille ) :
2019-11-04 16:55:03 +00:00
password + = random . choice ( chars )
2018-12-10 20:50:48 +00:00
2019-11-04 16:55:03 +00:00
self . stdout . write (
self . style . HTTP_NOT_MODIFIED (
" The password will be: {} " . format ( password )
)
)
2018-12-10 20:50:48 +00:00
2019-11-04 16:55:03 +00:00
u . update ( pwd_ntlm = hashNT ( password ) )
u . update ( password = makeSecret ( password ) )
self . stdout . write ( self . style . SUCCESS ( " done... " ) )
2018-12-10 20:50:48 +00:00
2019-11-04 16:55:03 +00:00
self . stdout . write ( " Suppression de l ' historique (This may take some time) " )
2019-01-09 18:14:06 +00:00
Revision . objects . all ( ) . delete ( )
2019-11-04 16:55:03 +00:00
self . stdout . write ( self . style . SUCCESS ( " done... " ) )
2019-01-09 18:14:06 +00:00
2018-12-10 20:50:48 +00:00
self . stdout . write ( " Data anonymized! " )
else :
2019-11-04 16:55:03 +00:00
self . stdout . write ( " Anonymisation aborted " )