Please test Nette Security 4.0 RC
- David Grudl
- founder | 8339
Hi,
I'd like to ask you to test nette/security 4.0.0-RC1. It's a major version with a new way of deciding permissions, but for a typical application it should be fully backward compatible. You shouldn't need to change anything in your code, whether you use an ACL or not. The only exceptions are a few less common cases listed below. Users stay logged in, identities stored in the session by 3.x load fine. That's exactly what I need to verify on your projects.
composer require nette/security:^4.0.0-RC1
It requires PHP 8.4 or newer and nette/utils 4.1. If you use
it with Nette DI and Nette HTTP, it also needs nette/di 3.2.4 and
nette/http 3.4 or newer.
What to watch out for
You'll only need to change something in one of these cases:
- You have an ACL (
Permission): it keeps working and you callisAllowed()as before. For a role or resource the ACL doesn't know, though, it now returnsfalseinstead of throwing, so a typo in a name no longer shows up as an exception. And$user->getAuthorizator()now throws; if you need the ACL itself, have thePermissionservice injected. - You have a custom authorizator (a class implementing
Authorizator): it keeps working, Nette just reports it as deprecated. Voters, described below, replace it. - You have your own identity class implementing
IIdentity:getId()needs the return typestring|intand the class needs agetData()method. Your ownIdentityHandlerneeds agetGuestIdentity()method (returnnullif you don't use a guest identity). - You read identity data that may not be there: reading a
SimpleIdentitykey that isn't set now throws instead of returningnull. So$identity->cart[] = $itemneeds$identity->cart = []first. - You return roles as
Roleobjects: roles are plain names now, sogetRoles()throws on an object. - You extend
Userand overrideisAllowed()orgetRoles(): both are final. - Your code uses old names: removed are the
IAuthenticatorinterface, the constantsUser::MANUALandUser::INACTIVITY(useUser::LogoutManualandLogoutInactivity) and the service names@user,@nette.authenticator,@nette.authorizator,@nette.userStorage(use@security.*or autowiring). The magic properties$user->id,$user->rolesand others are deprecated; callgetId(),getRoles()and so on.
All of it, with examples of how to rewrite each case, is in the upgrade guide.
What's new
For over ten years I've felt the ACL was more of a relic. It's quite limited: it can tell what a role may do, but asking whether this user may edit this article takes workarounds. Yet I wasn't sure what to replace it with, because every alternative had some “but”. In the end I found the solution, and it's already described in the documentation: voters.
Whether a user may edit a particular article is decided by a small class that looks at that article and casts a vote. No more role objects carrying the user's ID just to feed an ACL assertion:
use Nette\Security\{Principal, Vote, Voter};
class ArticleVoter implements Voter
{
public function vote(Principal $principal, mixed $resource, string|\UnitEnum|null $privilege): Vote
{
if (!$resource instanceof Article) { // not my business, I abstain
return Vote::Abstain;
}
// I allow the author to edit, otherwise I abstain and other voters decide
return Vote::allowIf($privilege === 'edit' && $principal->hasId($resource->authorId));
}
}
Register it as a service and ask the same way as before:
$user->isAllowed($article, 'edit').
On top of that: privileges as enum cases instead of strings, a Tracy panel
that shows every question asked during the request along with the voters' votes,
and storage: memory for tests and CLI scripts. There will be a blog
post covering all of it; the documentation has it already.
How to test
Clear the cache, run the application and go through the places that check
login and permissions. I'm especially interested in whether a user who logged in
under 3.x stays logged in, and whether isAllowed() gives the same
answers as before.
If something breaks or you run into unexpected deprecation notices, please let me know. Thanks!