Last fixes.

see #609
This commit is contained in:
Antoine Nguyen
2014-10-08 09:57:43 +02:00
parent 38a3b88224
commit 534b21c896
2 changed files with 46 additions and 19 deletions
@@ -45,12 +45,12 @@ used by PostfixAdmin.
"""
from optparse import make_option
import os
import re
import sys
from django.contrib.auth.models import Group
from django.core.management.base import BaseCommand
from django.db import transaction
from django.db.models import Q
import modoboa.core.models as core_models
import modoboa.extensions.admin.models as admin_models
@@ -60,6 +60,19 @@ from modoboa.extensions.admin import grant_access_to_all_objects
from modoboa.tools.pfxadmin_migrate import models as pf_models
def set_account_password(account, password, scheme):
"""Correclty format the password before storing it.
It seems postfixadmin sometimes prepend the scheme... but not
always :p
"""
if re.search(r"\{[\w-]+\}", password):
account.password = password
else:
account.password = "{%s}%s" % (scheme.upper(), password)
class Command(BaseCommand):
"""Migration from postfixadmin."""
@@ -190,8 +203,8 @@ class Command(BaseCommand):
new_user.email = old_mb.username
new_user.is_active = old_mb.active
new_user.date_joined = old_mb.created
new_user.password = "{%s}%s" % \
(options["passwords_scheme"].upper(), old_mb.password)
set_account_password(
new_user, old_mb.password, options["passwords_scheme"])
new_user.dates = self._migrate_dates(old_mb)
new_user.save(creator=creator, using=options["_to"])
new_user.set_role("SimpleUsers")
@@ -226,29 +239,41 @@ class Command(BaseCommand):
def _migrate_admins(self, options, creator, pf_domain, newdom=None):
"""Administrators migration.
:param pf_domain: The ``pf_models.Domain`` to get the admins to migrate.
:param newdom: If None the admins will not be linked to any domain and
they will be SuperAdmins. Otherwise link them to this domain and make them
DomainAdmins.
:param pf_domain: The ``pf_models.Domain`` to get the admins to migrate.
:param newdom: If None the admins will not be linked to any domain
and they will be SuperAdmins. Otherwise link them to
this domain and make them DomainAdmins.
"""
if (newdom is None):
if newdom is None:
print "\nMigrating super administrators"
else:
print "\tMigrating administrators"
dagroup = Group.objects.using(options["_to"]).get(name="DomainAdmins")
for old_admin in pf_domain.admins.all():
if (newdom is None):
if newdom is None:
print "\tMigrating %s" % old_admin.username
#In postfixadmin, it's possible to have an admin account and a user account with the same username but they are completely different entities, so they do not have a common password. In Modoboa this does not makes sense, so we actually merge these two entities into a single user if both are found. Question is, which password should we use?. The admin password is used to be on the secure side.
# In postfixadmin, it's possible to have an admin account
# and a user account with the same username but they are
# completely different entities, so they do not have a
# common password. In Modoboa this does not makes sense, so
# we actually merge these two entities into a single user
# if both are found. Question is, which password should we
# use?. The admin password is used to be on the secure
# side.
try:
user = core_models.User.objects \
.using(options["_to"]).get(username=old_admin.username)
if ("SimpleUsers" == user.group):
print "Warning: Found an admin account with the same username as normal user '%s'. The existing user will be promoted to admin and it's password changed to the admin's account password. " % user.username
if "SimpleUsers" == user.group:
print (
"Warning: Found an admin account with the same "
"username as normal user '%s'. The existing user will "
"be promoted to admin and it's password changed to the "
"admin's account password. " % user.username
)
except core_models.User.DoesNotExist:
user = core_models.User()
@@ -258,10 +283,10 @@ class Command(BaseCommand):
user.save(creator=creator, using=options["_to"])
user.date_joined = old_admin.modified
user.password = "{%s}%s" % \
(options["passwords_scheme"].upper(), old_admin.password)
set_account_password(
user, old_admin.password, options["passwords_scheme"])
if (newdom is None):
if newdom is None:
grant_access_to_all_objects(user, "SuperAdmins")
user.is_superuser = True
else:
@@ -272,7 +297,8 @@ class Command(BaseCommand):
def _do_migration(self, options, creator_username='admin'):
"""Run the complete migration."""
pf_domains = pf_models.Domain.objects.using(options["_from"]).exclude(domain = 'ALL')
pf_domains = pf_models.Domain.objects.using(
options["_from"]).exclude(domain='ALL')
creator = core_models.User.objects.using(options["_to"]).get(
username=creator_username)
for pf_domain in pf_domains:
@@ -288,7 +314,8 @@ class Command(BaseCommand):
self._migrate_domain(pf_domain, options, creator)
#Handle the ALL domain
pf_domain = pf_models.Domain.objects.using(options["_from"]).get(domain = 'ALL')
pf_domain = pf_models.Domain.objects.using(
options["_from"]).get(domain='ALL')
self._migrate_admins(options, creator, pf_domain)
def handle(self, *args, **options):
+2 -2
View File
@@ -150,7 +150,7 @@ class Mailbox(models.Model):
class Quota(models.Model):
username = models.CharField(max_length=255, primary_key=True)
path = models.CharField(max_length=300, primary_key=True)
path = models.CharField(max_length=100, primary_key=True)
current = models.BigIntegerField(null=True, blank=True)
class Meta:
@@ -160,7 +160,7 @@ class Quota(models.Model):
class Quota2(models.Model):
username = models.CharField(max_length=300, primary_key=True)
username = models.CharField(max_length=100, primary_key=True)
bytes = models.BigIntegerField()
messages = models.IntegerField()