nodebb-plugin-calendar-onekite 11.1.18 → 11.1.20

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 (2) hide show
  1. package/library.js +36 -33
  2. package/package.json +1 -1
package/library.js CHANGED
@@ -20,62 +20,65 @@ const mw = (...fns) => fns.filter(isFn);
20
20
  Plugin.init = async function (params) {
21
21
  const { router, middleware } = params;
22
22
 
23
+ // Build middleware arrays safely and always spread them into Express route methods.
24
+ // Express will throw if any callback is undefined, so we filter strictly.
25
+ const publicExpose = mw(middleware && middleware.exposeUid);
26
+ const publicAuth = mw(middleware && middleware.exposeUid, middleware && middleware.ensureLoggedIn);
27
+
28
+ // Robust admin guard: avoid middleware.admin.checkPrivileges() signature differences
29
+ // across NodeBB versions. We treat membership in the 'administrators' group as admin.
30
+ const Groups = require.main.require('./src/groups');
31
+ async function adminOnly(req, res, next) {
32
+ try {
33
+ const uid = req.uid;
34
+ if (!uid) {
35
+ return res.status(401).json({ status: { code: 'not-authorized', message: 'Not logged in' } });
36
+ }
37
+ const isAdmin = await Groups.isMember(uid, 'administrators');
38
+ if (!isAdmin) {
39
+ return res.status(403).json({ status: { code: 'not-authorized', message: 'Not allowed' } });
40
+ }
41
+ return next();
42
+ } catch (err) {
43
+ return next(err);
44
+ }
45
+ }
46
+ const adminMws = mw(middleware && middleware.exposeUid, middleware && middleware.ensureLoggedIn, adminOnly);
47
+
23
48
  // Page routes (HTML)
24
49
  // IMPORTANT: pass an ARRAY for middlewares (even if empty), otherwise
25
50
  // setupPageRoute will throw "middlewares is not iterable".
26
51
  routeHelpers.setupPageRoute(router, '/calendar', mw(), controllers.renderCalendar);
27
52
  routeHelpers.setupAdminPageRoute(router, '/admin/plugins/calendar-onekite', mw(), admin.renderAdmin);
28
53
 
29
- // Public API (JSON)
54
+ // Public API (JSON)
30
55
  // We register both v3 and legacy (/api/...) endpoints as a compatibility fallback.
31
- const publicExpose = mw(middleware.exposeUid);
32
- const publicAuth = mw(middleware.exposeUid, middleware.ensureLoggedIn);
33
56
 
34
57
  ['/api/v3/plugins/calendar-onekite/events', '/api/plugins/calendar-onekite/events'].forEach((p) => {
35
- router.get(p, publicExpose, api.getEvents);
58
+ router.get(p, ...publicExpose, api.getEvents);
36
59
  });
37
60
 
38
61
  ['/api/v3/plugins/calendar-onekite/items', '/api/plugins/calendar-onekite/items'].forEach((p) => {
39
- router.get(p, publicExpose, api.getItems);
62
+ router.get(p, ...publicExpose, api.getItems);
40
63
  });
41
64
 
42
65
  ['/api/v3/plugins/calendar-onekite/reservations', '/api/plugins/calendar-onekite/reservations'].forEach((p) => {
43
- router.post(p, publicAuth, api.createReservation);
66
+ router.post(p, ...publicAuth, api.createReservation);
44
67
  });
45
68
 
46
69
  // Admin API (JSON)
47
- // Admin guard: avoid calling middleware.admin.checkPrivileges directly because its signature
48
- // differs across NodeBB versions (and can crash the server if invoked incorrectly).
49
- const Groups = require.main.require('./src/groups');
50
- const requireAdmin = async (req, res, next) => {
51
- try {
52
- if (!req.uid) {
53
- return res.status(403).json({ status: { code: 'forbidden', message: 'not-logged-in' } });
54
- }
55
- const isAdmin = await Groups.isMember(req.uid, 'administrators');
56
- if (!isAdmin) {
57
- return res.status(403).json({ status: { code: 'forbidden', message: 'admin-only' } });
58
- }
59
- return next();
60
- } catch (err) {
61
- return next(err);
62
- }
63
- };
64
-
65
- const adminMws = mw(middleware.exposeUid, middleware.ensureLoggedIn, requireAdmin);
66
-
67
70
  const adminBases = ['/api/v3/admin/plugins/calendar-onekite', '/api/admin/plugins/calendar-onekite'];
68
71
 
69
72
  adminBases.forEach((base) => {
70
- router.get(`${base}/settings`, adminMws, admin.getSettings);
71
- router.put(`${base}/settings`, adminMws, admin.saveSettings);
73
+ router.get(`${base}/settings`, ...adminMws, admin.getSettings);
74
+ router.put(`${base}/settings`, ...adminMws, admin.saveSettings);
72
75
 
73
- router.get(`${base}/pending`, adminMws, admin.listPending);
74
- router.put(`${base}/reservations/:rid/approve`, adminMws, admin.approveReservation);
75
- router.put(`${base}/reservations/:rid/refuse`, adminMws, admin.refuseReservation);
76
+ router.get(`${base}/pending`, ...adminMws, admin.listPending);
77
+ router.put(`${base}/reservations/:rid/approve`, ...adminMws, admin.approveReservation);
78
+ router.put(`${base}/reservations/:rid/refuse`, ...adminMws, admin.refuseReservation);
76
79
 
77
- router.post(`${base}/purge`, adminMws, admin.purgeByYear);
78
- router.get(`${base}/debug`, adminMws, admin.debugHelloAsso);
80
+ router.post(`${base}/purge`, ...adminMws, admin.purgeByYear);
81
+ router.get(`${base}/debug`, ...adminMws, admin.debugHelloAsso);
79
82
  });
80
83
  scheduler.start();
81
84
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nodebb-plugin-calendar-onekite",
3
- "version": "11.1.18",
3
+ "version": "11.1.20",
4
4
  "description": "FullCalendar-based equipment reservation workflow with admin approval & HelloAsso payment for NodeBB",
5
5
  "main": "library.js",
6
6
  "license": "MIT",