Merge remote-tracking branch 'origin/main-v2'
# Conflicts: # ESConnect.py # app.py
This commit is contained in:
		
							
								
								
									
										189
									
								
								ESConnect.py
									
									
									
									
									
								
							
							
						
						
									
										189
									
								
								ESConnect.py
									
									
									
									
									
								
							@@ -3,6 +3,7 @@ from elasticsearch import Elasticsearch
 | 
			
		||||
# import json
 | 
			
		||||
import hashlib
 | 
			
		||||
import requests
 | 
			
		||||
import json
 | 
			
		||||
 | 
			
		||||
# Elasticsearch连接配置
 | 
			
		||||
ES_URL = "http://localhost:9200"
 | 
			
		||||
@@ -60,6 +61,9 @@ def create_index_with_mapping():
 | 
			
		||||
        write_user_data(admin)
 | 
			
		||||
    else:
 | 
			
		||||
        print(f"索引 {users_index_name} 已存在")
 | 
			
		||||
def update_document(es, index_name, doc_id=None, updated_doc=None):
 | 
			
		||||
    """更新指定ID的文档"""
 | 
			
		||||
    es.update(index=index_name, id=doc_id, body={"doc": updated_doc})
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def get_doc_id(data):
 | 
			
		||||
@@ -142,6 +146,49 @@ def delete_by_id(doc_id):
 | 
			
		||||
        print("删除失败:", str(e))
 | 
			
		||||
        return False
 | 
			
		||||
 | 
			
		||||
def update_by_id(doc_id, updated_data):
 | 
			
		||||
    """
 | 
			
		||||
    根据文档ID更新数据
 | 
			
		||||
 | 
			
		||||
    参数:
 | 
			
		||||
        doc_id (str): 要更新的文档ID
 | 
			
		||||
        updated_data (dict): 更新的数据内容
 | 
			
		||||
 | 
			
		||||
    返回:
 | 
			
		||||
        bool: 更新成功返回True,失败返回False
 | 
			
		||||
    """
 | 
			
		||||
    try:
 | 
			
		||||
        # 执行更新操作
 | 
			
		||||
        es.update(index=index_name, id=doc_id, body={"doc": updated_data})
 | 
			
		||||
        print(f"文档 {doc_id} 更新成功")
 | 
			
		||||
        return True
 | 
			
		||||
    except Exception as e:
 | 
			
		||||
        print(f"更新失败: {str(e)}")
 | 
			
		||||
        return False
 | 
			
		||||
 | 
			
		||||
def get_by_id(doc_id):
 | 
			
		||||
    """
 | 
			
		||||
    根据文档ID获取单个文档
 | 
			
		||||
 | 
			
		||||
    参数:
 | 
			
		||||
        doc_id (str): 要获取的文档ID
 | 
			
		||||
 | 
			
		||||
    返回:
 | 
			
		||||
        dict or None: 成功返回文档数据,失败返回None
 | 
			
		||||
    """
 | 
			
		||||
    try:
 | 
			
		||||
        # 执行获取操作
 | 
			
		||||
        result = es.get(index=index_name, id=doc_id)
 | 
			
		||||
        if result['found']:
 | 
			
		||||
            return {
 | 
			
		||||
                "_id": result['_id'],
 | 
			
		||||
                **result['_source']
 | 
			
		||||
            }
 | 
			
		||||
        return None
 | 
			
		||||
    except Exception as e:
 | 
			
		||||
        print(f"获取文档失败: {str(e)}")
 | 
			
		||||
        return None
 | 
			
		||||
 | 
			
		||||
def search_by_any_field(keyword):
 | 
			
		||||
    """全字段模糊搜索(支持拼写错误)"""
 | 
			
		||||
    try:
 | 
			
		||||
@@ -216,11 +263,11 @@ def write_user_data(data):
 | 
			
		||||
def verify_user(username, password):
 | 
			
		||||
    """
 | 
			
		||||
    验证用户登录信息
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    参数:
 | 
			
		||||
        username (str): 用户名
 | 
			
		||||
        password (str): 密码
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
    返回:
 | 
			
		||||
        dict or None: 验证成功返回用户信息,失败返回None
 | 
			
		||||
    """
 | 
			
		||||
@@ -239,7 +286,7 @@ def verify_user(username, password):
 | 
			
		||||
        )
 | 
			
		||||
        response.raise_for_status()
 | 
			
		||||
        results = response.json()["hits"]["hits"]
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        if results:
 | 
			
		||||
            user_data = results[0]["_source"]
 | 
			
		||||
            # 验证密码
 | 
			
		||||
