Skip to content

Commit

Permalink
支持用户注册
Browse files Browse the repository at this point in the history
  • Loading branch information
mouday committed Sep 21, 2024
1 parent 2101ae9 commit 1656abf
Show file tree
Hide file tree
Showing 11 changed files with 328 additions and 15 deletions.
18 changes: 9 additions & 9 deletions domain_admin/api/auth_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,25 @@ def login():
return {'token': token}


def register():
def login_by_email():
"""
用户注册
邮箱登录
:return:
"""
raise AppException('暂未开放')

username = request.json['username']
password = request.json['password']
password_repeat = request.json['password_repeat']
email = request.json['email']
code = request.json['code']

auth_service.register(username, password, password_repeat)
token = auth_service.login_by_email(email, code)

return {'token': token}


def send_code():
"""
发送验证码
:return:
"""
username = request.json['username']
email = request.json['email']

# auth_service.register()
auth_service.send_verify_code_async(email)
2 changes: 1 addition & 1 deletion domain_admin/config/env_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@
ALLOW_COMMANDS = [cmd.strip() for cmd in env.str("ALLOW_COMMANDS", '').split(';') if cmd.strip()]

# ENABLED_REGISTER
ENABLED_REGISTER = env.str("ENABLED_REGISTER", 'false')
ENABLED_REGISTER = env.bool("ENABLED_REGISTER", False)
3 changes: 3 additions & 0 deletions domain_admin/config/runtime_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
# acme_dir
ACME_DIR = os.path.join(DATABASE_DIR, 'acme')

# cache_dir
CACHE_DIR = os.path.join(DATABASE_DIR, 'cache')

# sqlite 数据库
SQLITE_DATABASE_PATH = os.path.join(DATABASE_DIR, 'database.db')

Expand Down
2 changes: 1 addition & 1 deletion domain_admin/router/api_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

# 登录注册
"/api/login": auth_api.login,
"/api/register": auth_api.register,
"/api/loginByEmail": auth_api.login_by_email,
"/api/sendCode": auth_api.send_code,

# 域名(SSL证书)
Expand Down
5 changes: 4 additions & 1 deletion domain_admin/service/async_task_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ def async_task_decorator(task_name):
def outer_wrapper(func):
@wraps(func)
def wrapper(*args, **kwargs):
current_user_id = g.user_id
if hasattr(g, 'user_id'):
current_user_id = g.user_id
else:
current_user_id = 0

