Security
Access to operations and filters is controlled with Symfony expression language expressions - the same mechanism TYPO3 uses for TypoScript conditions. An expression is evaluated for every request and has to return true to grant access.
Operation security
Every operation accepts two attributes:
security- checked before the operation is processed. For item operations (GET,PATCH,PUT,DELETE) it is checked after the record is loaded from the database, so the record is available asobject.security_- checked after request payload is deserialized into the object (only for operations which read the payload: collectionpost_ denormalize POSTand itemPATCH/PUT).objectcontains the entity with data from the request already applied, so it is possible to validate what the user is trying to save.
use SourceBroker\T3api\Annotation as T3api;
/**
* @T3api\ApiResource (
* collectionOperations={
* "get"={
* "path"="/votes",
* },
* "post"={
* "method"="POST",
* "path"="/votes",
* "security"="frontend.user.isLoggedIn",
* },
* },
* itemOperations={
* "get"={
* "path"="/votes/{id}",
* },
* "patch"={
* "method"="PATCH",
* "path"="/votes/{id}",
* "security"="frontend.user.isLoggedIn && object.getUser().getUid() == frontend.user.userId",
* "security_post_denormalize"="object.getUser().getUid() == frontend.user.userId",
* },
* "delete"={
* "method"="DELETE",
* "path"="/votes/{id}",
* "security"="backend.user.isAdmin",
* },
* },
* )
*/
class Vote extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity
{
}
In the example above, the PATCH operation is protected twice: security makes sure the user edits their own vote and security_ makes sure the user did not change the owner of the vote in the request payload.
If an expression returns false, the operation is not processed and the response has status 403 Forbidden. If security is not set (default), the operation is available for everyone.
Note
object exists only in item operations and in security_. Collection operations (GET and POST on a collection) do not have it in security - for collection GET use filters or your own repository constraints to limit records, for POST use security_.
Storage pages
storage in persistence attributes limits records of collection GET operations and is the page where new records are stored. It does not limit item operations: GET, PATCH, PUT and DELETE on / load the record by its UID from any page, the same as find in Extbase. Hidden records, start and end time and frontend user groups are still respected.
When records must be available only on the storage pages of the resource (e.g. one resource per site or client), check the page of the record in security of every item operation. security gets the record as stored in the database, also for PATCH and PUT.
Do not allow to write pid: when an operation sets denormalization groups, properties without groups (pid too) are not deserialized. A new record is then stored on the first page from storage, and PATCH / PUT cannot change the page of a record.
/**
* @T3api\ApiResource (
* attributes={
* "persistence"={"storagePid"="3"}
* },
* collectionOperations={
* "get"={"path"="/products"},
* "post"={
* "method"="POST",
* "path"="/products",
* "denormalizationContext"={"groups"={"api_write_product"}},
* },
* },
* itemOperations={
* "get"={
* "path"="/products/{id}",
* "security"="object.getPid() in t3apiOperation.getPersistenceSettings().getStoragePids()",
* },
* "patch"={
* "method"="PATCH",
* "path"="/products/{id}",
* "security"="object.getPid() in t3apiOperation.getPersistenceSettings().getStoragePids()",
* "denormalizationContext"={"groups"={"api_write_product"}},
* },
* "delete"={
* "method"="DELETE",
* "path"="/products/{id}",
* "security"="object.getPid() in t3apiOperation.getPersistenceSettings().getStoragePids()",
* },
* },
* )
*/
t3api contains the pages from storage only, without subpages of recursive - list the pages in the expression when you use recursive.
Important
security_ alone does not limit item operations to the storage pages, as it checks the record with the data from the request. If pid is writable, check it there in addition to security and allow null: pid of a new record (POST) and of a replaced record (PUT) is null until the record is stored, unless the request sends it, e.g. object..
Filter security
Built-in and custom filters can be enabled conditionally. Instead of a strategy name, pass an array with name (strategy) and condition (expression) for a property:
use SourceBroker\T3api\Annotation as T3api;
use SourceBroker\T3api\Filter\SearchFilter;
/**
* @T3api\ApiFilter(
* SearchFilter::class,
* properties={
* "lastName": "partial",
* "email": {"name": "partial", "condition": "backend.user.isLoggedIn"},
* },
* )
*/
class User extends \TYPO3\CMS\Extbase\Domain\Model\FrontendUser
{
}
A filter whose condition returns false is silently ignored - the request does not fail, the filter just does not affect the results. In the example above, anonymous users can search users by last, but only a logged-in backend user can search by email (?email=... is ignored for everyone else).
Available variables and functions
User variables
Built from TYPO3 Context aspects frontend. and backend., the same as in TypoScript conditions.
| Variable | Description |
|---|---|
frontend. | true if a frontend user is logged in. |
frontend. | UID of the logged in frontend user (0 if not logged in). |
frontend. | Array of frontend user group UIDs. |
frontend. | Comma separated list of frontend user group UIDs. |
backend. | true if a backend user is logged in (backend session cookie is sent with the API request). |
backend. | true if the logged in backend user is an admin. |
backend. | UID of the logged in backend user. |
backend. | Array of backend user group UIDs. |
backend. | Comma separated list of backend user group UIDs. |
Examples:
- Only logged in frontend users:
frontend.user. is Logged In - Only frontend users from group 3:
3 in frontend.user. user Group Ids - Only frontend users from group 3 or 4:
3 in frontend.user. user Group Ids | | 4 in frontend. user. user Group Ids - Only a specific frontend user:
frontend.user. user Id == 12 - Only logged in backend users:
backend.user. is Logged In - Only backend admins:
backend.user. is Admin - Only backend users from group 2:
2 in backend.user. user Group Ids
Tip
Prefer in frontend. over like - the latter matches also groups 13, 30 etc.
T3api variables
object- the entity, see operation security above.t3api- the operation being checked (Operation \Source), e.g.Broker\ T3api\ Domain\ Model\ Operation Interface t3api. Available in operation security only.Operation. get Path () t3api- the filter being checked (Filter \Source). Available in filter security only.Broker\ T3api\ Domain\ Model\ Api Filter context- TYPO3\TYPO3\object, so any aspect can be read, e.g.CMS\ Core\ Context\ Context context..get Property From Aspect ('workspace', 'id') == 0
TYPO3 core variables and functions
T3api uses TYPO3 core default expression language provider and TypoScript condition functions, so most of TypoScript conditions work the same way:
- variables:
application,Context typo3.,version typo3.,branch typo3.,dev Ip Mask date,features, - functions:
compat,Version () like,() date,() feature,() traverse.()
T3api adds also function force_ (see Expression language).
Examples:
- Only in development context:
applicationContext matches "/^Development/" - Only on TYPO3 v13 or newer:
compatVersion ("13. 0") - Only before a deadline:
date("Y- m- d") < "2027- 01- 01"
Important
Variables related to a page rendering are not available, as an API request does not render any page: page, tree, request, site, site and functions using them (ip, site, site, session, locale, get). ip throws an exception on TYPO3 v13 and newer because there is no request variable (on TYPO3 v12 it still works with a deprecation notice). get exists only on TYPO3 v12. If you need such data, add your own variables with an event (see below).
Extending expression language
There are two ways to make additional variables or functions available:
- Register an expression language provider for
t3apicontext - available in security and serialization expressions. See Expression Language. - Listen to
Before,Operation Access Granted Event BeforeorOperation Access Granted Post Denormalize Event Beforeand add variables withFilter Access Granted Event set- available only in security expressions. See Events.Expression Language Variable ($name, $value)