def create(self, validated_data): """Create administrator and other stuff if needed.""" domain_admin = validated_data.pop("domain_admin", None) domain = super().create(validated_data) if not domain_admin: return domain # 1. Create a domain administrator username = "******" % (domain_admin["username"], domain.name) try: da = core_models.User.objects.get(username=username) except core_models.User.DoesNotExist: pass else: raise lib_exceptions.Conflict( _("User '%s' already exists") % username) user = self.context["request"].user core_signals.can_create_object.send(self.__class__, context=user, klass=models.Mailbox) da = core_models.User(username=username, email=username, is_active=True) password = domain_admin.get("password") if password is None: password = param_tools.get_global_parameter("default_password", app="core") da.set_password(password) da.save() da.role = "DomainAdmins" da.post_create(user) # 2. Create mailbox if needed if domain_admin["with_mailbox"]: dom_admin_username = domain_admin["username"] mb = models.Mailbox(address=dom_admin_username, domain=domain, user=da, use_domain_quota=True) mb.set_quota(override_rules=user.has_perm("admin.change_domain")) mb.save(creator=user) # 3. Create aliases if needed condition = (domain.type == "domain" and domain_admin["with_aliases"] and dom_admin_username != "postmaster") if condition: core_signals.can_create_object.send(self.__class__, context=user, klass=models.Alias) address = u"postmaster@{}".format(domain.name) alias = models.Alias.objects.create(address=address, domain=domain, enabled=True) alias.set_recipients([mb.full_address]) alias.post_create(user) domain.add_admin(da) return domain
def import_account_mailbox(sender, user, account, row, **kwargs): """Handle extra fields when an account is imported. Expected fields: email address; quota; [domain; ...] :param User user: user importing the account :param User account: account being imported :param list rom: list of fields (strings) """ account.email = row[0].strip().lower() if account.email: mailbox, domname = split_mailbox(account.email) domain = models.Domain.objects.filter(name=domname).first() if not domain: raise exceptions.BadRequest( _("Account import failed (%s): domain does not exist") % account.username) if not user.can_access(domain): raise exceptions.PermDeniedException core_signals.can_create_object.send(sender="import", context=user, klass=models.Mailbox) core_signals.can_create_object.send(sender="import", context=domain, object_type="mailboxes") account.save() qset = models.Mailbox.objects.filter(address=mailbox, domain=domain) if qset.exists(): raise exceptions.Conflict( _("Mailbox {} already exists").format(account.email)) if len(row) == 1: quota = None else: try: quota = int(row[1].strip()) except ValueError: raise exceptions.BadRequest( _("Account import failed (%s): wrong quota value") % account.username) use_domain_quota = True if not quota else False mb = models.Mailbox(address=mailbox, domain=domain, user=account, use_domain_quota=use_domain_quota) mb.set_quota(quota, override_rules=user.has_perm("admin.change_domain")) mb.save(creator=user) if account.role == "DomainAdmins": for domname in row[2:]: try: dom = models.Domain.objects.get(name=domname.strip()) except models.Domain.DoesNotExist: continue dom.add_admin(account)
def save(self, *args, **kwargs): """Custom save. We check that the address is unique and we make sure a quota record is defined for this mailbox. """ qset = Mailbox.objects.filter(address=self.address, domain=self.domain) if self.pk: qset = qset.exclude(pk=self.pk) if qset.exists(): raise lib_exceptions.Conflict( _("Mailbox {} already exists").format(self)) if self.quota_value is None: self.quota_value, created = Quota.objects.get_or_create( username=self.full_address) super(Mailbox, self).save(*args, **kwargs)