@budibase/worker 2.1.30 → 2.1.32-alpha.0

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.
Files changed (182) hide show
  1. package/__mocks__/aws-sdk.ts +15 -0
  2. package/__mocks__/oauth.ts +57 -0
  3. package/coverage/clover.xml +2160 -0
  4. package/coverage/coverage-final.json +78 -0
  5. package/coverage/lcov-report/base.css +224 -0
  6. package/coverage/lcov-report/block-navigation.js +87 -0
  7. package/coverage/lcov-report/favicon.png +0 -0
  8. package/coverage/lcov-report/index.html +431 -0
  9. package/coverage/lcov-report/prettify.css +1 -0
  10. package/coverage/lcov-report/prettify.js +2 -0
  11. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  12. package/coverage/lcov-report/sorter.js +196 -0
  13. package/coverage/lcov-report/src/api/controllers/global/auth.ts.html +913 -0
  14. package/coverage/lcov-report/src/api/controllers/global/configs.js.html +1390 -0
  15. package/coverage/lcov-report/src/api/controllers/global/email.js.html +193 -0
  16. package/coverage/lcov-report/src/api/controllers/global/index.html +236 -0
  17. package/coverage/lcov-report/src/api/controllers/global/license.ts.html +187 -0
  18. package/coverage/lcov-report/src/api/controllers/global/roles.js.html +289 -0
  19. package/coverage/lcov-report/src/api/controllers/global/self.ts.html +577 -0
  20. package/coverage/lcov-report/src/api/controllers/global/templates.ts.html +316 -0
  21. package/coverage/lcov-report/src/api/controllers/global/users.ts.html +796 -0
  22. package/coverage/lcov-report/src/api/controllers/global/workspaces.js.html +253 -0
  23. package/coverage/lcov-report/src/api/controllers/system/accounts.ts.html +148 -0
  24. package/coverage/lcov-report/src/api/controllers/system/environment.ts.html +124 -0
  25. package/coverage/lcov-report/src/api/controllers/system/index.html +191 -0
  26. package/coverage/lcov-report/src/api/controllers/system/migrations.ts.html +124 -0
  27. package/coverage/lcov-report/src/api/controllers/system/restore.ts.html +124 -0
  28. package/coverage/lcov-report/src/api/controllers/system/status.ts.html +133 -0
  29. package/coverage/lcov-report/src/api/controllers/system/tenants.ts.html +154 -0
  30. package/coverage/lcov-report/src/api/index.html +116 -0
  31. package/coverage/lcov-report/src/api/index.ts.html +583 -0
  32. package/coverage/lcov-report/src/api/routes/global/auth.js.html +349 -0
  33. package/coverage/lcov-report/src/api/routes/global/configs.js.html +460 -0
  34. package/coverage/lcov-report/src/api/routes/global/email.js.html +196 -0
  35. package/coverage/lcov-report/src/api/routes/global/index.html +236 -0
  36. package/coverage/lcov-report/src/api/routes/global/license.ts.html +124 -0
  37. package/coverage/lcov-report/src/api/routes/global/roles.js.html +121 -0
  38. package/coverage/lcov-report/src/api/routes/global/self.ts.html +139 -0
  39. package/coverage/lcov-report/src/api/routes/global/templates.ts.html +196 -0
  40. package/coverage/lcov-report/src/api/routes/global/users.js.html +403 -0
  41. package/coverage/lcov-report/src/api/routes/global/workspaces.js.html +199 -0
  42. package/coverage/lcov-report/src/api/routes/index.html +116 -0
  43. package/coverage/lcov-report/src/api/routes/index.ts.html +193 -0
  44. package/coverage/lcov-report/src/api/routes/system/accounts.ts.html +142 -0
  45. package/coverage/lcov-report/src/api/routes/system/environment.ts.html +109 -0
  46. package/coverage/lcov-report/src/api/routes/system/index.html +191 -0
  47. package/coverage/lcov-report/src/api/routes/system/migrations.ts.html +142 -0
  48. package/coverage/lcov-report/src/api/routes/system/restore.ts.html +109 -0
  49. package/coverage/lcov-report/src/api/routes/system/status.ts.html +109 -0
  50. package/coverage/lcov-report/src/api/routes/system/tenants.ts.html +124 -0
  51. package/coverage/lcov-report/src/api/routes/validation/index.html +131 -0
  52. package/coverage/lcov-report/src/api/routes/validation/index.ts.html +88 -0
  53. package/coverage/lcov-report/src/api/routes/validation/users.ts.html +235 -0
  54. package/coverage/lcov-report/src/constants/index.html +116 -0
  55. package/coverage/lcov-report/src/constants/index.ts.html +613 -0
  56. package/coverage/lcov-report/src/constants/templates/index.html +116 -0
  57. package/coverage/lcov-report/src/constants/templates/index.js.html +304 -0
  58. package/coverage/lcov-report/src/db/index.html +116 -0
  59. package/coverage/lcov-report/src/db/index.ts.html +115 -0
  60. package/coverage/lcov-report/src/environment.ts.html +364 -0
  61. package/coverage/lcov-report/src/index.html +131 -0
  62. package/coverage/lcov-report/src/index.ts.html +397 -0
  63. package/coverage/lcov-report/src/middleware/cloudRestricted.js.html +136 -0
  64. package/coverage/lcov-report/src/middleware/index.html +116 -0
  65. package/coverage/lcov-report/src/migrations/functions/globalInfoSyncUsers.ts.html +145 -0
  66. package/coverage/lcov-report/src/migrations/functions/index.html +116 -0
  67. package/coverage/lcov-report/src/migrations/index.html +116 -0
  68. package/coverage/lcov-report/src/migrations/index.ts.html +271 -0
  69. package/coverage/lcov-report/src/sdk/accounts/accounts.ts.html +262 -0
  70. package/coverage/lcov-report/src/sdk/accounts/index.html +131 -0
  71. package/coverage/lcov-report/src/sdk/accounts/index.ts.html +88 -0
  72. package/coverage/lcov-report/src/sdk/index.html +116 -0
  73. package/coverage/lcov-report/src/sdk/index.ts.html +106 -0
  74. package/coverage/lcov-report/src/sdk/users/events.ts.html +577 -0
  75. package/coverage/lcov-report/src/sdk/users/index.html +146 -0
  76. package/coverage/lcov-report/src/sdk/users/index.ts.html +88 -0
  77. package/coverage/lcov-report/src/sdk/users/users.ts.html +1903 -0
  78. package/coverage/lcov-report/src/tests/TestConfiguration.ts.html +1051 -0
  79. package/coverage/lcov-report/src/tests/api/accounts.ts.html +160 -0
  80. package/coverage/lcov-report/src/tests/api/auth.ts.html +220 -0
  81. package/coverage/lcov-report/src/tests/api/base.ts.html +133 -0
  82. package/coverage/lcov-report/src/tests/api/configs.ts.html +226 -0
  83. package/coverage/lcov-report/src/tests/api/email.ts.html +148 -0
  84. package/coverage/lcov-report/src/tests/api/environment.ts.html +130 -0
  85. package/coverage/lcov-report/src/tests/api/index.html +296 -0
  86. package/coverage/lcov-report/src/tests/api/index.ts.html +205 -0
  87. package/coverage/lcov-report/src/tests/api/migrations.ts.html +151 -0
  88. package/coverage/lcov-report/src/tests/api/restore.ts.html +127 -0
  89. package/coverage/lcov-report/src/tests/api/self.ts.html +163 -0
  90. package/coverage/lcov-report/src/tests/api/status.ts.html +121 -0
  91. package/coverage/lcov-report/src/tests/api/tenants.ts.html +130 -0
  92. package/coverage/lcov-report/src/tests/api/users.ts.html +511 -0
  93. package/coverage/lcov-report/src/tests/controllers.js.html +106 -0
  94. package/coverage/lcov-report/src/tests/index.html +146 -0
  95. package/coverage/lcov-report/src/tests/index.ts.html +139 -0
  96. package/coverage/lcov-report/src/tests/mocks/email.js.html +115 -0
  97. package/coverage/lcov-report/src/tests/mocks/index.html +131 -0
  98. package/coverage/lcov-report/src/tests/mocks/index.ts.html +106 -0
  99. package/coverage/lcov-report/src/tests/structures/configs.js.html +313 -0
  100. package/coverage/lcov-report/src/tests/structures/groups.ts.html +118 -0
  101. package/coverage/lcov-report/src/tests/structures/index.html +161 -0
  102. package/coverage/lcov-report/src/tests/structures/index.ts.html +151 -0
  103. package/coverage/lcov-report/src/tests/structures/users.ts.html +196 -0
  104. package/coverage/lcov-report/src/utilities/appService.js.html +184 -0
  105. package/coverage/lcov-report/src/utilities/email.ts.html +850 -0
  106. package/coverage/lcov-report/src/utilities/fileSystem.js.html +100 -0
  107. package/coverage/lcov-report/src/utilities/index.html +206 -0
  108. package/coverage/lcov-report/src/utilities/index.js.html +112 -0
  109. package/coverage/lcov-report/src/utilities/redis.ts.html +424 -0
  110. package/coverage/lcov-report/src/utilities/templates.ts.html +232 -0
  111. package/coverage/lcov-report/src/utilities/users.js.html +136 -0
  112. package/coverage/lcov.info +3986 -0
  113. package/jest.config.ts +29 -0
  114. package/package.json +13 -23
  115. package/scripts/localdomain.js +33 -7
  116. package/src/api/controllers/global/auth.ts +35 -31
  117. package/src/api/controllers/global/configs.js +17 -17
  118. package/src/api/controllers/global/self.ts +2 -2
  119. package/src/api/controllers/global/users.ts +3 -1
  120. package/src/api/controllers/system/{environment.js → environment.ts} +3 -2
  121. package/src/api/controllers/system/{status.js → status.ts} +4 -3
  122. package/src/api/controllers/system/tenants.ts +8 -51
  123. package/src/api/index.ts +29 -6
  124. package/src/api/routes/global/auth.js +22 -39
  125. package/src/api/routes/global/configs.js +9 -9
  126. package/src/api/routes/global/templates.ts +2 -2
  127. package/src/api/routes/global/tests/auth.spec.ts +134 -64
  128. package/src/api/routes/global/tests/configs.spec.ts +34 -35
  129. package/src/api/routes/global/tests/email.spec.ts +4 -3
  130. package/src/api/routes/global/tests/license.spec.ts +31 -0
  131. package/src/api/routes/global/tests/realEmail.spec.ts +2 -3
  132. package/src/api/routes/global/tests/roles.spec.ts +27 -0
  133. package/src/api/routes/global/tests/self.spec.ts +3 -4
  134. package/src/api/routes/global/tests/templates.spec.ts +35 -0
  135. package/src/api/routes/global/tests/users.spec.ts +51 -43
  136. package/src/api/routes/global/tests/workspaces.spec.ts +29 -0
  137. package/src/api/routes/system/environment.ts +8 -0
  138. package/src/api/routes/system/status.ts +8 -0
  139. package/src/api/routes/system/tenants.ts +13 -0
  140. package/src/api/routes/system/tests/accounts.spec.ts +5 -6
  141. package/src/api/routes/system/tests/environment.spec.ts +29 -0
  142. package/src/api/routes/system/tests/migrations.spec.ts +63 -0
  143. package/src/api/routes/system/tests/restore.spec.ts +36 -0
  144. package/src/api/routes/system/tests/status.spec.ts +48 -0
  145. package/src/api/routes/system/tests/tenants.spec.ts +61 -0
  146. package/src/constants/{index.js → index.ts} +57 -64
  147. package/src/constants/templates/index.js +2 -2
  148. package/src/db/index.ts +10 -0
  149. package/src/index.ts +1 -1
  150. package/src/middleware/cloudRestricted.js +2 -2
  151. package/src/middleware/tests/tenancy.spec.ts +73 -0
  152. package/src/tests/TestConfiguration.ts +98 -49
  153. package/src/tests/api/accounts.ts +5 -8
  154. package/src/tests/api/auth.ts +3 -6
  155. package/src/tests/api/base.ts +16 -0
  156. package/src/tests/api/configs.ts +15 -8
  157. package/src/tests/api/email.ts +3 -6
  158. package/src/tests/api/environment.ts +15 -0
  159. package/src/tests/api/index.ts +15 -0
  160. package/src/tests/api/migrations.ts +22 -0
  161. package/src/tests/api/restore.ts +14 -0
  162. package/src/tests/api/self.ts +11 -6
  163. package/src/tests/api/status.ts +12 -0
  164. package/src/tests/api/tenants.ts +15 -0
  165. package/src/tests/api/users.ts +38 -6
  166. package/src/tests/index.ts +5 -1
  167. package/{scripts/jestSetup.js → src/tests/jestSetup.ts} +12 -3
  168. package/src/tests/structures/configs.js +6 -6
  169. package/src/tests/structures/index.ts +4 -2
  170. package/src/tests/structures/users.ts +1 -0
  171. package/src/utilities/appService.js +3 -3
  172. package/src/utilities/{email.js → email.ts} +78 -45
  173. package/src/utilities/{redis.js → redis.ts} +29 -24
  174. package/src/utilities/templates.ts +49 -0
  175. package/tsconfig.build.json +2 -2
  176. package/tsconfig.json +3 -1
  177. package/src/api/routes/system/environment.js +0 -8
  178. package/src/api/routes/system/status.js +0 -8
  179. package/src/api/routes/system/tenants.js +0 -12
  180. package/src/db/index.js +0 -11
  181. package/src/tests/structures/accounts.ts +0 -24
  182. package/src/utilities/templates.js +0 -49