# before
async_task_row = AsyncTaskModel.create(
Expand Down
51 changes: 49 additions & 2 deletions domain_admin/service/auth_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
from domain_admin.enums.role_enum import RoleEnum, ROLE_PERMISSION
from domain_admin.enums.status_enum import StatusEnum
from domain_admin.model.user_model import UserModel
from domain_admin.service import token_service
from domain_admin.utils import bcrypt_util
from domain_admin.service import token_service, notify_service, async_task_service
from domain_admin.utils import bcrypt_util, cache_util, validate_util, secret_util
from domain_admin.utils.flask_ext.app_exception import AppException


Expand Down Expand Up @@ -124,3 +124,50 @@ def has_role_permission(current_role, need_permission):
current_permission = item['permission']

return need_permission in current_permission


@async_task_service.async_task_decorator("发送邮箱验证码")
def send_verify_code_async(email):
send_verify_code(email)


def send_verify_code(email):
if not validate_util.is_email(email):
raise AppException('邮箱格式不正确')

code = secret_util.get_random_password()

cache_util.set_value(key=email, value=code, expire=5 * 60)

notify_service.send_email_to_user(
template='code-email.html',
subject='验证码',
data={'code': code},
email_list=[email]
)


def login_by_email(email, code):
if not validate_util.is_email(email):
raise AppException('邮箱格式不正确')

cache_code = cache_util.get_value(email)

if cache_code and code == cache_code:
pass
else:
raise AppException('验证码不正确')

user_row = UserModel.select().where(
UserModel.username == email
).get_or_none()

if not user_row:
user_row = UserModel.create(
username=email,
password=''
)

return token_service.encode_token({
'user_id': user_row.id
})
22 changes: 22 additions & 0 deletions domain_admin/service/notify_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,28 @@ def notify_user_by_email(
subject,
data,
email_list,
):
"""
通过邮件通知用户证书到期
:param template: str
:param subject: str
:param data: Dict
:param email_list: List[str]
:return:
"""
send_email_to_user(
template,
subject,
data,
email_list
)


def send_email_to_user(
template,
subject,
data,
email_list,
):
"""
通过邮件通知用户证书到期
Expand Down
210 changes: 210 additions & 0 deletions domain_admin/templates/code-email.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
{# 查询域名证书到期情况 #}
<html lang="zh">
<head>
<meta charset=utf-8>
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>

<body>
<div>
<div
style="
box-sizing: border-box;
text-align: center;
min-width: 320px;
max-width: 800px;
border: 1px solid #f6f6f6;
background-color: #f6f6f6;
margin: auto;
padding: 20px 0 30px;
font-family: 'helvetica neue', PingFangSC-Light, arial,
'hiragino sans gb', 'microsoft yahei ui', 'microsoft yahei', simsun,
sans-serif;
"
>
<div>
<table
style="
width: 100%;
font-weight: 300;
margin-bottom: 10px;
border-collapse: collapse;
"
>
<tbody>
<tr style="font-weight: 300">
<td style="width: 3%; max-width: 30px"></td>
<td style="max-width: 600px">
<div
style="
height: 25px;
text-align: left;
margin-bottom: 10px;
margin-top: 10px;
"
>
<a
href="https://github.com/mouday/domain-admin"
rel="noopener"
target="_blank"
style="text-decoration: none; color: black"
><div style="display: flex">
<span
style="
font-size: 30px;
margin-left: 3px;
font-family: sans-serif;
height: 24px;
line-height: 24px;
"
>Domain Admin</span
>
</div></a
>
</div>
<p
style="
height: 2px;
background-color: #006EFF;
border: 0;
font-size: 0;
padding: 0;
width: 100%;
margin-top: 10px;
"
></p>
<div
style="
background-color: #fff;
padding: 23px 0 20px;
box-shadow: 0px 1px 1px 0px rgba(122, 55, 55, 0.2);
text-align: left;
"
>
<table
style="
width: 100%;
font-weight: 300;
margin-bottom: 10px;
border-collapse: collapse;
text-align: left;
"
>
<tbody>
<tr style="font-weight: 300">
<td style="width: 3.2%; max-width: 30px"><br /></td>
<td style="max-width: 480px; text-align: left">
<div
style="
font-size: 14px;
color: #333;
line-height: 24px;
margin: 6px 0 0 0;
word-wrap: break-word;
word-break: break-all;
"
>

<div style="font-size: 12px;">您的验证码为:</div>

<div style="font-size: 28px;line-height: 3;">{{ code }}</div>

<div style="margin-top: 20px; font-size: 12px;">欢迎注册Domain Admin。</div>
</div>
<br />
<dl
style="
font-size: 14px;
color: #333;
line-height: 18px;
"
></dl>
<p
style="
color: #333;
font-size: 14px;
line-height: 26px;
word-wrap: break-word;
word-break: break-all;
margin-top: 32px;
"
>
此致<br /><strong>Domain Admin团队</strong>
</p>
</td>
<td style="width: 3.2%; max-width: 30px"></td>
</tr>
</tbody>
</table>
</div>
<div
style="
text-align: center;
font-size: 12px;
line-height: 18px;
color: #999;
"
>
<table
style="
width: 100%;
font-weight: 300;
margin-bottom: 10px;
border-collapse: collapse;
"
>
<tbody>
<tr style="font-weight: 300">
<td style="width: 3.2%; max-width: 30px"></td>
<td style="max-width: 540px">
<p
style="
text-align: center;
margin: 20px auto 14px auto;
font-size: 12px;
color: #999;
font-family: sans-serif;
"
>
此为系统邮件,请勿回复。
</p>
<p
style="
text-align: center;
margin: 20px auto 5px auto;
font-size: 12px;
color: #999;
font-family: sans-serif;
"
>
Copyright © 2023 Domain Admin
</p>
<p
style="
text-align: center;
margin: 5px auto 0px auto;
font-size: 12px;
color: #999;
font-family: sans-serif;
"
>
All Rights Reserved Domain Admin 版权所有
</p>
</td>
<td style="width: 3.2%; max-width: 30px"></td>
</tr>
</tbody>
</table>
</div>
</td>
<td style="width: 3%; max-width: 30px"></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>

</body>
</html>
24 changes: 24 additions & 0 deletions domain_admin/utils/cache_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# -*- coding: utf-8 -*-
"""
cache_util.py
"""
from diskcache import Cache

from domain_admin.config import CACHE_DIR

# 指定文件夹
cache = Cache(CACHE_DIR)


def set_value(key, value, expire=None):
"""
存,单位:秒s
"""
cache.set(key=key, value=value, expire=expire)


def get_value(key):
"""
"""
return cache.get(key=key)
Loading

0 comments on commit 1656abf

Please sign in to comment.