@budibase/worker 1.0.211 → 1.0.212-alpha.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.dockerignore ADDED
@@ -0,0 +1,7 @@
1
+ node_modules
2
+ npm-debug.log
3
+ Dockerfile
4
+ .dockerignore
5
+ .git
6
+ .gitignore
7
+
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@budibase/worker",
3
3
  "email": "hi@budibase.com",
4
- "version": "1.0.211",
4
+ "version": "1.0.212-alpha.10",
5
5
  "description": "Budibase background service",
6
6
  "main": "src/index.ts",
7
7
  "repository": {
@@ -34,9 +34,10 @@
34
34
  "author": "Budibase",
35
35
  "license": "GPL-3.0",
36
36
  "dependencies": {
37
- "@budibase/backend-core": "^1.0.211",
38
- "@budibase/pro": "1.0.210",
39
- "@budibase/string-templates": "^1.0.211",
37
+ "@budibase/backend-core": "^1.0.212-alpha.10",
38
+ "@budibase/pro": "1.0.212-alpha.9",
39
+ "@budibase/string-templates": "^1.0.212-alpha.10",
40
+ "@budibase/types": "^1.0.212-alpha.10",
40
41
  "@koa/router": "8.0.8",
41
42
  "@sentry/node": "6.17.7",
42
43
  "@techpass/passport-openidconnect": "0.3.2",
@@ -66,7 +67,6 @@
66
67
  "server-destroy": "1.0.1"
67
68
  },
68
69
  "devDependencies": {
69
- "@budibase/types": "^1.0.211",
70
70
  "@types/jest": "26.0.23",
71
71
  "@types/koa": "2.13.4",
72
72
  "@types/koa-router": "7.4.4",
@@ -100,5 +100,5 @@
100
100
  "./scripts/jestSetup.js"
101
101
  ]
102
102
  },
103
- "gitHead": "e5c895c869a4cf8fba86e759a6812b4c3bf7c953"
103
+ "gitHead": "d5a8da563fdf65795bf908de0359f0ea5e9a9b1d"
104
104
  }
@@ -1,7 +1,6 @@
1
1
  const {
2
2
  generateConfigID,
3
3
  getConfigParams,
4
- getGlobalUserParams,
5
4
  getScopedFullConfig,
6
5
  getAllApps,
7
6
  } = require("@budibase/backend-core/db")
@@ -20,6 +19,7 @@ const {
20
19
  bustCache,
21
20
  } = require("@budibase/backend-core/cache")
22
21
  const { events } = require("@budibase/backend-core")
22
+ const { checkAnyUserExists } = require("../../../utilities/users")
23
23
 
24
24
  const BB_TENANT_CDN = "https://tenants.cdn.budi.live"
25
25
 
@@ -405,12 +405,7 @@ exports.configChecklist = async function (ctx) {
405
405
  })
406
406
 
407
407
  // They have set up an global user
