Skip to content

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 CASLHere
a rule is a function that returns true or falsea rule is a permit or a forbid in a .cedar file; it has no code
a rule reads the model it getsa rule reads entities that an entity source loads, by the attributes the schema declares
rules run in the order they were addedorder never matters; a matching forbid always wins and nothing is allowed by default
the ability or the gate lives in the requestthe policies are compiled at build time and the request holds only the principal and the entities

Step by step

  1. List your actions (view, update, delete, …) and your models. Write them as entity and action lines in server/policies/schema.cedarschema.
  2. Write each gate, policy method or can rule as a permit, and each cannot or inverted rule as a forbid in server/policies/*.cedar.
  3. Write server/policies/entities.ts: the signed-in principal, and a source for each model that a policy reads (tableEntities for a table).
  4. Add the authorize key to each handler that a controller action or route guarded. Replace a collection scope with authorizedWhere.
  5. Replace every @can or ability.can in a view with can from hamur/app, and call exposeDecisions in the handler that loads the data.
  6. Move the policy tests to createAuthorizer.

From Laravel

Laravelhamur serverStatusDifferences
Gate::define('update-post', fn)an action in the schema and a permit in a .cedar filedifferentthe 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 methodsan entity type, its actions and a .cedar filedifferentno class and no registration: the files of server/policies/ are scanned
$this->authorize('update', $post) in a controllerthe authorize key of defineHandler, or authorize from hamur/serverdifferentthe 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/serversamethe user is the request's principal; can is false for no principal and for a resource that does not exist
can:update,post route middlewarethe authorize key of a handler with a routedifferentthe key is part of the handler, so a form post, a socket and an MCP call are checked too
@can('update', $post) in Bladecan from hamur/app, with exposeDecisions from hamur/server in the handlerdifferentcan shows and hides; the server decides again on every call. A hidden button is not protection
viewAny and a query scope that filters by userauthorizedWhere from hamur/serverdifferentone 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) policydifferenta 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')nonenonea 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 keysamea missing resource is always 404; with denyStatus: 404 a denied one looks the same
Gate::inspectexplain from hamur/serverdifferentit names the policies that decided, with file and line, and the errors
a policy method that takes ?User $user for guestsnonenonea 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 testsdifferenta check in a request is for the request's own principal
actingAs($user) and assertForbidden() in policy testscreateAuthorizer from hamur/testdifferentthe test passes the entities itself, so it needs no database and no session

From CASL

CASLhamur serverStatusDifferences
defineAbility((can, cannot) => …), AbilityBuilderthe .cedar files of server/policies/differentthe 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 }differentthe 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(…)forbiddifferentin 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 allan action with no scope, or action in [ … ], or an action groupdifferentthere is no wildcard word; a policy with no action condition matches every action
a role stored on the user and read in conditionsa Role entity that the user is indifferentprincipal 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 EntityRefdifferentyou 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 servercan, authorize from hamur/serversameone call, and the entities of many calls load together; canEach does a list
ForbiddenError.from(ability).throwUnlessCan(…)authorize from hamur/serversameit throws the error answer for 401, 403 or 404
ability.can in the browser, with packRules to send rulescan from hamur/app, with exposeDecisions from hamur/serverdifferentthe browser gets decisions (1 or 0), never rules, so no policy and no attribute reaches it
accessibleBy(ability), rulesToQueryauthorizedWhere from hamur/serverdifferentthe 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/serverdifferentCedar has no field rules; give each guarded field an action of its own and name it in redact
ability.relevantRuleFor, rulesForexplain from hamur/serverdifferentthe 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.