Browse Source

fix: allow env variables to recover missing super user

Signed-off-by: mertmit <mertmit99@gmail.com>
pull/2760/head
mertmit 2 years ago
parent
commit
2751836458
  1. 53
      packages/nocodb/src/lib/meta/api/userApi/initAdminFromEnv.ts

53
packages/nocodb/src/lib/meta/api/userApi/initAdminFromEnv.ts

@ -63,11 +63,10 @@ export default async function initAdminFromEnv(_ncMeta = Noco.ncMeta) {
salt salt
); );
const email_verification_token = uuidv4(); const email_verification_token = uuidv4();
const roles = 'user,super';
// if super admin not present // if super admin not present
if (await User.isFirst(ncMeta)) { if (await User.isFirst(ncMeta)) {
const roles = 'user,super';
// roles = 'owner,creator,editor' // roles = 'owner,creator,editor'
Tele.emit('evt', { Tele.emit('evt', {
evt_type: 'project:invite', evt_type: 'project:invite',
@ -97,7 +96,55 @@ export default async function initAdminFromEnv(_ncMeta = Noco.ncMeta) {
roles: 'user,super', roles: 'user,super',
}); });
if (email !== superUser.email) { 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}`
);
// 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 // update admin email and password and migrate projects
// if user already present and associated with some project // if user already present and associated with some project

Loading…
Cancel
Save