@budibase/worker 2.1.43-alpha.1 → 2.1.43

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 (208) hide show
  1. package/package.json +24 -14
  2. package/{src/tests/jestSetup.ts → scripts/jestSetup.js} +3 -12
  3. package/scripts/localdomain.js +7 -33
  4. package/src/api/controllers/global/auth.ts +43 -51
  5. package/src/api/controllers/global/{configs.ts → configs.js} +114 -119
  6. package/src/api/controllers/global/{email.ts → email.js} +5 -6
  7. package/src/api/controllers/global/roles.js +68 -0
  8. package/src/api/controllers/global/self.ts +2 -2
  9. package/src/api/controllers/global/users.ts +2 -4
  10. package/src/api/controllers/global/{workspaces.ts → workspaces.js} +18 -15
  11. package/src/api/controllers/system/{environment.ts → environment.js} +2 -3
  12. package/src/api/controllers/system/restore.ts +1 -1
  13. package/src/api/controllers/system/{status.ts → status.js} +3 -4
  14. package/src/api/controllers/system/tenants.ts +51 -8
  15. package/src/api/index.ts +8 -31
  16. package/src/api/routes/global/{auth.ts → auth.js} +48 -31
  17. package/src/api/routes/global/{configs.ts → configs.js} +23 -22
  18. package/src/api/routes/global/{email.ts → email.js} +10 -9
  19. package/src/api/routes/global/license.ts +1 -1
  20. package/src/api/routes/global/roles.js +12 -0
  21. package/src/api/routes/global/self.ts +2 -2
  22. package/src/api/routes/global/templates.ts +4 -4
  23. package/src/api/routes/global/tests/auth.spec.ts +64 -134
  24. package/src/api/routes/global/tests/configs.spec.ts +35 -34
  25. package/src/api/routes/global/tests/email.spec.ts +3 -4
  26. package/src/api/routes/global/tests/realEmail.spec.ts +3 -2
  27. package/src/api/routes/global/tests/self.spec.ts +4 -3
  28. package/src/api/routes/global/tests/users.spec.ts +45 -61
  29. package/src/api/routes/global/{users.ts → users.js} +26 -28
  30. package/src/api/routes/global/{workspaces.ts → workspaces.js} +10 -9
  31. package/src/api/routes/index.ts +1 -2
  32. package/src/api/routes/system/accounts.ts +1 -1
  33. package/src/api/routes/system/environment.js +8 -0
  34. package/src/api/routes/system/migrations.ts +1 -1
  35. package/src/api/routes/system/restore.ts +1 -1
  36. package/src/api/routes/system/status.js +8 -0
  37. package/src/api/routes/system/tenants.js +12 -0
  38. package/src/api/routes/system/tests/accounts.spec.ts +6 -5
  39. package/src/api/routes/validation/users.ts +3 -3
  40. package/src/constants/{index.ts → index.js} +64 -57
  41. package/src/constants/templates/{index.ts → index.js} +19 -23
  42. package/src/db/index.js +11 -0
  43. package/src/environment.ts +7 -9
  44. package/src/index.ts +16 -15
  45. package/src/middleware/{cloudRestricted.ts → cloudRestricted.js} +4 -5
  46. package/src/tests/TestConfiguration.ts +50 -99
  47. package/src/tests/api/accounts.ts +8 -5
  48. package/src/tests/api/auth.ts +6 -3
  49. package/src/tests/api/configs.ts +8 -15
  50. package/src/tests/api/email.ts +6 -3
  51. package/src/tests/api/index.ts +0 -15
  52. package/src/tests/api/self.ts +6 -11
  53. package/src/tests/api/users.ts +6 -38
  54. package/src/tests/controllers.js +7 -0
  55. package/src/tests/index.ts +1 -5
  56. package/src/tests/structures/accounts.ts +24 -0
  57. package/src/tests/structures/{configs.ts → configs.js} +11 -11
  58. package/src/tests/structures/index.ts +3 -5
  59. package/src/tests/structures/users.ts +0 -1
  60. package/src/utilities/appService.js +33 -0
  61. package/src/utilities/{email.ts → email.js} +45 -78
  62. package/src/utilities/fileSystem.js +5 -0
  63. package/src/utilities/{index.ts → index.js} +1 -1
  64. package/src/utilities/{redis.ts → redis.js} +24 -29
  65. package/src/utilities/templates.js +49 -0
  66. package/src/utilities/users.js +17 -0
  67. package/tsconfig.build.json +3 -2
  68. package/tsconfig.json +1 -3
  69. package/__mocks__/aws-sdk.ts +0 -15
  70. package/__mocks__/oauth.ts +0 -57
  71. package/coverage/clover.xml +0 -2157
  72. package/coverage/coverage-final.json +0 -78
  73. package/coverage/lcov-report/base.css +0 -224
  74. package/coverage/lcov-report/block-navigation.js +0 -87
  75. package/coverage/lcov-report/favicon.png +0 -0
  76. package/coverage/lcov-report/index.html +0 -431
  77. package/coverage/lcov-report/prettify.css +0 -1
  78. package/coverage/lcov-report/prettify.js +0 -2
  79. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  80. package/coverage/lcov-report/sorter.js +0 -196
  81. package/coverage/lcov-report/src/api/controllers/global/auth.ts.html +0 -925
  82. package/coverage/lcov-report/src/api/controllers/global/configs.ts.html +0 -1405
  83. package/coverage/lcov-report/src/api/controllers/global/email.ts.html +0 -196
  84. package/coverage/lcov-report/src/api/controllers/global/index.html +0 -236
  85. package/coverage/lcov-report/src/api/controllers/global/license.ts.html +0 -187
  86. package/coverage/lcov-report/src/api/controllers/global/roles.ts.html +0 -283
  87. package/coverage/lcov-report/src/api/controllers/global/self.ts.html +0 -577
  88. package/coverage/lcov-report/src/api/controllers/global/templates.ts.html +0 -316
  89. package/coverage/lcov-report/src/api/controllers/global/users.ts.html +0 -796
  90. package/coverage/lcov-report/src/api/controllers/global/workspaces.ts.html +0 -244
  91. package/coverage/lcov-report/src/api/controllers/system/accounts.ts.html +0 -148
  92. package/coverage/lcov-report/src/api/controllers/system/environment.ts.html +0 -124
  93. package/coverage/lcov-report/src/api/controllers/system/index.html +0 -191
  94. package/coverage/lcov-report/src/api/controllers/system/migrations.ts.html +0 -124
  95. package/coverage/lcov-report/src/api/controllers/system/restore.ts.html +0 -124
  96. package/coverage/lcov-report/src/api/controllers/system/status.ts.html +0 -133
  97. package/coverage/lcov-report/src/api/controllers/system/tenants.ts.html +0 -154
  98. package/coverage/lcov-report/src/api/index.html +0 -116
  99. package/coverage/lcov-report/src/api/index.ts.html +0 -583
  100. package/coverage/lcov-report/src/api/routes/global/auth.ts.html +0 -349
  101. package/coverage/lcov-report/src/api/routes/global/configs.ts.html +0 -457
  102. package/coverage/lcov-report/src/api/routes/global/email.ts.html +0 -193
  103. package/coverage/lcov-report/src/api/routes/global/index.html +0 -236
  104. package/coverage/lcov-report/src/api/routes/global/license.ts.html +0 -124
  105. package/coverage/lcov-report/src/api/routes/global/roles.ts.html +0 -133
  106. package/coverage/lcov-report/src/api/routes/global/self.ts.html +0 -139
  107. package/coverage/lcov-report/src/api/routes/global/templates.ts.html +0 -196
  108. package/coverage/lcov-report/src/api/routes/global/users.ts.html +0 -433
  109. package/coverage/lcov-report/src/api/routes/global/workspaces.ts.html +0 -196
  110. package/coverage/lcov-report/src/api/routes/index.html +0 -116
  111. package/coverage/lcov-report/src/api/routes/index.ts.html +0 -196
  112. package/coverage/lcov-report/src/api/routes/system/accounts.ts.html +0 -142
  113. package/coverage/lcov-report/src/api/routes/system/environment.ts.html +0 -109
  114. package/coverage/lcov-report/src/api/routes/system/index.html +0 -191
  115. package/coverage/lcov-report/src/api/routes/system/migrations.ts.html +0 -142
  116. package/coverage/lcov-report/src/api/routes/system/restore.ts.html +0 -109
  117. package/coverage/lcov-report/src/api/routes/system/status.ts.html +0 -109
  118. package/coverage/lcov-report/src/api/routes/system/tenants.ts.html +0 -124
  119. package/coverage/lcov-report/src/api/routes/validation/index.html +0 -131
  120. package/coverage/lcov-report/src/api/routes/validation/index.ts.html +0 -88
  121. package/coverage/lcov-report/src/api/routes/validation/users.ts.html +0 -235
  122. package/coverage/lcov-report/src/constants/index.html +0 -116
  123. package/coverage/lcov-report/src/constants/index.ts.html +0 -613
  124. package/coverage/lcov-report/src/constants/templates/index.html +0 -116
  125. package/coverage/lcov-report/src/constants/templates/index.ts.html +0 -316
  126. package/coverage/lcov-report/src/db/index.html +0 -116
  127. package/coverage/lcov-report/src/db/index.ts.html +0 -115
  128. package/coverage/lcov-report/src/environment.ts.html +0 -373
  129. package/coverage/lcov-report/src/index.html +0 -131
  130. package/coverage/lcov-report/src/index.ts.html +0 -394
  131. package/coverage/lcov-report/src/middleware/cloudRestricted.ts.html +0 -139
  132. package/coverage/lcov-report/src/middleware/index.html +0 -116
  133. package/coverage/lcov-report/src/migrations/functions/globalInfoSyncUsers.ts.html +0 -145
  134. package/coverage/lcov-report/src/migrations/functions/index.html +0 -116
  135. package/coverage/lcov-report/src/migrations/index.html +0 -116
  136. package/coverage/lcov-report/src/migrations/index.ts.html +0 -271
  137. package/coverage/lcov-report/src/sdk/accounts/accounts.ts.html +0 -262
  138. package/coverage/lcov-report/src/sdk/accounts/index.html +0 -131
  139. package/coverage/lcov-report/src/sdk/accounts/index.ts.html +0 -88
  140. package/coverage/lcov-report/src/sdk/index.html +0 -116
  141. package/coverage/lcov-report/src/sdk/index.ts.html +0 -106
  142. package/coverage/lcov-report/src/sdk/users/events.ts.html +0 -577
  143. package/coverage/lcov-report/src/sdk/users/index.html +0 -146
  144. package/coverage/lcov-report/src/sdk/users/index.ts.html +0 -88
  145. package/coverage/lcov-report/src/sdk/users/users.ts.html +0 -1903
  146. package/coverage/lcov-report/src/tests/TestConfiguration.ts.html +0 -1051
  147. package/coverage/lcov-report/src/tests/api/accounts.ts.html +0 -160
  148. package/coverage/lcov-report/src/tests/api/auth.ts.html +0 -220
  149. package/coverage/lcov-report/src/tests/api/base.ts.html +0 -133
  150. package/coverage/lcov-report/src/tests/api/configs.ts.html +0 -226
  151. package/coverage/lcov-report/src/tests/api/email.ts.html +0 -148
  152. package/coverage/lcov-report/src/tests/api/environment.ts.html +0 -130
  153. package/coverage/lcov-report/src/tests/api/index.html +0 -296
  154. package/coverage/lcov-report/src/tests/api/index.ts.html +0 -205
  155. package/coverage/lcov-report/src/tests/api/migrations.ts.html +0 -151
  156. package/coverage/lcov-report/src/tests/api/restore.ts.html +0 -127
  157. package/coverage/lcov-report/src/tests/api/self.ts.html +0 -163
  158. package/coverage/lcov-report/src/tests/api/status.ts.html +0 -121
  159. package/coverage/lcov-report/src/tests/api/tenants.ts.html +0 -130
  160. package/coverage/lcov-report/src/tests/api/users.ts.html +0 -511
  161. package/coverage/lcov-report/src/tests/controllers.ts.html +0 -100
  162. package/coverage/lcov-report/src/tests/index.html +0 -146
  163. package/coverage/lcov-report/src/tests/index.ts.html +0 -139
  164. package/coverage/lcov-report/src/tests/mocks/email.js.html +0 -115
  165. package/coverage/lcov-report/src/tests/mocks/index.html +0 -131
  166. package/coverage/lcov-report/src/tests/mocks/index.ts.html +0 -106
  167. package/coverage/lcov-report/src/tests/structures/configs.ts.html +0 -313
  168. package/coverage/lcov-report/src/tests/structures/groups.ts.html +0 -118
  169. package/coverage/lcov-report/src/tests/structures/index.html +0 -161
  170. package/coverage/lcov-report/src/tests/structures/index.ts.html +0 -151
  171. package/coverage/lcov-report/src/tests/structures/users.ts.html +0 -196
  172. package/coverage/lcov-report/src/utilities/appService.ts.html +0 -181
  173. package/coverage/lcov-report/src/utilities/email.ts.html +0 -850
  174. package/coverage/lcov-report/src/utilities/fileSystem.ts.html +0 -100
  175. package/coverage/lcov-report/src/utilities/index.html +0 -206
  176. package/coverage/lcov-report/src/utilities/index.ts.html +0 -112
  177. package/coverage/lcov-report/src/utilities/redis.ts.html +0 -424
  178. package/coverage/lcov-report/src/utilities/templates.ts.html +0 -232
  179. package/coverage/lcov-report/src/utilities/users.ts.html +0 -133
  180. package/coverage/lcov.info +0 -3970
  181. package/jest.config.ts +0 -29
  182. package/src/api/controllers/global/roles.ts +0 -66
  183. package/src/api/routes/global/roles.ts +0 -16
  184. package/src/api/routes/global/tests/license.spec.ts +0 -31
  185. package/src/api/routes/global/tests/roles.spec.ts +0 -27
  186. package/src/api/routes/global/tests/templates.spec.ts +0 -35
  187. package/src/api/routes/global/tests/workspaces.spec.ts +0 -29
  188. package/src/api/routes/system/environment.ts +0 -8
  189. package/src/api/routes/system/status.ts +0 -8
  190. package/src/api/routes/system/tenants.ts +0 -13
  191. package/src/api/routes/system/tests/environment.spec.ts +0 -29
  192. package/src/api/routes/system/tests/migrations.spec.ts +0 -63
  193. package/src/api/routes/system/tests/restore.spec.ts +0 -36
  194. package/src/api/routes/system/tests/status.spec.ts +0 -48
  195. package/src/api/routes/system/tests/tenants.spec.ts +0 -61
  196. package/src/db/index.ts +0 -10
  197. package/src/middleware/tests/tenancy.spec.ts +0 -73
  198. package/src/tests/api/base.ts +0 -16
  199. package/src/tests/api/environment.ts +0 -15
  200. package/src/tests/api/migrations.ts +0 -22
  201. package/src/tests/api/restore.ts +0 -14
  202. package/src/tests/api/status.ts +0 -12
  203. package/src/tests/api/tenants.ts +0 -15
  204. package/src/tests/controllers.ts +0 -5
  205. package/src/utilities/appService.ts +0 -32
  206. package/src/utilities/fileSystem.ts +0 -5
  207. package/src/utilities/templates.ts +0 -49
  208. package/src/utilities/users.ts +0 -16
