@budibase/server 1.0.98-alpha.6 → 1.0.98-alpha.9

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 (194) hide show
  1. package/builder/assets/{index.26b024e1.js → index.b20cc87e.js} +1 -1
  2. package/builder/index.html +1 -1
  3. package/coverage/clover.xml +148 -140
  4. package/coverage/coverage-final.json +3 -3
  5. package/coverage/lcov-report/api/controllers/analytics.js.html +1 -1
  6. package/coverage/lcov-report/api/controllers/apikeys.js.html +1 -1
  7. package/coverage/lcov-report/api/controllers/application.js.html +1 -1
  8. package/coverage/lcov-report/api/controllers/auth.js.html +1 -1
  9. package/coverage/lcov-report/api/controllers/automation.js.html +1 -1
  10. package/coverage/lcov-report/api/controllers/backup.js.html +1 -1
  11. package/coverage/lcov-report/api/controllers/cloud.js.html +1 -1
  12. package/coverage/lcov-report/api/controllers/component.js.html +1 -1
  13. package/coverage/lcov-report/api/controllers/datasource.js.html +1 -1
  14. package/coverage/lcov-report/api/controllers/dev.js.html +1 -1
  15. package/coverage/lcov-report/api/controllers/index.html +5 -5
  16. package/coverage/lcov-report/api/controllers/integration.js.html +1 -1
  17. package/coverage/lcov-report/api/controllers/layout.js.html +1 -1
  18. package/coverage/lcov-report/api/controllers/metadata.js.html +1 -1
  19. package/coverage/lcov-report/api/controllers/migrations.js.html +1 -1
  20. package/coverage/lcov-report/api/controllers/permission.js.html +7 -7
  21. package/coverage/lcov-report/api/controllers/query/index.html +1 -1
  22. package/coverage/lcov-report/api/controllers/query/index.js.html +1 -1
  23. package/coverage/lcov-report/api/controllers/query/validation.js.html +1 -1
  24. package/coverage/lcov-report/api/controllers/role.js.html +1 -1
  25. package/coverage/lcov-report/api/controllers/routing.js.html +1 -1
  26. package/coverage/lcov-report/api/controllers/row/external.js.html +1 -1
  27. package/coverage/lcov-report/api/controllers/row/index.html +1 -1
  28. package/coverage/lcov-report/api/controllers/row/index.js.html +1 -1
  29. package/coverage/lcov-report/api/controllers/row/internal.js.html +1 -1
  30. package/coverage/lcov-report/api/controllers/row/internalSearch.js.html +1 -1
  31. package/coverage/lcov-report/api/controllers/row/staticFormula.js.html +1 -1
  32. package/coverage/lcov-report/api/controllers/row/utils.js.html +1 -1
  33. package/coverage/lcov-report/api/controllers/screen.js.html +1 -1
  34. package/coverage/lcov-report/api/controllers/script.js.html +1 -1
  35. package/coverage/lcov-report/api/controllers/table/bulkFormula.js.html +1 -1
  36. package/coverage/lcov-report/api/controllers/table/external.js.html +1 -1
  37. package/coverage/lcov-report/api/controllers/table/index.html +1 -1
  38. package/coverage/lcov-report/api/controllers/table/index.js.html +1 -1
  39. package/coverage/lcov-report/api/controllers/table/internal.js.html +1 -1
  40. package/coverage/lcov-report/api/controllers/table/utils.js.html +1 -1
  41. package/coverage/lcov-report/api/controllers/templates.js.html +1 -1
  42. package/coverage/lcov-report/api/controllers/user.js.html +1 -1
  43. package/coverage/lcov-report/api/controllers/view/exporters.js.html +1 -1
  44. package/coverage/lcov-report/api/controllers/view/index.html +1 -1
  45. package/coverage/lcov-report/api/controllers/view/index.js.html +1 -1
  46. package/coverage/lcov-report/api/controllers/view/utils.js.html +1 -1
  47. package/coverage/lcov-report/api/controllers/view/viewBuilder.js.html +1 -1
  48. package/coverage/lcov-report/api/controllers/webhook.js.html +1 -1
  49. package/coverage/lcov-report/api/index.html +1 -1
  50. package/coverage/lcov-report/api/index.js.html +1 -1
  51. package/coverage/lcov-report/api/routes/analytics.js.html +1 -1
  52. package/coverage/lcov-report/api/routes/apikeys.js.html +1 -1
  53. package/coverage/lcov-report/api/routes/application.js.html +1 -1
  54. package/coverage/lcov-report/api/routes/auth.js.html +1 -1
  55. package/coverage/lcov-report/api/routes/automation.js.html +1 -1
  56. package/coverage/lcov-report/api/routes/backup.js.html +1 -1
  57. package/coverage/lcov-report/api/routes/cloud.js.html +1 -1
  58. package/coverage/lcov-report/api/routes/component.js.html +1 -1
  59. package/coverage/lcov-report/api/routes/datasource.js.html +1 -1
  60. package/coverage/lcov-report/api/routes/deploy.js.html +1 -1
  61. package/coverage/lcov-report/api/routes/dev.js.html +1 -1
  62. package/coverage/lcov-report/api/routes/index.html +1 -1
  63. package/coverage/lcov-report/api/routes/index.js.html +1 -1
  64. package/coverage/lcov-report/api/routes/integration.js.html +1 -1
  65. package/coverage/lcov-report/api/routes/layout.js.html +1 -1
  66. package/coverage/lcov-report/api/routes/metadata.js.html +1 -1
  67. package/coverage/lcov-report/api/routes/migrations.js.html +1 -1
  68. package/coverage/lcov-report/api/routes/permission.js.html +1 -1
  69. package/coverage/lcov-report/api/routes/query.js.html +1 -1
  70. package/coverage/lcov-report/api/routes/role.js.html +1 -1
  71. package/coverage/lcov-report/api/routes/routing.js.html +1 -1
  72. package/coverage/lcov-report/api/routes/row.js.html +1 -1
  73. package/coverage/lcov-report/api/routes/screen.js.html +1 -1
  74. package/coverage/lcov-report/api/routes/script.js.html +1 -1
  75. package/coverage/lcov-report/api/routes/static.js.html +1 -1
  76. package/coverage/lcov-report/api/routes/table.js.html +2 -2
  77. package/coverage/lcov-report/api/routes/templates.js.html +1 -1
  78. package/coverage/lcov-report/api/routes/user.js.html +1 -1
  79. package/coverage/lcov-report/api/routes/utils/index.html +1 -1
  80. package/coverage/lcov-report/api/routes/utils/validators.js.html +1 -1
  81. package/coverage/lcov-report/api/routes/view.js.html +1 -1
  82. package/coverage/lcov-report/api/routes/webhook.js.html +1 -1
  83. package/coverage/lcov-report/automations/actions.js.html +1 -1
  84. package/coverage/lcov-report/automations/automationUtils.js.html +1 -1
  85. package/coverage/lcov-report/automations/bullboard.js.html +1 -1
  86. package/coverage/lcov-report/automations/index.html +12 -12
  87. package/coverage/lcov-report/automations/index.js.html +1 -1
  88. package/coverage/lcov-report/automations/steps/bash.js.html +1 -1
  89. package/coverage/lcov-report/automations/steps/createRow.js.html +1 -1
  90. package/coverage/lcov-report/automations/steps/delay.js.html +1 -1
  91. package/coverage/lcov-report/automations/steps/deleteRow.js.html +1 -1
  92. package/coverage/lcov-report/automations/steps/discord.js.html +1 -1
  93. package/coverage/lcov-report/automations/steps/executeQuery.js.html +1 -1
  94. package/coverage/lcov-report/automations/steps/executeScript.js.html +1 -1
  95. package/coverage/lcov-report/automations/steps/filter.js.html +1 -1
  96. package/coverage/lcov-report/automations/steps/index.html +1 -1
  97. package/coverage/lcov-report/automations/steps/integromat.js.html +1 -1
  98. package/coverage/lcov-report/automations/steps/outgoingWebhook.js.html +1 -1
  99. package/coverage/lcov-report/automations/steps/queryRows.js.html +1 -1
  100. package/coverage/lcov-report/automations/steps/sendSmtpEmail.js.html +1 -1
  101. package/coverage/lcov-report/automations/steps/serverLog.js.html +1 -1
  102. package/coverage/lcov-report/automations/steps/slack.js.html +1 -1
  103. package/coverage/lcov-report/automations/steps/updateRow.js.html +1 -1
  104. package/coverage/lcov-report/automations/steps/utils.js.html +1 -1
  105. package/coverage/lcov-report/automations/steps/zapier.js.html +1 -1
  106. package/coverage/lcov-report/automations/triggerInfo/app.js.html +1 -1
  107. package/coverage/lcov-report/automations/triggerInfo/cron.js.html +1 -1
  108. package/coverage/lcov-report/automations/triggerInfo/index.html +1 -1
  109. package/coverage/lcov-report/automations/triggerInfo/index.js.html +1 -1
  110. package/coverage/lcov-report/automations/triggerInfo/rowDeleted.js.html +1 -1
  111. package/coverage/lcov-report/automations/triggerInfo/rowSaved.js.html +1 -1
  112. package/coverage/lcov-report/automations/triggerInfo/rowUpdated.js.html +1 -1
  113. package/coverage/lcov-report/automations/triggerInfo/webhook.js.html +1 -1
  114. package/coverage/lcov-report/automations/triggers.js.html +1 -1
  115. package/coverage/lcov-report/automations/utils.js.html +20 -8
  116. package/coverage/lcov-report/constants/index.html +1 -1
  117. package/coverage/lcov-report/constants/index.js.html +1 -1
  118. package/coverage/lcov-report/constants/layouts.js.html +1 -1
  119. package/coverage/lcov-report/constants/screens.js.html +1 -1
  120. package/coverage/lcov-report/db/client.js.html +1 -1
  121. package/coverage/lcov-report/db/inMemoryView.js.html +1 -1
  122. package/coverage/lcov-report/db/index.html +1 -1
  123. package/coverage/lcov-report/db/index.js.html +1 -1
  124. package/coverage/lcov-report/db/linkedRows/LinkController.js.html +1 -1
  125. package/coverage/lcov-report/db/linkedRows/index.html +1 -1
  126. package/coverage/lcov-report/db/linkedRows/index.js.html +1 -1
  127. package/coverage/lcov-report/db/linkedRows/linkUtils.js.html +1 -1
  128. package/coverage/lcov-report/db/newid.js.html +1 -1
  129. package/coverage/lcov-report/db/utils.js.html +1 -1
  130. package/coverage/lcov-report/events/AutomationEmitter.js.html +1 -1
  131. package/coverage/lcov-report/events/index.html +1 -1
  132. package/coverage/lcov-report/events/index.js.html +1 -1
  133. package/coverage/lcov-report/events/utils.js.html +1 -1
  134. package/coverage/lcov-report/index.html +21 -21
  135. package/coverage/lcov-report/integrations/tests/TestConfiguration.js.html +1 -1
  136. package/coverage/lcov-report/integrations/tests/index.html +1 -1
  137. package/coverage/lcov-report/middleware/appInfo.js.html +1 -1
  138. package/coverage/lcov-report/middleware/authorized.js.html +75 -15
  139. package/coverage/lcov-report/middleware/builder.js.html +1 -1
  140. package/coverage/lcov-report/middleware/currentapp.js.html +1 -1
  141. package/coverage/lcov-report/middleware/index.html +15 -15
  142. package/coverage/lcov-report/middleware/joi-validator.js.html +1 -1
  143. package/coverage/lcov-report/middleware/publicApi.js.html +1 -1
  144. package/coverage/lcov-report/middleware/resourceId.js.html +1 -1
  145. package/coverage/lcov-report/middleware/selfhost.js.html +1 -1
  146. package/coverage/lcov-report/middleware/usageQuota.js.html +1 -1
  147. package/coverage/lcov-report/middleware/utils.js.html +1 -1
  148. package/coverage/lcov-report/threads/automation.js.html +1 -1
  149. package/coverage/lcov-report/threads/index.html +1 -1
  150. package/coverage/lcov-report/threads/index.js.html +1 -1
  151. package/coverage/lcov-report/threads/query.js.html +1 -1
  152. package/coverage/lcov-report/threads/utils.js.html +1 -1
  153. package/coverage/lcov-report/utilities/bcrypt.js.html +1 -1
  154. package/coverage/lcov-report/utilities/budibaseDir.js.html +1 -1
  155. package/coverage/lcov-report/utilities/centralPath.js.html +1 -1
  156. package/coverage/lcov-report/utilities/csvParser.js.html +1 -1
  157. package/coverage/lcov-report/utilities/global.js.html +1 -1
  158. package/coverage/lcov-report/utilities/index.html +1 -1
  159. package/coverage/lcov-report/utilities/index.js.html +1 -1
  160. package/coverage/lcov-report/utilities/queue/inMemoryQueue.js.html +1 -1
  161. package/coverage/lcov-report/utilities/queue/index.html +1 -1
  162. package/coverage/lcov-report/utilities/routing/index.html +1 -1
  163. package/coverage/lcov-report/utilities/routing/index.js.html +1 -1
  164. package/coverage/lcov-report/utilities/rowProcessor/index.html +1 -1
  165. package/coverage/lcov-report/utilities/rowProcessor/index.js.html +1 -1
  166. package/coverage/lcov-report/utilities/rowProcessor/utils.js.html +1 -1
  167. package/coverage/lcov-report/utilities/scriptRunner.js.html +1 -1
  168. package/coverage/lcov-report/utilities/security.js.html +1 -1
  169. package/coverage/lcov-report/utilities/statusCodes.js.html +1 -1
  170. package/coverage/lcov-report/utilities/usageQuota/index.html +1 -1
  171. package/coverage/lcov-report/utilities/usageQuota/index.js.html +1 -1
  172. package/coverage/lcov-report/utilities/usageQuota/rows.js.html +1 -1
  173. package/coverage/lcov-report/utilities/usageQuota/usageQuoteReset.js.html +1 -1
  174. package/coverage/lcov-report/utilities/users.js.html +1 -1
  175. package/coverage/lcov-report/utilities/workerRequests.js.html +1 -1
  176. package/coverage/lcov.info +276 -254
  177. package/dist/api/controllers/permission.js +3 -4
  178. package/dist/api/routes/table.js +1 -1
  179. package/dist/automations/utils.js +3 -1
  180. package/dist/integrations/mysql.js +14 -1
  181. package/dist/middleware/authorized.js +24 -7
  182. package/dist/package.json +4 -4
  183. package/dist/tsconfig.tsbuildinfo +1 -1
  184. package/package.json +5 -5
  185. package/specs/openapi.json +23 -3
  186. package/specs/openapi.yaml +20 -4
  187. package/specs/resources/query.js +27 -5
  188. package/src/api/controllers/permission.js +3 -3
  189. package/src/api/routes/table.js +1 -1
  190. package/src/automations/utils.js +5 -1
  191. package/src/definitions/datasource.ts +1 -5
  192. package/src/definitions/openapi.ts +15 -5
  193. package/src/integrations/mysql.ts +15 -1
  194. package/src/middleware/authorized.js +26 -6
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@budibase/server",
3
3
  "email": "hi@budibase.com",
