Skip to content

Commit 6a8fa35

Browse files
committed
Merge branch 'main' of https://github.com/dataease/SQLBot
2 parents 1acf86c + 469abaf commit 6a8fa35

File tree

5 files changed

+49
-25
lines changed

5 files changed

+49
-25
lines changed

backend/apps/chat/task/llm.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -536,8 +536,8 @@ def generate_filter(self, sql: str, tables: List):
536536
).first()
537537
if obj is not None:
538538
res.append(transRecord2DTO(self.session, permission))
539-
wheres = transFilterTree(self.session, res, self.ds)
540-
filters.append({"table": table.table_name, "filter": wheres})
539+
where_str = transFilterTree(self.session, res, self.ds)
540+
filters.append({"table": table.table_name, "filter": where_str})
541541

542542
filter = json.dumps(filters, ensure_ascii=False)
543543

backend/apps/datasource/crud/datasource.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,8 @@ def preview(session: SessionDep, current_user: CurrentUser, id: int, data: Table
278278
).first()
279279
if obj is not None:
280280
res.append(transRecord2DTO(session, permission))
281-
wheres = transFilterTree(session, res, ds)
282-
where = (' where ' + wheres) if wheres is not None and wheres != '' else ''
281+
where_str = transFilterTree(session, res, ds)
282+
where = (' where ' + where_str) if where_str is not None and where_str != '' else ''
283283

284284
fields = [f.field_name for f in f_list]
285285
if fields is None or len(fields) == 0:

backend/apps/db/db.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -254,20 +254,17 @@ def get_fields(ds: CoreDatasource, table_name: str = None):
254254

255255

256256
def exec_sql(ds: CoreDatasource | AssistantOutDsSchema, sql: str):
257-
session = get_session(ds)
258-
result = session.execute(text(sql))
259-
try:
260-
columns = result.keys()._keys
261-
res = result.fetchall()
262-
result_list = [
263-
{columns[i]: float(value) if isinstance(value, Decimal) else value for i, value in enumerate(tuple_item)}
264-
for tuple_item in res
265-
]
266-
return {"fields": columns, "data": result_list, "sql": bytes.decode(base64.b64encode(bytes(sql, 'utf-8')))}
267-
except Exception as ex:
268-
raise ex
269-
finally:
270-
if result is not None:
271-
result.close()
272-
if session is not None:
273-
session.close()
257+
with get_session(ds) as session:
258+
with session.execute(text(sql)) as result:
259+
try:
260+
columns = result.keys()._keys
261+
res = result.fetchall()
262+
result_list = [
263+
{columns[i]: float(value) if isinstance(value, Decimal) else value for i, value in
264+
enumerate(tuple_item)}
265+
for tuple_item in res
266+
]
267+
return {"fields": columns, "data": result_list,
268+
"sql": bytes.decode(base64.b64encode(bytes(sql, 'utf-8')))}
269+
except Exception as ex:
270+
raise ex

backend/apps/system/api/user.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from typing import Optional
22
from fastapi import APIRouter, HTTPException, Query
33
from sqlmodel import func, or_, select, delete as sqlmodel_delete
4-
from apps.system.crud.user import check_account_exists, check_email_exists, get_db_user, single_delete, user_ws_options
4+
from apps.system.crud.user import check_account_exists, check_email_exists, check_email_format, check_pwd_format, get_db_user, single_delete, user_ws_options
55
from apps.system.models.system_model import UserWsModel
66
from apps.system.models.user import UserModel
77
from apps.system.schemas.auth import CacheName, CacheNamespace
@@ -120,6 +120,8 @@ async def create(session: SessionDep, creator: UserCreator):
120120
raise Exception(f"Account [{creator.account}] already exists!")
121121
if check_email_exists(session=session, email=creator.email):
122122
raise Exception(f"Email [{creator.email}] already exists!")
123+
if not check_email_format(creator.email):
124+
raise Exception(f"Email [{creator.email}] format is invalid!")
123125
data = creator.model_dump(exclude_unset=True)
124126
user_model = UserModel.model_validate(data)
125127
#user_model.create_time = get_timestamp()
@@ -150,6 +152,8 @@ async def update(session: SessionDep, editor: UserEditor):
150152
raise Exception(f"account cannot be changed!")
151153
if editor.email != user_model.email and check_email_exists(session=session, account=editor.email):
152154
raise Exception(f"Email [{editor.email}] already exists!")
155+
if not check_email_format(editor.email):
156+
raise Exception(f"Email [{editor.email}] format is invalid!")
153157
origin_oid: int = user_model.oid
154158
del_stmt = sqlmodel_delete(UserWsModel).where(UserWsModel.uid == editor.id)
155159
session.exec(del_stmt)
@@ -206,9 +210,12 @@ async def pwdReset(session: SessionDep, current_user: CurrentUser, id: int):
206210
@router.put("/pwd")
207211
@clear_cache(namespace=CacheNamespace.AUTH_INFO, cacheName=CacheName.USER_INFO, keyExpression="current_user.id")
208212
async def pwdUpdate(session: SessionDep, current_user: CurrentUser, editor: PwdEditor):
213+
new_pwd = editor.new_pwd
214+
if not check_pwd_format(new_pwd):
215+
raise Exception("Password format is invalid!")
209216
db_user: UserModel = get_db_user(session=session, user_id=current_user.id)
210217
if not verify_md5pwd(editor.pwd, db_user.password):
211-
raise HTTPException("pwd error")
212-
db_user.password = md5pwd(editor.new_pwd)
218+
raise Exception(f"pwd [{editor.pwd}] error")
219+
db_user.password = md5pwd(new_pwd)
213220
session.add(db_user)
214221
session.commit()

backend/apps/system/crud/user.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from common.utils.utils import SQLBotLogUtil
1111
from ..models.user import UserModel
1212
from common.core.security import verify_md5pwd
13+
import re
1314

1415
def get_db_user(*, session: Session, user_id: int) -> UserModel:
1516
db_user = session.get(UserModel, user_id)
@@ -74,4 +75,23 @@ async def clean_user_cache(id: int):
7475
def check_account_exists(*, session: Session, account: str) -> bool:
7576
session.exec(select(func.count()).select_from(UserModel).where(UserModel.account == account)).one() > 0
7677
def check_email_exists(*, session: Session, email: str) -> bool:
77-
return session.exec(select(func.count()).select_from(UserModel).where(UserModel.email == email)).one() > 0
78+
return session.exec(select(func.count()).select_from(UserModel).where(UserModel.email == email)).one() > 0
79+
80+
81+
# 预编译正则表达式,提高效率
82+
EMAIL_REGEX = re.compile(
83+
r"^[a-zA-Z0-9]+([._-][a-zA-Z0-9]+)*@"
84+
r"([a-zA-Z0-9]+(-[a-zA-Z0-9]+)*\.)+"
85+
r"[a-zA-Z]{2,}$"
86+
)
87+
88+
PWD_REGEX = re.compile(
89+
r"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)"
90+
r"(?=.*[~!@#$%^&*()_+\-={}|:\"<>?`\[\];',./])"
91+
r"[A-Za-z\d~!@#$%^&*()_+\-={}|:\"<>?`\[\];',./]{8,20}$"
92+
)
93+
def check_email_format(email: str) -> bool:
94+
return bool(EMAIL_REGEX.fullmatch(email))
95+
96+
def check_pwd_format(pwd: str) -> bool:
97+
return bool(PWD_REGEX.fullmatch(pwd))

0 commit comments

Comments
 (0)