Migrating from Laravel and CASL
These tables map the authorization of Laravel (gates, policies, @can) and of CASL to the authorization of hamur server. Both are rules written in code. Here a rule is a Cedar policy file, and the code that remains is small: who is signed in, and where the facts come from. There is no hamur migrate command for either; the move is by hand, with the tables below.
Read Authorization first. It builds one app in steps, and every name below is explained there.
The shape changes
| In Laravel or CASL | Here |
|---|---|
| a rule is a function that returns true or false | a rule is a permit or a forbid in a .cedar file; it has no code |
| a rule reads the model it gets | a rule reads entities that an entity source loads, by the attributes the schema declares |
| rules run in the order they were added | order never matters; a matching forbid always wins and nothing is allowed by default |
| the ability or the gate lives in the request | the policies are compiled at build time and the request holds only the principal and the entities |
Step by step
- List your actions (
view,update,delete, …) and your models. Write them asentityandactionlines inserver/policies/schema.cedarschema. - Write each gate, policy method or
canrule as apermit, and eachcannotor inverted rule as aforbidinserver/policies/*.cedar. - Write
server/policies/entities.ts: the signed-in principal, and a source for each model that a policy reads (tableEntitiesfor a table). - Add the
authorizekey to each handler that a controller action or route guarded. Replace a collection scope withauthorizedWhere. - Replace every
@canorability.canin a view withcanfromhamur/app, and callexposeDecisionsin the handler that loads the data. - Move the policy tests to
createAuthorizer.
From Laravel
| Laravel | hamur server | Status | Differences |
|---|---|---|---|
Gate::define('update-post', fn) | an action in the schema and a permit in a .cedar file | different | the rule is a condition in Cedar, not PHP; a fact that needs code goes in an entity source |
a policy class with view and update methods | an entity type, its actions and a .cedar file | different | no class and no registration: the files of server/policies/ are scanned |
$this->authorize('update', $post) in a controller | the authorize key of defineHandler, or authorize from hamur/server | different | the key runs after input and before handler, on every way into the handler |
Gate::authorize, Gate::allows, $user->can('update', $post) | authorize, can from hamur/server | same | the user is the request's principal; can is false for no principal and for a resource that does not exist |
can:update,post route middleware | the authorize key of a handler with a route | different | the key is part of the handler, so a form post, a socket and an MCP call are checked too |
@can('update', $post) in Blade | can from hamur/app, with exposeDecisions from hamur/server in the handler | different | can shows and hides; the server decides again on every call. A hidden button is not protection |
viewAny and a query scope that filters by user | authorizedWhere from hamur/server | different | one SQL condition built from the policies, so paging and counts are right; the source must be tableEntities |
Gate::before(fn ($user) => $user->isAdmin() ? true : null) | a permit (principal in Role::"admin", action, resource) policy | different | a forbid still beats it, as no permit overrides a forbid; add unless { principal in Role::"admin" } to a forbid that admins may pass |
Response::deny('message') | none | none | a denial carries no reason, so a caller learns nothing about your rules. Read the reason with explain, the devtools or the debug log |
Response::denyAsNotFound(), denyWithStatus(404) | denyStatus: 404 in the authorize key | same | a missing resource is always 404; with denyStatus: 404 a denied one looks the same |
Gate::inspect | explain from hamur/server | different | it names the policies that decided, with file and line, and the errors |
a policy method that takes ?User $user for guests | none | none | a request with no principal is answered 401 before any policy runs. A handler that guests may call has no authorize key |
Gate::forUser($user) | createAuthorizer().check({ principal, … }) in tests | different | a check in a request is for the request's own principal |
actingAs($user) and assertForbidden() in policy tests | createAuthorizer from hamur/test | different | the test passes the entities itself, so it needs no database and no session |
From CASL
| CASL | hamur server | Status | Differences |
|---|---|---|---|
defineAbility((can, cannot) => …), AbilityBuilder | the .cedar files of server/policies/ | different | the rules are data, read at build time; nothing is built for each request |
can('update', 'Article', { authorId: user.id }) | permit (principal, action == Action::"update", resource is Article) when { resource.author == principal } | different | the condition is a Cedar expression over the attributes of the schema, not a Mongo-style object. $in, $gt and the like are in, contains, > |
cannot(…) | forbid | different | in CASL a later rule overrides an earlier one. Here order never matters and a forbid always wins, so a "cannot, then can again" is an unless |
manage and all | an action with no scope, or action in [ … ], or an action group | different | there is no wildcard word; a policy with no action condition matches every action |
| a role stored on the user and read in conditions | a Role entity that the user is in | different | principal in Role::"admin"; the role comes from a parent column of a tableEntities source |
subject('Article', article), or a class instance | { type: "Article", id }, an EntityRef | different | you pass the type and the id; the entity source loads the attributes, so a check never trusts an object the caller built |
ability.can('update', article) on the server | can, authorize from hamur/server | same | one call, and the entities of many calls load together; canEach does a list |
ForbiddenError.from(ability).throwUnlessCan(…) | authorize from hamur/server | same | it throws the error answer for 401, 403 or 404 |
ability.can in the browser, with packRules to send rules | can from hamur/app, with exposeDecisions from hamur/server | different | the browser gets decisions (1 or 0), never rules, so no policy and no attribute reaches it |
accessibleBy(ability), rulesToQuery | authorizedWhere from hamur/server | different | the result is one SQL fragment for your query, for a tableEntities source; a condition with no SQL form fails the build when it can see the call |
permittedFieldsOf, fields in can('read', 'Article', ['title']) | redact from hamur/server | different | Cedar has no field rules; give each guarded field an action of its own and name it in redact |
ability.relevantRuleFor, rulesFor | explain from hamur/server | different | the answer names the policies that decided, not the rules that could apply |
Differences that bite
- No wildcard resource and no "any attribute". A policy names the entity types it applies to, and the schema is checked: a policy that reads an attribute the type does not have fails the build.
- Facts come from your data, in batches. An entity source gets all the ids a check needs in one call, so a list of 50 documents is one call for each type, not 50.
- Nothing is cached across requests. A role taken away applies at the next request.
- Anyone who may call a handler may call it without your page. The check belongs to the handler, not to the button.