Merge pull request #2769 from Budibase/fix/account-email-clash
Prevent root account users being re-created as internal budibase users
This commit is contained in:
commit
4d274f79b3
|
@ -0,0 +1 @@
|
||||||
|
module.exports = require("./src/cloud/accounts")
|
|
@ -11,6 +11,7 @@ const { sendEmail } = require("../../../utilities/email")
|
||||||
const { user: userCache } = require("@budibase/auth/cache")
|
const { user: userCache } = require("@budibase/auth/cache")
|
||||||
const { invalidateSessions } = require("@budibase/auth/sessions")
|
const { invalidateSessions } = require("@budibase/auth/sessions")
|
||||||
const CouchDB = require("../../../db")
|
const CouchDB = require("../../../db")
|
||||||
|
const accounts = require("@budibase/auth/accounts")
|
||||||
const {
|
const {
|
||||||
getGlobalDB,
|
getGlobalDB,
|
||||||
getTenantId,
|
getTenantId,
|
||||||
|
@ -49,10 +50,27 @@ async function saveUser(
|
||||||
// make sure another user isn't using the same email
|
// make sure another user isn't using the same email
|
||||||
let dbUser
|
let dbUser
|
||||||
if (email) {
|
if (email) {
|
||||||
|
// check budibase users inside the tenant
|
||||||
dbUser = await getGlobalUserByEmail(email)
|
dbUser = await getGlobalUserByEmail(email)
|
||||||
if (dbUser != null && (dbUser._id !== _id || Array.isArray(dbUser))) {
|
if (dbUser != null && (dbUser._id !== _id || Array.isArray(dbUser))) {
|
||||||
throw "Email address already in use."
|
throw "Email address already in use."
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check budibase users in other tenants
|
||||||
|
if (env.MULTI_TENANCY) {
|
||||||
|
dbUser = await getTenantUser(email)
|
||||||
|
if (dbUser != null) {
|
||||||
|
throw "Email address already in use."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// check root account users in account portal
|
||||||
|
if (!env.SELF_HOSTED) {
|
||||||
|
const account = await accounts.getAccount(email)
|
||||||
|
if (account) {
|
||||||
|
throw "Email address already in use."
|
||||||
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
dbUser = await db.get(_id)
|
dbUser = await db.get(_id)
|
||||||
}
|
}
|
||||||
|
@ -267,13 +285,22 @@ exports.find = async ctx => {
|
||||||
ctx.body = user
|
ctx.body = user
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.tenantUserLookup = async ctx => {
|
// lookup, could be email or userId, either will return a doc
|
||||||
const id = ctx.params.id
|
const getTenantUser = async identifier => {
|
||||||
// lookup, could be email or userId, either will return a doc
|
|
||||||
const db = new CouchDB(PLATFORM_INFO_DB)
|
const db = new CouchDB(PLATFORM_INFO_DB)
|
||||||
try {
|
try {
|
||||||
ctx.body = await db.get(id)
|
return await db.get(identifier)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.tenantUserLookup = async ctx => {
|
||||||
|
const id = ctx.params.id
|
||||||
|
const user = await getTenantUser(id)
|
||||||
|
if (user) {
|
||||||
|
ctx.body = user
|
||||||
|
} else {
|
||||||
ctx.throw(400, "No tenant user found.")
|
ctx.throw(400, "No tenant user found.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue