From 136eec338890f1aa7158e63c22017a0dd0df47cd Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Thu, 30 Jun 2022 16:01:14 +0100 Subject: [PATCH] Updating to maintain the old worker API for deprecation purposes, but removing from frontend usage. --- .../builder/portal/manage/users/index.svelte | 2 +- packages/builder/src/stores/portal/users.js | 10 +++++----- packages/frontend-core/src/api/user.js | 17 +++++++++++++---- .../worker/src/api/controllers/global/roles.js | 13 +------------ .../worker/src/api/controllers/global/users.ts | 17 ++++++++++++++--- packages/worker/src/api/routes/global/users.js | 1 + packages/worker/src/sdk/users/users.ts | 10 ++++++++++ 7 files changed, 45 insertions(+), 25 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 bc14b7e8d2..061c686317 100644 --- a/packages/builder/src/pages/builder/portal/manage/users/index.svelte +++ b/packages/builder/src/pages/builder/portal/manage/users/index.svelte @@ -35,7 +35,7 @@ async function fetchUsers(page, search) { try { - await users.fetch({ page, search }) + await users.search({ page, search }) pageInfo.fetched($users.hasNextPage, $users.nextPage) } catch (error) { notifications.error("Error getting user list") diff --git a/packages/builder/src/stores/portal/users.js b/packages/builder/src/stores/portal/users.js index 1e612f6537..12bce413b9 100644 --- a/packages/builder/src/stores/portal/users.js +++ b/packages/builder/src/stores/portal/users.js @@ -6,8 +6,8 @@ export function createUsersStore() { const { subscribe, set } = writable({}) // opts can contain page and search params - async function fetch(opts = {}) { - const paged = await API.getUsers(opts) + async function search(opts = {}) { + const paged = await API.searchUsers(opts) set({ ...paged, ...opts, @@ -60,8 +60,8 @@ export function createUsersStore() { body.admin = { global: true } } await API.saveUser(body) - // re-fetch from first page - await fetch() + // re-search from first page + await search() } async function del(id) { @@ -75,7 +75,7 @@ export function createUsersStore() { return { subscribe, - fetch, + search, get, invite, acceptInvite, diff --git a/packages/frontend-core/src/api/user.js b/packages/frontend-core/src/api/user.js index a7a9fae9f1..b86e02c75b 100644 --- a/packages/frontend-core/src/api/user.js +++ b/packages/frontend-core/src/api/user.js @@ -1,10 +1,19 @@ export const buildUserEndpoints = API => ({ + /** + * Gets a list of users in the current tenant. + */ + getUsers: async () => { + return await API.get({ + url: "/api/global/users", + }) + }, + /** * Gets a list of users in the current tenant. * @param {string} page The page to retrieve * @param {string} search The starts with string to search username/email by. */ - getUsers: async ({ page, search } = {}) => { + searchUsers: async ({ page, search } = {}) => { const opts = {} if (page) { opts.page = page @@ -12,9 +21,9 @@ export const buildUserEndpoints = API => ({ if (search) { opts.search = search } - const params = new URLSearchParams(opts) - return await API.get({ - url: `/api/global/users?${params.toString()}`, + return await API.post({ + url: `/api/global/users/search`, + body: opts, }) }, diff --git a/packages/worker/src/api/controllers/global/roles.js b/packages/worker/src/api/controllers/global/roles.js index aacf0102f1..0859fc1a21 100644 --- a/packages/worker/src/api/controllers/global/roles.js +++ b/packages/worker/src/api/controllers/global/roles.js @@ -3,22 +3,11 @@ const { getAllApps, getProdAppID, DocumentTypes, - getGlobalUserParams, } = require("@budibase/backend-core/db") const { doInAppContext, getAppDB } = require("@budibase/backend-core/context") const { user: userCache } = require("@budibase/backend-core/cache") const { getGlobalDB } = require("@budibase/backend-core/tenancy") - -// TODO: this function needs to be removed and replaced -export const allUsers = async () => { - const db = getGlobalDB() - const response = await db.allDocs( - getGlobalUserParams(null, { - include_docs: true, - }) - ) - return response.rows.map(row => row.doc) -} +const { allUsers } = require("../../../sdk/users") exports.fetch = async ctx => { const tenantId = ctx.user.tenantId diff --git a/packages/worker/src/api/controllers/global/users.ts b/packages/worker/src/api/controllers/global/users.ts index 34a199be0f..f84a44098a 100644 --- a/packages/worker/src/api/controllers/global/users.ts +++ b/packages/worker/src/api/controllers/global/users.ts @@ -88,9 +88,8 @@ export const destroy = async (ctx: any) => { } } -// called internally by app server user fetch -export const fetch = async (ctx: any) => { - const paginated = await users.paginatedUsers(ctx.request.query) +export const search = async (ctx: any) => { + const paginated = await users.paginatedUsers(ctx.request.body) // user hashed password shouldn't ever be returned for (let user of paginated.data) { if (user) { @@ -100,6 +99,18 @@ export const fetch = async (ctx: any) => { ctx.body = paginated } +// called internally by app server user fetch +export const fetch = async (ctx: any) => { + const all = await users.allUsers() + // user hashed password shouldn't ever be returned + for (let user of all) { + if (user) { + delete user.password + } + } + ctx.body = all +} + // called internally by app server user find export const find = async (ctx: any) => { ctx.body = await users.getUser(ctx.params.id) diff --git a/packages/worker/src/api/routes/global/users.js b/packages/worker/src/api/routes/global/users.js index 10739957fc..726e61413e 100644 --- a/packages/worker/src/api/routes/global/users.js +++ b/packages/worker/src/api/routes/global/users.js @@ -46,6 +46,7 @@ router controller.save ) .get("/api/global/users", builderOrAdmin, controller.fetch) + .post("/api/global/users/search", builderOrAdmin, controller.search) .delete("/api/global/users/:id", adminOnly, controller.destroy) .get("/api/global/roles/:appId") .post( diff --git a/packages/worker/src/sdk/users/users.ts b/packages/worker/src/sdk/users/users.ts index db254badeb..35a4083df9 100644 --- a/packages/worker/src/sdk/users/users.ts +++ b/packages/worker/src/sdk/users/users.ts @@ -19,6 +19,16 @@ import { MigrationType } from "@budibase/types" const PAGE_LIMIT = 8 +export const allUsers = async () => { + const db = tenancy.getGlobalDB() + const response = await db.allDocs( + dbUtils.getGlobalUserParams(null, { + include_docs: true, + }) + ) + return response.rows.map((row: any) => row.doc) +} + export const paginatedUsers = async ({ page, search,