From 5cd767161d061039f5b332248eb87a8f9dd89d75 Mon Sep 17 00:00:00 2001 From: sophie <16087569+sophie_liang@user.noreply.gitee.com> Date: Thu, 30 Apr 2026 15:09:17 +0800 Subject: [PATCH] fix bug --- backend/app/api/admin.py | 71 ++++++++++++++++++++++++++++---- frontend/src/i18n.ts | 4 ++ frontend/src/views/AdminView.vue | 7 ++++ 3 files changed, 74 insertions(+), 8 deletions(-) diff --git a/backend/app/api/admin.py b/backend/app/api/admin.py index 60287efd..c37a4b55 100644 --- a/backend/app/api/admin.py +++ b/backend/app/api/admin.py @@ -15,6 +15,7 @@ from sqlalchemy.exc import IntegrityError from datetime import datetime, timedelta import csv import io +import re import requests import uuid from app.services.wecom_notify import send_wecom_event_message @@ -22,6 +23,25 @@ from app.services.wecom_notify import send_wecom_event_message admin_bp = Blueprint('admin', __name__) +def _empty_to_none(value): + if value is None: + return None + if isinstance(value, str) and value.strip() == '': + return None + return value + + +def _is_valid_email(email: str) -> bool: + # Practical validation: local@domain.tld (avoid over-strict RFC parsing) + if not isinstance(email, str): + return False + email = email.strip() + if len(email) > 254: + return False + pattern = r"^[A-Za-z0-9._%+\-]+@[A-Za-z0-9.\-]+\.[A-Za-z]{2,}$" + return re.match(pattern, email) is not None + + def _serialize_wecom_notify_settings(): return { 'enabled': bool(current_app.config.get('WECOM_NOTIFY_ENABLED', True)), @@ -547,15 +567,19 @@ def create_user(): if db.session.query(User).filter_by(username=data['username']).first(): return jsonify({'error': 'Username exists'}), 400 + email = _empty_to_none(data.get('email')) + if email is not None and not _is_valid_email(email): + return jsonify({'error': 'Invalid email format'}), 400 + user = User( username=data['username'], - email=data.get('email'), - department=data.get('department'), + email=email, + department=_empty_to_none(data.get('department')), role=data.get('role', 'user'), status=data.get('status', 'active'), - real_name=data.get('real_name'), - employee_id=data.get('employee_id'), - phone_number=data.get('phone_number') + real_name=_empty_to_none(data.get('real_name')), + employee_id=_empty_to_none(data.get('employee_id')), + phone_number=_empty_to_none(data.get('phone_number')) ) if data.get('password'): user.set_password(data['password']) @@ -564,7 +588,18 @@ def create_user(): user.agents = agents db.session.add(user) - db.session.commit() + try: + db.session.commit() + except IntegrityError as e: + db.session.rollback() + message = str(getattr(e, 'orig', e)) + if 'users_email_key' in message: + return jsonify({'error': 'Email exists'}), 400 + if 'users_employee_id_key' in message: + return jsonify({'error': 'Employee ID exists'}), 400 + if 'users_phone_number_key' in message: + return jsonify({'error': 'Phone number exists'}), 400 + return jsonify({'error': 'Data uniqueness conflict'}), 400 return jsonify({'id': user.id}), 201 @admin_bp.route('/users/', methods=['PUT']) @@ -574,8 +609,17 @@ def update_user(id): if not user: return jsonify({'error': 'Not found'}), 404 data = request.get_json() or {} + if 'email' in data: + email = _empty_to_none(data.get('email')) + if email is not None and not _is_valid_email(email): + return jsonify({'error': 'Invalid email format'}), 400 + for k in ['email', 'department', 'role', 'status', 'real_name', 'employee_id', 'phone_number']: - if k in data: setattr(user, k, data[k]) + if k in data: + if k in ['email', 'department', 'real_name', 'employee_id', 'phone_number']: + setattr(user, k, _empty_to_none(data[k])) + else: + setattr(user, k, data[k]) if data.get('password'): user.set_password(data['password']) @@ -583,7 +627,18 @@ def update_user(id): agents = db.session.query(Agent).filter(Agent.id.in_(data['agent_ids'])).all() user.agents = agents - db.session.commit() + try: + db.session.commit() + except IntegrityError as e: + db.session.rollback() + message = str(getattr(e, 'orig', e)) + if 'users_email_key' in message: + return jsonify({'error': 'Email exists'}), 400 + if 'users_employee_id_key' in message: + return jsonify({'error': 'Employee ID exists'}), 400 + if 'users_phone_number_key' in message: + return jsonify({'error': 'Phone number exists'}), 400 + return jsonify({'error': 'Data uniqueness conflict'}), 400 return jsonify({'status': 'ok'}) @admin_bp.route('/users/cleanup-rejected', methods=['POST']) diff --git a/frontend/src/i18n.ts b/frontend/src/i18n.ts index 57697eaa..76635129 100644 --- a/frontend/src/i18n.ts +++ b/frontend/src/i18n.ts @@ -323,6 +323,7 @@ const messages = { msgs: { usernameRequired: 'Username is required', passwordRequired: 'Password is required for new users', + invalidEmail: 'Invalid email format', created: 'User created successfully', updated: 'User updated successfully', deleted: 'User deleted successfully', @@ -1209,6 +1210,7 @@ const messages = { msgs: { usernameRequired: '用户名必填', passwordRequired: '新用户必须设置密码', + invalidEmail: '邮箱格式不正确', created: '用户创建成功', updated: '用户更新成功', deleted: '用户删除成功', @@ -2058,6 +2060,7 @@ const messages = { msgs: { usernameRequired: '用戶名必填', passwordRequired: '新用戶必須設置密碼', + invalidEmail: '郵箱格式不正確', created: '用戶創建成功', updated: '用戶更新成功', deleted: '用戶刪除成功', @@ -2912,6 +2915,7 @@ const messages = { msgs: { usernameRequired: 'กรุณากรอกชื่อผู้ใช้', passwordRequired: 'ต้องระบุรหัสผ่านสำหรับผู้ใช้ใหม่', + invalidEmail: 'รูปแบบอีเมลไม่ถูกต้อง', created: 'สร้างผู้ใช้สำเร็จ', updated: 'อัปเดตผู้ใช้สำเร็จ', deleted: 'ลบผู้ใช้สำเร็จ', diff --git a/frontend/src/views/AdminView.vue b/frontend/src/views/AdminView.vue index 1599c617..34e14ced 100644 --- a/frontend/src/views/AdminView.vue +++ b/frontend/src/views/AdminView.vue @@ -49,6 +49,7 @@ import * as XLSX from 'xlsx'; const { t, locale } = useI18n(); const { showMessageDialog } = useGlobalMessageDialog(); +const EMAIL_PATTERN = /^[A-Za-z0-9._%+\-]+@[A-Za-z0-9.\-]+\.[A-Za-z]{2,}$/; const sidebarItems = computed(() => { const items = [ @@ -307,6 +308,12 @@ const saveUser = async () => { showMessageDialog('warning', t('admin.user.msgs.usernameRequired')); return; } + + const email = (editingUser.value.email || '').trim(); + if (email && !EMAIL_PATTERN.test(email)) { + showMessageDialog('warning', t('admin.user.msgs.invalidEmail')); + return; + } if (!editingUser.value.id && !editingUser.value.password) { showMessageDialog('warning', t('admin.user.msgs.passwordRequired'));