package/jest.config.ts ADDED
@@ -0,0 +1,29 @@
1
+ import { Config } from "@jest/types"
2
+ import * as fs from "fs"
3
+
4
+ const config: Config.InitialOptions = {
5
+ preset: "ts-jest",
6
+ testEnvironment: "node",
7
+ setupFiles: ["./src/tests/jestSetup.ts"],
8
+ collectCoverageFrom: ["src/**/*.{js,ts}"],
9
+ coverageReporters: ["lcov", "json", "clover"],
10
+ }
11
+
12
+ if (!process.env.CI) {
13
+ // use sources when not in CI
14
+ config.moduleNameMapper = {
15
+ "@budibase/backend-core/(.*)": "<rootDir>/../backend-core/$1",
16
+ "@budibase/backend-core": "<rootDir>/../backend-core/src",
17
+ "@budibase/types": "<rootDir>/../types/src",
18
+ "^axios.*$": "<rootDir>/node_modules/axios/lib/axios.js",
19
+ }
20
+ // add pro sources if they exist
21
+ if (fs.existsSync("../../../budibase-pro")) {
22
+ config.moduleNameMapper["@budibase/pro"] =
23
+ "<rootDir>/../../../budibase-pro/packages/pro/src"
24
+ }
25
+ } else {
26
+ console.log("Running tests with compiled dependency sources")
27
+ }
28
+
29
+ export default config
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@budibase/worker",
3
3
  "email": "hi@budibase.com",