4
- "version": "1.0.98-alpha.6",
4
+ "version": "1.0.98-alpha.9",
5
5
  "description": "Budibase Web Server",
6
6
  "main": "src/index.ts",
7
7
  "repository": {
@@ -68,9 +68,9 @@
68
68
  "license": "GPL-3.0",
69
69
  "dependencies": {
70
70
  "@apidevtools/swagger-parser": "^10.0.3",
71
- "@budibase/backend-core": "^1.0.98-alpha.6",
72
- "@budibase/client": "^1.0.98-alpha.6",
73
- "@budibase/string-templates": "^1.0.98-alpha.6",
71
+ "@budibase/backend-core": "^1.0.98-alpha.9",
72
+ "@budibase/client": "^1.0.98-alpha.9",
73
+ "@budibase/string-templates": "^1.0.98-alpha.9",
74
74
  "@bull-board/api": "^3.7.0",
75
75
  "@bull-board/koa": "^3.7.0",
76
76
  "@elastic/elasticsearch": "7.10.0",
@@ -174,5 +174,5 @@
174
174
  "optionalDependencies": {
175
175
  "oracledb": "^5.3.0"
176
176
  },
177
- "gitHead": "ca5bc790f5ddfedb44e87859339ba2db809b489d"
177
+ "gitHead": "0e309aca3ad35e5e9d42fae3a4c074af3cd77544"
178
178
  }
@@ -1260,10 +1260,30 @@
1260
1260
  ]