@@ -252,7 +299,7 @@ def verify_user(username, password):
 | 
			
		||||
        else:
 | 
			
		||||
            print(f"用户 {username} 不存在")
 | 
			
		||||
            return None
 | 
			
		||||
            
 | 
			
		||||
 | 
			
		||||
    except requests.exceptions.HTTPError as e:
 | 
			
		||||
        print(f"用户验证失败: {e.response.text}")
 | 
			
		||||
        return None
 | 
			
		||||
@@ -260,10 +307,10 @@ def verify_user(username, password):
 | 
			
		||||
def get_user_by_username(username):
 | 
			
		||||
    """
 | 
			
		||||
    根据用户名查询用户信息
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    参数:
 | 
			
		||||
        username (str): 用户名
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
    返回:
 | 
			
		||||
        dict or None: 查询成功返回用户信息,失败返回None
 | 
			
		||||
    """
 | 
			
		||||
@@ -281,12 +328,12 @@ def get_user_by_username(username):
 | 
			
		||||
        )
 | 
			
		||||
        response.raise_for_status()
 | 
			
		||||
        results = response.json()["hits"]["hits"]
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        if results:
 | 
			
		||||
            return results[0]["_source"]
 | 
			
		||||
        else:
 | 
			
		||||
            return None
 | 
			
		||||
            
 | 
			
		||||
 | 
			
		||||
    except requests.exceptions.HTTPError as e:
 | 
			
		||||
        print(f"用户查询失败: {e.response.text}")
 | 
			
		||||
        return None
 | 
			
		||||
@@ -294,12 +341,12 @@ def get_user_by_username(username):
 | 
			
		||||
def create_user(username, password, permission=1):
 | 
			
		||||
    """
 | 
			
		||||
    创建新用户
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    参数:
 | 
			
		||||
        username (str): 用户名
 | 
			
		||||
        password (str): 密码
 | 
			
		||||
        permission (int): 权限级别,默认为1(普通用户)
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
    返回:
 | 
			
		||||
        bool: 创建成功返回True,失败返回False
 | 
			
		||||
    """
 | 
			
		||||
@@ -307,24 +354,24 @@ def create_user(username, password, permission=1):
 | 
			
		||||
    if get_user_by_username(username):
 | 
			
		||||
        print(f"用户名 {username} 已存在")
 | 
			
		||||
        return False
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    # 生成新的用户ID
 | 
			
		||||
    import time
 | 
			
		||||
    user_id = int(time.time() * 1000)  # 使用时间戳作为用户ID
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    user_data = {
 | 
			
		||||
        "user_id": user_id,
 | 
			
		||||
        "username": username,
 | 
			
		||||
        "password": password,
 | 
			
		||||
        "premission": permission
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    return write_user_data(user_data)
 | 
			
		||||
 | 
			
		||||
def get_all_users():
 | 
			
		||||
    """
 | 
			
		||||
    获取所有用户信息
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    返回:
 | 
			
		||||
        list: 包含所有用户信息的列表
 | 
			
		||||
    """
 | 
			
		||||
@@ -341,15 +388,15 @@ def get_all_users():
 | 
			
		||||
        )
 | 
			
		||||
        response.raise_for_status()
 | 
			
		||||
        results = response.json()["hits"]["hits"]
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        users = []
 | 
			
		||||
        for hit in results:
 | 
			
		||||
            user_data = hit["_source"]
 | 
			
		||||
            user_data["_id"] = hit["_id"]  # 添加文档ID用于后续操作
 | 
			
		||||
            users.append(user_data)
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        return users
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
    except requests.exceptions.HTTPError as e:
 | 
			
		||||
        print(f"获取用户列表失败: {e.response.text}")
 | 
			
		||||
        return []
 | 
			
		||||
@@ -357,11 +404,11 @@ def get_all_users():
 | 
			
		||||
def update_user_password(username, new_password):
 | 
			
		||||
    """
 | 
			
		||||
    更新用户密码
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    参数:
 | 
			
		||||
        username (str): 用户名
 | 
			
		||||
        new_password (str): 新密码
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
    返回:
 | 
			
		||||
        bool: 更新成功返回True,失败返回False
 | 
			
		||||
    """
 | 
			
		||||