4
- "version": "2.1.30",
4
+ "version": "2.1.32-alpha.0",
5
5
  "description": "Budibase background service",
6
6
  "main": "src/index.ts",
7
7
  "repository": {
@@ -22,7 +22,7 @@
22
22
  "build:docker": "docker build . -t worker-service --label version=$BUDIBASE_RELEASE_VERSION",
23
23
  "dev:stack:init": "node ./scripts/dev/manage.js init",
24
24
  "dev:builder": "npm run dev:stack:init && nodemon",
25
- "test": "jest --runInBand",
25
+ "test": "jest --coverage --runInBand",
26
26
  "test:watch": "jest --watch",
27
27
  "env:multi:enable": "node scripts/multiTenancy.js enable",
28
28
  "env:multi:disable": "node scripts/multiTenancy.js disable",
@@ -36,10 +36,10 @@
36
36
  "author": "Budibase",
37
37
  "license": "GPL-3.0",
38
38
  "dependencies": {
39
- "@budibase/backend-core": "^2.1.30",
40
- "@budibase/pro": "2.1.29",
41
- "@budibase/string-templates": "^2.1.30",
42
- "@budibase/types": "^2.1.30",
39
+ "@budibase/backend-core": "2.1.32-alpha.0",
40
+ "@budibase/pro": "2.1.31",
41
+ "@budibase/string-templates": "2.1.32-alpha.0",
42
+ "@budibase/types": "2.1.32-alpha.0",
43
43
  "@koa/router": "8.0.8",
44
44
  "@sentry/node": "6.17.7",
45
45
  "@techpass/passport-openidconnect": "0.3.2",
@@ -71,38 +71,28 @@
71
71
  },
72
72
  "devDependencies": {
73
73
  "@types/jest": "26.0.23",
74
+ "@types/jsonwebtoken": "8.5.1",
74
75
  "@types/koa": "2.13.4",
75
76
  "@types/koa__router": "8.0.11",
76
77
  "@types/node": "14.18.20",
78
+ "@types/node-fetch": "2.6.1",
77
79
  "@types/pouchdb": "6.4.0",
78
80
  "@types/uuid": "8.3.4",
79
81
  "@typescript-eslint/parser": "5.12.0",
80
82
  "copyfiles": "2.4.1",
81
83
  "eslint": "6.8.0",
82
- "jest": "27.4.7",
84
+ "jest": "28.1.1",
83
85
  "nodemon": "2.0.15",
84
86
  "pouchdb-adapter-memory": "7.2.2",
85
87
  "prettier": "2.3.1",
86
88
  "rimraf": "3.0.2",
87
89
  "supertest": "6.2.2",
88
90
  "timekeeper": "2.2.0",
89
- "ts-jest": "27.1.3",
90
- "ts-node": "10.4.0",
91
+ "ts-jest": "28.0.4",
92
+ "ts-node": "10.8.1",
91
93
  "tsconfig-paths": "4.0.0",
92
- "typescript": "4.5.5",
94
+ "typescript": "4.7.3",
93
95
  "update-dotenv": "1.1.1"
94
96
  },
95
- "jest": {
96
- "preset": "ts-jest",
97
- "testEnvironment": "node",
98
- "moduleNameMapper": {
99
- "@budibase/backend-core/(.*)": "<rootDir>/../backend-core/$1",
100
- "@budibase/backend-core": "<rootDir>/../backend-core/src",
101
- "@budibase/types": "<rootDir>/../types/src"
102
- },
103
- "setupFiles": [
104
- "./scripts/jestSetup.js"
105
- ]
106
- },
107
- "gitHead": "105b7bb83f432105bc412077364b69f124fc8791"
97
+ "gitHead": "f192fc72b3035780f5d1b7595f24227841f50cf6"
108
98
  }
