mirror of
https://github.com/jwinterm/modoboa.git
synced 2026-10-17 05:41:25 +02:00
Use plistlib to generate mobileconfig
This commit is contained in:
@@ -9,7 +9,7 @@
|
||||
<hostname>{{ connection_settings.imap.HOSTNAME }}</hostname>
|
||||
<port>{{ connection_settings.imap.PORT }}</port>
|
||||
<socketType>{{ connection_settings.imap.SOCKET_TYPE }}</socketType>
|
||||
<authentication>password-cleartext</authentication>
|
||||
<authentication>plain</authentication>
|
||||
<username>{{ emailaddress }}</username>
|
||||
</incomingServer>
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
<hostname>{{ connection_settings.smtp.HOSTNAME }}</hostname>
|
||||
<port>{{ connection_settings.smtp.PORT }}</port>
|
||||
<socketType>{{ connection_settings.smtp.SOCKET_TYPE }}</socketType>
|
||||
<authentication>password-cleartext</authentication>
|
||||
<authentication>plain</authentication>
|
||||
<username>{{ emailaddress }}</username>
|
||||
</outgoingServer>
|
||||
</emailProvider>
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
|
||||
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<payloadContent>
|
||||
<dict>
|
||||
<PayloadType>com.apple.mail.managed</PayloadType>
|
||||
<PayloadVersion>1</PayloadVersion>
|
||||
<PayloadIdentifier>{{ reverse_domain }}.email</PayloadIdentifier>
|
||||
<PayloadUUID>{{ content_uuid }}</PayloadUUID>
|
||||
<PayloadDisplayName>Mail</PayloadDisplayName>
|
||||
|
||||
<EmailAccountDescription>Modoboa</EmailAccountDescription>
|
||||
<EmailAccountType>EmailTypeIMAP</EmailAccountType>
|
||||
<EmailAddress>{{ emailaddress }}</EmailAddress>
|
||||
<IncomingMailServerHostName>{{ connection_settings.imap.HOSTNAME }}</IncomingMailServerHostName>
|
||||
<IncomingMailServerPortNumber>{{ connection_settings.imap.PORT }}</IncomingMailServerPortNumber>
|
||||
<IncomingMailServerUseSSL>true</IncomingMailServerUseSSL>
|
||||
<IncomingMailServerUsername>{{ emailaddress }}</IncomingMailServerUsername>
|
||||
|
||||
<OutgoingMailServerHostName>{{ connection_settings.smtp.HOSTNAME }}</OutgoingMailServerHostName>
|
||||
<OutgoingMailServerPortNumber>{{ connection_settings.smtp.PORT }}</OutgoingMailServerPortNumber>
|
||||
<OutgoingMailServerUseSSL>true</OutgoingMailServerUseSSL>
|
||||
<OutgoingMailServerUsername>{{ emailaddress }}</OutgoingMailServerUsername>
|
||||
<OutgoingPasswordSameAsIncomingPassword>true</OutgoingPasswordSameAsIncomingPassword>
|
||||
|
||||
</dict>
|
||||
</payloadContent>
|
||||
<PayloadOrganization>Modoboa</PayloadOrganization>
|
||||
<PayloadDisplayName>Mail Configuration</PayloadDisplayName>
|
||||
<PayloadIdentifier>{{ reverse_domain }}.mailconfig</PayloadIdentifier>
|
||||
<PayloadUUID>{{ global_uuid }}</PayloadUUID>
|
||||
<PayloadVersion>1</PayloadVersion>
|
||||
<PayloadType>Configuration</PayloadType>
|
||||
</dict>
|
||||
</plist>
|
||||
@@ -32,7 +32,7 @@ class ViewsTestCase(TestCase):
|
||||
resp = self.client.get(url)
|
||||
self.assertEqual(resp.status_code, 404)
|
||||
|
||||
resp = self.client.get(f"{url}?email=test@test.com")
|
||||
resp = self.client.get(f"{url}?emailaddress=test@test.com")
|
||||
self.assertEqual(resp.status_code, 200)
|
||||
|
||||
self.assertIn(b"<EmailAddress>test@test.com", resp.content)
|
||||
|
||||
+46
-21
@@ -1,8 +1,8 @@
|
||||
import plistlib
|
||||
import uuid
|
||||
|
||||
from django.conf import settings
|
||||
from django.http import Http404, HttpResponse
|
||||
from django.template.loader import render_to_string
|
||||
from django.utils.decorators import method_decorator
|
||||
from django.views import generic
|
||||
from django.views.decorators.csrf import csrf_exempt
|
||||
@@ -56,31 +56,56 @@ class AutoDiscoverView(ConfigBaseMixin, generic.TemplateView):
|
||||
return self.render_to_response(context)
|
||||
|
||||
|
||||
class MobileConfigView(ConfigBaseMixin, generic.TemplateView):
|
||||
class MobileConfigView(generic.View):
|
||||
|
||||
content_type = "application/x-apple-aspen-config"
|
||||
databases = "__all__"
|
||||
http_method_names = ["get"]
|
||||
template_name = "autoconfig/mobileconfig.xml"
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
emailaddress = self.request.GET.get("email")
|
||||
def get(self, request, *args, **kwargs):
|
||||
emailaddress = self.request.GET.get("emailaddress")
|
||||
if not emailaddress:
|
||||
raise Http404
|
||||
context = super().get_context_data(**kwargs)
|
||||
context.update(self.get_common_context(emailaddress))
|
||||
local_part, domain = split_address(emailaddress)
|
||||
context = self.get_common_context(emailaddress)
|
||||
parts = context["domain"].split(".")
|
||||
parts.reverse()
|
||||
context.update(
|
||||
{
|
||||
"reverse_domain": ".".join(parts),
|
||||
"content_uuid": uuid.uuid4(),
|
||||
"global_uuid": uuid.uuid4(),
|
||||
}
|
||||
reverse_domain = ".".join(parts)
|
||||
imap_settings = settings.EMAIL_CLIENT_CONNECTION_SETTINGS["imap"]
|
||||
smtp_settings = settings.EMAIL_CLIENT_CONNECTION_SETTINGS["smtp"]
|
||||
profile = {
|
||||
"PayloadType": "Configuration",
|
||||
"PayloadVersion": 1,
|
||||
"PayloadIdentifier": f"{reverse_domain}.mailprofile",
|
||||
"PayloadUUID": uuid.uuid4(),
|
||||
"PayloadDisplayName": f"{domain} Mail Configuration",
|
||||
"PayloadOrganization": domain,
|
||||
"PayloadContent": [
|
||||
{
|
||||
"PayloadType": "com.apple.mail.managed",
|
||||
"PayloadVersion": 1,
|
||||
"PayloadIdentifier": f"{reverse_domain}.mailprofile.mail",
|
||||
"PayloadUUID": uuid.uuid4(),
|
||||
"PayloadDisplayName": "Mail",
|
||||
"EmailAccountDescription": f"{domain} Mail",
|
||||
"EmailAccountType": "EmailTypeIMAP",
|
||||
"EmailAddress": emailaddress,
|
||||
# incoming
|
||||
"IncomingMailServerHostName": imap_settings["HOSTNAME"],
|
||||
"IncomingMailServerPortNumber": imap_settings["PORT"],
|
||||
"IncomingMailServerUseSSL": True,
|
||||
"IncomingMailServerUsername": emailaddress,
|
||||
# outgoing
|
||||
"OutgoingMailServerHostName": smtp_settings["HOSTNAME"],
|
||||
"OutgoingMailServerPortNumber": smtp_settings["PORT"],
|
||||
"OutgoingMailServerUseSSL": True,
|
||||
"OutgoingMailServerUsername": emailaddress,
|
||||
"OutgoingPasswordSameAsIncomingPassword": True,
|
||||
}
|
||||
],
|
||||
}
|
||||
plist_bytes = plistlib.dumps(profile, fmt=plistlib.FMT_XML)
|
||||
resp = HttpResponse(
|
||||
plist_bytes, content_type="application/x-apple-aspen-config"
|
||||
)
|
||||
return context
|
||||
|
||||
def render_to_response(self, context, **responsse_kwargs):
|
||||
content = render_to_string(self.template_name, context)
|
||||
response = HttpResponse(content, content_type=self.content_type)
|
||||
response["Content-Disposition"] = 'attachment; filename="mail.mobileconfig"'
|
||||
return response
|
||||
resp["Content-Disposition"] = 'attachment; filename="mail.mobileconfig"'
|
||||
return resp
|
||||
|
||||
Reference in New Issue
Block a user