Skip to content

Commit

Permalink
Merge branch 'master' into update-pro-ref
Browse files Browse the repository at this point in the history
  • Loading branch information
adrinr authored Oct 20, 2023
2 parents 8a939dd + de7dcce commit 13a8b20
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 16 deletions.
10 changes: 5 additions & 5 deletions packages/backend-core/src/users/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,14 @@ export class UserDB {
}
}

static async getUsersByAppAccess(appId?: string) {
const opts: any = {
static async getUsersByAppAccess(opts: { appId?: string; limit?: number }) {
const params: any = {
include_docs: true,
limit: 50,
limit: opts.limit || 50,
}
let response: User[] = await usersCore.searchGlobalUsersByAppAccess(
appId,
opts
opts.appId,
params
)
return response
}
Expand Down
9 changes: 6 additions & 3 deletions packages/backend-core/src/users/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
SearchUsersRequest,
User,
ContextUser,
DatabaseQueryOpts,
} from "@budibase/types"
import { getGlobalDB } from "../context"
import * as context from "../context"
Expand Down Expand Up @@ -241,12 +242,14 @@ export const paginatedUsers = async ({
bookmark,
query,
appId,
limit,
}: SearchUsersRequest = {}) => {
const db = getGlobalDB()
const pageLimit = limit ? limit + 1 : PAGE_LIMIT + 1
// get one extra document, to have the next page
const opts: any = {
const opts: DatabaseQueryOpts = {
include_docs: true,
limit: PAGE_LIMIT + 1,
limit: pageLimit,
}
// add a startkey if the page was specified (anchor)
if (bookmark) {
Expand All @@ -269,7 +272,7 @@ export const paginatedUsers = async ({
const response = await db.allDocs(getGlobalUserParams(null, opts))
userList = response.rows.map((row: any) => row.doc)
}
return pagination(userList, PAGE_LIMIT, {
return pagination(userList, pageLimit, {
paginate: true,
property,
getKey,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,9 @@
query: {
appId: query || !filterByAppAccess ? null : prodAppId,
email: query,
paginated: query || !filterByAppAccess ? null : false,
},
limit: 50,
paginate: query || !filterByAppAccess ? null : false,
})
await usersFetch.refresh()
Expand Down
1 change: 1 addition & 0 deletions packages/types/src/api/web/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export interface SearchUsersRequest {
bookmark?: string
query?: SearchQuery
appId?: string
limit?: number
paginate?: boolean
}

Expand Down
5 changes: 4 additions & 1 deletion packages/worker/src/api/controllers/global/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,10 @@ export const destroy = async (ctx: any) => {

export const getAppUsers = async (ctx: Ctx<SearchUsersRequest>) => {
const body = ctx.request.body
const users = await userSdk.db.getUsersByAppAccess(body?.appId)
const users = await userSdk.db.getUsersByAppAccess({
appId: body.appId,
limit: body.limit,
})

ctx.body = { data: users }
}
Expand Down
6 changes: 5 additions & 1 deletion packages/worker/src/api/routes/global/tests/users.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -569,9 +569,13 @@ describe("/api/global/users", () => {
{
query: { equal: { firstName: user.firstName } },
},
501
{ status: 501 }
)
})

it("should throw an error if public query performed", async () => {
await config.api.users.searchUsers({}, { status: 403, noHeaders: true })
})
})

describe("DELETE /api/global/users/:userId", () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/worker/src/api/routes/global/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ router
)

.get("/api/global/users", auth.builderOrAdmin, controller.fetch)
.post("/api/global/users/search", auth.builderOrAdmin, controller.search)
// search can be used by any user now, to retrieve users for user column
.post("/api/global/users/search", controller.search)
.delete("/api/global/users/:id", auth.adminOnly, controller.destroy)
.get(
"/api/global/users/count/:appId",
Expand Down
14 changes: 10 additions & 4 deletions packages/worker/src/tests/api/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,19 @@ export class UserAPI extends TestAPI {
.expect(status ? status : 200)
}

searchUsers = ({ query }: { query?: SearchQuery }, status = 200) => {
return this.request
searchUsers = (
{ query }: { query?: SearchQuery },
opts?: { status?: number; noHeaders?: boolean }
) => {
const req = this.request
.post("/api/global/users/search")
.set(this.config.defaultHeaders())
.send({ query })
.expect("Content-Type", /json/)
.expect(status ? status : 200)
.expect(opts?.status ? opts.status : 200)
if (!opts?.noHeaders) {
req.set(this.config.defaultHeaders())
}
return req
}

getUser = (userId: string, opts?: TestAPIOpts) => {
Expand Down

0 comments on commit 13a8b20

Please sign in to comment.