@@ -380,18 +427,18 @@ def update_user_password(username, new_password):
 | 
			
		||||
        )
 | 
			
		||||
        response.raise_for_status()
 | 
			
		||||
        results = response.json()["hits"]["hits"]
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        if not results:
 | 
			
		||||
            print(f"用户 {username} 不存在")
 | 
			
		||||
            return False
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        # 获取用户文档ID
 | 
			
		||||
        doc_id = results[0]["_id"]
 | 
			
		||||
        user_data = results[0]["_source"]
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        # 更新密码
 | 
			
		||||
        user_data["password"] = new_password
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        # 更新文档
 | 
			
		||||
        update_response = requests.post(
 | 
			
		||||
            f"{ES_URL}/{users_index_name}/_doc/{doc_id}",
 | 
			
		||||
@@ -400,10 +447,10 @@ def update_user_password(username, new_password):
 | 
			
		||||
            headers={"Content-Type": "application/json"}
 | 
			
		||||
        )
 | 
			
		||||
        update_response.raise_for_status()
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        print(f"用户 {username} 密码更新成功")
 | 
			
		||||
        return True
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
    except requests.exceptions.HTTPError as e:
 | 
			
		||||
        print(f"更新用户密码失败: {e.response.text}")
 | 
			
		||||
        return False
 | 
			
		||||
@@ -411,10 +458,10 @@ def update_user_password(username, new_password):
 | 
			
		||||
def delete_user(username):
 | 
			
		||||
    """
 | 
			
		||||
    删除用户
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    参数:
 | 
			
		||||
        username (str): 要删除的用户名
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
    返回:
 | 
			
		||||
        bool: 删除成功返回True,失败返回False
 | 
			
		||||
    """
 | 
			
		||||
@@ -423,7 +470,7 @@ def delete_user(username):
 | 
			
		||||
        if username == "admin":
 | 
			
		||||
            print("不能删除管理员账户")
 | 
			
		||||
            return False
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        # 先查找用户
 | 
			
		||||
        response = requests.post(
 | 
			
		||||
            f"{ES_URL}/{users_index_name}/_search",
 | 
			
		||||
@@ -438,24 +485,24 @@ def delete_user(username):
 | 
			
		||||
        )
 | 
			
		||||
        response.raise_for_status()
 | 
			
		||||
        results = response.json()["hits"]["hits"]
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        if not results:
 | 
			
		||||
            print(f"用户 {username} 不存在")
 | 
			
		||||
            return False
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        # 获取用户文档ID
 | 
			
		||||
        doc_id = results[0]["_id"]
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        # 删除用户
 | 
			
		||||
        delete_response = requests.delete(
 | 
			
		||||
            f"{ES_URL}/{users_index_name}/_doc/{doc_id}",
 | 
			
		||||
            auth=AUTH
 | 
			
		||||
        )
 | 
			
		||||
        delete_response.raise_for_status()
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        print(f"用户 {username} 删除成功")
 | 
			
		||||
        return True
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
    except requests.exceptions.HTTPError as e:
 | 
			
		||||
        print(f"删除用户失败: {e.response.text}")
 | 
			
		||||
        return False
 | 
			
		||||
@@ -463,11 +510,11 @@ def delete_user(username):
 | 
			
		||||
def update_user_permission(username, new_permission):
 | 
			
		||||
    """
 | 
			
		||||
    更新用户权限
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    参数:
 | 
			
		||||
        username (str): 用户名
 | 
			
		||||
        new_permission (int): 新权限级别
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
    返回:
 | 
			
		||||
        bool: 更新成功返回True,失败返回False
 | 
			
		||||
    """
 | 
			
		||||
@@ -476,7 +523,7 @@ def update_user_permission(username, new_permission):
 | 
			
		||||
        if username == "admin":
 | 
			
		||||
            print("不能修改管理员权限")
 | 
			
		||||
            return False
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        # 先查找用户
 | 
			
		||||
        response = requests.post(
 | 
			
		||||
            f"{ES_URL}/{users_index_name}/_search",
 | 
			
		||||
@@ -491,18 +538,18 @@ def update_user_permission(username, new_permission):
 | 
			
		||||
        )
 | 
			
		||||
        response.raise_for_status()
 | 
			
		||||
        results = response.json()["hits"]["hits"]
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        if not results:
 | 
			
		||||
            print(f"用户 {username} 不存在")
 | 
			
		||||
            return False
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        # 获取用户文档ID
 | 
			
		||||
        doc_id = results[0]["_id"]
 | 
			
		||||
        user_data = results[0]["_source"]
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        # 更新权限
 | 
			
		||||
        user_data["premission"] = new_permission
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        # 更新文档
 | 
			
		||||
        update_response = requests.post(
 | 
			
		||||
            f"{ES_URL}/{users_index_name}/_doc/{doc_id}",
 | 
			
		||||
@@ -511,10 +558,10 @@ def update_user_permission(username, new_permission):
 | 
			
		||||
            headers={"Content-Type": "application/json"}
 | 
			
		||||
        )
 | 
			
		||||
        update_response.raise_for_status()
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        print(f"用户 {username} 权限更新成功")
 | 
			
		||||
        return True
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
    except requests.exceptions.HTTPError as e:
 | 
			
		||||
        print(f"更新用户权限失败: {e.response.text}")
 | 
			
		||||
        return False
 | 
			
		||||
