diff --git a/modoboa/core/models.py b/modoboa/core/models.py index 68ac4bead..e817906fd 100644 --- a/modoboa/core/models.py +++ b/modoboa/core/models.py @@ -26,7 +26,7 @@ from modoboa.lib.sysutils import exec_cmd from modoboa.core.extensions import exts_pool try: - from modoboa.lib.ldaputils import * + from modoboa.lib.ldaputils import LDAPAuthBackend ldap_available = True except ImportError: ldap_available = False @@ -110,6 +110,7 @@ class User(AbstractBaseUser, PermissionsMixin): elif scheme == "md5crypt": # The salt may vary from 12 to 48 bits. (Using all six bytes here # with a subset of characters means we get only 35 random bits.) + import string salt = "".join(Random().sample(string.letters + string.digits, 6)) result = md5crypt(raw_value, salt) prefix = "" # the result already has $1$ prepended to it @@ -141,12 +142,9 @@ class User(AbstractBaseUser, PermissionsMixin): raise InternalError( _("Failed to update password: LDAP module not installed") ) - - ab = LDAPAuthBackend() - try: - ab.update_user_password(self.username, curvalue, raw_value) - except LDAPException as e: - raise InternalError(_("Failed to update password: %s" % str(e))) + LDAPAuthBackend().update_user_password( + self.username, curvalue, raw_value + ) events.raiseEvent( "PasswordUpdated", self, raw_value, self.pk is None ) diff --git a/modoboa/lib/ldaputils.py b/modoboa/lib/ldaputils.py index 28e5e178a..ea4723af1 100644 --- a/modoboa/lib/ldaputils.py +++ b/modoboa/lib/ldaputils.py @@ -40,11 +40,9 @@ import string import crypt from random import Random from django.conf import settings +from django.utils.translation import ugettext as _ from modoboa.lib import parameters - - -class LDAPException(Exception): - pass +from modoboa.lib.exceptions import InternalError class LDAPAuthBackend(object): @@ -126,5 +124,5 @@ class LDAPAuthBackend(object): self.pwd_attr, self._crypt_password(newpassword))] self.conn.modify_s(self.user_dn, ldif) - except ldap.LDAPError, e: - raise LDAPException(str(e)) + except ldap.LDAPError as e: + raise InternalError(_("Failed to update password: %s" % str(e)))