From ee7e98235acbc8f7822d1472b16059dd54defd72 Mon Sep 17 00:00:00 2001 From: sm4640 Date: Mon, 12 Jan 2026 23:13:22 +0900 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Feat:=20[#103]=20mail=20=EC=84=9C?= =?UTF-8?q?=EB=B2=84=20=EA=B3=84=EC=A0=95=20=EA=B8=B0=EB=8A=A5=20=EC=99=84?= =?UTF-8?q?=EB=A3=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/settings.py | 3 + config/urls.py | 5 ++ mail/__init__.py | 0 mail/admin.py | 3 + mail/apps.py | 6 ++ mail/models.py | 3 + mail/templates/mail/signup.html | 114 ++++++++++++++++++++++++++++++++ mail/tests.py | 3 + mail/urls.py | 8 +++ mail/views.py | 112 +++++++++++++++++++++++++++++++ 10 files changed, 257 insertions(+) create mode 100644 mail/__init__.py create mode 100644 mail/admin.py create mode 100644 mail/apps.py create mode 100644 mail/models.py create mode 100644 mail/templates/mail/signup.html create mode 100644 mail/tests.py create mode 100644 mail/urls.py create mode 100644 mail/views.py diff --git a/config/settings.py b/config/settings.py index f9d0b64..5d5dd28 100644 --- a/config/settings.py +++ b/config/settings.py @@ -36,6 +36,8 @@ SOLAPI_API_KEY = env('SOLAPI_API_KEY') SOLAPI_API_SECRET = env('SOLAPI_API_SECRET') FROM_PHONE_NUMBER = env('FROM_PHONE_NUMBER') +MAIL_SIGNUP_ENABLED = os.getenv("MAIL_SIGNUP_ENABLED", "0") == "1" + DEBUG = env.bool('DEBUG') ALLOWED_HOSTS = ['*'] @@ -67,6 +69,7 @@ INSTALLED_APPS = [ 'notifications', 'nocodetools', 'homes', + 'mail' ] MIDDLEWARE = [ diff --git a/config/urls.py b/config/urls.py index 4120ef7..bd6c865 100644 --- a/config/urls.py +++ b/config/urls.py @@ -15,5 +15,10 @@ urlpatterns = [ path('api/home/', include('homes.urls')), ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) +if getattr(settings, "MAIL_SIGNUP_ENABLED", False): + urlpatterns += [ + path('api/mail/', include('mail.urls')), + ] + # if settings.DEBUG: # urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) diff --git a/mail/__init__.py b/mail/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/mail/admin.py b/mail/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/mail/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/mail/apps.py b/mail/apps.py new file mode 100644 index 0000000..e59009d --- /dev/null +++ b/mail/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class MailConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'mail' diff --git a/mail/models.py b/mail/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/mail/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/mail/templates/mail/signup.html b/mail/templates/mail/signup.html new file mode 100644 index 0000000..58ad114 --- /dev/null +++ b/mail/templates/mail/signup.html @@ -0,0 +1,114 @@ + + + + + + + 메일 계정 신청 + + + + +

메일 계정 신청

+
+

HTTPS에서만 사용하세요. 생성 즉시 반영됩니다.

+ + + + + + + + + + + + +
+
+ + + + + \ No newline at end of file diff --git a/mail/tests.py b/mail/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/mail/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/mail/urls.py b/mail/urls.py new file mode 100644 index 0000000..fbfbf20 --- /dev/null +++ b/mail/urls.py @@ -0,0 +1,8 @@ +from django.urls import path +from .views import signup_page, request_account, admin_list + +urlpatterns = [ + path("signup/", signup_page, name="mail-signup-page"), + path("request/", request_account, name="mail-signup-request"), + path("list/", admin_list, name="mail-signup-list"), +] diff --git a/mail/views.py b/mail/views.py new file mode 100644 index 0000000..d1f9f01 --- /dev/null +++ b/mail/views.py @@ -0,0 +1,112 @@ +import os +import re +import subprocess + +from django.conf import settings +from django.shortcuts import render +from rest_framework.decorators import api_view, permission_classes +from rest_framework.permissions import AllowAny, IsAdminUser +from rest_framework.response import Response +from rest_framework import status +from rest_framework.exceptions import NotFound + +LOCAL_RE = re.compile(r"^[a-z0-9](?:[a-z0-9._-]{0,62}[a-z0-9])?$") + + +def _require_enabled(): + # settings.py에 MAIL_SIGNUP_ENABLED 토글이 있으면 사용 + if not getattr(settings, "MAIL_SIGNUP_ENABLED", False): + raise NotFound("Not Found") # 숨김(404) + + +def _run_setup(cmd_args): + """ + ./setup.sh -c dms-mailserver email add|list ... + """ + setup_sh = getattr(settings, "MAIL_SETUP_SH", None) or os.getenv("MAIL_SETUP_SH", "./setup.sh") + workdir = getattr(settings, "MAIL_SETUP_WORKDIR", None) or os.getenv("MAIL_SETUP_WORKDIR", os.getcwd()) + container = getattr(settings, "MAIL_DMS_CONTAINER", None) or os.getenv("MAIL_DMS_CONTAINER", "dms-mailserver") + + if not os.path.exists(setup_sh): + raise RuntimeError(f"setup.sh not found: {setup_sh}") + + full = [setup_sh, "-c", container] + cmd_args + p = subprocess.run(full, cwd=workdir, capture_output=True, text=True) + out = (p.stdout or "").strip() + err = (p.stderr or "").strip() + + if p.returncode != 0: + raise ValueError(err or out or "setup.sh failed") + + return out + + +def _email_list(): + out = _run_setup(["email", "list"]) + return [ln.strip() for ln in out.splitlines() if ln.strip()] + + +def signup_page(request): + _require_enabled() + return render(request, "mail/signup.html") + + +@api_view(["POST"]) +@permission_classes([AllowAny]) +def request_account(request): + _require_enabled() + + invite_expected = getattr(settings, "MAIL_INVITE_CODE", None) or os.getenv("MAIL_INVITE_CODE", "") + dms_domain = getattr(settings, "MAIL_DMS_DOMAIN", None) or os.getenv("MAIL_DMS_DOMAIN", "") + + local = (request.data.get("local") or "").strip().lower() + password = request.data.get("password") or "" + invite = (request.data.get("invite") or "").strip() + + if not invite_expected or invite != invite_expected: + return Response({"detail": "초대코드가 올바르지 않습니다."}, status=status.HTTP_403_FORBIDDEN) + + if not dms_domain: + return Response({"detail": "서버 설정(MAIL_DMS_DOMAIN)이 비어있습니다."}, + status=status.HTTP_500_INTERNAL_SERVER_ERROR) + + if not LOCAL_RE.match(local): + return Response({"detail": "아이디 형식이 올바르지 않습니다. (영문소문자/숫자/._- 허용)"}, + status=status.HTTP_400_BAD_REQUEST) + + if len(password) < 10: + return Response({"detail": "비밀번호는 10자 이상으로 설정하세요."}, + status=status.HTTP_400_BAD_REQUEST) + + address = f"{local}@{dms_domain}" + + try: + emails = _email_list() + if address in emails: + return Response({"message": f"ℹ️ {address} 는 이미 존재합니다. (email list에서 확인됨)"}) + + _run_setup(["email", "add", address, password]) + + emails2 = _email_list() + if address in emails2: + return Response({"message": f"✅ {address} 계정이 잘 만들어졌습니다! (email list에서 확인됨)"}) + + return Response({"detail": "계정 생성 후 list에서 확인이 안 됩니다."}, + status=status.HTTP_500_INTERNAL_SERVER_ERROR) + + except ValueError as e: + return Response({"detail": str(e)}, status=status.HTTP_400_BAD_REQUEST) + except Exception as e: + return Response({"detail": f"서버 오류: {e}"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) + + +@api_view(["GET"]) +@permission_classes([IsAdminUser]) +def admin_list(request): + _require_enabled() + + try: + emails = _email_list() + return Response({"count": len(emails), "emails": emails}) + except Exception as e: + return Response({"detail": f"서버 오류: {e}"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)