fix: handle existing admin during deploy

This commit is contained in:
hectorzhao
2026-07-10 13:48:23 +08:00
parent 09b78377e6
commit 709ac97764
2 changed files with 9 additions and 5 deletions
+1
View File
@@ -1387,3 +1387,4 @@ git diff --check
- 生产 API 搜索 `1882120` 返回“中国移动/上海/上海”;搜索“上海”首屏响应约 80ms。
- 生产 `cmpp-api``cmpp-gateway`、PostgreSQL、Nginx 均为 activeAPI health 正常。
- 隔离部署后曾因 `dist/assets` 被保留为 `700 root:root` 导致 Nginx 无权读取 JS/CSS、admin 页面空白;线上已修正为目录 `755`、文件 `644`,正式生产部署脚本同步固化权限。
- 正式部署发现已有生产管理员且未配置 `PROD_ADMIN_PASSWORD` 时,`upsert.create` 仍会对空密码执行哈希;已拆分 create/update 密码变量,已有账号不改密码,新建账号才生成临时密码。
+8 -5
View File
@@ -20,7 +20,8 @@ function hashPassword(value) {
async function main() {
const existingUser = await prisma.user.findUnique({ where: { username } });
const password = configuredPassword || (existingUser ? undefined : randomBytes(18).toString('base64url'));
const createPassword = configuredPassword || randomBytes(18).toString('base64url');
const updatePassword = configuredPassword;
const role = await prisma.role.upsert({
where: { code: 'platform_admin' },
update: { name: '平台管理员', scope: 'platform' },
@@ -31,7 +32,7 @@ async function main() {
update: {
email,
displayName: '生产平台管理员',
...(password ? { passwordHash: hashPassword(password) } : {}),
...(updatePassword ? { passwordHash: hashPassword(updatePassword) } : {}),
status: 'active',
failedLoginCount: 0,
lockedUntil: null,
@@ -42,7 +43,7 @@ async function main() {
username,
email,
displayName: '生产平台管理员',
passwordHash: hashPassword(password),
passwordHash: hashPassword(createPassword),
status: 'active',
},
});
@@ -56,7 +57,9 @@ async function main() {
'CMPP production admin account',
`username=${username}`,
`email=${email}`,
password ? `password=${password}` : 'password=unchanged',
existingUser
? (updatePassword ? `password=${updatePassword}` : 'password=unchanged')
: `password=${createPassword}`,
`generatedAt=${new Date().toISOString()}`,
'',
].join('\n');
@@ -65,7 +68,7 @@ async function main() {
}
console.log(`Production admin is ready: ${email}`);
if (!credentialFile) {
console.log(`Temporary password: ${password}`);
console.log(existingUser && !updatePassword ? 'Password unchanged' : `Temporary password: ${existingUser ? updatePassword : createPassword}`);
}
}