@saltcorn/server 0.7.3-beta.1 → 0.7.3-beta.6

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/admin.js CHANGED
@@ -18,7 +18,7 @@ const { spawn } = require("child_process");
18
18
  const User = require("@saltcorn/data/models/user");
19
19
  const path = require("path");
20
20
  const { getAllTenants } = require("@saltcorn/admin-models/models/tenant");
21
- const { post_btn, renderForm } = require("@saltcorn/markup");
21
+ const { post_btn, renderForm, mkTable, link } = require("@saltcorn/markup");
22
22
  const {
23
23
  div,
24
24
  a,
@@ -35,6 +35,17 @@ const {
35
35
  code,
36
36
  h5,
37
37
  pre,
38
+ button,
39
+ form,
40
+ label,
41
+ input,
42
+ select,
43
+ option,
44
+ fieldset,
45
+ legend,
46
+ ul,
47
+ li,
48
+ ol,
38
49
  } = require("@saltcorn/markup/tags");
39
50
  const db = require("@saltcorn/data/db");
40
51
  const {
@@ -136,6 +147,23 @@ const email_form = async (req) => {
136
147
  return form;
137
148
  };
138
149
 
150
+ const app_files_table = (file, req) =>
151
+ mkTable(
152
+ [
153
+ {
154
+ label: req.__("Filename"),
155
+ key: (r) => div(r.filename),
156
+ },
157
+ { label: req.__("Size (KiB)"), key: "size_kb", align: "right" },
158
+ { label: req.__("Media type"), key: (r) => r.mimetype },
159
+ {
160
+ label: req.__("Download"),
161
+ key: (r) => link(`/files/download/${r.id}`, req.__("Download")),
162
+ },
163
+ ],
164
+ [file]
165
+ );
166
+
139
167
  /**
140
168
  * Router get /
141
169
  * @name get
@@ -345,7 +373,13 @@ router.get(
345
373
  ? {
346
374
  type: "card",
347
375
  title: req.__("Automated backup"),
348
- contents: div(renderForm(backupForm, req.csrfToken())),
376
+ contents: div(
377
+ renderForm(backupForm, req.csrfToken()),
378
+ a(
379
+ { href: "/admin/auto-backup-list" },
380
+ "Restore/download automated backups »"
381
+ )
382
+ ),
349
383
  }
350
384
  : { type: "blank", contents: "" },
351
385
  ],
@@ -354,6 +388,93 @@ router.get(
354
388
  })
355
389
  );
356
390
 
391
+ /**
392
+ * @name get/backup
393
+ * @function
394
+ * @memberof module:routes/admin~routes/adminRouter
395
+ */
396
+ router.get(
397
+ "/auto-backup-list",
398
+ isAdmin,
399
+ error_catcher(async (req, res) => {
400
+ const isRoot = db.getTenantSchema() === db.connectObj.default_schema;
401
+ if (!isRoot) {
402
+ res.redirect("/admin/backup");
403
+ return;
404
+ }
405
+ const auto_backup_directory = getState().getConfig("auto_backup_directory");
406
+ const fileNms = await fs.promises.readdir(auto_backup_directory);
407
+ const backupFiles = fileNms.filter(
408
+ (fnm) => fnm.startsWith("sc-backup") && fnm.endsWith(".zip")
409
+ );
410
+ send_admin_page({
411
+ res,
412
+ req,
413
+ active_sub: "Backup",
414
+ contents: {
415
+ above: [
416
+ {
417
+ type: "card",
418
+ title: req.__("Download automated backup"),
419
+ contents: div(
420
+ ul(
421
+ backupFiles.map((fnm) =>
422
+ li(
423
+ a(
424
+ {
425
+ href: `/admin/auto-backup-download/${encodeURIComponent(
426
+ fnm
427
+ )}`,
428
+ },
429
+ fnm
430
+ )
431
+ )
432
+ )
433
+ )
434
+ ),
435
+ },
436
+ {
437
+ type: "card",
438
+ title: req.__("Restoring automated backup"),
439
+ contents: div(
440
+ ol(
441
+ li("Download one of the backups above"),
442
+ li(
443
+ a({ href: "/admin/clear-all" }, "Clear this application"),
444
+ " ",
445
+ "(tick all boxes)"
446
+ ),
447
+ li(
448
+ "When prompted to create the first user, click the link to restore a backup"
449
+ ),
450
+ li("Select the downloaded backup file")
451
+ )
452
+ ),
453
+ },
454
+ ],
455
+ },
456
+ });
457
+ })
458
+ );
459
+
460
+ router.get(
461
+ "/auto-backup-download/:filename",
462
+ isAdmin,
463
+ error_catcher(async (req, res) => {
464
+ const { filename } = req.params;
465
+ const isRoot = db.getTenantSchema() === db.connectObj.default_schema;
466
+ if (
467
+ !isRoot ||
468
+ !(filename.startsWith("sc-backup") && filename.endsWith(".zip"))
469
+ ) {
470
+ res.redirect("/admin/backup");
471
+ return;
472
+ }
473
+ const auto_backup_directory = getState().getConfig("auto_backup_directory");
474
+ res.download(path.join(auto_backup_directory, filename), filename);
475
+ })
476
+ );
477
+
357
478
  /**
358
479
  * Auto backup Form
359
480
  * @param {object} req
@@ -503,6 +624,7 @@ router.get(
503
624
  " ",
504
625
  req.__("Configuration check")
505
626
  ),
627
+
506
628
  hr(),
507
629
 
508
630
  a(
@@ -935,6 +1057,268 @@ router.get(
935
1057
  })
936
1058
  );
937
1059
 
1060
+ router.get(
1061
+ "/build-mobile-app",
1062
+ isAdmin,
1063
+ error_catcher(async (req, res) => {
1064
+ const isRoot = db.getTenantSchema() === db.connectObj.default_schema;
1065
+ const views = await View.find();
1066
+ const execBuildMsg =
1067
+ "This is still under development and might run longer.";
1068
+
1069
+ send_admin_page({
1070
+ res,
1071
+ req,
1072
+ active_sub: "Mobile app",
1073
+ contents: {
1074
+ above: [
1075
+ {
1076
+ type: "card",
1077
+ title: req.__("Build mobile app"),
1078
+ contents: form(
1079
+ {
1080
+ action: "/admin/build-mobile-app",
1081
+ method: "post",
1082
+ },
1083
+
1084
+ fieldset(
1085
+ input({
1086
+ type: "hidden",
1087
+ name: "_csrf",
1088
+ value: req.csrfToken(),
1089
+ }),
1090
+ div(
1091
+ { class: "container ps-2" },
1092
+ div(
1093
+ { class: "row pb-2" },
1094
+ div({ class: "col-sm-4 fw-bold" }, "Entry view"),
1095
+ div({ class: "col-sm-4 fw-bold" }, "Platform"),
1096
+ div(
1097
+ {
1098
+ class: "col-sm-1 fw-bold d-flex justify-content-center",
1099
+ },
1100
+ "docker"
1101
+ )
1102
+ ),
1103
+ div(
1104
+ { class: "row" },
1105
+ div(
1106
+ { class: "col-sm-4" },
1107
+ select(
1108
+ {
1109
+ class: "form-control",
1110
+ name: "entryView",
1111
+ id: "entryViewInput",
1112
+ },
1113
+ views
1114
+ .map((view) =>
1115
+ option({ value: view.name }, view.name)
1116
+ )
1117
+ .join(",")
1118
+ )
1119
+ ),
1120
+ div(
1121
+ { class: "col-sm-4" },
1122
+
1123
+ div(
1124
+ { class: "container ps-0" },
1125
+ div(
1126
+ { class: "row" },
1127
+ div({ class: "col-sm-8" }, "android"),
1128
+ div(
1129
+ { class: "col-sm" },
1130
+ input({
1131
+ type: "checkbox",
1132
+ class: "form-check-input",
1133
+ name: "androidPlatform",
1134
+ id: "androidCheckboxId",
1135
+ })
1136
+ )
1137
+ ),
1138
+ div(
1139
+ { class: "row" },
1140
+ div({ class: "col-sm-8" }, "iOS"),
1141
+ div(
1142
+ { class: "col-sm" },
1143
+ input({
1144
+ type: "checkbox",
1145
+ class: "form-check-input",
1146
+ name: "iOSPlatform",
1147
+ id: "iOSCheckboxId",
1148
+ })
1149
+ )
1150
+ )
1151
+ )
1152
+ ),
1153
+ div(
1154
+ { class: "col-sm-1 d-flex justify-content-center" },
1155
+ input({
1156
+ type: "checkbox",
1157
+ class: "form-check-input",
1158
+ name: "useDocker",
1159
+ id: "dockerCheckboxId",
1160
+ })
1161
+ )
1162
+ ),
1163
+ div(
1164
+ { class: "row pb-2" },
1165
+ div(
1166
+ { class: "col-sm-8" },
1167
+ label(
1168
+ {
1169
+ for: "appNameInputId",
1170
+ class: "form-label fw-bold",
1171
+ },
1172
+ "App file"
1173
+ ),
1174
+ input({
1175
+ type: "text",
1176
+ class: "form-control",
1177
+ name: "appFile",
1178
+ id: "appFileInputId",
1179
+ placeholder: "app-debug",
1180
+ })
1181
+ )
1182
+ ),
1183
+ div(
1184
+ { class: "row pb-3" },
1185
+ div(
1186
+ { class: "col-sm-8" },
1187
+ label(
1188
+ {
1189
+ for: "serverURLInputId",
1190
+ class: "form-label fw-bold",
1191
+ },
1192
+ "Server URL"
1193
+ ),
1194
+ input({
1195
+ type: "text",
1196
+ class: "form-control",
1197
+ name: "serverURL",
1198
+ id: "serverURLInputId",
1199
+ placeholder: "http://10.0.2.2:3000",
1200
+ })
1201
+ )
1202
+ )
1203
+ ),
1204
+ button(
1205
+ {
1206
+ type: "submit",
1207
+ onClick: `notifyAlert('${execBuildMsg}'); press_store_button(this);`,
1208
+ class: "btn btn-warning",
1209
+ },
1210
+ i({ class: "fas fa-hammer pe-2" }),
1211
+
1212
+ "Build mobile app"
1213
+ )
1214
+ )
1215
+ ),
1216
+ },
1217
+ ],
1218
+ },
1219
+ });
1220
+ })
1221
+ );
1222
+
1223
+ router.post(
1224
+ "/build-mobile-app",
1225
+ isAdmin,
1226
+ error_catcher(async (req, res) => {
1227
+ let {
1228
+ entryView,
1229
+ androidPlatform,
1230
+ iOSPlatform,
1231
+ useDocker,
1232
+ appFile,
1233
+ serverURL,
1234
+ } = req.body;
1235
+ if (!androidPlatform && !iOSPlatform) {
1236
+ req.flash(
1237
+ "error",
1238
+ req.__("Please select at least one platform (android or iOS).")
1239
+ );
1240
+ return res.redirect("/admin/build-mobile-app");
1241
+ }
1242
+ if (!androidPlatform && useDocker) {
1243
+ req.flash("error", req.__("Only the android build supports docker."));
1244
+ return res.redirect("/admin/build-mobile-app");
1245
+ }
1246
+ if (appFile && !appFile.endsWith(".apk")) appFile = `${appFile}.apk`;
1247
+ const appOut = path.join(__dirname, "..", "mobile-app-out");
1248
+ const spawnParams = [
1249
+ "build-app",
1250
+ "-v",
1251
+ entryView,
1252
+ "-c",
1253
+ appOut,
1254
+ "-b",
1255
+ "/tmp/mobile_app_build",
1256
+ ];
1257
+ if (useDocker) spawnParams.push("-d");
1258
+ if (androidPlatform) spawnParams.push("-p", "android");
1259
+ if (iOSPlatform) spawnParams.push("-p", "ios");
1260
+ if (appFile) spawnParams.push("-a", appFile);
1261
+ if (serverURL) spawnParams.push("-s", serverURL);
1262
+ const child = spawn("saltcorn", spawnParams, {
1263
+ stdio: ["ignore", "pipe", process.stderr],
1264
+ cwd: ".",
1265
+ });
1266
+ const childOutputs = [];
1267
+ child.stdout.on("data", (data) => {
1268
+ // console.log(data.toString());
1269
+ childOutputs.push(data.toString());
1270
+ });
1271
+ child.on("exit", async function (exitCode, signal) {
1272
+ if (exitCode === 0) {
1273
+ const file = await File.from_existing_file(
1274
+ appOut,
1275
+ appFile ? appFile : "app-debug.apk",
1276
+ req.user.id
1277
+ );
1278
+ res.sendWrap(req.__(`Admin`), {
1279
+ above: [
1280
+ {
1281
+ type: "card",
1282
+ title: req.__("Build Result"),
1283
+ contents: div("The build was successfully"),
1284
+ },
1285
+ app_files_table(file, req),
1286
+ ],
1287
+ });
1288
+ } else
1289
+ res.sendWrap(req.__(`Admin`), {
1290
+ above: [
1291
+ {
1292
+ type: "card",
1293
+ title: req.__("Build Result"),
1294
+ contents: div(
1295
+ "Unable to build the app:",
1296
+ pre(code(childOutputs.join("<br/>")))
1297
+ ),
1298
+ },
1299
+ ],
1300
+ });
1301
+ });
1302
+ child.on("error", function (msg) {
1303
+ const message = msg.message ? msg.message : msg.code;
1304
+ const stack = msg.stack ? msg.stack : "";
1305
+ res.sendWrap(req.__(`Admin`), {
1306
+ above: [
1307
+ {
1308
+ type: "card",
1309
+ title: req.__("Build Result"),
1310
+ contents: div(
1311
+ p("Unable to build the app:"),
1312
+ pre(code(message)),
1313
+ pre(code(stack))
1314
+ ),
1315
+ },
1316
+ ],
1317
+ });
1318
+ });
1319
+ })
1320
+ );
1321
+
938
1322
  /**
939
1323
  * @name post/clear-all
940
1324
  * @function
package/routes/fields.js CHANGED
@@ -488,6 +488,7 @@ router.get(
488
488
  },
489
489
  {
490
490
  type: "card",
491
+ class: "mt-0",
491
492
  title: wizardCardTitle(field.label, wf, wfres),
492
493
  contents: renderForm(wfres.renderForm, req.csrfToken()),
493
494
  },
@@ -524,6 +525,7 @@ router.get(
524
525
  },
525
526
  {
526
527
  type: "card",
528
+ class: "mt-0",
527
529
  title: wizardCardTitle(req.__(`New field`), wf, wfres),
528
530
  contents: renderForm(wfres.renderForm, req.csrfToken()),
529
531
  },
@@ -589,6 +591,7 @@ router.post(
589
591
  },
590
592
  {
591
593
  type: "card",
594
+ class: "mt-0",
592
595
  title: wizardCardTitle(
593
596
  wfres.context.label || req.__("New field"),
594
597
  wf,
package/routes/files.js CHANGED
@@ -184,11 +184,11 @@ router.get(
184
184
  * @function
185
185
  */
186
186
  router.get(
187
- "/resize/:id/:width_str",
187
+ "/resize/:id/:width_str/:height_str?",
188
188
  error_catcher(async (req, res) => {
189
189
  const role = req.user && req.user.id ? req.user.role_id : 10;
190
190
  const user_id = req.user && req.user.id;
191
- const { id, width_str } = req.params;
191
+ const { id, width_str, height_str } = req.params;
192
192
  let file;
193
193
  if (typeof strictParseInt(id) !== "undefined")
194
194
  file = await File.findOne({ id });
@@ -208,15 +208,17 @@ router.get(
208
208
  if (file.s3_store) s3storage.serveObject(file, res, false);
209
209
  else {
210
210
  const width = strictParseInt(width_str);
211
+ const height = height_str ? strictParseInt(height_str) : null;
211
212
  if (!width) {
212
213
  res.sendFile(file.location);
213
214
  return;
214
215
  }
215
- const fnm = `${file.location}_w${width}`;
216
+ const fnm = `${file.location}_w${width}${height ? `_h${height}` : ""}`;
216
217
  if (!fs.existsSync(fnm)) {
217
218
  await resizer({
218
219
  fromFileName: file.location,
219
220
  width,
221
+ height,
220
222
  toFileName: fnm,
221
223
  });
222
224
  }
@@ -49,7 +49,7 @@ const tableTable = (tables, req) =>
49
49
  */
50
50
  const tableCard = (tables, req) => ({
51
51
  type: "card",
52
- class: "welcome-page-entity-list",
52
+ class: "welcome-page-entity-list mt-1",
53
53
  title: link("/table", req.__("Tables")),
54
54
  contents:
55
55
  (tables.length <= 1
@@ -102,7 +102,7 @@ const viewTable = (views, req) =>
102
102
  const viewCard = (views, req) => ({
103
103
  type: "card",
104
104
  title: link("/viewedit", req.__("Views")),
105
- class: "welcome-page-entity-list",
105
+ class: "welcome-page-entity-list mt-1",
106
106
  bodyClass: "py-0 pe-0",
107
107
  contents:
108
108
  (views.length <= 1
@@ -156,7 +156,7 @@ const pageTable = (pages, req) =>
156
156
  const pageCard = (pages, req) => ({
157
157
  type: "card",
158
158
  title: link("/pageedit", req.__("Pages")),
159
- class: "welcome-page-entity-list",
159
+ class: "welcome-page-entity-list mt-1",
160
160
  contents:
161
161
  (pages.length <= 1
162
162
  ? p(
@@ -369,9 +369,9 @@ const welcome_page = async (req) => {
369
369
  above: [
370
370
  {
371
371
  besides: [
372
- pageCard(pages, req),
373
- viewCard(views, req),
374
372
  tableCard(tables, req),
373
+ viewCard(views, req),
374
+ pageCard(pages, req),
375
375
  ],
376
376
  },
377
377
  {
@@ -380,7 +380,7 @@ const welcome_page = async (req) => {
380
380
  type: "card",
381
381
  //title: req.__("Install pack"),
382
382
  bodyClass: "py-0 pe-0",
383
- class: "welcome-page-entity-list",
383
+ class: "welcome-page-entity-list mt-2",
384
384
 
385
385
  tabContents:
386
386
  triggers.length > 0
@@ -399,7 +399,7 @@ const welcome_page = async (req) => {
399
399
  type: "card",
400
400
  //title: req.__("Learn"),
401
401
  bodyClass: "py-0 pe-0",
402
- class: "welcome-page-entity-list",
402
+ class: "welcome-page-entity-list mt-2",
403
403
  tabContents:
404
404
  users.length > 4
405
405
  ? {
package/routes/menu.js CHANGED
@@ -84,7 +84,15 @@ const menuForm = async (req) => {
84
84
  input_type: "select",
85
85
  class: "menutype item-menu",
86
86
  required: true,
87
- options: ["View", "Page", "Link", "Header", "Dynamic", "Search"],
87
+ options: [
88
+ "View",
89
+ "Page",
90
+ "Link",
91
+ "Header",
92
+ "Dynamic",
93
+ "Search",
94
+ "Separator",
95
+ ],
88
96
  },
89
97
  {
90
98
  name: "text",
@@ -92,6 +100,9 @@ const menuForm = async (req) => {
92
100
  class: "item-menu",
93
101
  input_type: "text",
94
102
  required: true,
103
+ showIf: {
104
+ type: ["View", "Page", "Link", "Header", "Dynamic", "Search"],
105
+ },
95
106
  },
96
107
  {
97
108
  name: "icon_btn",
@@ -271,6 +282,7 @@ const menuEditorScript = (menu_items) => `
271
282
  {
272
283
  listOptions: sortableListOptions,
273
284
  iconPicker: iconPickerOptions,
285
+ getLabelText: (item) => item?.text || item?.type,
274
286
  labelEdit: 'Edit&nbsp;<i class="fas fa-edit clickable"></i>',
275
287
  maxLevel: 1 // (Optional) Default is -1 (no level limit)
276
288
  // Valid levels are from [0, 1, 2, 3,...N]
@@ -326,6 +326,7 @@ router.get(
326
326
  {
327
327
  type: "card",
328
328
  title: req.__("Your pages"),
329
+ class: "mt-0",
329
330
  contents: getPageList(pages, roles, req),
330
331
  },
331
332
  {
package/routes/plugins.js CHANGED
@@ -506,6 +506,7 @@ const plugin_store_html = (items, req) => {
506
506
  },
507
507
  {
508
508
  type: "card",
509
+ class: "mt-0",
509
510
  contents: div(
510
511
  { class: "d-flex justify-content-between" },
511
512
  storeNavPills(req),
package/routes/tables.js CHANGED
@@ -448,6 +448,7 @@ router.get(
448
448
  },
449
449
  {
450
450
  type: "card",
451
+ class: "mt-0",
451
452
  title: cardHeaderTabs([
452
453
  { label: req.__("Your tables"), href: "/table" },
453
454
  {
@@ -636,7 +637,9 @@ router.get(
636
637
  }
637
638
  var viewCard;
638
639
  if (fields.length > 0) {
639
- const views = await View.find({ table_id: table.id });
640
+ const views = await View.find(
641
+ table.external ? { exttable_name: table.name } : { table_id: table.id }
642
+ );
640
643
  var viewCardContents;
641
644
  if (views.length > 0) {
642
645
  viewCardContents = mkTable(
@@ -801,15 +804,12 @@ router.get(
801
804
  type: "breadcrumbs",
802
805
  crumbs: [
803
806
  { text: req.__("Tables"), href: "/table" },
804
- { text: table.name },
807
+ { text: span({ class: "fw-bold text-body" }, table.name) },
805
808
  ],
806
809
  },
807
- {
808
- type: "pageHeader",
809
- title: req.__(`%s table`, table.name),
810
- },
811
810
  {
812
811
  type: "card",
812
+ class: "mt-0",
813
813
  title: req.__("Fields"),
814
814
  contents: fieldCard,
815
815
  },
@@ -1076,6 +1076,7 @@ router.get(
1076
1076
  },
1077
1077
  {
1078
1078
  type: "card",
1079
+ class: "mt-0",
1079
1080
  title: cardHeaderTabs([
1080
1081
  { label: req.__("Your tables"), href: "/table", active: true },
1081
1082
  {
@@ -197,6 +197,7 @@ router.get(
197
197
  },
198
198
  {
199
199
  type: "card",
200
+ class: "mt-0",
200
201
  title: req.__("Your views"),
201
202
  contents: [
202
203
  viewMarkup,
@@ -378,6 +379,7 @@ router.get(
378
379
  },
379
380
  {
380
381
  type: "card",
382
+ class: "mt-0",
381
383
  title: req.__(`Edit %s view`, viewname),
382
384
  contents: renderForm(form, req.csrfToken()),
383
385
  },
@@ -415,6 +417,7 @@ router.get(
415
417
  },
416
418
  {
417
419
  type: "card",
420
+ class: "mt-0",
418
421
  title: req.__(`Create view`),
419
422
  contents: renderForm(form, req.csrfToken()),
420
423
  },
@@ -452,6 +455,7 @@ router.post(
452
455
  },
453
456
  {
454
457
  type: "card",
458
+ class: "mt-0",
455
459
  title: req.__(`Edit view`),
456
460
  contents: renderForm(form, req.csrfToken()),
457
461
  },
@@ -527,6 +531,7 @@ const respondWorkflow = (view, wf, wfres, req, res) => {
527
531
  },
528
532
  {
529
533
  type: noCard ? "container" : "card",
534
+ class: !noCard && "mt-0",
530
535
  title: wfres.title,
531
536
  contents,
532
537
  },
package/serve.js CHANGED
@@ -348,7 +348,7 @@ const setupSocket = (...servers) => {
348
348
  io.use(wrap(setTenant));
349
349
  io.use(wrap(getSessionStore()));
350
350
  io.use(wrap(passport.initialize()));
351
- io.use(wrap(passport.session()));
351
+ io.use(wrap(passport.authenticate(["jwt", "session"])));
352
352
  if (process.send && !cluster.isMaster) io.adapter(createAdapter());
353
353
  getState().setRoomEmitter((viewname, room_id, msg) => {
354
354
  io.to(`${viewname}_${room_id}`).emit("message", msg);