@@ -2,6 +2,36 @@
2
2
  const updateDotEnv = require("update-dotenv")
3
3
 
4
4
  const arg = process.argv.slice(2)[0]
5
+ const isEnable = arg === "enable"
6
+
7
+ let domain = process.argv.slice(2)[1]
8
+ if (!domain) {
9
+ domain = "local.com"
10
+ }
11
+
12
+ const getAccountPortalUrl = () => {
13
+ if (isEnable) {
14
+ return `http://account.${domain}:10001`
15
+ } else {
16
+ return `http://localhost:10001`
17
+ }
18
+ }
19
+
20
+ const getBudibaseUrl = () => {
21
+ if (isEnable) {
22
+ return `http://${domain}:10000`
23
+ } else {
24
+ return `http://localhost:10000`
25
+ }
26
+ }
27
+
28
+ const getCookieDomain = () => {
29
+ if (isEnable) {
30
+ return `.${domain}`
31
+ } else {
32
+ return ""
33
+ }
34
+ }
5
35
 
6
36
  /**
7
37
  * For testing multi tenancy sub domains locally.
@@ -16,11 +46,7 @@ const arg = process.argv.slice(2)[0]
16
46
  * 127.0.0.1 t2.local.com
17
47
  */
18
48
  updateDotEnv({
19
- ACCOUNT_PORTAL_URL:
20
- arg === "enable"
21
- ? "http://account.local.com:10001"
22
- : "http://localhost:10001",
23
- COOKIE_DOMAIN: arg === "enable" ? ".local.com" : "",
24
- PLATFORM_URL:
25
- arg === "enable" ? "http://local.com:10000" : "http://localhost:10000",
49
+ ACCOUNT_PORTAL_URL: getAccountPortalUrl(),
50
+ COOKIE_DOMAIN: getCookieDomain(),
51
+ PLATFORM_URL: getBudibaseUrl(),
26
52
  }).then(() => console.log("Updated worker!"))