408
- const users = await db.allDocs(
409
- getGlobalUserParams(null, {
410
- include_docs: true,
411
- limit: 1,
412
- })
413
- )
408
+ const userExists = await checkAnyUserExists()
414
409
  return {
415
410
  apps: {
416
411
  checked: apps.length > 0,
@@ -423,7 +418,7 @@ exports.configChecklist = async function (ctx) {
423
418
  link: "/builder/portal/manage/email",
424
419
  },
425
420
  adminUser: {
426
- checked: users && users.rows.length >= 1,
421
+ checked: userExists,
427
422
  label: "Create your first user",
428
423
  link: "/builder/portal/manage/users",
429
424
  },
@@ -7,7 +7,7 @@ const {
7
7
  const { doInAppContext, getAppDB } = require("@budibase/backend-core/context")
8
8
  const { user: userCache } = require("@budibase/backend-core/cache")
9
9
  const { getGlobalDB } = require("@budibase/backend-core/tenancy")
10
- const { users } = require("../../../sdk")
10
+ const { allUsers } = require("../../../sdk/users")
11
11
 
12
12
  exports.fetch = async ctx => {
13
13
  const tenantId = ctx.user.tenantId
@@ -49,10 +49,10 @@ exports.find = async ctx => {
49
49
  exports.removeAppRole = async ctx => {
50
50
  const { appId } = ctx.params
51
51
  const db = getGlobalDB()
52
- const allUsers = await users.allUsers(ctx)
52
+ const users = await allUsers(ctx)
53
53
  const bulk = []
54
54
  const cacheInvalidations = []
55
- for (let user of allUsers) {
55
+ for (let user of users) {
56
56
  if (user.roles[appId]) {
57
57
  cacheInvalidations.push(userCache.invalidateUser(user._id))
58
58
  delete user.roles[appId]
@@ -8,16 +8,15 @@ import {
8
8
  events,
9
9
  errors,
10
10
  accounts,
11
- db as dbUtils,
12
11
  users as usersCore,
13
12
  tenancy,
14
13
  cache,
15
14
  } from "@budibase/backend-core"
15
+ import { checkAnyUserExists } from "../../../utilities/users"
16
16
 
17
17
  export const save = async (ctx: any) => {
18
18
  try {
19
- const user = await users.save(ctx.request.body)
20
- ctx.body = user
19
+ ctx.body = await users.save(ctx.request.body)
21
20
  } catch (err: any) {
22
21
  ctx.throw(err.status || 400, err)
23
22
  }
@@ -39,15 +38,8 @@ export const adminUser = async (ctx: any) => {
39
38
  ctx.throw(403, "Organisation already exists.")
40
39
  }
41
40
 
42
- const response = await tenancy.doWithGlobalDB(tenantId, async (db: any) => {
43
- return db.allDocs(
44
- dbUtils.getGlobalUserParams(null, {
45
- include_docs: true,
46
- })
47
- )
48
- })
49
-
50
- if (response.rows.some((row: any) => row.doc.admin)) {
41
+ const userExists = await checkAnyUserExists()
42
+ if (userExists) {
51
43
  ctx.throw(
52
44
  403,
53
45
  "You cannot initialise once an global user has been created."
@@ -96,6 +88,17 @@ export const destroy = async (ctx: any) => {
96
88
  }
97
89
  }
98
90
 
91
+ export const search = async (ctx: any) => {
92
+ const paginated = await users.paginatedUsers(ctx.request.body)
93
+ // user hashed password shouldn't ever be returned
94
+ for (let user of paginated.data) {
95
+ if (user) {
96
+ delete user.password
97
+ }
98
+ }
99
+ ctx.body = paginated
100
+ }
101
+
99
102
  // called internally by app server user fetch
100
103
  export const fetch = async (ctx: any) => {
101
104
  const all = await users.allUsers()
@@ -46,6 +46,7 @@ router
46
46
  controller.save
47
47
  )
48
48
  .get("/api/global/users", builderOrAdmin, controller.fetch)
49
+ .post("/api/global/users/search", builderOrAdmin, controller.search)
49
50
  .delete("/api/global/users/:id", adminOnly, controller.destroy)
50
51
  .get("/api/global/roles/:appId")
51
52
  .post(
@@ -17,9 +17,8 @@ import {
17
17
  } from "@budibase/backend-core"
18
18
  import { MigrationType } from "@budibase/types"
19
19
 
20
- /**
21
- * Retrieves all users from the current tenancy.
22
- */
20
+ const PAGE_LIMIT = 8
21
+
23
22
  export const allUsers = async () => {
24
23
  const db = tenancy.getGlobalDB()
25
24
  const response = await db.allDocs(
@@ -30,6 +29,37 @@ export const allUsers = async () => {
30
29
  return response.rows.map((row: any) => row.doc)
31
30
  }
32
31
 
32
+ export const paginatedUsers = async ({
33
+ page,
34
+ search,
35
+ }: { page?: string; search?: string } = {}) => {
36
+ const db = tenancy.getGlobalDB()
37
+ // get one extra document, to have the next page
38
+ const opts: any = {
39
+ include_docs: true,
40
+ limit: PAGE_LIMIT + 1,
41
+ }
42
+ // add a startkey if the page was specified (anchor)
43
+ if (page) {
44
+ opts.startkey = page
45
+ }
46
+ // property specifies what to use for the page/anchor
47
+ let userList, property
48
+ // no search, query allDocs
49
+ if (!search) {
50
+ const response = await db.allDocs(dbUtils.getGlobalUserParams(null, opts))
51
+ userList = response.rows.map((row: any) => row.doc)
52
+ property = "_id"
53
+ } else {
54
+ userList = await usersCore.searchGlobalUsersByEmail(search, opts)
55
+ property = "email"
56
+ }
57
+ return dbUtils.pagination(userList, PAGE_LIMIT, {
58
+ paginate: true,
59
+ property,
60
+ })
61
+ }
62
+
33
63
  /**
34
64
  * Gets a user by ID from the global database, based on the current tenancy.
35
65
  */
@@ -0,0 +1,17 @@
1
+ const { getGlobalDB } = require("@budibase/backend-core/tenancy")
2
+ const { getGlobalUserParams } = require("@budibase/backend-core/db")
3
+
4
+ exports.checkAnyUserExists = async () => {
5
+ try {
6
+ const db = getGlobalDB()
7
+ const users = await db.allDocs(
8
+ getGlobalUserParams(null, {
9
+ include_docs: true,
10
+ limit: 1,
11
+ })
12
+ )
13
+ return users && users.rows.length >= 1
14
+ } catch (err) {
15
+ throw new Error("Unable to retrieve user list")
16
+ }
17
+ }