@@ -522,11 +569,11 @@ def update_user_permission(username, new_permission):
 | 
			
		||||
def search_data_by_user(user_id, keyword=None):
 | 
			
		||||
    """
 | 
			
		||||
    根据用户ID查询该用户的数据,支持关键词搜索
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    参数:
 | 
			
		||||
        user_id (str): 用户ID
 | 
			
		||||
        keyword (str, optional): 搜索关键词
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
    返回:
 | 
			
		||||
        list: 包含文档ID和源数据的列表
 | 
			
		||||
    """
 | 
			
		||||
@@ -552,7 +599,7 @@ def search_data_by_user(user_id, keyword=None):
 | 
			
		||||
            query = {
 | 
			
		||||
                "term": {"user_id": user_id}
 | 
			
		||||
            }
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        response = requests.post(
 | 
			
		||||
            f"{ES_URL}/{data_index_name}/_search",
 | 
			
		||||
            auth=AUTH,
 | 
			
		||||
@@ -563,13 +610,13 @@ def search_data_by_user(user_id, keyword=None):
 | 
			
		||||
        )
 | 
			
		||||
        response.raise_for_status()
 | 
			
		||||
        results = response.json()["hits"]["hits"]
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        # 返回包含文档ID和源数据的列表
 | 
			
		||||
        return [{
 | 
			
		||||
            "_id": hit["_id"],
 | 
			
		||||
            **hit["_source"]
 | 
			
		||||
        } for hit in results]
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
    except requests.exceptions.HTTPError as e:
 | 
			
		||||
        print(f"查询用户数据失败: {e.response.text}")
 | 
			
		||||
        return []
 | 
			
		||||
@@ -577,12 +624,12 @@ def search_data_by_user(user_id, keyword=None):
 | 
			
		||||
def update_data_by_id(doc_id, updated_data, user_id):
 | 
			
		||||
    """
 | 
			
		||||
    根据文档ID更新数据(仅允许数据所有者修改)
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    参数:
 | 
			
		||||
        doc_id (str): 文档ID
 | 
			
		||||
        updated_data (dict): 更新的数据
 | 
			
		||||
        user_id (str): 当前用户ID
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
    返回:
 | 
			
		||||
        bool: 更新成功返回True,失败返回False
 | 
			
		||||
    """
 | 
			
		||||
@@ -594,20 +641,20 @@ def update_data_by_id(doc_id, updated_data, user_id):
 | 
			
		||||
        )
 | 
			
		||||
        response.raise_for_status()
 | 
			
		||||
        doc = response.json()
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        # 检查文档是否存在
 | 
			
		||||
        if not doc.get("found"):
 | 
			
		||||
            print(f"文档 {doc_id} 不存在")
 | 
			
		||||
            return False
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        # 检查用户权限(只能修改自己的数据)
 | 
			
		||||
        if doc["_source"].get("user_id") != user_id:
 | 
			
		||||
            print(f"用户 {user_id} 无权修改文档 {doc_id}")
 | 
			
		||||
            return False
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        # 保持用户ID不变
 | 
			
		||||
        updated_data["user_id"] = user_id
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        # 更新文档
 | 
			
		||||
        update_response = requests.post(
 | 
			
		||||
            f"{ES_URL}/{data_index_name}/_doc/{doc_id}",
 | 
			
		||||
@@ -616,10 +663,10 @@ def update_data_by_id(doc_id, updated_data, user_id):
 | 
			
		||||
            headers={"Content-Type": "application/json"}
 | 
			
		||||
        )
 | 
			
		||||
        update_response.raise_for_status()
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        print(f"文档 {doc_id} 更新成功")
 | 
			
		||||
        return True
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
    except requests.exceptions.HTTPError as e:
 | 
			
		||||
        print(f"更新文档失败: {e.response.text}")
 | 
			
		||||
        return False
 | 
			
		||||