@@ -1,22 +1,26 @@
1
- const core = require("@budibase/backend-core")
2
- const { Configs, EmailTemplatePurpose } = require("../../../constants")
3
- const { sendEmail, isEmailConfigured } = require("../../../utilities/email")
4
- const { setCookie, getCookie, clearCookie, hash, platformLogout } = core.utils
5
- const { Cookies, Headers } = core.constants
6
- const { passport, ssoCallbackUrl, google, oidc } = core.auth
7
- const { checkResetPasswordCode } = require("../../../utilities/redis")
8
- const { getGlobalDB } = require("@budibase/backend-core/tenancy")
9
- const env = require("../../../environment")
10
- import { events, users as usersCore, context } from "@budibase/backend-core"
1
+ import core from "@budibase/backend-core"
2
+ import {
3
+ events,
4
+ users as usersCore,
5
+ context,
6
+ tenancy,
7
+ } from "@budibase/backend-core"
8
+ import { Config, EmailTemplatePurpose } from "../../../constants"
9
+ import { sendEmail, isEmailConfigured } from "../../../utilities/email"
10
+ import { checkResetPasswordCode } from "../../../utilities/redis"
11
+ import env from "../../../environment"
11
12
  import sdk from "../../../sdk"
12
13
  import { User } from "@budibase/types"
14
+ const { setCookie, getCookie, clearCookie, hash, platformLogout } = core.utils
15
+ const { Cookie, Header } = core.constants
16
+ const { passport, ssoCallbackUrl, google, oidc } = core.auth
13
17
 
14
18
  export const googleCallbackUrl = async (config: any) => {
15
- return ssoCallbackUrl(getGlobalDB(), config, "google")
19
+ return ssoCallbackUrl(tenancy.getGlobalDB(), config, "google")
16
20
  }
17
21
 
18
22
  export const oidcCallbackUrl = async (config: any) => {
19
- return ssoCallbackUrl(getGlobalDB(), config, "oidc")
23
+ return ssoCallbackUrl(tenancy.getGlobalDB(), config, "oidc")
20
24
  }
21
25
 
22
26
  async function authInternal(ctx: any, user: any, err = null, info = null) {
@@ -30,13 +34,13 @@ async function authInternal(ctx: any, user: any, err = null, info = null) {
30
34
  }
31
35
 
32
36
  // set a cookie for browser access
33
- setCookie(ctx, user.token, Cookies.Auth, { sign: false })
37
+ setCookie(ctx, user.token, Cookie.Auth, { sign: false })
34
38
  // set the token in a header as well for APIs
35
- ctx.set(Headers.TOKEN, user.token)
39
+ ctx.set(Header.TOKEN, user.token)
36
40
  // get rid of any app cookies on login
37
41
  // have to check test because this breaks cypress
38
42
  if (!env.isTest()) {
39
- clearCookie(ctx, Cookies.CurrentApp)
43
+ clearCookie(ctx, Cookie.CurrentApp)
40
44
  }
41
45
  }
42
46
 
