From 6d4e3082e355d0853f2deaf6b1487e92f1125358 Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Sun, 24 Sep 2023 22:07:40 +0100 Subject: [PATCH] type tidy up and lint --- packages/backend-core/src/platform/users.ts | 16 ++++++-- packages/backend-core/src/users/db.ts | 7 +++- .../core/utilities/structures/accounts.ts | 37 +++++++++++++++++-- packages/types/src/api/web/user.ts | 1 + .../types/src/documents/account/account.ts | 6 ++- .../types/src/documents/platform/users.ts | 5 ++- .../src/api/controllers/global/users.ts | 2 - 7 files changed, 61 insertions(+), 13 deletions(-) diff --git a/packages/backend-core/src/platform/users.ts b/packages/backend-core/src/platform/users.ts index 684dd40052..6f030afb7c 100644 --- a/packages/backend-core/src/platform/users.ts +++ b/packages/backend-core/src/platform/users.ts @@ -4,7 +4,8 @@ import env from "../environment" import { PlatformUser, PlatformUserByEmail, - PlatformUserById, PlatformUserBySsoId, + PlatformUserById, + PlatformUserBySsoId, User, } from "@budibase/types" @@ -49,7 +50,7 @@ function newUserSsoIdDoc( ssoId: string, email: string, userId: string, - tenantId: string, + tenantId: string ): PlatformUserBySsoId { return { _id: ssoId, @@ -78,14 +79,21 @@ async function addUserDoc(emailOrId: string, newDocFn: () => PlatformUser) { } } -export async function addUser(tenantId: string, userId: string, email: string, ssoId?: string) { +export async function addUser( + tenantId: string, + userId: string, + email: string, + ssoId?: string +) { const promises = [ addUserDoc(userId, () => newUserIdDoc(userId, tenantId)), addUserDoc(email, () => newUserEmailDoc(userId, email, tenantId)), ] if (ssoId) { - promises.push(addUserDoc(ssoId, () => newUserSsoIdDoc(ssoId, email, userId, tenantId))) + promises.push( + addUserDoc(ssoId, () => newUserSsoIdDoc(ssoId, email, userId, tenantId)) + ) } await Promise.all(promises) diff --git a/packages/backend-core/src/users/db.ts b/packages/backend-core/src/users/db.ts index b39fbb5d61..1d02bebc32 100644 --- a/packages/backend-core/src/users/db.ts +++ b/packages/backend-core/src/users/db.ts @@ -278,7 +278,12 @@ export class UserDB { builtUser._rev = response.rev await eventHelpers.handleSaveEvents(builtUser, dbUser) - await platform.users.addUser(tenantId, builtUser._id!, builtUser.email, builtUser.ssoId) + await platform.users.addUser( + tenantId, + builtUser._id!, + builtUser.email, + builtUser.ssoId + ) await cache.user.invalidateUser(response.id) await Promise.all(groupPromises) diff --git a/packages/backend-core/tests/core/utilities/structures/accounts.ts b/packages/backend-core/tests/core/utilities/structures/accounts.ts index 67e4411ea3..515f94db1e 100644 --- a/packages/backend-core/tests/core/utilities/structures/accounts.ts +++ b/packages/backend-core/tests/core/utilities/structures/accounts.ts @@ -1,4 +1,4 @@ -import { generator, uuid, quotas } from "." +import { generator, quotas, uuid } from "." import { generateGlobalUserID } from "../../../../src/docIds" import { Account, @@ -6,10 +6,11 @@ import { AccountSSOProviderType, AuthType, CloudAccount, - Hosting, - SSOAccount, CreateAccount, CreatePassswordAccount, + CreateVerifiableSSOAccount, + Hosting, + SSOAccount, } from "@budibase/types" import sample from "lodash/sample" @@ -68,6 +69,23 @@ export function ssoAccount(account: Account = cloudAccount()): SSOAccount { } } +export function verifiableSsoAccount( + account: Account = cloudAccount() +): SSOAccount { + return { + ...account, + authType: AuthType.SSO, + oauth2: { + accessToken: generator.string(), + refreshToken: generator.string(), + }, + pictureUrl: generator.url(), + provider: AccountSSOProvider.MICROSOFT, + providerType: AccountSSOProviderType.MICROSOFT, + thirdPartyProfile: { id: "abc123" }, + } +} + export const cloudCreateAccount: CreatePassswordAccount = { email: "cloud@budibase.com", tenantId: "cloud", @@ -91,6 +109,19 @@ export const cloudSSOCreateAccount: CreateAccount = { profession: "Software Engineer", } +export const cloudVerifiableSSOCreateAccount: CreateVerifiableSSOAccount = { + email: "cloud-sso@budibase.com", + tenantId: "cloud-sso", + hosting: Hosting.CLOUD, + authType: AuthType.SSO, + tenantName: "cloudsso", + name: "Budi Armstrong", + size: "10+", + profession: "Software Engineer", + provider: AccountSSOProvider.MICROSOFT, + thirdPartyProfile: { id: "abc123" }, +} + export const selfCreateAccount: CreatePassswordAccount = { email: "self@budibase.com", tenantId: "self", diff --git a/packages/types/src/api/web/user.ts b/packages/types/src/api/web/user.ts index 619362805a..85e2d89ad1 100644 --- a/packages/types/src/api/web/user.ts +++ b/packages/types/src/api/web/user.ts @@ -61,6 +61,7 @@ export interface CreateAdminUserRequest { email: string password: string tenantId: string + ssoId?: string } export interface CreateAdminUserResponse { diff --git a/packages/types/src/documents/account/account.ts b/packages/types/src/documents/account/account.ts index 035f95c4f2..2f74b9e7b3 100644 --- a/packages/types/src/documents/account/account.ts +++ b/packages/types/src/documents/account/account.ts @@ -94,9 +94,11 @@ export enum AccountSSOProvider { MICROSOFT = "microsoft", } -const verifiableSSOProviders: AccountSSOProvider[] = [AccountSSOProvider.MICROSOFT] +const verifiableSSOProviders: AccountSSOProvider[] = [ + AccountSSOProvider.MICROSOFT, +] export function isVerifiableSSOProvider(provider: AccountSSOProvider): boolean { - return verifiableSSOProviders.includes(provider); + return verifiableSSOProviders.includes(provider) } export interface AccountSSO { diff --git a/packages/types/src/documents/platform/users.ts b/packages/types/src/documents/platform/users.ts index 42754b5e5a..8f24329502 100644 --- a/packages/types/src/documents/platform/users.ts +++ b/packages/types/src/documents/platform/users.ts @@ -24,4 +24,7 @@ export interface PlatformUserBySsoId extends Document { email: string } -export type PlatformUser = PlatformUserByEmail | PlatformUserById | PlatformUserBySsoId +export type PlatformUser = + | PlatformUserByEmail + | PlatformUserById + | PlatformUserBySsoId diff --git a/packages/worker/src/api/controllers/global/users.ts b/packages/worker/src/api/controllers/global/users.ts index 8968ebdcd0..822a16d33e 100644 --- a/packages/worker/src/api/controllers/global/users.ts +++ b/packages/worker/src/api/controllers/global/users.ts @@ -95,7 +95,6 @@ const parseBooleanParam = (param: any) => { export const adminUser = async ( ctx: Ctx ) => { - // @ts-ignore const { email, password, tenantId, ssoId } = ctx.request.body if (await platform.tenants.exists(tenantId)) { @@ -137,7 +136,6 @@ export const adminUser = async ( global: true, }, tenantId, - // @ts-ignore ssoId, } try {