sage/reset_superuser_password.py

51 lines
1.6 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import asyncio
from sqlor.dbpools import DBPools
from appPublic.jsonConfig import getConfig
from ahserver.globalEnv import password_encode
async def reset_superuser_password():
config = getConfig('.')
db = DBPools(config.databases)
# 直接使用sage数据库从配置看只有一个sage数据库
# rbac模块的表应该也在sage数据库中
dbname = 'sage'
new_password = 'admin123'
encoded_password = password_encode(new_password)
async with db.sqlorContext(dbname) as sor:
# 先检查superuser是否存在
users = await sor.R('users', {'username': 'superuser'})
if not users:
print('superuser用户不存在')
return False
user = users[0]
print(f'找到用户: id={user.id}, username={user.username}')
# 更新密码
ns = {
'id': user.id,
'password': encoded_password
}
await sor.U('users', ns)
print(f'密码已更新为新密码: {new_password}')
# 验证更新 - 使用sqlExe直接查询
verify_sql = "select username from users where username='superuser' and password=${password}$"
verify_result = await sor.sqlExe(verify_sql, {'password': encoded_password})
if verify_result:
print('密码验证成功')
else:
print('密码验证失败')
return True
if __name__ == '__main__':
success = asyncio.get_event_loop().run_until_complete(reset_superuser_password())
if success:
print('superuser密码重置完成')
else:
print('密码重置失败')