From b9cc7a83c81e84da5e38c9dade8bfbd2236d7e82 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Mon, 8 Aug 2022 16:40:32 +0100 Subject: [PATCH] Fix users not being added to group when invited via email --- .../builder/portal/manage/users/index.svelte | 1 + packages/frontend-core/src/api/user.js | 1 + packages/frontend-core/src/fetch/UserFetch.js | 6 ++++++ .../src/api/controllers/global/users.ts | 20 ++++++++++++++++++- .../worker/src/api/routes/global/users.js | 6 ------ 5 files changed, 27 insertions(+), 7 deletions(-) diff --git a/packages/builder/src/pages/builder/portal/manage/users/index.svelte b/packages/builder/src/pages/builder/portal/manage/users/index.svelte index 54604543c9..42f1c2edf0 100644 --- a/packages/builder/src/pages/builder/portal/manage/users/index.svelte +++ b/packages/builder/src/pages/builder/portal/manage/users/index.svelte @@ -111,6 +111,7 @@ email: user.email, builder: user.role === Constants.BudibaseRoles.Developer, admin: user.role === Constants.BudibaseRoles.Admin, + groups: userData.groups, })) try { const res = await users.invite(payload) diff --git a/packages/frontend-core/src/api/user.js b/packages/frontend-core/src/api/user.js index 653376aa55..accf1f430b 100644 --- a/packages/frontend-core/src/api/user.js +++ b/packages/frontend-core/src/api/user.js @@ -151,6 +151,7 @@ export const buildUserEndpoints = API => ({ userInfo: { admin: user.admin ? { global: true } : undefined, builder: user.admin || user.builder ? { global: true } : undefined, + groups: user.groups, }, })), }) diff --git a/packages/frontend-core/src/fetch/UserFetch.js b/packages/frontend-core/src/fetch/UserFetch.js index 3bd0cdbaa4..9aeadbc0f5 100644 --- a/packages/frontend-core/src/fetch/UserFetch.js +++ b/packages/frontend-core/src/fetch/UserFetch.js @@ -20,6 +20,12 @@ export default class UserFetch extends DataFetch { } } + async getDefinition() { + return { + schema: {}, + } + } + async getData() { const { cursor, query } = get(this.store) try { diff --git a/packages/worker/src/api/controllers/global/users.ts b/packages/worker/src/api/controllers/global/users.ts index 1f9af3514b..1acb229477 100644 --- a/packages/worker/src/api/controllers/global/users.ts +++ b/packages/worker/src/api/controllers/global/users.ts @@ -3,7 +3,7 @@ import { checkInviteCode } from "../../../utilities/redis" import { sendEmail } from "../../../utilities/email" import { users } from "../../../sdk" import env from "../../../environment" -import { CloudAccount, User } from "@budibase/types" +import { CloudAccount, groupUser, User, UserGroup } from "@budibase/types" import { accounts, cache, @@ -263,6 +263,24 @@ export const inviteAccept = async (ctx: any) => { const db = tenancy.getGlobalDB() const user = await db.get(saved._id) await events.user.inviteAccepted(user) + + // add user to groups if required + if (info.groups?.length) { + let groups: UserGroup[] = [] + for (const groupId of info.groups) { + try { + let group: UserGroup = await db.get(groupId) + if (group) { + groups.push(group) + } + } catch (error) { + // group was probably deleted + } + } + const groupUser: groupUser = { _id: saved._id, email: saved.email } + await groupUtils.bulkSaveGroupUsers(groups, [groupUser]) + } + return saved }) } catch (err: any) { diff --git a/packages/worker/src/api/routes/global/users.js b/packages/worker/src/api/routes/global/users.js index e0a221a795..2b442d13a0 100644 --- a/packages/worker/src/api/routes/global/users.js +++ b/packages/worker/src/api/routes/global/users.js @@ -74,12 +74,6 @@ router buildInviteValidation(), controller.invite ) - .post( - "/api/global/users/invite", - adminOnly, - buildInviteValidation(), - controller.invite - ) .post( "/api/global/users/multi/invite", adminOnly,