From 2751836458c5b2d23b5159fa28c9ced5c3485f34 Mon Sep 17 00:00:00 2001 From: mertmit Date: Wed, 20 Jul 2022 22:29:10 +0300 Subject: [PATCH] fix: allow env variables to recover missing super user Signed-off-by: mertmit --- .../lib/meta/api/userApi/initAdminFromEnv.ts | 53 +++++++++++++++++-- 1 file changed, 50 insertions(+), 3 deletions(-) diff --git a/packages/nocodb/src/lib/meta/api/userApi/initAdminFromEnv.ts b/packages/nocodb/src/lib/meta/api/userApi/initAdminFromEnv.ts index 82896ad0c3..fc8e08264e 100644 --- a/packages/nocodb/src/lib/meta/api/userApi/initAdminFromEnv.ts +++ b/packages/nocodb/src/lib/meta/api/userApi/initAdminFromEnv.ts @@ -63,11 +63,10 @@ export default async function initAdminFromEnv(_ncMeta = Noco.ncMeta) { salt ); const email_verification_token = uuidv4(); + const roles = 'user,super'; // if super admin not present if (await User.isFirst(ncMeta)) { - const roles = 'user,super'; - // roles = 'owner,creator,editor' Tele.emit('evt', { evt_type: 'project:invite', @@ -96,8 +95,56 @@ export default async function initAdminFromEnv(_ncMeta = Noco.ncMeta) { const superUser = await ncMeta.metaGet2(null, null, MetaTable.USERS, { roles: 'user,super', }); + + if (!superUser?.id) { + const existingUserWithNewEmail = await User.getByEmail(email, ncMeta); + if (existingUserWithNewEmail?.id) { + // clear cache + await NocoCache.delAll( + CacheScope.USER, + `${existingUserWithNewEmail.email}___*` + ); + await NocoCache.del( + `${CacheScope.USER}:${existingUserWithNewEmail.id}` + ); + await NocoCache.del( + `${CacheScope.USER}:${existingUserWithNewEmail.email}` + ); - if (email !== superUser.email) { + // Update email and password of super admin account + await User.update( + existingUserWithNewEmail.id, + { + salt, + email, + password, + email_verification_token, + token_version: null, + refresh_token: null, + roles, + }, + ncMeta + ); + } else { + Tele.emit('evt', { + evt_type: 'project:invite', + count: 1, + }); + + await User.insert( + { + firstname: '', + lastname: '', + email, + salt, + password, + email_verification_token, + roles, + }, + ncMeta + ); + } + } else if (email !== superUser.email) { // update admin email and password and migrate projects // if user already present and associated with some project