Feature: #111236 - Security voters for authorization decisions
See forge#111236
Description
TYPO3 introduces security voters as the unified concept for authorization
decisions. Instead of asking a user object, such as
$GLOBALS or
->check,
code asks one question: is the current principal granted an attribute on a
subject?
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use TYPO3\CMS\Core\Security\Authorization\Policy\Scope;
public function __construct(
private readonly AuthorizationCheckerInterface $authorizationChecker,
private readonly ModuleRegistry $moduleRegistry,
) {}
public function canAccessLayoutModule(): bool
{
return $this->authorizationChecker->isGranted(
Scope::ACCESS,
$this->moduleRegistry->getModule('web_layout')
);
}
The authorization checker of symfony/ answers the question.
It asks every registered voter that supports the attribute and the subject.
Access is granted if at least one voter grants it and no voter denies it.
If all voters abstain, access is denied. A voter can therefore restrict
any decision, also one of TYPO3 Core or of another extension, but cannot
overrule the denial of another voter.
The authentication middlewares put the principal of the current request
into the token storage, so
is decides for the
authenticated backend or frontend user. Any other principal is checked
with
User.
Backend module access as first decision
The access to backend modules is decided by voters now. The new
\TYPO3\ decides on the
access values of TYPO3 Core given in the access option of a module:
- no access value: every backend user
user: administrators, and users granted the module or one of its aliases through their user record or one of their groupsadmin: administratorssystem: system maintainersMaintainer
Frontend users are denied every module.
The workspace restriction given in the workspaces option of a module is
decided by a voter as well: with EXT:workspaces loaded, a module that is not
available in the current workspace of the backend user is denied.
The access value is no closed list. An extension may register a module with
an access value of its own, such as 'access' => 'project, and decide
it with a voter that supports modules with this value. A module whose access
value no voter decides is not accessible.
\TYPO3\ asks the
voters for every module and any backend user, so the module menu, the module
dispatching and every other place checking module access follow the voters,
and decide as
is does. The decision for user is taken on
the group data of the given backend user object, as before, the workspace
restriction on its current workspace, unless
$respect is set to
false
.
Compared to the module access gates, the following details differ:
- A backend user object without a user record (no uid) is granted modules without access value only.
Backendevaluates the switch user mode of the user object itself instead ofUser Authentication:: is System Maintainer () $GLOBALS.['BE_ USER'] - A grant of any alias of a module grants the module.
Writing a voter
A voter is a service with the
# attribute. Extending
\TYPO3\ provides
the principal of the decision; requests without principal are denied.
namespace MyVendor\MyExtension\Security;
use Symfony\Component\Security\Core\Authorization\Voter\Vote;
use TYPO3\CMS\Backend\Module\ModuleInterface;
use TYPO3\CMS\Core\Attribute\AsSecurityVoter;
use TYPO3\CMS\Core\Authentication\Principal;
use TYPO3\CMS\Core\Security\Authorization\Policy\Scope;
use TYPO3\CMS\Core\Security\Authorization\SecurityRole;
use TYPO3\CMS\Core\Security\Authorization\Voter\PrincipalVoter;
/**
* Only administrators may open the page information overview, regardless of group grants.
*/
#[AsSecurityVoter]
final class InfoModuleVoter extends PrincipalVoter
{
protected function supports(string $attribute, mixed $subject): bool
{
return $attribute === Scope::ACCESS
&& $subject instanceof ModuleInterface
&& $subject->getIdentifier() === 'web_info_overview';
}
protected function voteOnPrincipal(
string $attribute,
mixed $subject,
Principal $principal,
?Vote $vote = null,
): bool {
if ($this->hasRole($principal, SecurityRole::ADMIN)) {
return true;
}
$vote?->addReason('The page information overview is restricted to administrators.');
return false;
}
}
The attribute takes an optional priority. Overriding
supports and
supports lets the checker skip
the voter without calling
supports.
Roles
Every principal holds roles, decided by the role voter of the security
component, e.g.
is. The roles are
the cases of the new enum
\TYPO3\:
Security(Role:: USER ROLE_): the base role of the security component, held by every authenticated principal, backend and frontend users alikeUSER Security,Role:: ADMIN Security,Role:: MAINTAINER Security: administrators, system maintainers and the system; the roles are cumulative, a system maintainer holds the role of an administrator as wellRole:: SYSTEM Security,Role:: BACKEND_ USER Security: every principal of the backend or of a frontend realmRole:: FRONTEND_ USER
In addition, every principal holds one role per group, such as
ROLE_ or ROLE_.
There is no role for anonymous requests, as they have no principal. The
attributes of the authenticated voter of the security component decide on
them. For frontend users, they correspond to the special groups of the
fe_ field of records:
- "Show at any login" (
-2):isGranted (Security Role:: FRONTEND_ USER->value) - "Hide at login" (
-1): notisGranted (Authenticated Voter:: IS_ AUTHENTICATED) - any visitor:
isGranted (Authenticated Voter:: PUBLIC_ ACCESS)
Module access gates
Module access gates, see Feature: #109365 - Introduce module access gates for backend modules, keep working and are deprecated, see Deprecation: #111236 - Module access gates. A gate is asked through a voter for an access value that is not known to TYPO3 Core, and for an access value of TYPO3 Core whose gate an extension replaced.
Public API
The following classes are public API:
\TYPO3\: registers a voterCMS\ Core\ Attribute\ As Security Voter \TYPO3\: base class of voters deciding for a principalCMS\ Core\ Security\ Authorization\ Voter\ Principal Voter \TYPO3\: the roles of a principalCMS\ Core\ Security\ Authorization\ Security Role \TYPO3\: a principal as user of the security component, e.g. forCMS\ Core\ Security\ Authorization\ Principal User isGranted For User () \TYPO3\: the attributes TYPO3 Core decides onCMS\ Core\ Security\ Authorization\ Policy\ Scope \TYPO3\: the access values of modules decided by TYPO3 CoreCMS\ Backend\ Security\ Authorization\ Module Voter:: ACCESS_ VALUES
The checker is used through the interfaces of the security component,
\Symfony\
and
\Symfony\.
All other new classes are internal. The principal a voter decides for,
\TYPO3\, is passed to
Principal. Its read surface is public API:
the role, id and group ids of its context,
$principal->context->role
,
$principal->context->id
and
$principal->context->group, and
$principal->is.
Creating a principal and its other properties are not part of the API yet,
as its identity is still bound to a local user record.
Impact
Authorization decisions can be taken for any principal with one API, and
extensions add rules with voters instead of XCLASSes or hooks. The access to
backend modules is the first decision taken this way; the permission checks
of
\TYPO3\ will
follow.