@@ -1,33 +1,15 @@
1
- import env from "../environment"
2
- import { EmailTemplatePurpose, TemplateType, Config } from "../constants"
3
- import { getTemplateByPurpose } from "../constants/templates"
4
- import { getSettingsTemplateContext } from "./templates"
5
- import { processString } from "@budibase/string-templates"
6
- import { getResetPasswordCode, getInviteCode } from "./redis"
7
- import { User, Database } from "@budibase/types"
8
- import { tenancy, db as dbCore } from "@budibase/backend-core"
9
1
  const nodemailer = require("nodemailer")
10
-
11
- type SendEmailOpts = {
12
- // workspaceId If finer grain controls being used then this will lookup config for workspace.
13
- workspaceId?: string
14
- // user If sending to an existing user the object can be provided, this is used in the context.
15
- user: User
16
- // from If sending from an address that is not what is configured in the SMTP config.
17
- from?: string
18
- // contents If sending a custom email then can supply contents which will be added to it.
19
- contents?: string
20
- // subject A custom subject can be specified if the config one is not desired.
21
- subject?: string
22
- // info Pass in a structure of information to be stored alongside the invitation.
23
- info?: any
24
- cc?: boolean
25
- bcc?: boolean
26
- automation?: boolean
27
- }
2
+ const env = require("../environment")
3
+ const { getScopedConfig } = require("@budibase/backend-core/db")
4
+ const { EmailTemplatePurpose, TemplateTypes, Configs } = require("../constants")
5
+ const { getTemplateByPurpose } = require("../constants/templates")
6
+ const { getSettingsTemplateContext } = require("./templates")
7
+ const { processString } = require("@budibase/string-templates")
8
+ const { getResetPasswordCode, getInviteCode } = require("../utilities/redis")
9
+ const { getGlobalDB } = require("@budibase/backend-core/tenancy")
28
10
 