@@ -627,11 +674,11 @@ def update_data_by_id(doc_id, updated_data, user_id):
 | 
			
		||||
def delete_data_by_id(doc_id, user_id):
 | 
			
		||||
    """
 | 
			
		||||
    根据文档ID删除数据(仅允许数据所有者或管理员删除)
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    参数:
 | 
			
		||||
        doc_id (str): 文档ID
 | 
			
		||||
        user_id (str): 当前用户ID
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
    返回:
 | 
			
		||||
        bool: 删除成功返回True,失败返回False
 | 
			
		||||
    """
 | 
			
		||||
@@ -643,12 +690,12 @@ def delete_data_by_id(doc_id, user_id):
 | 
			
		||||
        )
 | 
			
		||||
        response.raise_for_status()
 | 
			
		||||
        doc = response.json()
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        # 检查文档是否存在
 | 
			
		||||
        if not doc.get("found"):
 | 
			
		||||
            print(f"文档 {doc_id} 不存在")
 | 
			
		||||
            return False
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        # 检查用户权限(只能删除自己的数据,管理员可以删除所有数据)
 | 
			
		||||
        doc_user_id = doc["_source"].get("user_id")
 | 
			
		||||
        if doc_user_id != user_id:
 | 
			
		||||
@@ -657,17 +704,17 @@ def delete_data_by_id(doc_id, user_id):
 | 
			
		||||
            if not user_info or user_info.get("premission") != 0:
 | 
			
		||||
                print(f"用户 {user_id} 无权删除文档 {doc_id}")
 | 
			
		||||
                return False
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        # 删除文档
 | 
			
		||||
        delete_response = requests.delete(
 | 
			
		||||
            f"{ES_URL}/{data_index_name}/_doc/{doc_id}",
 | 
			
		||||
            auth=AUTH
 | 
			
		||||
        )
 | 
			
		||||
        delete_response.raise_for_status()
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        print(f"文档 {doc_id} 删除成功")
 | 
			
		||||
        return True
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
    except requests.exceptions.HTTPError as e:
 | 
			
		||||
        print(f"删除文档失败: {e.response.text}")
 | 
			
		||||
        return False
 | 
			
		||||
@@ -675,12 +722,12 @@ def delete_data_by_id(doc_id, user_id):
 | 
			
		||||
def update_user_own_password(user_id, old_password, new_password):
 | 
			
		||||
    """
 | 
			
		||||
    用户修改自己的密码
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    参数:
 | 
			
		||||
        user_id (str): 用户ID
 | 
			
		||||
        old_password (str): 旧密码
 | 
			
		||||
        new_password (str): 新密码
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
    返回:
 | 
			
		||||
        bool: 修改成功返回True,失败返回False
 | 
			
		||||
    """
 | 
			
		||||
@@ -699,22 +746,22 @@ def update_user_own_password(user_id, old_password, new_password):
 | 
			
		||||
        )
 | 
			
		||||
        response.raise_for_status()
 | 
			
		||||
        results = response.json()["hits"]["hits"]
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        if not results:
 | 
			
		||||
            print(f"用户 {user_id} 不存在")
 | 
			
		||||
            return False
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        user_data = results[0]["_source"]
 | 
			
		||||
        doc_id = results[0]["_id"]
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        # 验证旧密码
 | 
			
		||||
        if user_data.get("password") != old_password:
 | 
			
		||||
            print("旧密码错误")
 | 
			
		||||
            return False
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        # 更新密码
 | 
			
		||||
        user_data["password"] = new_password
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        # 更新文档
 | 
			
		||||
        update_response = requests.post(
 | 
			
		||||
            f"{ES_URL}/{users_index_name}/_doc/{doc_id}",
 | 
			
		||||
@@ -723,10 +770,10 @@ def update_user_own_password(user_id, old_password, new_password):
 | 
			
		||||
            headers={"Content-Type": "application/json"}
 | 
			
		||||
        )
 | 
			
		||||
        update_response.raise_for_status()
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        print(f"用户 {user_id} 密码修改成功")
 | 
			
		||||
        return True
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
    except requests.exceptions.HTTPError as e:
 | 
			
		||||
        print(f"修改密码失败: {e.response.text}")
 | 
			
		||||
        return False
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user