Feat: [#103] mail 서버 계정 기능 완료

This commit is contained in:
sm4640
2026-01-12 23:13:22 +09:00
parent d9ac5578c9
commit ee7e98235a
10 changed files with 257 additions and 0 deletions

0
mail/__init__.py Normal file
View File

3
mail/admin.py Normal file
View File

@@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

6
mail/apps.py Normal file
View File

@@ -0,0 +1,6 @@
from django.apps import AppConfig
class MailConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'mail'

3
mail/models.py Normal file
View File

@@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

View File

@@ -0,0 +1,114 @@
<!doctype html>
<html lang="ko">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<title>메일 계정 신청</title>
<style>
body {
font-family: system-ui, sans-serif;
max-width: 720px;
margin: 40px auto;
padding: 0 16px;
}
.card {
border: 1px solid #ddd;
border-radius: 12px;
padding: 18px;
}
label {
display: block;
margin-top: 12px;
font-weight: 600;
}
input {
width: 100%;
padding: 10px;
margin-top: 6px;
border: 1px solid #ccc;
border-radius: 8px;
}
button {
margin-top: 16px;
padding: 10px 14px;
border: 0;
border-radius: 10px;
cursor: pointer;
}
.ok {
margin-top: 14px;
padding: 12px;
border-radius: 10px;
background: #eef;
}
.err {
margin-top: 14px;
padding: 12px;
border-radius: 10px;
background: #fee;
}
small {
color: #666;
}
</style>
</head>
<body>
<h1>메일 계정 신청</h1>
<div class="card">
<p><small>HTTPS에서만 사용하세요. 생성 즉시 반영됩니다.</small></p>
<label>아이디(메일주소 앞부분)</label>
<input id="local" placeholder="예: seungmin" autocomplete="username" />
<label>비밀번호</label>
<input id="password" type="password" placeholder="10자 이상 권장" autocomplete="new-password" />
<label>초대코드</label>
<input id="invite" placeholder="관리자에게 받은 코드" />
<button id="btn">제출</button>
<div id="msg"></div>
</div>
<script>
const msg = (type, text) => {
const el = document.getElementById('msg');
el.className = type;
el.textContent = text;
};
document.getElementById('btn').addEventListener('click', async () => {
const local = document.getElementById('local').value.trim();
const password = document.getElementById('password').value;
const invite = document.getElementById('invite').value.trim();
msg('', '');
try {
const res = await fetch('/api/mail/request/', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ local, password, invite })
});
const data = await res.json();
if (!res.ok) throw new Error(data.detail || '요청 실패');
msg('ok', data.message);
} catch (e) {
msg('err', e.message);
}
});
</script>
</body>
</html>

3
mail/tests.py Normal file
View File

@@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

8
mail/urls.py Normal file
View File

@@ -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"),
]

112
mail/views.py Normal file
View File

@@ -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)