Browse Source

Merge pull request #2760 from nocodb/fix/2759-missing-super

fix: allow env variables to recover missing super user
pull/4229/head
Pranav C 2 years ago committed by GitHub
parent
commit
fd3d6fb131
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  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
);
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

Loading…
Cancel
Save