1
0
Fork 0
mirror of synced 2024-09-28 23:31:43 +12:00

Updating to maintain the old worker API for deprecation purposes, but removing from frontend usage.

This commit is contained in:
mike12345567 2022-06-30 16:01:14 +01:00
parent 2733f48492
commit 136eec3388
7 changed files with 45 additions and 25 deletions

View file

@ -35,7 +35,7 @@
async function fetchUsers(page, search) { async function fetchUsers(page, search) {
try { try {
await users.fetch({ page, search }) await users.search({ page, search })
pageInfo.fetched($users.hasNextPage, $users.nextPage) pageInfo.fetched($users.hasNextPage, $users.nextPage)
} catch (error) { } catch (error) {
notifications.error("Error getting user list") notifications.error("Error getting user list")

View file

@ -6,8 +6,8 @@ export function createUsersStore() {
const { subscribe, set } = writable({}) const { subscribe, set } = writable({})
// opts can contain page and search params // opts can contain page and search params
async function fetch(opts = {}) { async function search(opts = {}) {
const paged = await API.getUsers(opts) const paged = await API.searchUsers(opts)
set({ set({
...paged, ...paged,
...opts, ...opts,
@ -60,8 +60,8 @@ export function createUsersStore() {
body.admin = { global: true } body.admin = { global: true }
} }
await API.saveUser(body) await API.saveUser(body)
// re-fetch from first page // re-search from first page
await fetch() await search()
} }
async function del(id) { async function del(id) {
@ -75,7 +75,7 @@ export function createUsersStore() {
return { return {
subscribe, subscribe,
fetch, search,
get, get,
invite, invite,
acceptInvite, acceptInvite,

View file

@ -1,10 +1,19 @@
export const buildUserEndpoints = API => ({ 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. * Gets a list of users in the current tenant.
* @param {string} page The page to retrieve * @param {string} page The page to retrieve
* @param {string} search The starts with string to search username/email by. * @param {string} search The starts with string to search username/email by.
*/ */
getUsers: async ({ page, search } = {}) => { searchUsers: async ({ page, search } = {}) => {
const opts = {} const opts = {}
if (page) { if (page) {
opts.page = page opts.page = page
@ -12,9 +21,9 @@ export const buildUserEndpoints = API => ({
if (search) { if (search) {
opts.search = search opts.search = search
} }
const params = new URLSearchParams(opts) return await API.post({
return await API.get({ url: `/api/global/users/search`,
url: `/api/global/users?${params.toString()}`, body: opts,
}) })
}, },

View file

@ -3,22 +3,11 @@ const {
getAllApps, getAllApps,
getProdAppID, getProdAppID,
DocumentTypes, DocumentTypes,
getGlobalUserParams,
} = require("@budibase/backend-core/db") } = require("@budibase/backend-core/db")
const { doInAppContext, getAppDB } = require("@budibase/backend-core/context") const { doInAppContext, getAppDB } = require("@budibase/backend-core/context")
const { user: userCache } = require("@budibase/backend-core/cache") const { user: userCache } = require("@budibase/backend-core/cache")
const { getGlobalDB } = require("@budibase/backend-core/tenancy") const { getGlobalDB } = require("@budibase/backend-core/tenancy")
const { allUsers } = require("../../../sdk/users")
// 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)
}
exports.fetch = async ctx => { exports.fetch = async ctx => {
const tenantId = ctx.user.tenantId const tenantId = ctx.user.tenantId

View file

@ -88,9 +88,8 @@ export const destroy = async (ctx: any) => {
} }
} }
// called internally by app server user fetch export const search = async (ctx: any) => {
export const fetch = async (ctx: any) => { const paginated = await users.paginatedUsers(ctx.request.body)
const paginated = await users.paginatedUsers(ctx.request.query)
// user hashed password shouldn't ever be returned // user hashed password shouldn't ever be returned
for (let user of paginated.data) { for (let user of paginated.data) {
if (user) { if (user) {
@ -100,6 +99,18 @@ export const fetch = async (ctx: any) => {
ctx.body = paginated 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 // called internally by app server user find
export const find = async (ctx: any) => { export const find = async (ctx: any) => {
ctx.body = await users.getUser(ctx.params.id) ctx.body = await users.getUser(ctx.params.id)

View file

@ -46,6 +46,7 @@ router
controller.save controller.save
) )
.get("/api/global/users", builderOrAdmin, controller.fetch) .get("/api/global/users", builderOrAdmin, controller.fetch)
.post("/api/global/users/search", builderOrAdmin, controller.search)
.delete("/api/global/users/:id", adminOnly, controller.destroy) .delete("/api/global/users/:id", adminOnly, controller.destroy)
.get("/api/global/roles/:appId") .get("/api/global/roles/:appId")
.post( .post(

View file

@ -19,6 +19,16 @@ import { MigrationType } from "@budibase/types"
const PAGE_LIMIT = 8 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 ({ export const paginatedUsers = async ({
page, page,
search, search,