Authorization
Introduction
Authorization is powered by Gate + Policy primitives (jcc-express-mvc/lib/Authorization):
GateFacade.define(...)for named abilitiesGateFacade.policy(Model, PolicyClass)for model policiesGateFacade.can(...)andGateFacade.authorize(...)for checksAuthorizeMiddleware.authorize(...)for route-level ability enforcement
Global helpers are also available: Gate, can(...), authorize(...).
Define gates and policies
In a provider (for example app/Providers/AuthServiceProvider.ts):
TypeScript
Gate decisions deny by default when ability/policy is not found.
Policy classes
Extend Policy and implement ability methods:
TypeScript
Supported conventional methods include viewAny, view, create, update, delete, restore, forceDelete.
Check permissions in code
Using helpers:
TypeScript
Using facade directly:
TypeScript
authorize(...) throws AuthorizationException when denied.
Route middleware authorization
Use the middleware factory:
TypeScript
Behavior:
- unauthenticated ->
401JSON or redirect to login - missing model ->
404 - unauthorized ->
403
Integration with error handling
Authorization failures raise AuthorizationException, handled by AppErrorHandler as:
- JSON requests:
403 { message } - web requests: flash +
redirectBack()
Summary
- Register gates/policies in a provider.
- Use
can(...)for boolean checks andauthorize(...)for enforced checks. - Use
AuthorizeMiddlewarewhen authorization should happen at route edge.