@@ -55,15 +59,15 @@ export const authenticate = async (ctx: any, next: any) => {
55
59
 
56
60
  export const setInitInfo = (ctx: any) => {
57
61
  const initInfo = ctx.request.body
58
- setCookie(ctx, initInfo, Cookies.Init)
62
+ setCookie(ctx, initInfo, Cookie.Init)
59
63
  ctx.status = 200
60
64
  }
61
65
 
62
66
  export const getInitInfo = (ctx: any) => {
63
67
  try {
64
- ctx.body = getCookie(ctx, Cookies.Init) || {}
68
+ ctx.body = getCookie(ctx, Cookie.Init) || {}
65
69
  } catch (err) {
66
- clearCookie(ctx, Cookies.Init)
70
+ clearCookie(ctx, Cookie.Init)
67
71
  ctx.body = {}
68
72
  }
69
73
  }
@@ -106,7 +110,7 @@ export const resetUpdate = async (ctx: any) => {
106
110
  const { resetCode, password } = ctx.request.body
107
111
  try {
108
112
  const { userId } = await checkResetPasswordCode(resetCode)
109
- const db = getGlobalDB()
113
+ const db = tenancy.getGlobalDB()
110
114
  const user = await db.get(userId)
111
115
  user.password = await hash(password)
112
116
  await db.put(user)
@@ -141,14 +145,14 @@ export const datasourcePreAuth = async (ctx: any, next: any) => {
141
145
  appId: ctx.query.appId,
142
146
  datasourceId: ctx.query.datasourceId,
143
147
  },
144
- Cookies.DatasourceAuth
148
+ Cookie.DatasourceAuth
145
149
  )
146
150
 
147
151
  return handler.preAuth(passport, ctx, next)
148
152
  }
149
153
 
150
154
  export const datasourceAuth = async (ctx: any, next: any) => {
151
- const authStateCookie = getCookie(ctx, Cookies.DatasourceAuth)
155
+ const authStateCookie = getCookie(ctx, Cookie.DatasourceAuth)
152
156
  const provider = authStateCookie.provider
153
157
  const middleware = require(`@budibase/backend-core/middleware`)
154
158
  const handler = middleware.datasource[provider]
@@ -160,10 +164,10 @@ export const datasourceAuth = async (ctx: any, next: any) => {
160
164
  * On a successful login, you will be redirected to the googleAuth callback route.
161
165
  */
162
166
  export const googlePreAuth = async (ctx: any, next: any) => {
163
- const db = getGlobalDB()
167
+ const db = tenancy.getGlobalDB()
164
168
 
165
169
  const config = await core.db.getScopedConfig(db, {
166
- type: Configs.GOOGLE,
170
+ type: Config.GOOGLE,
167
171
  workspace: ctx.query.workspace,
168
172
  })
169
173
  let callbackUrl = await exports.googleCallbackUrl(config)
@@ -181,10 +185,10 @@ export const googlePreAuth = async (ctx: any, next: any) => {
181
185
  }
182
186
 
183
187
  export const googleAuth = async (ctx: any, next: any) => {
184
- const db = getGlobalDB()
188
+ const db = tenancy.getGlobalDB()
185
189
 
186
190
  const config = await core.db.getScopedConfig(db, {
187
- type: Configs.GOOGLE,
191
+ type: Config.GOOGLE,
188
192
  workspace: ctx.query.workspace,
189
193
  })
190
194
  const callbackUrl = await exports.googleCallbackUrl(config)
@@ -208,9 +212,9 @@ export const googleAuth = async (ctx: any, next: any) => {
208
212
  }
209
213
 
210
214
  export const oidcStrategyFactory = async (ctx: any, configId: any) => {
211
- const db = getGlobalDB()
215
+ const db = tenancy.getGlobalDB()
212
216
  const config = await core.db.getScopedConfig(db, {
213
- type: Configs.OIDC,
217
+ type: Config.OIDC,
214
218
  group: ctx.query.group,
215
219
  })
216
220
 
@@ -233,11 +237,11 @@ export const oidcPreAuth = async (ctx: any, next: any) => {
233
237
  const { configId } = ctx.params
234
238
  const strategy = await oidcStrategyFactory(ctx, configId)
235
239
 
236
- setCookie(ctx, configId, Cookies.OIDC_CONFIG)
240
+ setCookie(ctx, configId, Cookie.OIDC_CONFIG)
237
241
 
238
- const db = getGlobalDB()
242
+ const db = tenancy.getGlobalDB()
239
243
  const config = await core.db.getScopedConfig(db, {
240
- type: Configs.OIDC,
244
+ type: Config.OIDC,
241
245
  group: ctx.query.group,
242
246
  })
243
247
 
@@ -255,7 +259,7 @@ export const oidcPreAuth = async (ctx: any, next: any) => {
255
259
  }
256
260
 
257
261
  export const oidcAuth = async (ctx: any, next: any) => {
258
- const configId = getCookie(ctx, Cookies.OIDC_CONFIG)
262
+ const configId = getCookie(ctx, Cookie.OIDC_CONFIG)
259
263
  const strategy = await oidcStrategyFactory(ctx, configId)
260
264
 
261
265
  return passport.authenticate(
@@ -4,7 +4,7 @@ const {
4
4
  getScopedFullConfig,
5
5
  getAllApps,
6
6
  } = require("@budibase/backend-core/db")
7
- const { Configs } = require("../../../constants")
7
+ const { Config } = require("../../../constants")
8
8
  const email = require("../../../utilities/email")
9
9
  const {
10
10
  upload,
@@ -33,25 +33,25 @@ const getEventFns = async (db, config) => {
33
33
 
34
34
  if (!existing) {
35
35
  switch (config.type) {
36
- case Configs.SMTP: {
36
+ case Config.SMTP: {
37
37
  fns.push(events.email.SMTPCreated)
38
38
  break
39
39
  }
40
- case Configs.GOOGLE: {
40
+ case Config.GOOGLE: {
41
41
  fns.push(() => events.auth.SSOCreated(type))
42
42
  if (config.config.activated) {
43
43
  fns.push(() => events.auth.SSOActivated(type))
44
44
  }
45
45
  break
46
46
  }
47
- case Configs.OIDC: {
47
+ case Config.OIDC: {
48
48
  fns.push(() => events.auth.SSOCreated(type))
49
49
  if (config.config.configs[0].activated) {
50
50
  fns.push(() => events.auth.SSOActivated(type))
51
51
  }
52
52
  break
53
53
  }
54
- case Configs.SETTINGS: {
54
+ case Config.SETTINGS: {
55
55
  // company
56
56
  const company = config.config.company
57
57
  if (company && company !== "Budibase") {
@@ -78,11 +78,11 @@ const getEventFns = async (db, config) => {
78
78
  }
79
79
  } else {
80
80
  switch (config.type) {
81
- case Configs.SMTP: {
81
+ case Config.SMTP: {
82
82
  fns.push(events.email.SMTPUpdated)
83
83
  break
84
84
  }
85
- case Configs.GOOGLE: {
85
+ case Config.GOOGLE: {
86
86
  fns.push(() => events.auth.SSOUpdated(type))
87
87
  if (!existing.config.activated && config.config.activated) {
88
88
  fns.push(() => events.auth.SSOActivated(type))
@@ -91,7 +91,7 @@ const getEventFns = async (db, config) => {
91
91
  }
92
92
  break
93
93
  }
94
- case Configs.OIDC: {
94
+ case Config.OIDC: {
95
95
  fns.push(() => events.auth.SSOUpdated(type))
96
96
  if (
97
97
  !existing.config.configs[0].activated &&
@@ -106,7 +106,7 @@ const getEventFns = async (db, config) => {
106
106
  }
107
107
  break
108
108
  }
109
- case Configs.SETTINGS: {
109
+ case Config.SETTINGS: {
110
110
  // company
111
111
  const existingCompany = existing.config.company
112
112
  const company = config.config.company
@@ -155,7 +155,7 @@ exports.save = async function (ctx) {
155
155
  try {
156
156
  // verify the configuration
157
157
  switch (type) {
158
- case Configs.SMTP:
158
+ case Config.SMTP:
159
159
  await email.verifyConfig(config)
160
160
  break
161
161
  }
@@ -237,7 +237,7 @@ exports.publicOidc = async function (ctx) {
237
237
  try {
238
238
  // Find the config with the most granular scope based on context
239
239
  const oidcConfig = await getScopedFullConfig(db, {
240
- type: Configs.OIDC,
240
+ type: Config.OIDC,
241
241
  })
242
242
 
243
243
  if (!oidcConfig) {
@@ -260,15 +260,15 @@ exports.publicSettings = async function (ctx) {
260
260
  try {
261
261
  // Find the config with the most granular scope based on context
262
262
  const publicConfig = await getScopedFullConfig(db, {
263
- type: Configs.SETTINGS,
263
+ type: Config.SETTINGS,
264
264
  })
265
265
 
266
266
  const googleConfig = await getScopedFullConfig(db, {
267
- type: Configs.GOOGLE,
267
+ type: Config.GOOGLE,
268
268
  })
269
269
 
270
270
  const oidcConfig = await getScopedFullConfig(db, {
271
- type: Configs.OIDC,
271
+ type: Config.OIDC,
272
272
  })
273
273
 
274
274
  let config
@@ -390,17 +390,17 @@ exports.configChecklist = async function (ctx) {
390
390
 
391
391
  // They have set up SMTP
392
392
  const smtpConfig = await getScopedFullConfig(db, {
393
- type: Configs.SMTP,
393
+ type: Config.SMTP,
394
394
  })
395
395
 
396
396
  // They have set up Google Auth
397
397
  const googleConfig = await getScopedFullConfig(db, {
398
- type: Configs.GOOGLE,
398
+ type: Config.GOOGLE,
399
399
  })
400
400
 
401
401
  // They have set up OIDC
402
402
  const oidcConfig = await getScopedFullConfig(db, {
403
- type: Configs.OIDC,
403
+ type: Config.OIDC,
404
404
  })
405
405
 
406
406
  // They have set up an global user
@@ -73,12 +73,12 @@ export async function fetchAPIKey(ctx: any) {
73
73
  }
74
74
 
75
75
  const checkCurrentApp = (ctx: any) => {
76
- const appCookie = getCookie(ctx, constants.Cookies.CurrentApp)
76
+ const appCookie = getCookie(ctx, constants.Cookie.CurrentApp)
77
77
  if (appCookie && !tenancy.isUserInAppTenant(appCookie.appId)) {
78
78
  // there is a currentapp cookie from another tenant
79
79
  // remove the cookie as this is incompatible with the builder
80
80
  // due to builder and admin permissions being removed
81
- clearCookie(ctx, constants.Cookies.CurrentApp)
81
+ clearCookie(ctx, constants.Cookie.CurrentApp)
82
82
  }
83
83
  }
84
84
 
@@ -5,6 +5,7 @@ import {
5
5
  BulkUserRequest,
6
6
  BulkUserResponse,
7
7
  CloudAccount,
8
+ CreateAdminUserRequest,
8
9
  InviteUserRequest,
9
10
  InviteUsersRequest,
10
11
  SearchUsersRequest,
@@ -67,7 +68,8 @@ const parseBooleanParam = (param: any) => {
67
68
  }
68
69
 
69
70
  export const adminUser = async (ctx: any) => {
70
- const { email, password, tenantId } = ctx.request.body
71
+ const { email, password, tenantId } = ctx.request
72
+ .body as CreateAdminUserRequest
71
73
  await tenancy.doInTenant(tenantId, async () => {
72
74
  // account portal sends a pre-hashed password - honour param to prevent double hashing
73
75
  const hashPassword = parseBooleanParam(ctx.request.query.hashPassword)
@@ -1,6 +1,7 @@
1
- const env = require("../../../environment")
1
+ import { BBContext } from "@budibase/types"
2
+ import env from "../../../environment"
2
3
 
3
- exports.fetch = async ctx => {
4
+ export const fetch = async (ctx: BBContext) => {
4
5
  ctx.body = {
5
6
  multiTenancy: !!env.MULTI_TENANCY,
6
7
  cloud: !env.SELF_HOSTED,
@@ -1,7 +1,8 @@
1
- const accounts = require("@budibase/backend-core/accounts")
2
- const env = require("../../../environment")
1
+ import { accounts } from "@budibase/backend-core"
2
+ import env from "../../../environment"
3
+ import { BBContext } from "@budibase/types"
3
4
 
4
- exports.fetch = async ctx => {
5
+ export const fetch = async (ctx: BBContext) => {
5
6
  if (!env.SELF_HOSTED && !env.DISABLE_ACCOUNT_PORTAL) {
6
7
  const status = await accounts.getStatus()
7
8
  ctx.body = status
@@ -1,61 +1,18 @@
1
- const { StaticDatabases, doWithDB } = require("@budibase/backend-core/db")
2
- const { getTenantId } = require("@budibase/backend-core/tenancy")
3
- const { deleteTenant } = require("@budibase/backend-core/deprovision")
1
+ import { BBContext } from "@budibase/types"
2
+ import { deprovisioning } from "@budibase/backend-core"
4
3
  import { quotas } from "@budibase/pro"
5
4
 
6
- export const exists = async (ctx: any) => {
7
- const tenantId = ctx.request.params
8
- ctx.body = {
9
- exists: await doWithDB(
10
- StaticDatabases.PLATFORM_INFO.name,
11
- async (db: any) => {
12
- let exists = false
13
- try {
14
- const tenantsDoc = await db.get(
15
- StaticDatabases.PLATFORM_INFO.docs.tenants
16
- )
17
- if (tenantsDoc) {
18
- exists = tenantsDoc.tenantIds.indexOf(tenantId) !== -1
19
- }
20
- } catch (err) {
21
- // if error it doesn't exist
22
- }
23
- return exists
24
- }
25
- ),
26
- }
27
- }
28
-
29
- export const fetch = async (ctx: any) => {
30
- ctx.body = await doWithDB(
31
- StaticDatabases.PLATFORM_INFO.name,
32
- async (db: any) => {
33
- let tenants = []
34
- try {
35
- const tenantsDoc = await db.get(
36
- StaticDatabases.PLATFORM_INFO.docs.tenants
37
- )
38
- if (tenantsDoc) {
39
- tenants = tenantsDoc.tenantIds
40
- }
41
- } catch (err) {
42
- // if error it doesn't exist
43
- }
44
- return tenants
45
- }
46
- )
47
- }
48
-
49
- const _delete = async (ctx: any) => {
50
- const tenantId = getTenantId()
5
+ const _delete = async (ctx: BBContext) => {
6
+ const user = ctx.user!
7
+ const tenantId = ctx.params.tenantId
51
8
 
52
- if (ctx.params.tenantId !== tenantId) {
53
- ctx.throw(403, "Unauthorized")
9
+ if (tenantId !== user.tenantId) {
10
+ ctx.throw(403, "Tenant ID does not match current user")
54
11
  }
55
12
 
56
13
  try {
57
- await deleteTenant(tenantId)
58
14
  await quotas.bustCache()
15
+ await deprovisioning.deleteTenant(tenantId)
59
16
  ctx.status = 204
60
17
  } catch (err) {
61
18
  ctx.log.error(err)
package/src/api/index.ts CHANGED
@@ -7,11 +7,12 @@ import { errors, auth, middleware } from "@budibase/backend-core"
7
7
  import { APIError } from "@budibase/types"
8
8
 
9
9
  const PUBLIC_ENDPOINTS = [
10
- // old deprecated endpoints kept for backwards compat
10
+ // deprecated single tenant sso callback
11
11
  {
12
12
  route: "/api/admin/auth/google/callback",
13
13
  method: "GET",
14
14
  },
15
+ // deprecated single tenant sso callback
15
16
  {
16
17
  route: "/api/admin/auth/oidc/callback",
17
18
  method: "GET",
@@ -44,17 +45,19 @@ const PUBLIC_ENDPOINTS = [
44
45
  method: "POST",
45
46
  },
46
47
  {
47
- route: "api/system/environment",
48
+ route: "/api/system/environment",
48
49
  method: "GET",
49
50
  },
50
51
  {
51
- route: "api/system/status",
52
+ route: "/api/system/status",
52
53
  method: "GET",
53
54
  },
55
+ // TODO: This should be an internal api
54
56
  {
55
57
  route: "/api/global/users/tenant/:id",
56
58
  method: "GET",
57
59
  },
60
+ // TODO: This should be an internal api
58
61
  {
59
62
  route: "/api/system/restored",
60
63
  method: "POST",
@@ -62,17 +65,37 @@ const PUBLIC_ENDPOINTS = [
62
65
  ]
63
66
 
64
67
  const NO_TENANCY_ENDPOINTS = [
65
- ...PUBLIC_ENDPOINTS,
68
+ // system endpoints are not specific to any tenant
66
69
  {
67
70
  route: "/api/system",
68
71
  method: "ALL",
69
72
  },
73
+ // tenant is determined in request body
74
+ // used for creating the tenant
70
75
  {
71
- route: "/api/global/users/self",
76
+ route: "/api/global/users/init",
77
+ method: "POST",
78
+ },
79
+ // deprecated single tenant sso callback
80
+ {
81
+ route: "/api/admin/auth/google/callback",
82
+ method: "GET",
83
+ },
84
+ // deprecated single tenant sso callback
85
+ {
86
+ route: "/api/admin/auth/oidc/callback",
72
87
  method: "GET",
73
88
  },
89
+ // tenant is determined from code in redis
74
90
  {
75
- route: "/api/global/self",
91
+ route: "/api/global/users/invite/accept",
92
+ method: "POST",
93
+ },
94
+ // global user search - no tenancy
95
+ // :id is user id
96
+ // TODO: this should really be `/api/system/users/:id`
97
+ {
98
+ route: "/api/global/users/tenant/:id",
76
99
  method: "GET",
77
100
  },
78
101
  ]