29
11
  const TEST_MODE = false
30
- const TYPE = TemplateType.EMAIL
12
+ const TYPE = TemplateTypes.EMAIL
31
13
 
32
14
  const FULL_EMAIL_PURPOSES = [
33
15
  EmailTemplatePurpose.INVITATION,
@@ -36,8 +18,8 @@ const FULL_EMAIL_PURPOSES = [
36
18
  EmailTemplatePurpose.CUSTOM,
37
19
  ]
38
20
 
39
- function createSMTPTransport(config: any) {
40
- let options: any
21
+ function createSMTPTransport(config) {
22
+ let options
41
23
  let secure = config.secure
42
24
  // default it if not specified
43
25
  if (secure == null) {
@@ -70,15 +52,10 @@ function createSMTPTransport(config: any) {
70
52
  return nodemailer.createTransport(options)
71
53
  }
72
54
 
73
- async function getLinkCode(
74
- purpose: EmailTemplatePurpose,
75
- email: string,
76
- user: User,
77
- info: any = null
78
- ) {
55
+ async function getLinkCode(purpose, email, user, info = null) {
79
56
  switch (purpose) {
80
57
  case EmailTemplatePurpose.PASSWORD_RECOVERY:
81
- return getResetPasswordCode(user._id!, info)
58
+ return getResetPasswordCode(user._id, info)
82
59
  case EmailTemplatePurpose.INVITATION:
83
60
  return getInviteCode(email, info)
84
61
  default:
@@ -95,12 +72,7 @@ async function getLinkCode(
95
72
  * @param {string|null} contents if using a custom template can supply contents for context.
96
73
  * @return {Promise<string>} returns the built email HTML if all provided parameters were valid.
97
74
  */
98
- async function buildEmail(
99
- purpose: EmailTemplatePurpose,
100
- email: string,
101
- context: any,
102
- { user, contents }: any = {}
103
- ) {
75
+ async function buildEmail(purpose, email, context, { user, contents } = {}) {
104
76
  // this isn't a full email
105
77
  if (FULL_EMAIL_PURPOSES.indexOf(purpose) === -1) {
106
78
  throw `Unable to build an email of type ${purpose}`
@@ -141,19 +113,15 @@ async function buildEmail(
141
113
  * @param {boolean|null} automation Whether or not the configuration is being fetched for an email automation.
142
114
  * @return {Promise<object|null>} returns the SMTP configuration if it exists
143
115
  */
144
- async function getSmtpConfiguration(
145
- db: Database,
146
- workspaceId?: string,
147
- automation?: boolean
148
- ) {
149
- const params: any = {
150
- type: Config.SMTP,
116
+ async function getSmtpConfiguration(db, workspaceId = null, automation) {
117
+ const params = {
118
+ type: Configs.SMTP,
151
119
  }
152
120
  if (workspaceId) {
153
121
  params.workspace = workspaceId
154
122
  }
155
123
 
156
- const customConfig = await dbCore.getScopedConfig(db, params)
124
+ const customConfig = await getScopedConfig(db, params)
157
125
 
158
126
  if (customConfig) {
159
127
  return customConfig
@@ -178,12 +146,12 @@ async function getSmtpConfiguration(
178
146
  * Checks if a SMTP config exists based on passed in parameters.
179
147
  * @return {Promise<boolean>} returns true if there is a configuration that can be used.
180
148
  */
181
- export async function isEmailConfigured(workspaceId?: string) {
149
+ exports.isEmailConfigured = async (workspaceId = null) => {
182
150
  // when "testing" or smtp fallback is enabled simply return true
183
151
  if (TEST_MODE || env.SMTP_FALLBACK_ENABLED) {
184
152
  return true
185
153
  }
186
- const db = tenancy.getGlobalDB()
154
+ const db = getGlobalDB()
187
155
  const config = await getSmtpConfiguration(db, workspaceId)
188
156
  return config != null
189
157
  }
@@ -193,49 +161,48 @@ export async function isEmailConfigured(workspaceId?: string) {
193
161
  * send an email using it.
194
162
  * @param {string} email The email address to send to.
195
163
  * @param {string} purpose The purpose of the email being sent (e.g. reset password).
196
- * @param {object} opts The options for sending the email.
164
+ * @param {string|undefined} workspaceId If finer grain controls being used then this will lookup config for workspace.
165
+ * @param {object|undefined} user If sending to an existing user the object can be provided, this is used in the context.
166
+ * @param {string|undefined} from If sending from an address that is not what is configured in the SMTP config.
167
+ * @param {string|undefined} contents If sending a custom email then can supply contents which will be added to it.
168
+ * @param {string|undefined} subject A custom subject can be specified if the config one is not desired.
169
+ * @param {object|undefined} info Pass in a structure of information to be stored alongside the invitation.
170
+ * @param {boolean|undefined} disableFallback Prevent email being sent from SMTP fallback to avoid spam.
197
171
  * @return {Promise<object>} returns details about the attempt to send email, e.g. if it is successful; based on
198
172
  * nodemailer response.
199
173
  */
200
- export async function sendEmail(
201
- email: string,
202
- purpose: EmailTemplatePurpose,
203
- opts: SendEmailOpts
204
- ) {
205
- const db = tenancy.getGlobalDB()
206
- let config =
207
- (await getSmtpConfiguration(db, opts?.workspaceId, opts?.automation)) || {}
174
+ exports.sendEmail = async (
175
+ email,
176
+ purpose,
177
+ { workspaceId, user, from, contents, subject, info, cc, bcc, automation } = {}
178
+ ) => {
179
+ const db = getGlobalDB()
180
+ let config = (await getSmtpConfiguration(db, workspaceId, automation)) || {}
208
181
  if (Object.keys(config).length === 0 && !TEST_MODE) {
209
182
  throw "Unable to find SMTP configuration."
210
183
  }
211
184
  const transport = createSMTPTransport(config)
212
185
  // if there is a link code needed this will retrieve it
213
- const code = await getLinkCode(purpose, email, opts.user, opts?.info)
214
- let context
215
- if (code) {
216
- context = await getSettingsTemplateContext(purpose, code)
217
- }
186
+ const code = await getLinkCode(purpose, email, user, info)
187
+ const context = await getSettingsTemplateContext(purpose, code)
218
188
 
219
- let message: any = {
220
- from: opts?.from || config.from,
189
+ let message = {
190
+ from: from || config.from,
221
191
  html: await buildEmail(purpose, email, context, {
222
- user: opts?.user,
223
- contents: opts?.contents,
192
+ user,
193
+ contents,
224
194
  }),
225
195
  }
226
196
 
227
197
  message = {
228
198
  ...message,
229
199
  to: email,
230
- cc: opts?.cc,
231
- bcc: opts?.bcc,
200
+ cc: cc,
201
+ bcc: bcc,
232
202
  }
233
203
 
234
- if (opts?.subject || config.subject) {
235
- message.subject = await processString(
236
- opts?.subject || config.subject,
237
- context
238
- )
204
+ if (subject || config.subject) {
205
+ message.subject = await processString(subject || config.subject, context)
239
206
  }
240
207
  const response = await transport.sendMail(message)
241
208
  if (TEST_MODE) {
@@ -249,7 +216,7 @@ export async function sendEmail(
249
216
  * @param {object} config an SMTP configuration - this is based on the nodemailer API.
250
217
  * @return {Promise<boolean>} returns true if the configuration is valid.
251
218
  */
252
- export async function verifyConfig(config: any) {
219
+ exports.verifyConfig = async config => {
253
220
  const transport = createSMTPTransport(config)
254
221
  await transport.verify()
255
222
  }
@@ -0,0 +1,5 @@
1
+ const { readFileSync } = require("fs")
2
+
3
+ exports.readStaticFile = path => {
4
+ return readFileSync(path, "utf-8")
5
+ }
@@ -4,6 +4,6 @@
4
4
  * @param {string} url The URL to test and remove any extra double slashes.
5
5
  * @return {string} The updated url.
6
6
  */
7
- export function checkSlashesInUrl(url: string) {
7
+ exports.checkSlashesInUrl = url => {
8
8
  return url.replace(/(https?:\/\/)|(\/)+/g, "$1$2")
9
9
  }
@@ -1,35 +1,36 @@
1
- import { redis, utils } from "@budibase/backend-core"
1
+ const { Client, utils } = require("@budibase/backend-core/redis")
2
+ const { newid } = require("@budibase/backend-core/utils")
2
3
 
3
- function getExpirySecondsForDB(db: string) {
4
+ function getExpirySecondsForDB(db) {
4
5
  switch (db) {
5
- case redis.utils.Databases.PW_RESETS:
6
+ case utils.Databases.PW_RESETS:
6
7
  // a hour
7
8
  return 3600
8
- case redis.utils.Databases.INVITATIONS:
9
+ case utils.Databases.INVITATIONS:
9
10
  // a day
10
11
  return 86400
11
12
  }
12
13
  }
13
14
 
14
- let pwResetClient: any, invitationClient: any
15
+ let pwResetClient, invitationClient
15
16
 
16
- function getClient(db: string) {
17
+ function getClient(db) {
17
18
  switch (db) {
18
- case redis.utils.Databases.PW_RESETS:
19
+ case utils.Databases.PW_RESETS:
19
20
  return pwResetClient
20
- case redis.utils.Databases.INVITATIONS:
21
+ case utils.Databases.INVITATIONS:
21
22
  return invitationClient
22
23
  }
23
24
  }
24
25
 
25
- async function writeACode(db: string, value: any) {
26
+ async function writeACode(db, value) {
26
27
  const client = await getClient(db)
27
- const code = utils.newid()
28
+ const code = newid()
28
29
  await client.store(code, value, getExpirySecondsForDB(db))
29
30
  return code
30
31
  }
31
32
 
32
- async function getACode(db: string, code: string, deleteCode = true) {
33
+ async function getACode(db, code, deleteCode = true) {
33
34
  const client = await getClient(db)
34
35
  const value = await client.get(code)
35
36
  if (!value) {
@@ -41,9 +42,9 @@ async function getACode(db: string, code: string, deleteCode = true) {
41
42
  return value
42
43
  }
43
44
 
44
- export async function init() {
45
- pwResetClient = new redis.Client(redis.utils.Databases.PW_RESETS)
46
- invitationClient = new redis.Client(redis.utils.Databases.INVITATIONS)
45
+ exports.init = async () => {
46
+ pwResetClient = new Client(utils.Databases.PW_RESETS)
47
+ invitationClient = new Client(utils.Databases.INVITATIONS)
47
48
  await pwResetClient.init()
48
49
  await invitationClient.init()
49
50
  }
@@ -51,7 +52,7 @@ export async function init() {
51
52
  /**
52
53
  * make sure redis connection is closed.
53
54
  */
54
- export async function shutdown() {
55
+ exports.shutdown = async () => {
55
56
  if (pwResetClient) await pwResetClient.finish()
56
57
  if (invitationClient) await invitationClient.finish()
57
58
  console.log("Redis shutdown")
@@ -64,8 +65,8 @@ export async function shutdown() {
64
65
  * @param {object} info Info about the user/the reset process.
65
66
  * @return {Promise<string>} returns the code that was stored to redis.
66
67
  */
67
- export async function getResetPasswordCode(userId: string, info: any) {
68
- return writeACode(redis.utils.Databases.PW_RESETS, { userId, info })
68
+ exports.getResetPasswordCode = async (userId, info) => {
69
+ return writeACode(utils.Databases.PW_RESETS, { userId, info })
69
70
  }
70
71
 
71
72
  /**
@@ -74,12 +75,9 @@ export async function getResetPasswordCode(userId: string, info: any) {
74
75
  * @param {boolean} deleteCode If the code is used/finished with this will delete it - defaults to true.
75
76
  * @return {Promise<string>} returns the user ID if it is found
76
77
  */
77
- export async function checkResetPasswordCode(
78
- resetCode: string,
79
- deleteCode = true
80
- ) {
78
+ exports.checkResetPasswordCode = async (resetCode, deleteCode = true) => {
81
79
  try {
82
- return getACode(redis.utils.Databases.PW_RESETS, resetCode, deleteCode)
80
+ return getACode(utils.Databases.PW_RESETS, resetCode, deleteCode)
83
81
  } catch (err) {
84
82
  throw "Provided information is not valid, cannot reset password - please try again."
85
83
  }
@@ -91,8 +89,8 @@ export async function checkResetPasswordCode(
91
89
  * @param {object|null} info Information to be carried along with the invitation.
92
90
  * @return {Promise<string>} returns the code that was stored to redis.
93
91
  */
94
- export async function getInviteCode(email: string, info: any) {
95
- return writeACode(redis.utils.Databases.INVITATIONS, { email, info })
92
+ exports.getInviteCode = async (email, info) => {
93
+ return writeACode(utils.Databases.INVITATIONS, { email, info })
96
94
  }
97
95
 
98
96
  /**
@@ -101,12 +99,9 @@ export async function getInviteCode(email: string, info: any) {
101
99
  * @param {boolean} deleteCode whether or not the code should be deleted after retrieval - defaults to true.
102
100
  * @return {Promise<object>} If the code is valid then an email address will be returned.
103
101
  */
104
- export async function checkInviteCode(
105
- inviteCode: string,
106
- deleteCode: boolean = true
107
- ) {
102
+ exports.checkInviteCode = async (inviteCode, deleteCode = true) => {
108
103
  try {
109
- return getACode(redis.utils.Databases.INVITATIONS, inviteCode, deleteCode)
104
+ return getACode(utils.Databases.INVITATIONS, inviteCode, deleteCode)
110
105
  } catch (err) {
111
106
  throw "Invitation is not valid or has expired, please request a new one."
112
107
  }
@@ -0,0 +1,49 @@
1
+ const { getScopedConfig } = require("@budibase/backend-core/db")
2
+ const {
3
+ Configs,
4
+ InternalTemplateBindings,
5
+ LOGO_URL,
6
+ EmailTemplatePurpose,
7
+ } = require("../constants")
8
+ const { checkSlashesInUrl } = require("./index")
9
+ const {
10
+ getGlobalDB,
11
+ addTenantToUrl,
12
+ } = require("@budibase/backend-core/tenancy")
13
+ const BASE_COMPANY = "Budibase"
14
+
15
+ exports.getSettingsTemplateContext = async (purpose, code = null) => {
16
+ const db = getGlobalDB()
17
+ // TODO: use more granular settings in the future if required
18
+ let settings = (await getScopedConfig(db, { type: Configs.SETTINGS })) || {}
19
+ const URL = settings.platformUrl
20
+ const context = {
21
+ [InternalTemplateBindings.LOGO_URL]:
22
+ checkSlashesInUrl(`${URL}/${settings.logoUrl}`) || LOGO_URL,
23
+ [InternalTemplateBindings.PLATFORM_URL]: URL,
24
+ [InternalTemplateBindings.COMPANY]: settings.company || BASE_COMPANY,
25
+ [InternalTemplateBindings.DOCS_URL]:
26
+ settings.docsUrl || "https://docs.budibase.com/",
27
+ [InternalTemplateBindings.LOGIN_URL]: checkSlashesInUrl(
28
+ addTenantToUrl(`${URL}/login`)
29
+ ),
30
+ [InternalTemplateBindings.CURRENT_DATE]: new Date().toISOString(),
31
+ [InternalTemplateBindings.CURRENT_YEAR]: new Date().getFullYear(),
32
+ }
33
+ // attach purpose specific context
34
+ switch (purpose) {
35
+ case EmailTemplatePurpose.PASSWORD_RECOVERY:
36
+ context[InternalTemplateBindings.RESET_CODE] = code
37
+ context[InternalTemplateBindings.RESET_URL] = checkSlashesInUrl(
38
+ addTenantToUrl(`${URL}/builder/auth/reset?code=${code}`)
39
+ )
40
+ break
41
+ case EmailTemplatePurpose.INVITATION:
42
+ context[InternalTemplateBindings.INVITE_CODE] = code
43
+ context[InternalTemplateBindings.INVITE_URL] = checkSlashesInUrl(
44
+ addTenantToUrl(`${URL}/builder/invite?code=${code}`)
45
+ )
46
+ break
47
+ }
48
+ return context
49
+ }
@@ -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
+ }
@@ -3,6 +3,7 @@
3
3
  "target": "es6",
4
4
  "module": "commonjs",
5
5
  "lib": ["es2020"],
6
+ "allowJs": true,
6
7
  "strict": true,
7
8
  "noImplicitAny": true,
8
9
  "esModuleInterop": true,
@@ -19,7 +20,7 @@
19
20
  "node_modules",
20
21
  "dist",
21
22
  "src/tests",
22
- "**/*.spec.js",
23
- "**/*.spec.ts"
23
+ "**/*.spec.ts",
24
+ "**/*.spec.js"
24
25
  ]
25
26
  }
package/tsconfig.json CHANGED
@@ -8,8 +8,7 @@
8
8
  "paths": {
9
9
  "@budibase/types": ["../types/src"],
10
10
  "@budibase/backend-core": ["../backend-core/src"],
11
- "@budibase/backend-core/*": ["../backend-core/*"],
12
- "@budibase/pro": ["../../../budibase-pro/packages/pro/src"]
11
+ "@budibase/backend-core/*": ["../backend-core/*"]
13
12
  }
14
13
  },
15
14
  "ts-node": {
@@ -18,7 +17,6 @@
18
17
  "references": [
19
18
  { "path": "../types" },
20
19
  { "path": "../backend-core" },
21
- { "path": "../../../budibase-pro/packages/pro" }
22
20
  ],
23
21
  "include": [
24
22
  "src/**/*",
@@ -1,15 +0,0 @@
1
- const mockS3 = {
2
- headBucket: jest.fn().mockReturnThis(),
3
- deleteObject: jest.fn().mockReturnThis(),
4
- deleteObjects: jest.fn().mockReturnThis(),
5
- createBucket: jest.fn().mockReturnThis(),
6
- listObjects: jest.fn().mockReturnThis(),
7
- promise: jest.fn().mockReturnThis(),
8
- catch: jest.fn(),
9
- }
10
-
11
- const AWS = {
12
- S3: jest.fn(() => mockS3),
13
- }
14
-
15
- export default AWS
@@ -1,57 +0,0 @@
1
- import * as jwt from "jsonwebtoken"
2
-
3
- const mockOAuth2 = {
4
- getOAuthAccessToken: (code: string, p: any, cb: any) => {
5
- const err = null
6
- const accessToken = "access_token"
7
- const refreshToken = "refresh_token"
8
-
9
- const exp = new Date()
10
- exp.setDate(exp.getDate() + 1)
11
-
12
- const iat = new Date()
13
- iat.setDate(iat.getDate() - 1)
14
-
15
- const claims = {
16
- iss: "test",
17
- sub: "sub",
18
- aud: "clientId",
19
- exp: exp.getTime() / 1000,
20
- iat: iat.getTime() / 1000,
21
- email: "oauth@example.com",
22
- }
23
-
24
- const idToken = jwt.sign(claims, "secret")
25
-
26
- const params = {
27
- id_token: idToken,
28
- }
29
- return cb(err, accessToken, refreshToken, params)
30
- },
31
- _request: (
32
- method: string,
33
- url: string,
34
- headers: any,
35
- postBody: any,
36
- accessToken: string,
37
- cb: any
38
- ) => {
39
- const err = null
40
- const body = {
41
- sub: "sub",
42
- user_id: "userId",
43
- name: "OAuth",
44
- family_name: "2",
45
- given_name: "OAuth",
46
- middle_name: "",
47
- }
48
- const res = {}
49
- return cb(err, JSON.stringify(body), res)
50
- },
51
- }
52
-
53
- const oauth = {
54
- OAuth2: jest.fn(() => mockOAuth2),
55
- }
56
-
57
- export = oauth