2016-03-20 16:36:56 +02:00
|
|
|
from freeposte.admin import db
|
2016-02-20 14:57:26 +02:00
|
|
|
|
2016-03-19 21:37:48 +02:00
|
|
|
from sqlalchemy.ext import declarative
|
|
|
|
from passlib import context
|
2016-03-20 12:31:14 +02:00
|
|
|
from datetime import datetime
|
2016-03-19 21:37:48 +02:00
|
|
|
|
2016-03-20 16:36:56 +02:00
|
|
|
import re
|
|
|
|
|
2016-03-19 21:37:48 +02:00
|
|
|
|
2016-04-24 19:17:40 +02:00
|
|
|
# Many-to-many association table for domain managers
|
|
|
|
managers = db.Table('manager',
|
2016-03-19 21:37:48 +02:00
|
|
|
db.Column('domain_name', db.String(80), db.ForeignKey('domain.name')),
|
2016-05-01 20:04:40 +02:00
|
|
|
db.Column('user_email', db.String(255), db.ForeignKey('user.email'))
|
2016-03-19 21:37:48 +02:00
|
|
|
)
|
2016-02-20 14:57:26 +02:00
|
|
|
|
2016-02-20 21:11:59 +02:00
|
|
|
|
2016-03-20 12:31:14 +02:00
|
|
|
class Base(db.Model):
|
|
|
|
""" Base class for all models
|
|
|
|
"""
|
|
|
|
|
|
|
|
__abstract__ = True
|
|
|
|
|
|
|
|
created_at = db.Column(db.Date, nullable=False, default=datetime.now)
|
|
|
|
updated_at = db.Column(db.Date, nullable=True, onupdate=datetime.now)
|
2016-03-20 12:38:37 +02:00
|
|
|
comment = db.Column(db.String(255), nullable=True)
|
2016-03-20 12:31:14 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Domain(Base):
|
2016-03-19 21:37:48 +02:00
|
|
|
""" A DNS domain that has mail addresses associated to it.
|
|
|
|
"""
|
|
|
|
name = db.Column(db.String(80), primary_key=True, nullable=False)
|
2016-04-24 19:17:40 +02:00
|
|
|
managers = db.relationship('User', secondary=managers,
|
|
|
|
backref=db.backref('manager_of'), lazy='dynamic')
|
2016-03-22 22:22:49 +02:00
|
|
|
max_users = db.Column(db.Integer, nullable=False, default=0)
|
|
|
|
max_aliases = db.Column(db.Integer, nullable=False, default=0)
|
2016-02-20 14:57:26 +02:00
|
|
|
|
2016-05-01 20:04:40 +02:00
|
|
|
def has_email(self, localpart):
|
|
|
|
for email in self.users + self.aliases:
|
|
|
|
if email.localpart == localpart:
|
2016-03-22 20:47:15 +02:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2016-02-20 14:57:26 +02:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
|
2016-05-01 20:04:40 +02:00
|
|
|
class Email(Base):
|
|
|
|
""" Abstraction for an email address (localpart and domain).
|
2016-03-19 21:37:48 +02:00
|
|
|
"""
|
|
|
|
__abstract__ = True
|
|
|
|
|
2016-04-20 21:14:04 +02:00
|
|
|
localpart = db.Column(db.String(80), nullable=False)
|
2016-03-19 21:37:48 +02:00
|
|
|
|
|
|
|
@declarative.declared_attr
|
|
|
|
def domain_name(cls):
|
|
|
|
return db.Column(db.String(80), db.ForeignKey(Domain.name),
|
2016-04-20 21:14:04 +02:00
|
|
|
nullable=False)
|
2016-02-20 14:57:26 +02:00
|
|
|
|
2016-04-20 21:14:04 +02:00
|
|
|
# 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
|
2016-05-01 20:04:40 +02:00
|
|
|
def email(cls):
|
2016-04-20 21:14:04 +02:00
|
|
|
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)
|
2016-02-20 14:57:26 +02:00
|
|
|
|
2016-04-20 21:14:04 +02:00
|
|
|
def __str__(self):
|
2016-05-01 20:04:40 +02:00
|
|
|
return self.email
|
2016-02-20 14:57:26 +02:00
|
|
|
|
2016-03-19 21:37:48 +02:00
|
|
|
|
2016-05-01 20:04:40 +02:00
|
|
|
class User(Email):
|
|
|
|
""" A user is an email address that has a password to access a mailbox.
|
2016-03-19 21:37:48 +02:00
|
|
|
"""
|
|
|
|
domain = db.relationship(Domain, backref='users')
|
|
|
|
password = db.Column(db.String(255), nullable=False)
|
|
|
|
quota_bytes = db.Column(db.Integer(), nullable=False, default=10**9)
|
|
|
|
global_admin = db.Column(db.Boolean(), nullable=False, default=False)
|
|
|
|
|
2016-03-22 22:05:08 +02:00
|
|
|
# Features
|
|
|
|
enable_imap = db.Column(db.Boolean(), nullable=False, default=True)
|
|
|
|
enable_pop = db.Column(db.Boolean(), nullable=False, default=True)
|
|
|
|
|
2016-03-20 12:00:01 +02:00
|
|
|
# Filters
|
2016-05-04 16:12:56 +02:00
|
|
|
forward_enabled = db.Column(db.Boolean(), nullable=False, default=False)
|
|
|
|
forward_destination = db.Column(db.String(255), nullable=True, default=None)
|
|
|
|
reply_enabled = db.Column(db.Boolean(), nullable=False, default=False)
|
2016-03-20 12:14:27 +02:00
|
|
|
reply_subject = db.Column(db.String(255), nullable=True, default=None)
|
2016-03-20 12:00:01 +02:00
|
|
|
reply_body = db.Column(db.Text(), nullable=True, default=None)
|
|
|
|
|
2016-03-20 12:09:06 +02:00
|
|
|
# Settings
|
|
|
|
displayed_name = db.Column(db.String(160), nullable=False, default="")
|
|
|
|
spam_enabled = db.Column(db.Boolean(), nullable=False, default=True)
|
|
|
|
spam_threshold = db.Column(db.Numeric(), nullable=False, default=5.0)
|
|
|
|
|
|
|
|
# Flask-login attributes
|
2016-03-19 21:37:48 +02:00
|
|
|
is_authenticated = True
|
|
|
|
is_active = True
|
|
|
|
is_anonymous = False
|
|
|
|
|
2016-04-20 21:14:04 +02:00
|
|
|
def get_id(self):
|
2016-05-01 20:04:40 +02:00
|
|
|
return self.email
|
2016-04-20 21:14:04 +02:00
|
|
|
|
2016-03-20 16:36:56 +02:00
|
|
|
pw_context = context.CryptContext(
|
|
|
|
["sha512_crypt", "sha256_crypt", "md5_crypt"]
|
|
|
|
)
|
2016-03-19 21:37:48 +02:00
|
|
|
|
|
|
|
def check_password(self, password):
|
2016-03-20 16:36:56 +02:00
|
|
|
reference = re.match('({[^}]+})?(.*)', self.password).group(2)
|
|
|
|
return User.pw_context.verify(password, reference)
|
2016-03-19 21:37:48 +02:00
|
|
|
|
|
|
|
def set_password(self, password):
|
2016-03-20 16:36:56 +02:00
|
|
|
self.password = '{SHA512-CRYPT}' + User.pw_context.encrypt(password)
|
2016-03-19 21:37:48 +02:00
|
|
|
|
|
|
|
def get_managed_domains(self):
|
|
|
|
if self.global_admin:
|
|
|
|
return Domain.query.all()
|
|
|
|
else:
|
2016-04-24 19:42:02 +02:00
|
|
|
return self.manager_of
|
2016-03-19 21:37:48 +02:00
|
|
|
|
2016-05-01 20:04:40 +02:00
|
|
|
def get_managed_emails(self):
|
|
|
|
emails = []
|
2016-03-22 21:34:21 +02:00
|
|
|
for domain in self.get_managed_domains():
|
2016-05-01 20:04:40 +02:00
|
|
|
emails.extend(domain.users)
|
|
|
|
emails.extend(domain.aliases)
|
|
|
|
return emails
|
2016-03-22 21:34:21 +02:00
|
|
|
|
2016-03-19 21:37:48 +02:00
|
|
|
@classmethod
|
|
|
|
def login(cls, email, password):
|
2016-05-01 20:09:47 +02:00
|
|
|
user = cls.query.get(email)
|
2016-03-19 21:37:48 +02:00
|
|
|
return user if (user and user.check_password(password)) else None
|
|
|
|
|
|
|
|
|
2016-05-01 20:04:40 +02:00
|
|
|
class Alias(Email):
|
|
|
|
""" An alias is an email address that redirects to some destination.
|
2016-03-19 21:37:48 +02:00
|
|
|
"""
|
|
|
|
domain = db.relationship(Domain, backref='aliases')
|
|
|
|
destination = db.Column(db.String(), nullable=False)
|
2016-04-28 20:07:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Fetch(Base):
|
|
|
|
""" A fetched account is a repote POP/IMAP account fetched into a local
|
|
|
|
account.
|
|
|
|
"""
|
|
|
|
id = db.Column(db.Integer(), primary_key=True)
|
2016-05-01 20:04:40 +02:00
|
|
|
user_email = db.Column(db.String(255), db.ForeignKey(User.email),
|
2016-04-28 20:07:38 +02:00
|
|
|
nullable=False)
|
|
|
|
user = db.relationship(User, backref='fetches')
|
|
|
|
protocol = db.Column(db.Enum('imap', 'pop3'), nullable=False)
|
|
|
|
host = db.Column(db.String(255), nullable=False)
|
|
|
|
port = db.Column(db.Integer(), nullable=False)
|
|
|
|
tls = db.Column(db.Boolean(), nullable=False)
|
|
|
|
username = db.Column(db.String(255), nullable=False)
|
|
|
|
password = db.Column(db.String(255), nullable=False)
|