|
|
@ -89,62 +89,21 @@ export default async function initAdminFromEnv(_ncMeta = Noco.ncMeta) { |
|
|
|
salt, |
|
|
|
salt, |
|
|
|
); |
|
|
|
); |
|
|
|
const email_verification_token = uuidv4(); |
|
|
|
const email_verification_token = uuidv4(); |
|
|
|
const superUser = await ncMeta.metaGet2(null, null, MetaTable.USERS, { |
|
|
|
// TODO improve this
|
|
|
|
roles: 'user,super', |
|
|
|
const superUsers = await ncMeta.metaList2(null, null, MetaTable.USERS); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!superUser?.id) { |
|
|
|
for (const user of superUsers) { |
|
|
|
const existingUserWithNewEmail = await User.getByEmail(email, ncMeta); |
|
|
|
if (!user.roles?.includes('super')) continue; |
|
|
|
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
|
|
|
|
if (email !== user.email) { |
|
|
|
await User.update( |
|
|
|
|
|
|
|
existingUserWithNewEmail.id, |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
salt, |
|
|
|
|
|
|
|
email, |
|
|
|
|
|
|
|
password, |
|
|
|
|
|
|
|
email_verification_token, |
|
|
|
|
|
|
|
token_version: randomTokenString(), |
|
|
|
|
|
|
|
refresh_token: null, |
|
|
|
|
|
|
|
roles, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
ncMeta, |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
T.emit('evt', { |
|
|
|
|
|
|
|
evt_type: 'project:invite', |
|
|
|
|
|
|
|
count: 1, |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await User.insert( |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
// check user account already present with the new admin email
|
|
|
|
// check user account already present with the new admin email
|
|
|
|
const existingUserWithNewEmail = await User.getByEmail(email, ncMeta); |
|
|
|
const existingUserWithNewEmail = await User.getByEmail( |
|
|
|
|
|
|
|
email, |
|
|
|
|
|
|
|
ncMeta, |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
if (existingUserWithNewEmail?.id) { |
|
|
|
if (existingUserWithNewEmail?.id) { |
|
|
|
// get all project access belongs to the existing account
|
|
|
|
// get all project access belongs to the existing account
|
|
|
@ -161,7 +120,7 @@ export default async function initAdminFromEnv(_ncMeta = Noco.ncMeta) { |
|
|
|
for (const existingUserProject of existingUserProjects) { |
|
|
|
for (const existingUserProject of existingUserProjects) { |
|
|
|
const userProject = await ProjectUser.get( |
|
|
|
const userProject = await ProjectUser.get( |
|
|
|
existingUserProject.project_id, |
|
|
|
existingUserProject.project_id, |
|
|
|
superUser.id, |
|
|
|
user.id, |
|
|
|
ncMeta, |
|
|
|
ncMeta, |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
@ -174,7 +133,7 @@ export default async function initAdminFromEnv(_ncMeta = Noco.ncMeta) { |
|
|
|
) { |
|
|
|
) { |
|
|
|
await ProjectUser.update( |
|
|
|
await ProjectUser.update( |
|
|
|
userProject.project_id, |
|
|
|
userProject.project_id, |
|
|
|
superUser.id, |
|
|
|
user.id, |
|
|
|
existingUserProject.roles, |
|
|
|
existingUserProject.roles, |
|
|
|
ncMeta, |
|
|
|
ncMeta, |
|
|
|
); |
|
|
|
); |
|
|
@ -184,7 +143,7 @@ export default async function initAdminFromEnv(_ncMeta = Noco.ncMeta) { |
|
|
|
await ProjectUser.insert( |
|
|
|
await ProjectUser.insert( |
|
|
|
{ |
|
|
|
{ |
|
|
|
...existingUserProject, |
|
|
|
...existingUserProject, |
|
|
|
fk_user_id: superUser.id, |
|
|
|
fk_user_id: user.id, |
|
|
|
}, |
|
|
|
}, |
|
|
|
ncMeta, |
|
|
|
ncMeta, |
|
|
|
); |
|
|
|
); |
|
|
@ -219,7 +178,7 @@ export default async function initAdminFromEnv(_ncMeta = Noco.ncMeta) { |
|
|
|
|
|
|
|
|
|
|
|
// Update email and password of super admin account
|
|
|
|
// Update email and password of super admin account
|
|
|
|
await User.update( |
|
|
|
await User.update( |
|
|
|
superUser.id, |
|
|
|
user.id, |
|
|
|
{ |
|
|
|
{ |
|
|
|
salt, |
|
|
|
salt, |
|
|
|
email, |
|
|
|
email, |
|
|
@ -233,7 +192,7 @@ export default async function initAdminFromEnv(_ncMeta = Noco.ncMeta) { |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
// if email's are not different update the password and hash
|
|
|
|
// if email's are not different update the password and hash
|
|
|
|
await User.update( |
|
|
|
await User.update( |
|
|
|
superUser.id, |
|
|
|
user.id, |
|
|
|
{ |
|
|
|
{ |
|
|
|
salt, |
|
|
|
salt, |
|
|
|
email, |
|
|
|
email, |
|
|
@ -248,14 +207,14 @@ export default async function initAdminFromEnv(_ncMeta = Noco.ncMeta) { |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
const newPasswordHash = await promisify(bcrypt.hash)( |
|
|
|
const newPasswordHash = await promisify(bcrypt.hash)( |
|
|
|
process.env.NC_ADMIN_PASSWORD, |
|
|
|
process.env.NC_ADMIN_PASSWORD, |
|
|
|
superUser.salt, |
|
|
|
user.salt, |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
if (newPasswordHash !== superUser.password) { |
|
|
|
if (newPasswordHash !== user.password) { |
|
|
|
// if email's are same and passwords are different
|
|
|
|
// if email's are same and passwords are different
|
|
|
|
// then update the password and token version
|
|
|
|
// then update the password and token version
|
|
|
|
await User.update( |
|
|
|
await User.update( |
|
|
|
superUser.id, |
|
|
|
user.id, |
|
|
|
{ |
|
|
|
{ |
|
|
|
salt, |
|
|
|
salt, |
|
|
|
password, |
|
|
|
password, |
|
|
@ -268,11 +227,13 @@ export default async function initAdminFromEnv(_ncMeta = Noco.ncMeta) { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
await ncMeta.commit(); |
|
|
|
await ncMeta.commit(); |
|
|
|
} catch (e) { |
|
|
|
} catch (e) { |
|
|
|
console.log('Error occurred while updating/creating admin user'); |
|
|
|
console.log('Error occurred while updating/creating admin user'); |
|
|
|
console.log(e); |
|
|
|
|
|
|
|
await ncMeta.rollback(e); |
|
|
|
await ncMeta.rollback(e); |
|
|
|
|
|
|
|
throw e; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|