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 as object.
  • security_post_denormalize - checked after request payload is deserialized into the object (only for operations which read the payload: collection POST and item PATCH / PUT). object contains 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
{
}
Copied!

In the example above, the PATCH operation is protected twice: security makes sure the user edits their own vote and security_post_denormalize 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.

Storage pages 

storagePid 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 /{id} load the record by its UID from any page, the same as findByUid() 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 denormalizationContext groups, properties without groups (pid too) are not deserialized. A new record is then stored on the first page from storagePid, 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()",
 *          },
 *     },
 * )
 */
Copied!

t3apiOperation.getPersistenceSettings().getStoragePids() contains the pages from storagePid only, without subpages of recursive - list the pages in the expression when you use recursive.

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
{
}
Copied!

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 lastName, 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.user and backend.user, the same as in TypoScript conditions.

Variable Description
frontend.user.isLoggedIn true if a frontend user is logged in.
frontend.user.userId UID of the logged in frontend user (0 if not logged in).
frontend.user.userGroupIds Array of frontend user group UIDs.
frontend.user.userGroupList Comma separated list of frontend user group UIDs.
backend.user.isLoggedIn true if a backend user is logged in (backend session cookie is sent with the API request).
backend.user.isAdmin true if the logged in backend user is an admin.
backend.user.userId UID of the logged in backend user.
backend.user.userGroupIds Array of backend user group UIDs.
backend.user.userGroupList Comma separated list of backend user group UIDs.

Examples:

  • Only logged in frontend users: frontend.user.isLoggedIn
  • Only frontend users from group 3: 3 in frontend.user.userGroupIds
  • Only frontend users from group 3 or 4: 3 in frontend.user.userGroupIds || 4 in frontend.user.userGroupIds
  • Only a specific frontend user: frontend.user.userId == 12
  • Only logged in backend users: backend.user.isLoggedIn
  • Only backend admins: backend.user.isAdmin
  • Only backend users from group 2: 2 in backend.user.userGroupIds

T3api variables 

  • object - the entity, see operation security above.
  • t3apiOperation - the operation being checked (\SourceBroker\T3api\Domain\Model\OperationInterface), e.g. t3apiOperation.getPath(). Available in operation security only.
  • t3apiFilter - the filter being checked (\SourceBroker\T3api\Domain\Model\ApiFilter). Available in filter security only.
  • context - TYPO3 \TYPO3\CMS\Core\Context\Context object, so any aspect can be read, e.g. context.getPropertyFromAspect('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: applicationContext, typo3.version, typo3.branch, typo3.devIpMask, date, features,
  • functions: compatVersion(), like(), date(), feature(), traverse().

T3api adds also function force_absolute_url() (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"

Extending expression language 

There are two ways to make additional variables or functions available:

  • Register an expression language provider for t3api context - available in security and serialization expressions. See Expression Language.
  • Listen to BeforeOperationAccessGrantedEvent, BeforeOperationAccessGrantedPostDenormalizeEvent or BeforeFilterAccessGrantedEvent and add variables with setExpressionLanguageVariable($name, $value) - available only in security expressions. See Events.