diff --git a/admin/freeposte/admin/models.py b/admin/freeposte/admin/models.py index 17111fb2..28f05c60 100644 --- a/admin/freeposte/admin/models.py +++ b/admin/freeposte/admin/models.py @@ -55,24 +55,32 @@ class Address(Base): """ __abstract__ = True - localpart = db.Column(db.String(80), primary_key=True, nullable=False) + localpart = db.Column(db.String(80), nullable=False) @declarative.declared_attr def domain_name(cls): return db.Column(db.String(80), db.ForeignKey(Domain.name), - primary_key=True, nullable=False) + nullable=False) - def __str__(self): - return '{0}@{1}'.format(self.localpart, self.domain_name) - - def get_id(self): - return str(self) + # This field is redundant with both localpart and domain name. + # It is however very useful for quick lookups without joining tables, + # especially when the mail server il reading the database. + @declarative.declared_attr + def address(cls): + updater = lambda context: "{0}@{1}".format( + context.current_parameters["localpart"], + context.current_parameters["domain_name"], + ) + return db.Column(db.String(255), + primary_key=True, nullable=False, + default=updater) @classmethod def get_by_email(cls, email): - localpart, domain = email.split('@', maxsplit=1) - # Get the user object - return cls.query.filter_by(domain_name=domain, localpart=localpart).first() + return cls.query.filter_by(address=email).first() + + def __str__(self): + return self.address class User(Address): @@ -102,6 +110,9 @@ class User(Address): is_active = True is_anonymous = False + def get_id(self): + return self.address + pw_context = context.CryptContext( ["sha512_crypt", "sha256_crypt", "md5_crypt"] ) diff --git a/admin/freeposte/admin/templates/alias/list.html b/admin/freeposte/admin/templates/alias/list.html index c595466a..2e5810b3 100644 --- a/admin/freeposte/admin/templates/alias/list.html +++ b/admin/freeposte/admin/templates/alias/list.html @@ -26,8 +26,8 @@ Alias list {% for alias in domain.aliases %} -   - +   + {{ alias }} {{ alias.destination or '-' }} diff --git a/admin/freeposte/admin/templates/user/list.html b/admin/freeposte/admin/templates/user/list.html index 7634ee48..23c025bd 100644 --- a/admin/freeposte/admin/templates/user/list.html +++ b/admin/freeposte/admin/templates/user/list.html @@ -28,14 +28,14 @@ User list {% for user in domain.users %} -   - +   + -   -   -   -   +   +   +   +   {{ user }} diff --git a/dovecot/conf/dovecot-sql.conf.ext b/dovecot/conf/dovecot-sql.conf.ext index c590b195..7cb60250 100644 --- a/dovecot/conf/dovecot-sql.conf.ext +++ b/dovecot/conf/dovecot-sql.conf.ext @@ -5,12 +5,10 @@ connect = /data/freeposte.db password_query = \ SELECT password \ FROM user \ - WHERE user.domain_name = '%d' \ - AND user.localpart = '%n' + WHERE user.address = '%u' # Mostly get the user quota user_query = \ SELECT '*:bytes=' || user.quota_bytes AS quota_rule \ FROM user \ - WHERE user.domain_name = '%d' \ - AND user.localpart = '%n' + WHERE user.address = '%u' diff --git a/postfix/conf/sqlite-virtual_alias_maps.cf b/postfix/conf/sqlite-virtual_alias_maps.cf index 7c7f28f6..979133ff 100644 --- a/postfix/conf/sqlite-virtual_alias_maps.cf +++ b/postfix/conf/sqlite-virtual_alias_maps.cf @@ -2,5 +2,4 @@ dbpath = /data/freeposte.db query = SELECT destination FROM alias - WHERE alias.domain_name = '%d' - AND alias.localpart = '%u' + WHERE alias.address = '%s'