mirror of
https://github.com/jwinterm/modoboa.git
synced 2026-10-20 06:08:35 +02:00
Fixed pep8 issues.
This commit is contained in:
@@ -3,19 +3,15 @@
|
||||
import os
|
||||
import shutil
|
||||
import tempfile
|
||||
from unittest import mock
|
||||
|
||||
from django.core.management import call_command
|
||||
from django.test import override_settings
|
||||
from django.urls import reverse
|
||||
from django.db import transaction
|
||||
|
||||
from modoboa.core.tests.test_views import SETTINGS_SAMPLE
|
||||
|
||||
from modoboa.lib.tests import ModoTestCase
|
||||
from .. import factories, models
|
||||
from .. import factories
|
||||
|
||||
from modoboa.core import models as core_models
|
||||
|
||||
class NeedDovecotUpdateTestCase(ModoTestCase):
|
||||
"""Test need dovecot ldap update command."""
|
||||
@@ -49,7 +45,7 @@ class NeedDovecotUpdateTestCase(ModoTestCase):
|
||||
self.localconfig.need_dovecot_update = True
|
||||
self.localconfig.save()
|
||||
self.assertTrue(self.localconfig.need_dovecot_update)
|
||||
|
||||
|
||||
tmp_file = os.path.join(self.workdir, "test-dovecot-ldap.conf")
|
||||
self.set_global_parameters({
|
||||
"authentication_type": "ldap",
|
||||
@@ -79,6 +75,6 @@ class NeedDovecotUpdateTestCase(ModoTestCase):
|
||||
self.assertIn("base = CN=Users,DC=test,DC=lan", content)
|
||||
self.assertIn("user_filter = (& (objectClass=user) (|(mail=%u)(sAMAccountName=%u)) )", content)
|
||||
self.assertIn("pass_filter = (& (objectClass=user) (|(mail=%u)(sAMAccountName=%u)) )", content)
|
||||
|
||||
|
||||
self.localconfig.refresh_from_db()
|
||||
self.assertFalse(self.localconfig.need_dovecot_update)
|
||||
|
||||
@@ -18,6 +18,7 @@ from modoboa.parameters import forms as param_forms, tools as param_tools
|
||||
from . import constants
|
||||
from . import sms_backends
|
||||
|
||||
|
||||
def enabled_applications():
|
||||
"""Return the list of installed extensions."""
|
||||
from modoboa.core.extensions import exts_pool
|
||||
@@ -372,7 +373,8 @@ class GeneralParametersForm(param_forms.AdminParametersForm):
|
||||
label=ugettext_lazy("Enable Dovecot LDAP sync"),
|
||||
initial=False,
|
||||
help_text=ugettext_lazy(
|
||||
"Enable synchronisation of Dovecot LDAP parameters from Modoboa."
|
||||
"LDAP authentication settings will be applied to Dovecot "
|
||||
"configuration."
|
||||
)
|
||||
)
|
||||
|
||||
@@ -381,7 +383,8 @@ class GeneralParametersForm(param_forms.AdminParametersForm):
|
||||
initial="/etc/dovecot/dovecot-modoboa.conf",
|
||||
required=False,
|
||||
help_text=ugettext_lazy(
|
||||
"Dovecot LDAP configuration file location"
|
||||
"Location of the configuration file which contains "
|
||||
"Dovecot LDAP settings."
|
||||
)
|
||||
)
|
||||
|
||||
@@ -700,6 +703,8 @@ class GeneralParametersForm(param_forms.AdminParametersForm):
|
||||
from modoboa.lib.authbackends import LDAPSecondaryBackend
|
||||
self._apply_ldap_settings(values, LDAPSecondaryBackend)
|
||||
|
||||
def to_dovecot_settings(self):
|
||||
def save(self):
|
||||
"""Extra save actions."""
|
||||
super().save()
|
||||
self.localconfig.need_dovecot_update = True
|
||||
self.localconfig.save()
|
||||
self.localconfig.save(update_fields=["need_dovecot_update"])
|
||||
|
||||
@@ -35,7 +35,6 @@ def parameters(request, tplname="core/parameters.html"):
|
||||
if form.is_valid():
|
||||
form.save()
|
||||
form.to_django_settings()
|
||||
form.to_dovecot_settings()
|
||||
continue
|
||||
return render_to_json_response(
|
||||
{"form_errors": form.errors, "prefix": form.app}, status=400
|
||||
|
||||
+10
-8
@@ -241,33 +241,34 @@ def import_accounts_from_ldap(config):
|
||||
|
||||
# FIXME: handle delete and rename operations?
|
||||
|
||||
|
||||
def build_ldap_uri(config, node=""):
|
||||
""" Building LDAP uris for dovecot conf """
|
||||
if node:
|
||||
node += "_"
|
||||
return "{}://{}:{}".format(
|
||||
"ldaps" if config["ldap_secured"]=="ssl" else "ldap",
|
||||
"ldaps" if config["ldap_secured"] == "ssl" else "ldap",
|
||||
config["ldap_{}server_address".format(node)],
|
||||
config["ldap_{}server_port".format(node)]
|
||||
)
|
||||
|
||||
|
||||
def update_dovecot_config_file(config):
|
||||
""" Update dovecot configuration file from LDAP parameters """
|
||||
|
||||
"""Update dovecot configuration file from LDAP parameters."""
|
||||
conf_file = config["ldap_dovecot_conf_file"]
|
||||
|
||||
|
||||
# Hosts conf
|
||||
uris = build_ldap_uri(config)
|
||||
if config["ldap_enable_secondary_server"]:
|
||||
uris += " " + build_ldap_uri(config, "secondary")
|
||||
|
||||
|
||||
# Auth conf
|
||||
bind_dn = config["ldap_bind_dn"]
|
||||
bind_pwd = config["ldap_bind_password"]
|
||||
|
||||
|
||||
# Search conf
|
||||
base = config["ldap_search_base"]
|
||||
user_filter = config["ldap_search_filter"].replace("(user)s","u")
|
||||
user_filter = config["ldap_search_filter"].replace("(user)s", "u")
|
||||
|
||||
with open(conf_file, "w") as fp:
|
||||
fp.write("""uris = {uris}
|
||||
@@ -276,5 +277,6 @@ dnpass = '{bind_pwd}'
|
||||
base = {base}
|
||||
user_filter = {user_filter}
|
||||
pass_filter = {pass_filter}
|
||||
""".format(uris=uris, bind_dn=bind_dn, bind_pwd=bind_pwd, base=base, user_filter=user_filter, pass_filter=user_filter)
|
||||
""".format(uris=uris, bind_dn=bind_dn, bind_pwd=bind_pwd, base=base,
|
||||
user_filter=user_filter, pass_filter=user_filter)
|
||||
)
|
||||
|
||||
@@ -1,25 +1,29 @@
|
||||
"""Command to populate local database from a remote LDAP directory."""
|
||||
"""Command to overwrite dovecot LDAP configuration (auth)."""
|
||||
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.conf import settings
|
||||
|
||||
from modoboa.parameters import tools as param_tools
|
||||
|
||||
from ... import lib
|
||||
from modoboa.core import models
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
"""Command definition."""
|
||||
|
||||
help = "Update dovecot configuration file from modoboa LDAP parameters"
|
||||
help = "Update dovecot configuration file to enable LDAP auth"
|
||||
|
||||
def handle(self, *args, **options):
|
||||
"""Command entry point."""
|
||||
localconfig = models.LocalConfig.objects.first()
|
||||
|
||||
if localconfig.need_dovecot_update:
|
||||
config = dict(param_tools.get_global_parameters("core"))
|
||||
if config["authentication_type"] == "ldap" and config["ldap_dovecot_sync"]:
|
||||
lib.update_dovecot_config_file(config)
|
||||
localconfig.need_dovecot_update = False
|
||||
localconfig.save()
|
||||
if not localconfig.need_dovecot_update:
|
||||
return
|
||||
config = dict(param_tools.get_global_parameters("core"))
|
||||
condition = (
|
||||
config["authentication_type"] == "ldap" and
|
||||
config["ldap_dovecot_sync"]
|
||||
)
|
||||
if condition:
|
||||
lib.update_dovecot_config_file(config)
|
||||
localconfig.need_dovecot_update = False
|
||||
localconfig.save(update_fields=["need_dovecot_update"])
|
||||
|
||||
@@ -87,9 +87,6 @@ class AdminParametersForm(GenericParametersForm):
|
||||
"""Inject parameters into django settings module."""
|
||||
pass
|
||||
|
||||
def to_dovecot_settings(self):
|
||||
pass
|
||||
|
||||
|
||||
class UserParametersForm(GenericParametersForm):
|
||||
"""Base form to declare user level parameters."""
|
||||
|
||||
Reference in New Issue
Block a user