EverydayTech Platform - Developer Reference
Complete Source Code Documentation - All Applications
Loading...
Searching...
No Matches
tenantAdmin.js
Go to the documentation of this file.
1/**
2 * @file Tenant Admin Role Authorization Middleware
3 * @module middleware/tenantAdmin
4 * @description
5 * Express middleware for restricting routes to tenant administrator users. Validates user role
6 * from req.user (set by auth middleware). Requires admin, msp, or root roles - denies staff.
7 *
8 * Use this for sensitive operations like refunds, financial adjustments, and account management
9 * that should not be accessible to regular staff users.
10 *
11 * Role hierarchy:
12 * - root: Super admin (Root MSP) ✅ Allowed
13 * - msp: MSP admin ✅ Allowed
14 * - admin: Tenant admin ✅ Allowed
15 * - staff: Regular user ❌ DENIED
16 * @requires middleware/auth - Must run AFTER authenticateToken middleware
17 * @see {@link module:middleware/auth} for authentication middleware
18 * @see {@link module:middleware/adminOnly} for similar admin-only middleware
19 */
20
21/**
22 * Restricts route access to tenant administrator users (admin/msp/root roles).
23 *
24 * Checks req.user.role set by authenticateToken middleware. Allows admins, MSPs, and
25 * root users, denies staff. Returns 403 for staff users, 401 for unauthenticated.
26 *
27 * **Use Cases:**
28 * - Processing refunds
29 * - Voiding invoices
30 * - Financial adjustments
31 * - Account configuration
32 * - Sensitive data access
33 * @function requireTenantAdmin
34 * @param {object} req - Express request object
35 * @param {object} req.user - User object from authenticateToken middleware
36 * @param {string} req.user.role - User role (admin/msp/root/staff)
37 * @param {number} req.user.user_id - User ID for audit logging
38 * @param {object} res - Express response object
39 * @param {Function} next - Express next middleware function
40 * @returns {void} Calls next() for admin users, sends 401/403 otherwise
41 * @throws {401} Authentication required - Missing req.user
42 * @throws {403} Tenant admin access required - User role is staff
43 * @example
44 * // Apply to tenant admin-only routes
45 * const authenticateToken = require('./middleware/auth');
46 * const requireTenantAdmin = require('./middleware/tenantAdmin');
47 *
48 * router.post('/invoices/:id/refund', authenticateToken, requireTenantAdmin, (req, res) => {
49 * // Only tenant admins can process refunds
50 * });
51 * @example
52 * // Error response for staff user attempting refund
53 * {
54 * "error": "Tenant administrator access required. This action is restricted to admins only."
55 * }
56 */
57function requireTenantAdmin(req, res, next) {
58 if (!req.user) {
59 return res.status(401).json({
60 error: 'Authentication required',
61 message: 'You must be logged in to perform this action'
62 });
63 }
64
65 const userRole = req.user.role;
66
67 // Allow admin, msp, and root roles (all administrator-level)
68 if (userRole === 'admin' || userRole === 'msp' || userRole === 'root') {
69 return next();
70 }
71
72 // Deny staff and any other roles
73 return res.status(403).json({
74 error: 'Tenant administrator access required',
75 message: 'This action is restricted to tenant administrators. Staff users do not have permission.',
76 requiredRole: 'admin',
77 userRole: userRole
78 });
79}
80
81module.exports = requireTenantAdmin;