1261
1261
  },
1262
1262
  "executeQuery": {
1263
- "description": "The query body must contain the required parameters for the query, this depends on query type, setup and bindings.",
1263
+ "description": "The parameters required for executing a query.",
1264
1264
  "type": "object",
1265
- "additionalProperties": {
1266
- "description": "Key value properties of any type, depending on the query output schema."
1265
+ "properties": {
1266
+ "parameters": {
1267
+ "type": "object",
1268
+ "description": "This contains the required parameters for the query, this depends on query type, setup and bindings.",
1269
+ "additionalProperties": {
1270
+ "description": "Key value properties of any type, depending on the query output schema."
1271
+ }
1272
+ },
1273
+ "pagination": {
1274
+ "type": "object",
1275
+ "description": "For supported query types (currently on REST) pagination can be performed using these properties.",
1276
+ "properties": {
1277
+ "page": {
1278
+ "type": "string",
1279
+ "description": "The page which has been returned from a previous query."
1280
+ },
1281
+ "limit": {
1282
+ "type": "number",
1283
+ "description": "The number of rows to return per page."
1284
+ }
1285
+ }
1286
+ }
1267
1287
  }
1268
1288
  },
1269
1289
  "executeQueryOutput": {
@@ -951,11 +951,27 @@ components:
951
951
  required:
952
952
  - data
953
953
  executeQuery:
954
- description: The query body must contain the required parameters for the query,
955
- this depends on query type, setup and bindings.
954
+ description: The parameters required for executing a query.
956
955
  type: object
957
- additionalProperties:
958
- description: Key value properties of any type, depending on the query output schema.
956
+ properties:
957
+ parameters:
958
+ type: object
959
+ description: This contains the required parameters for the query, this depends
960
+ on query type, setup and bindings.
961
+ additionalProperties:
962
+ description: Key value properties of any type, depending on the query output
963
+ schema.
964
+ pagination:
965
+ type: object
966
+ description: For supported query types (currently on REST) pagination can be
967
+ performed using these properties.
968
+ properties:
969
+ page:
970
+ type: string
971
+ description: The page which has been returned from a previous query.
972
+ limit:
973
+ type: number
974
+ description: The number of rows to return per page.
959
975
  executeQueryOutput:
960
976
  type: object
961
977
  properties:
@@ -124,12 +124,34 @@ const querySchema = object(
124
124
  )
125
125
 
126
126
  const executeQuerySchema = {
127
- description:
128
- "The query body must contain the required parameters for the query, this depends on query type, setup and bindings.",
127
+ description: "The parameters required for executing a query.",
129
128
  type: "object",
130
- additionalProperties: {
131
- description:
132
- "Key value properties of any type, depending on the query output schema.",
129
+ properties: {
130
+ parameters: {
131
+ type: "object",
132
+ description:
133
+ "This contains the required parameters for the query, this depends on query type, setup and bindings.",
134
+ additionalProperties: {
135
+ description:
136
+ "Key value properties of any type, depending on the query output schema.",
137
+ },
138
+ },
139
+ pagination: {
140
+ type: "object",
141
+ description:
142
+ "For supported query types (currently on REST) pagination can be performed using these properties.",
143
+ properties: {
144
+ page: {
145
+ type: "string",
146
+ description:
147
+ "The page which has been returned from a previous query.",
148
+ },
149
+ limit: {
150
+ type: "number",
151
+ description: "The number of rows to return per page.",
152
+ },
153
+ },
154
+ },
133
155
  },
134
156
  }
135
157
 
@@ -4,6 +4,7 @@ const {
4
4
  getDBRoleID,
5
5
  getExternalRoleID,
6
6
  getBuiltinRoles,
7
+ checkForRoleResourceArray,
7
8
  } = require("@budibase/backend-core/roles")
8
9
  const { getRoleParams } = require("../../db/utils")
9
10
  const {
@@ -144,12 +145,11 @@ exports.getResourcePerms = async function (ctx) {
144
145
  for (let level of SUPPORTED_LEVELS) {
145
146
  // update the various roleIds in the resource permissions
146
147
  for (let role of roles) {
147
- const rolePerms = role.permissions
148
+ const rolePerms = checkForRoleResourceArray(role.permissions, resourceId)
148
149
  if (
149
150
  rolePerms &&
150
151
  rolePerms[resourceId] &&
151
- (rolePerms[resourceId] === level ||
152
- rolePerms[resourceId].indexOf(level) !== -1)
152
+ rolePerms[resourceId].indexOf(level) !== -1
153
153
  ) {
154
154
  permissions[level] = getExternalRoleID(role._id)
155
155
  }
@@ -40,7 +40,7 @@ router
40
40
  .get(
41
41
  "/api/tables/:tableId",
42
42
  paramResource("tableId"),
43
- authorized(PermissionTypes.TABLE, PermissionLevels.READ),
43
+ authorized(PermissionTypes.TABLE, PermissionLevels.READ, { schema: true }),
44
44
  tableController.find
45
45
  )
46
46
  /**
@@ -17,10 +17,14 @@ const Runner = new Thread(ThreadType.AUTOMATION)
17
17
  exports.processEvent = async job => {
18
18
  try {
19
19
  // need to actually await these so that an error can be captured properly
20
+ console.log(
21
+ `${job.data.automation.appId} automation ${job.data.automation._id} running`
22
+ )
20
23
  return await Runner.run(job)
21
24
  } catch (err) {
25
+ const errJson = JSON.stringify(err)
22
26
  console.error(
23
- `${job.data.automation.appId} automation ${job.data.automation._id} was unable to run - ${err}`
27
+ `${job.data.automation.appId} automation ${job.data.automation._id} was unable to run - ${errJson}`
24
28
  )
25
29
  console.trace(err)
26
30
  return { err }
@@ -182,11 +182,7 @@ export interface QueryJson {
182
182
 
183
183
  export interface SqlQuery {
184
184
  sql: string
185
- bindings?:
186
- | string[]
187
- | {
188
- [key: string]: any
189
- }
185
+ bindings?: string[]
190
186
  }
191
187
 
192
188
  export interface QueryOptions {
@@ -932,11 +932,21 @@ export interface components {
932
932
  }
933
933
  }
934
934
  /** @description The ID of the table. */
935
- _id: string
936
- }[]
937
- }
938
- /** @description The query body must contain the required parameters for the query, this depends on query type, setup and bindings. */
939
- executeQuery: { [key: string]: unknown }
935
+ _id: string;
936
+ }[];
937
+ };
938
+ /** @description The parameters required for executing a query. */
939
+ executeQuery: {
940
+ /** @description This contains the required parameters for the query, this depends on query type, setup and bindings. */
941
+ parameters?: { [key: string]: unknown };
942
+ /** @description For supported query types (currently on REST) pagination can be performed using these properties. */
943
+ pagination?: {
944
+ /** @description The page which has been returned from a previous query. */
945
+ page?: string;
946
+ /** @description The number of rows to return per page. */
947
+ limit?: number;
948
+ };
949
+ };
940
950
  executeQueryOutput: {
941
951
  /** @description The data response from the query. */
942
952
  data: { [key: string]: unknown }[]
@@ -80,6 +80,20 @@ module MySQLModule {
80
80
  },
81
81
  }
82
82
 
83
+ function bindingTypeCoerce(bindings: any[]) {
84
+ for (let i = 0; i < bindings.length; i++) {
85
+ const binding = bindings[i]
86
+ if (typeof binding !== "string") {
87
+ continue
88
+ }
89
+ const matches = binding.match(/^\d*/g)
90
+ if (matches && matches[0] !== "" && !isNaN(Number(matches[0]))) {
91
+ bindings[i] = parseFloat(binding)
92
+ }
93
+ }
94
+ return bindings
95
+ }
96
+
83
97
  class MySQLIntegration extends Sql implements DatasourcePlus {
84
98
  private config: MySQLConfig
85
99
  private client: any
@@ -122,7 +136,7 @@ module MySQLModule {
122
136
  // Node MySQL is callback based, so we must wrap our call in a promise
123
137
  const response = await this.client.query(
124
138
  query.sql,
125
- query.bindings || []
139
+ bindingTypeCoerce(query.bindings || [])
126
140
  )
127
141
  return response[0]
128
142
  } finally {
@@ -5,6 +5,7 @@ const {
5
5
  } = require("@budibase/backend-core/roles")
6
6
  const {
7
7
  PermissionTypes,
8
+ PermissionLevels,
8
9
  doesHaveBasePermission,
9
10
  } = require("@budibase/backend-core/permissions")
10
11
  const builderMiddleware = require("./builder")
@@ -64,7 +65,7 @@ const checkAuthorizedResource = async (
64
65
  }
65
66
 
66
67
  module.exports =
67
- (permType, permLevel = null) =>
68
+ (permType, permLevel = null, opts = { schema: false }) =>
68
69
  async (ctx, next) => {
69
70
  // webhooks don't need authentication, each webhook unique
70
71
  // also internal requests (between services) don't need authorized
@@ -81,15 +82,25 @@ module.exports =
81
82
  await builderMiddleware(ctx, permType)
82
83
 
83
84
  // get the resource roles
84
- let resourceRoles = []
85
+ let resourceRoles = [],
86
+ otherLevelRoles
87
+ const otherLevel =
88
+ permLevel === PermissionLevels.READ
89
+ ? PermissionLevels.WRITE
90
+ : PermissionLevels.READ
85
91
  const appId = getAppId()
86
92
  if (appId && hasResource(ctx)) {
87
93
  resourceRoles = await getRequiredResourceRole(permLevel, ctx)
94
+ if (opts && opts.schema) {
95
+ otherLevelRoles = await getRequiredResourceRole(otherLevel, ctx)
96
+ }
88
97
  }
89
98
 
90
99
  // if the resource is public, proceed
91
- const isPublicResource = resourceRoles.includes(BUILTIN_ROLE_IDS.PUBLIC)
92
- if (isPublicResource) {
100
+ if (
101
+ resourceRoles.includes(BUILTIN_ROLE_IDS.PUBLIC) ||
102
+ (otherLevelRoles && otherLevelRoles.includes(BUILTIN_ROLE_IDS.PUBLIC))
103
+ ) {
93
104
  return next()
94
105
  }
95
106
 
@@ -98,8 +109,17 @@ module.exports =
98
109
  return ctx.throw(403, "Session not authenticated")
99
110
  }
100
111
 
101
- // check authorized
102
- await checkAuthorized(ctx, resourceRoles, permType, permLevel)
112
+ try {
113
+ // check authorized
114
+ await checkAuthorized(ctx, resourceRoles, permType, permLevel)
115
+ } catch (err) {
116
+ // this is a schema, check if
117
+ if (opts && opts.schema && permLevel) {
118
+ await checkAuthorized(ctx, otherLevelRoles, permType, otherLevel)
119
+ } else {
120
+ throw err
121
+ }
122
+ }
103
123
 
104
124
  // csrf protection
105
125
  return csrf(ctx, next)