forked from h88782481/Chat-Share
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.py
171 lines (135 loc) · 6.16 KB
/
user.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
from app import app
from flask import render_template, request, jsonify
import uuid
import utils.globals as globals
from utils.globals import *
from utils.tools import *
from werkzeug.security import generate_password_hash
@app.route('/user')
@admin_required
def user_management():
return render_template('user_management.html')
# 获取所有用户
@app.route('/api/users', methods=['GET'])
@admin_required
def get_users():
# 返回用户列表时不包含密码信息
return jsonify([{k: v for k, v in user.items() if k != 'password' and k != 'bind_token'} for user in globals.users])
# 创建新用户
@app.route('/api/users', methods=['POST'])
@admin_required
def create_user():
data = request.get_json()
# 检查用户名是否已存在
if any(user['username'] == data['username'] for user in globals.users):
return jsonify({'success': False, 'message': '用户名已存在'}), 400
new_user = {
'id': str(uuid.uuid4()),
'username': data['username'],
'password': generate_password_hash(data['password']),
'role': data['role'],
'bind_token': '',
'bind_email': '',
'expiration_time': data['expiration_time'],
'bind_claude_token': '',
'bind_claude_email': '',
'claude_expiration_time': data['claude_expiration_time']
}
globals.users.append(new_user)
save_users(globals.users)
return jsonify({'success': True, 'message': '用户创建成功'})
# 更新用户信息
@app.route('/api/users/<user_id>', methods=['PUT'])
@admin_required
def update_user(user_id):
data = request.get_json()
user_index = next((i for i, user in enumerate(globals.users) if user['id'] == user_id), None)
if user_index is None:
return jsonify({'success': False, 'message': '用户不存在'}), 404
# 检查用户名是否与其他用户冲突
if any(user['username'] == data['username'] and user['id'] != user_id for user in globals.users):
return jsonify({'success': False, 'message': '用户名已存在'}), 400
# 更新用户信息
globals.users[user_index]['username'] = data['username']
globals.users[user_index]['role'] = data['role']
# 如果提供了新密码,则更新密码
if data.get('password'):
globals.users[user_index]['password'] = generate_password_hash(data['password'])
globals.users[user_index]['expiration_time'] = data['expiration_time']
globals.users[user_index]['claude_expiration_time'] = data['claude_expiration_time']
save_users(globals.users)
return jsonify({'success': True, 'message': '用户更新成功'})
# 绑定ChatGPT账号
@app.route('/api/bind/<user_id>', methods=['PUT'])
@admin_required
def bind_account(user_id):
data = request.get_json()
user_index = next((i for i, user in enumerate(globals.users) if user['id'] == user_id), None)
token_index = next((i for i, token in enumerate(globals.chatToken) if token['email'] == data['email']), None)
res = set_seedmap(user_id,globals.chatToken[token_index]['access_token'])
if res == 200:
globals.users[user_index]['bind_email'] = data['email']
globals.users[user_index]['bind_token'] = globals.chatToken[token_index]['access_token']
save_users(globals.users)
return jsonify({'success': True, 'message': '账号绑定成功'})
else:
return jsonify({'success': False, 'message': '账号绑定失败'})
# 解绑ChatGPT账号
@app.route('/api/del_bind/<user_id>', methods=['DELETE'])
@admin_required
def del_bind_account(user_id):
res = del_seedmap(user_id)
if res == 200:
user_index = next((i for i, user in enumerate(globals.users) if user['id'] == user_id), None)
globals.users[user_index]['bind_email'] = ''
globals.users[user_index]['bind_token'] = ''
save_users(globals.users)
return jsonify({'success': True, 'message': '账号解绑成功'})
else:
return jsonify({'success': False, 'message': '账号解绑失败'})
# 获取全部ChatGPT账号的email
@app.route('/api/all_email', methods=['GET'])
@admin_required
def all_email():
# 返回账号的全部email
return jsonify([token['email'] for token in globals.chatToken if 'email' in token])
# 绑定Claude账号
@app.route('/api/bindClaude/<user_id>', methods=['PUT'])
@admin_required
def bind_claude_account(user_id):
data = request.get_json()
user_index = next((i for i, user in enumerate(globals.users) if user['id'] == user_id), None)
token_index = next((i for i, token in enumerate(globals.cluadeToken) if token['email'] == data['email']), None)
globals.users[user_index]['bind_claude_email'] = data['email']
globals.users[user_index]['bind_claude_token'] = globals.cluadeToken[token_index]['skToken']
save_users(globals.users)
return jsonify({'success': True, 'message': '账号绑定成功'})
# 解绑Claude账号
@app.route('/api/del_bindClaude/<user_id>', methods=['DELETE'])
@admin_required
def del_bind_claude_account(user_id):
user_index = next((i for i, user in enumerate(globals.users) if user['id'] == user_id), None)
globals.users[user_index]['bind_claude_email'] = ''
globals.users[user_index]['bind_claude_token'] = ''
save_users(globals.users)
return jsonify({'success': True, 'message': '账号解绑成功'})
# 获取全部Claude账号的email
@app.route('/api/all_claude_email', methods=['GET'])
@admin_required
def all_claude_email():
# 返回账号的全部email
return jsonify([token['email'] for token in globals.cluadeToken if 'email' in token])
# 删除用户
@app.route('/api/users/<user_id>', methods=['DELETE'])
@admin_required
def delete_user(user_id):
# 过滤掉要删除的用户
updated_users = [user for user in globals.users if user['id'] != user_id]
user = next((user for user in globals.users if user['id'] == user_id), None)
if len(updated_users) == len(globals.users):
return jsonify({'success': False, 'message': '用户不存在'}), 404
globals.users = updated_users
save_users(globals.users)
if user['bind_token'] != '':
del_seedmap(user_id)
return jsonify({'success': True, 'message': '用户删除成功'})