-
-
Notifications
You must be signed in to change notification settings - Fork 10
refactor(policy): decentralize subject resolution to modules #3381
Copy link
Copy link
Open
Description
Problem
Every new module must modify lib/middlewares/policy.js to register its CASL subjects:
- Add entries to
resolveSubject()(document-level checks) - Add entries to
deriveSubjectType()(route-level checks) - Add route exclusions for the org fallback
This couples every module to a shared file, breaking module independence.
Proposed solution
Each module defines its own authorization middleware instead of relying on the generic policy.isAllowed:
// modules/{module}/middlewares/{module}.authorize.js
const authorize = (action, subject) => (req, res, next) => {
if (req.ability.can(action, subject)) return next();
return responses.error(res, 403, 'Forbidden')();
};Routes use the module-local middleware:
.all(passport.authenticate('jwt'), organization.resolveOrganization)
.get(authorize('read', 'MySubject'), controller.list)Steps
- Add a shared
lib/helpers/authorize.jshelper (DRY across modules) - Migrate each module's routes from
policy.isAllowedtoauthorize(action, subject) - Remove
resolveSubject()andderiveSubjectType()frompolicy.js - Keep
defineAbilityFor()and policy auto-discovery (unchanged)
Migration
policy.isAllowed in custom routes must switch to the new authorize() helper. The old policy.isAllowed should be kept as deprecated for one release cycle.
Affected modules
All: tasks, billing, users, organizations, uploads, admin, audit + any downstream custom modules (scraps, historys, developers, etc.)
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels