diff --git a/packages/backend-core/src/db/views.js b/packages/backend-core/src/db/views.js index b70493c52a..9e5dd714c1 100644 --- a/packages/backend-core/src/db/views.js +++ b/packages/backend-core/src/db/views.js @@ -167,7 +167,9 @@ exports.createPlatformUserView = async () => { const view = { // if using variables in a map function need to inject them before use map: `function(doc) { - emit(doc._id.toLowerCase(), doc._id) + if (doc.tenantId) { + emit(doc._id.toLowerCase(), doc._id) + } }`, } designDoc.views = { @@ -178,39 +180,6 @@ exports.createPlatformUserView = async () => { }) } -exports.queryGlobalView = async (viewName, params, db = null) => { - const CreateFuncByName = { - [ViewName.USER_BY_EMAIL]: exports.createNewUserEmailView, - [ViewName.BY_API_KEY]: exports.createApiKeyView, - [ViewName.USER_BY_BUILDERS]: exports.createUserBuildersView, - [ViewName.USER_BY_APP]: exports.createUserAppView, - } - // can pass DB in if working with something specific - if (!db) { - db = getGlobalDB() - } - try { - let response = (await db.query(`database/${viewName}`, params)).rows - response = response.map(resp => - params.include_docs ? resp.doc : resp.value - ) - if (params.arrayResponse) { - return response - } else { - return response.length <= 1 ? response[0] : response - } - } catch (err) { - if (err != null && err.name === "not_found") { - const createFunc = CreateFuncByName[viewName] - await removeDeprecated(db, viewName) - await createFunc(db) - return exports.queryView(viewName, params, db, CreateFuncByName) - } else { - throw err - } - } -} - exports.queryView = async (viewName, params, db, CreateFuncByName) => { try { let response = (await db.query(`database/${viewName}`, params)).rows diff --git a/packages/backend-core/src/tenancy/tenancy.ts b/packages/backend-core/src/tenancy/tenancy.ts index ebff4dd056..769165ef23 100644 --- a/packages/backend-core/src/tenancy/tenancy.ts +++ b/packages/backend-core/src/tenancy/tenancy.ts @@ -1,5 +1,5 @@ import { doWithDB } from "../db" -import { StaticDatabases, UNICODE_MAX, ViewName } from "../db/constants" +import { StaticDatabases, ViewName } from "../db/constants" import { baseGlobalDBName } from "./utils" import { getTenantId, @@ -8,7 +8,7 @@ import { getTenantIDFromAppID, } from "../context" import env from "../environment" -import { queryGlobalView } from "../db/views" +import { queryPlatformView } from "../db/views" const TENANT_DOC = StaticDatabases.PLATFORM_INFO.docs.tenants const PLATFORM_INFO_DB = StaticDatabases.PLATFORM_INFO.name @@ -119,22 +119,12 @@ export const lookupTenantId = async (userId: string) => { // lookup, could be email or userId, either will return a doc export const getTenantUser = async (identifier: string) => { // use the view here and allow to find anyone regardless of casing - const lcIdentifier = identifier.toLowerCase() - - return await doWithDB(StaticDatabases.PLATFORM_INFO.name, async (db: any) => { - let response = await queryGlobalView( - ViewName.PLATFORM_USERS_LOWERCASE, - { - startkey: lcIdentifier, - endkey: `${lcIdentifier}${UNICODE_MAX}`, - }, - db - ) - if (!response) { - response = [] - } - return response + // Use lowercase to ensure email login is case insensitive + const response = await queryPlatformView(ViewName.PLATFORM_USERS_LOWERCASE, { + keys: [identifier.toLowerCase()], + include_docs: true, }) + return response } export const isUserInAppTenant = (appId: string, user: any) => { diff --git a/packages/server/src/migrations/functions/platformUserEmailViewCasing.ts b/packages/server/src/migrations/functions/platformUserEmailViewCasing.ts deleted file mode 100644 index 4a39a7d754..0000000000 --- a/packages/server/src/migrations/functions/platformUserEmailViewCasing.ts +++ /dev/null @@ -1,13 +0,0 @@ -const { createPlatformUserView } = require("@budibase/backend-core/db") - -/** - * Date: - * September 2022 - * - * Description: - * Create a view in platform DB with lowercase emails for all users. - */ - -export const run = async () => { - await createPlatformUserView() -} diff --git a/packages/server/src/migrations/index.ts b/packages/server/src/migrations/index.ts index fc0edf5f2b..494740d1d9 100644 --- a/packages/server/src/migrations/index.ts +++ b/packages/server/src/migrations/index.ts @@ -9,7 +9,6 @@ import * as appUrls from "./functions/appUrls" import * as developerQuota from "./functions/developerQuota" import * as publishedAppsQuota from "./functions/publishedAppsQuota" import * as backfill from "./functions/backfill" -import * as platformUsersEmailViewCasing from "./functions/platformUserEmailViewCasing" /** * Populate the migration function and additional configuration from @@ -85,13 +84,6 @@ export const buildMigrations = () => { }) break } - case MigrationName.PLATFORM_USERS_EMAIL_CASING: { - serverMigrations.push({ - ...definition, - fn: platformUsersEmailViewCasing.run, - }) - break - } } }