Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 50 additions & 3 deletions packages/nocodb/src/lib/meta/api/userApi/initAdminFromEnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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

Expand Down