@saltcorn/server 0.6.2-beta.1 → 0.6.2-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/routes/scapi.js CHANGED
@@ -9,7 +9,7 @@
9
9
  /** @type {module:express-promise-router} */
10
10
  const Router = require("express-promise-router");
11
11
  //const db = require("@saltcorn/data/db");
12
- const { setTenant, error_catcher } = require("./utils.js");
12
+ const { error_catcher } = require("./utils.js");
13
13
  const Table = require("@saltcorn/data/models/table");
14
14
  const View = require("@saltcorn/data/models/view");
15
15
  const Page = require("@saltcorn/data/models/page");
@@ -43,18 +43,17 @@ module.exports = router;
43
43
  * @param {object} user user based on access token
44
44
  * @returns {boolean}
45
45
  */
46
- function accessAllowedRead(req, user){
47
- const role = req.isAuthenticated()
48
- ? req.user.role_id
49
- : user && user.role_id
50
- ? user.role_id
51
- : 10;
52
-
53
- if (role === 1) return true;
54
- return false;
46
+ function accessAllowedRead(req, user) {
47
+ const role = req.isAuthenticated()
48
+ ? req.user.role_id
49
+ : user && user.role_id
50
+ ? user.role_id
51
+ : 10;
52
+
53
+ if (role === 1) return true;
54
+ return false;
55
55
  }
56
56
 
57
-
58
57
  // todo add paging
59
58
  // todo more granular access rights for api. Currently only admin can call this api.
60
59
  // todo add support of fields
@@ -66,25 +65,22 @@ function accessAllowedRead(req, user){
66
65
  * @function
67
66
  */
68
67
  router.get(
69
- "/sc_tables/",
70
- setTenant,
71
- error_catcher(async (req, res, next) => {
72
-
73
- await passport.authenticate(
74
- "api-bearer",
75
- { session: false },
76
- async function (err, user, info) {
77
- if (accessAllowedRead(req, user)) {
78
-
79
- const tables = await Table.find({});
80
-
81
- res.json({ success: tables });
82
- } else {
83
- res.status(401).json({ error: req.__("Not authorized") });
84
- }
85
- }
86
- )(req, res, next);
87
- })
68
+ "/sc_tables/",
69
+ error_catcher(async (req, res, next) => {
70
+ await passport.authenticate(
71
+ "api-bearer",
72
+ { session: false },
73
+ async function (err, user, info) {
74
+ if (accessAllowedRead(req, user)) {
75
+ const tables = await Table.find({});
76
+
77
+ res.json({ success: tables });
78
+ } else {
79
+ res.status(401).json({ error: req.__("Not authorized") });
80
+ }
81
+ }
82
+ )(req, res, next);
83
+ })
88
84
  );
89
85
 
90
86
  // todo add paging
@@ -97,28 +93,24 @@ router.get(
97
93
  * @function
98
94
  */
99
95
  router.get(
100
- "/sc_views/",
101
- setTenant,
102
- error_catcher(async (req, res, next) => {
103
-
104
- await passport.authenticate(
105
- "api-bearer",
106
- { session: false },
107
- async function (err, user, info) {
108
- if (accessAllowedRead(req, user)) {
109
-
110
- const views = await View.find({});
111
-
112
- res.json({ success: views });
113
- } else {
114
- res.status(401).json({ error: req.__("Not authorized") });
115
- }
116
- }
117
- )(req, res, next);
118
- })
96
+ "/sc_views/",
97
+ error_catcher(async (req, res, next) => {
98
+ await passport.authenticate(
99
+ "api-bearer",
100
+ { session: false },
101
+ async function (err, user, info) {
102
+ if (accessAllowedRead(req, user)) {
103
+ const views = await View.find({});
104
+
105
+ res.json({ success: views });
106
+ } else {
107
+ res.status(401).json({ error: req.__("Not authorized") });
108
+ }
109
+ }
110
+ )(req, res, next);
111
+ })
119
112
  );
120
113
 
121
-
122
114
  // todo add paging
123
115
  // todo more granular access rights to api. Currently only admin can call this api.
124
116
  /**
@@ -129,25 +121,22 @@ router.get(
129
121
  * @function
130
122
  */
131
123
  router.get(
132
- "/sc_pages/",
133
- setTenant,
134
- error_catcher(async (req, res, next) => {
135
-
136
- await passport.authenticate(
137
- "api-bearer",
138
- { session: false },
139
- async function (err, user, info) {
140
- if (accessAllowedRead(req, user)) {
141
-
142
- const pages = await Page.find({});
143
-
144
- res.json({ success: pages });
145
- } else {
146
- res.status(401).json({ error: req.__("Not authorized") });
147
- }
148
- }
149
- )(req, res, next);
150
- })
124
+ "/sc_pages/",
125
+ error_catcher(async (req, res, next) => {
126
+ await passport.authenticate(
127
+ "api-bearer",
128
+ { session: false },
129
+ async function (err, user, info) {
130
+ if (accessAllowedRead(req, user)) {
131
+ const pages = await Page.find({});
132
+
133
+ res.json({ success: pages });
134
+ } else {
135
+ res.status(401).json({ error: req.__("Not authorized") });
136
+ }
137
+ }
138
+ )(req, res, next);
139
+ })
151
140
  );
152
141
 
153
142
  // todo add paging
@@ -160,25 +149,22 @@ router.get(
160
149
  * @function
161
150
  */
162
151
  router.get(
163
- "/sc_files/",
164
- setTenant,
165
- error_catcher(async (req, res, next) => {
166
-
167
- await passport.authenticate(
168
- "api-bearer",
169
- { session: false },
170
- async function (err, user, info) {
171
- if (accessAllowedRead(req, user)) {
172
-
173
- const files = await File.find({});
174
-
175
- res.json({ success: files });
176
- } else {
177
- res.status(401).json({ error: req.__("Not authorized") });
178
- }
179
- }
180
- )(req, res, next);
181
- })
152
+ "/sc_files/",
153
+ error_catcher(async (req, res, next) => {
154
+ await passport.authenticate(
155
+ "api-bearer",
156
+ { session: false },
157
+ async function (err, user, info) {
158
+ if (accessAllowedRead(req, user)) {
159
+ const files = await File.find({});
160
+
161
+ res.json({ success: files });
162
+ } else {
163
+ res.status(401).json({ error: req.__("Not authorized") });
164
+ }
165
+ }
166
+ )(req, res, next);
167
+ })
182
168
  );
183
169
 
184
170
  // todo add paging
@@ -191,25 +177,22 @@ router.get(
191
177
  * @function
192
178
  */
193
179
  router.get(
194
- "/sc_triggers/",
195
- setTenant,
196
- error_catcher(async (req, res, next) => {
197
-
198
- await passport.authenticate(
199
- "api-bearer",
200
- { session: false },
201
- async function (err, user, info) {
202
- if (accessAllowedRead(req, user)) {
203
-
204
- const triggers = await Trigger.find({});
205
-
206
- res.json({ success: triggers });
207
- } else {
208
- res.status(401).json({ error: req.__("Not authorized") });
209
- }
210
- }
211
- )(req, res, next);
212
- })
180
+ "/sc_triggers/",
181
+ error_catcher(async (req, res, next) => {
182
+ await passport.authenticate(
183
+ "api-bearer",
184
+ { session: false },
185
+ async function (err, user, info) {
186
+ if (accessAllowedRead(req, user)) {
187
+ const triggers = await Trigger.find({});
188
+
189
+ res.json({ success: triggers });
190
+ } else {
191
+ res.status(401).json({ error: req.__("Not authorized") });
192
+ }
193
+ }
194
+ )(req, res, next);
195
+ })
213
196
  );
214
197
 
215
198
  // todo add paging
@@ -222,25 +205,22 @@ router.get(
222
205
  * @function
223
206
  */
224
207
  router.get(
225
- "/sc_roles/",
226
- setTenant,
227
- error_catcher(async (req, res, next) => {
228
-
229
- await passport.authenticate(
230
- "api-bearer",
231
- { session: false },
232
- async function (err, user, info) {
233
- if (accessAllowedRead(req, user)) {
234
-
235
- const roles = await Role.find({});
236
-
237
- res.json({ success: roles });
238
- } else {
239
- res.status(401).json({ error: req.__("Not authorized") });
240
- }
241
- }
242
- )(req, res, next);
243
- })
208
+ "/sc_roles/",
209
+ error_catcher(async (req, res, next) => {
210
+ await passport.authenticate(
211
+ "api-bearer",
212
+ { session: false },
213
+ async function (err, user, info) {
214
+ if (accessAllowedRead(req, user)) {
215
+ const roles = await Role.find({});
216
+
217
+ res.json({ success: roles });
218
+ } else {
219
+ res.status(401).json({ error: req.__("Not authorized") });
220
+ }
221
+ }
222
+ )(req, res, next);
223
+ })
244
224
  );
245
225
 
246
226
  // todo add paging
@@ -253,25 +233,22 @@ router.get(
253
233
  * @function
254
234
  */
255
235
  router.get(
256
- "/sc_tenants/",
257
- setTenant,
258
- error_catcher(async (req, res, next) => {
259
-
260
- await passport.authenticate(
261
- "api-bearer",
262
- { session: false },
263
- async function (err, user, info) {
264
- if (accessAllowedRead(req, user)) {
265
-
266
- const tenants = await Tenant.getAllTenants();
267
-
268
- res.json({ success: tenants });
269
- } else {
270
- res.status(401).json({ error: req.__("Not authorized") });
271
- }
272
- }
273
- )(req, res, next);
274
- })
236
+ "/sc_tenants/",
237
+ error_catcher(async (req, res, next) => {
238
+ await passport.authenticate(
239
+ "api-bearer",
240
+ { session: false },
241
+ async function (err, user, info) {
242
+ if (accessAllowedRead(req, user)) {
243
+ const tenants = await Tenant.getAllTenants();
244
+
245
+ res.json({ success: tenants });
246
+ } else {
247
+ res.status(401).json({ error: req.__("Not authorized") });
248
+ }
249
+ }
250
+ )(req, res, next);
251
+ })
275
252
  );
276
253
 
277
254
  // todo add paging
@@ -284,25 +261,22 @@ router.get(
284
261
  * @function
285
262
  */
286
263
  router.get(
287
- "/sc_plugins/",
288
- setTenant,
289
- error_catcher(async (req, res, next) => {
290
-
291
- await passport.authenticate(
292
- "api-bearer",
293
- { session: false },
294
- async function (err, user, info) {
295
- if (accessAllowedRead(req, user)) {
296
-
297
- const plugins = await Plugin.find({});
298
-
299
- res.json({ success: plugins });
300
- } else {
301
- res.status(401).json({ error: req.__("Not authorized") });
302
- }
303
- }
304
- )(req, res, next);
305
- })
264
+ "/sc_plugins/",
265
+ error_catcher(async (req, res, next) => {
266
+ await passport.authenticate(
267
+ "api-bearer",
268
+ { session: false },
269
+ async function (err, user, info) {
270
+ if (accessAllowedRead(req, user)) {
271
+ const plugins = await Plugin.find({});
272
+
273
+ res.json({ success: plugins });
274
+ } else {
275
+ res.status(401).json({ error: req.__("Not authorized") });
276
+ }
277
+ }
278
+ )(req, res, next);
279
+ })
306
280
  );
307
281
 
308
282
  // todo add paging
@@ -315,23 +289,20 @@ router.get(
315
289
  * @function
316
290
  */
317
291
  router.get(
318
- "/sc_config/",
319
- setTenant,
320
- error_catcher(async (req, res, next) => {
321
-
322
- await passport.authenticate(
323
- "api-bearer",
324
- { session: false },
325
- async function (err, user, info) {
326
- if (accessAllowedRead(req, user)) {
327
-
328
- const configVars = await Config.getAllConfig();
329
-
330
- res.json({ success: configVars });
331
- } else {
332
- res.status(401).json({ error: req.__("Not authorized") });
333
- }
334
- }
335
- )(req, res, next);
336
- })
337
- );
292
+ "/sc_config/",
293
+ error_catcher(async (req, res, next) => {
294
+ await passport.authenticate(
295
+ "api-bearer",
296
+ { session: false },
297
+ async function (err, user, info) {
298
+ if (accessAllowedRead(req, user)) {
299
+ const configVars = await Config.getAllConfig();
300
+
301
+ res.json({ success: configVars });
302
+ } else {
303
+ res.status(401).json({ error: req.__("Not authorized") });
304
+ }
305
+ }
306
+ )(req, res, next);
307
+ })
308
+ );
package/routes/search.js CHANGED
@@ -8,7 +8,7 @@ const Router = require("express-promise-router");
8
8
  const { span, h5, h4, nbsp, p, a, div } = require("@saltcorn/markup/tags");
9
9
 
10
10
  const { getState } = require("@saltcorn/data/db/state");
11
- const { setTenant, isAdmin, error_catcher } = require("./utils.js");
11
+ const { isAdmin, error_catcher } = require("./utils.js");
12
12
  const Form = require("@saltcorn/data/models/form");
13
13
  const Table = require("@saltcorn/data/models/table");
14
14
  const View = require("@saltcorn/data/models/view");
@@ -27,9 +27,9 @@ const router = new Router();
27
27
  module.exports = router;
28
28
 
29
29
  /**
30
- * @param {object[]} tables
31
- * @param {object[]} views
32
- * @param {object} req
30
+ * @param {object[]} tables
31
+ * @param {object[]} views
32
+ * @param {object} req
33
33
  * @returns {Forms}
34
34
  */
35
35
  const searchConfigForm = (tables, views, req) => {
@@ -75,7 +75,6 @@ const searchConfigForm = (tables, views, req) => {
75
75
  */
76
76
  router.get(
77
77
  "/config",
78
- setTenant,
79
78
  isAdmin,
80
79
  error_catcher(async (req, res) => {
81
80
  var views = await View.find({}, { orderBy: "name" });
@@ -95,7 +94,6 @@ router.get(
95
94
  })
96
95
  );
97
96
 
98
-
99
97
  /**
100
98
  * @name post/config
101
99
  * @function
@@ -104,7 +102,6 @@ router.get(
104
102
  */
105
103
  router.post(
106
104
  "/config",
107
- setTenant,
108
105
  isAdmin,
109
106
  error_catcher(async (req, res) => {
110
107
  var views = await View.find({}, { orderBy: "name" });
@@ -154,8 +151,8 @@ const searchForm = () =>
154
151
  * @param {*} opts._page
155
152
  * @param {*} opts.table
156
153
  * @param {object} opts
157
- * @param {object} req
158
- * @param {object} res
154
+ * @param {object} req
155
+ * @param {object} res
159
156
  * @returns {Promise<void>}
160
157
  */
161
158
  const runSearch = async ({ q, _page, table }, req, res) => {
@@ -253,7 +250,6 @@ const runSearch = async ({ q, _page, table }, req, res) => {
253
250
  */
254
251
  router.get(
255
252
  "/",
256
- setTenant,
257
253
  error_catcher(async (req, res) => {
258
254
  if (req.query && req.query.q) {
259
255
  await runSearch(req.query, req, res);
@@ -6,7 +6,7 @@
6
6
 
7
7
  const Router = require("express-promise-router");
8
8
  const { i, h3, p, a } = require("@saltcorn/markup/tags");
9
- const { setTenant, isAdmin, error_catcher } = require("./utils.js");
9
+ const { isAdmin, error_catcher } = require("./utils.js");
10
10
 
11
11
  /**
12
12
  * @type {object}
@@ -46,7 +46,6 @@ const settingsCard = ({ title, icon, blurb, href }) => ({
46
46
  */
47
47
  router.get(
48
48
  "/",
49
- setTenant,
50
49
  isAdmin,
51
50
  error_catcher(async (req, res) => {
52
51
  res.sendWrap(req.__("Settings"), {
package/routes/tables.js CHANGED
@@ -22,7 +22,7 @@ const {
22
22
  post_dropdown_item,
23
23
  } = require("@saltcorn/markup");
24
24
  const { recalculate_for_stored } = require("@saltcorn/data/models/expression");
25
- const { setTenant, isAdmin, error_catcher } = require("./utils.js");
25
+ const { isAdmin, error_catcher } = require("./utils.js");
26
26
  const Form = require("@saltcorn/data/models/form");
27
27
  const {
28
28
  span,
@@ -171,7 +171,6 @@ const tableForm = async (table, req) => {
171
171
  */
172
172
  router.get(
173
173
  "/new/",
174
- setTenant,
175
174
  isAdmin,
176
175
  error_catcher(async (req, res) => {
177
176
  res.sendWrap(req.__(`New table`), {
@@ -241,7 +240,6 @@ const discoverForm = (tables, req) => {
241
240
  */
242
241
  router.get(
243
242
  "/discover",
244
- setTenant,
245
243
  isAdmin,
246
244
  error_catcher(async (req, res) => {
247
245
  // get list of discoverable tables
@@ -276,7 +274,6 @@ router.get(
276
274
  */
277
275
  router.post(
278
276
  "/discover",
279
- setTenant,
280
277
  isAdmin,
281
278
  error_catcher(async (req, res) => {
282
279
  const tbls = await discoverable_tables();
@@ -304,7 +301,6 @@ router.post(
304
301
  */
305
302
  router.get(
306
303
  "/create-from-csv",
307
- setTenant,
308
304
  isAdmin,
309
305
  error_catcher(async (req, res) => {
310
306
  res.sendWrap(req.__(`Create table from CSV file`), {
@@ -356,7 +352,6 @@ router.get(
356
352
  */
357
353
  router.post(
358
354
  "/create-from-csv",
359
- setTenant,
360
355
  isAdmin,
361
356
  error_catcher(async (req, res) => {
362
357
  if (req.body.name && req.files && req.files.file) {
@@ -405,7 +400,6 @@ router.post(
405
400
  */
406
401
  router.get(
407
402
  "/relationship-diagram",
408
- setTenant,
409
403
  isAdmin,
410
404
  error_catcher(async (req, res) => {
411
405
  const tables = await Table.find_with_external({}, { orderBy: "name" });
@@ -535,7 +529,6 @@ const attribBadges = (f) => {
535
529
  */
536
530
  router.get(
537
531
  "/:idorname",
538
- setTenant,
539
532
  isAdmin,
540
533
  error_catcher(async (req, res) => {
541
534
  const { idorname } = req.params;
@@ -843,7 +836,6 @@ router.get(
843
836
  */
844
837
  router.post(
845
838
  "/",
846
- setTenant,
847
839
  isAdmin,
848
840
  error_catcher(async (req, res) => {
849
841
  const v = req.body;
@@ -918,7 +910,6 @@ router.post(
918
910
  */
919
911
  router.post(
920
912
  "/delete/:id",
921
- setTenant,
922
913
  isAdmin,
923
914
  error_catcher(async (req, res) => {
924
915
  const { id } = req.params;
@@ -945,7 +936,6 @@ router.post(
945
936
  );
946
937
  router.post(
947
938
  "/forget-table/:id",
948
- setTenant,
949
939
  isAdmin,
950
940
  error_catcher(async (req, res) => {
951
941
  const { id } = req.params;
@@ -1000,7 +990,6 @@ const tableBadges = (t, req) => {
1000
990
  */
1001
991
  router.get(
1002
992
  "/",
1003
- setTenant,
1004
993
  isAdmin,
1005
994
  error_catcher(async (req, res) => {
1006
995
  const rows = await Table.find_with_external({}, { orderBy: "name" });
@@ -1095,7 +1084,6 @@ router.get(
1095
1084
  */
1096
1085
  router.get(
1097
1086
  "/download/:name",
1098
- setTenant,
1099
1087
  isAdmin,
1100
1088
  error_catcher(async (req, res) => {
1101
1089
  const { name } = req.params;
@@ -1125,7 +1113,6 @@ router.get(
1125
1113
  */
1126
1114
  router.get(
1127
1115
  "/constraints/:id",
1128
- setTenant,
1129
1116
  isAdmin,
1130
1117
  error_catcher(async (req, res) => {
1131
1118
  const { id } = req.params;
@@ -1203,7 +1190,6 @@ const constraintForm = (req, table_id, fields) =>
1203
1190
  */
1204
1191
  router.get(
1205
1192
  "/add-constraint/:id",
1206
- setTenant,
1207
1193
  isAdmin,
1208
1194
  error_catcher(async (req, res) => {
1209
1195
  const { id } = req.params;
@@ -1248,7 +1234,6 @@ router.get(
1248
1234
  */
1249
1235
  router.post(
1250
1236
  "/add-constraint/:id",
1251
- setTenant,
1252
1237
  isAdmin,
1253
1238
  error_catcher(async (req, res) => {
1254
1239
  const { id } = req.params;
@@ -1303,7 +1288,6 @@ const renameForm = (table_id, req) =>
1303
1288
  */
1304
1289
  router.get(
1305
1290
  "/rename/:id",
1306
- setTenant,
1307
1291
  isAdmin,
1308
1292
  error_catcher(async (req, res) => {
1309
1293
  const { id } = req.params;
@@ -1341,7 +1325,6 @@ router.get(
1341
1325
  */
1342
1326
  router.post(
1343
1327
  "/rename/:id",
1344
- setTenant,
1345
1328
  isAdmin,
1346
1329
  error_catcher(async (req, res) => {
1347
1330
  const { id } = req.params;
@@ -1366,7 +1349,6 @@ router.post(
1366
1349
  */
1367
1350
  router.post(
1368
1351
  "/delete-constraint/:id",
1369
- setTenant,
1370
1352
  isAdmin,
1371
1353
  error_catcher(async (req, res) => {
1372
1354
  const { id } = req.params;
@@ -1385,7 +1367,6 @@ router.post(
1385
1367
  */
1386
1368
  router.post(
1387
1369
  "/upload_to_table/:name",
1388
- setTenant,
1389
1370
  isAdmin,
1390
1371
  error_catcher(async (req, res) => {
1391
1372
  const { name } = req.params;
@@ -1421,7 +1402,6 @@ router.post(
1421
1402
  */
1422
1403
  router.post(
1423
1404
  "/delete-all-rows/:name",
1424
- setTenant,
1425
1405
  isAdmin,
1426
1406
  error_catcher(async (req, res) => {
1427
1407
  const { name } = req.params;
@@ -1447,7 +1427,6 @@ router.post(
1447
1427
  */
1448
1428
  router.post(
1449
1429
  "/recalc-stored/:name",
1450
- setTenant,
1451
1430
  isAdmin,
1452
1431
  error_catcher(async (req, res) => {
1453
1432
  const { name } = req.params;