Upgrading

Upgrading to Version 4.0

Do you only define users in security: users: or the ACL in the configuration, and implement none of the library's interfaces? Then you most likely don't need to change anything in your code. Users stay logged in: identities that version 3.x stored in sessions are read by version 4.0.

The minimum required PHP version is 8.4, and the library needs nette/utils 4.1. If you use Nette Security with Nette DI or Nette HTTP, it needs nette/di 3.2.4 and nette/http 3.4 or newer.

A part of the work can be done for you by DressCode with the dresscode/upgrade-nette package: it rewrites renamed classes and constants and reports what it cannot rewrite. See Upgrading Nette.

The biggest change concerns authorization: access is now decided by voters instead of an authorizator that User asked about each role of the user. You call $user->isAllowed() the same way as before, and the ACL from the configuration keeps working, because Nette\Security\Permission is one of the voters.

Authorization

The ACL votes like other voters, see ACL as a Voter. An explicit deny() rule forbids access even if another voter allows it. For a role or a resource the ACL doesn't know, the ACL abstains, so $user->isAllowed() returns false (unless another voter allows access) instead of throwing an exception. A typo in a resource name is therefore no longer revealed by an exception. The method Permission::isAllowed() is final; if your subclass of Permission overrides it, write a voter instead.

If your ACL comes from the configuration, $user->getAuthorizator() no longer returns it but throws an exception, and getAuthorizatorIfExists() returns null, because the ACL is now a voter and not an authorizator. Wherever you need the ACL itself, have the Nette\Security\Permission service passed to you.

Your own authorizator implementing Nette\Security\Authorizator still works, but it is deprecated: if it is a service, Nette reports it as deprecated and names the service. The methods User::setAuthorizator(), getAuthorizator() and getAuthorizatorIfExists() are deprecated as well. Rewrite it as a voter. The voter gets all the roles of the user at once, so instead of answering for one role it checks them with isInRole():

// before
class MyAuthorizator implements Nette\Security\Authorizator
{
	public function isAllowed(?string $role, ?string $resource, ?string $privilege): bool
	{
		return $role === 'admin' || ($role === 'editor' && $resource === 'article');
	}
}

// after
use Nette\Security\Principal;
use Nette\Security\Vote;

class MyVoter implements Nette\Security\Voter
{
	public function vote(Principal $principal, mixed $resource, string|\UnitEnum|null $privilege): Vote
	{
		return Vote::allowIf(
			$principal->isInRole('admin')
			|| ($principal->isInRole('editor') && $resource === 'article'),
		);
	}
}

Are your rules naturally keyed by role? Then the voter can go through the roles one by one and keep the shape of the original method:

public function vote(Principal $principal, mixed $resource, string|\UnitEnum|null $privilege): Vote
{
	return Vote::allowIf(array_any(
		$principal->getRoles(),
		fn(string $role) => $role === 'admin' || ($role === 'editor' && $resource === 'article'),
	));
}

Where the authorizator returned false, the voter abstains, so that another voter may still allow access; see How Voters Reach a Decision.

Every voter declares the method exactly as the Voter interface has it, vote(Principal $principal, mixed $resource, string|\UnitEnum|null $privilege): Vote; a narrower type such as ?string $privilege ends with a fatal error. The privilege reaches the voter exactly as it was passed to isAllowed(), which besides a name now also accepts a case of an enum, see Enums Instead of Strings.

If you create the User object yourself, its third parameter is now a list of voters. Since Permission is a voter, new User($storage, $authenticator, $acl) keeps working. You cannot pass your own authorizator there, though; until you rewrite it as a voter, set it with the deprecated method setAuthorizator().

Roles are just names. The Nette\Security\Role interface is deprecated, an identity whose getRoles() returns role objects throws an exception, and Permission::isAllowed() accepts the role only as a string. Assertions receive names, and getQueriedRole() and getQueriedResource() return names, too. The Nette\Security\Resource interface stays: it tells the ACL which of its resources an object is.

An assertion that compared the user with an entity, typically to check who owns it, is rewritten as a voter. Ownership is checked by the hasId() method of the principal, which is never true for a user who is not logged in:

// before
$acl->allow('registered', 'article', 'edit', function (Permission $acl): bool {
	$role = $acl->getQueriedRole(); // Registered object holding the user ID
	$resource = $acl->getQueriedResource(); // Article object
	return $role->id === $resource->authorId;
});

$acl->isAllowed(new Registered($user->getId()), $article, 'edit');

// after
use Nette\Security\Principal;
use Nette\Security\Vote;

class ArticleVoter implements Nette\Security\Voter
{
	public function vote(Principal $principal, mixed $resource, string|\UnitEnum|null $privilege): Vote
	{
		return Vote::allowIf(
			$resource instanceof Article
			&& $privilege === 'edit'
			&& $principal->isInRole('registered')
			&& $principal->hasId($resource->authorId),
		);
	}
}

$user->isAllowed($article, 'edit');

With the session storage, this also stops a logged-out user whose identity was retained. With the cookie storage, the identity never survives a logout, so there replacing $user->getId() === $id with hasId($id) changes nothing about who gets access.

The method User::getRoles() is final and returns the same roles as the principal. The method User::isAllowed() is final as well: a subclass of User that overrides it ends with a fatal error, so move its rules into a voter. The property User::$authenticatedRole is deprecated because it is never used.

Did you use $user->isLoggedIn() as a permission, and now want to ask through isAllowed()? A voter that allows everything to any logged-in user takes a single line:

return Vote::allowIf($principal->isLoggedIn());

Don't add a role to the users in security: users: instead: identities already stored in sessions keep their old roles until the user logs in again.

Authentication and Identity

The Nette\Security\IAuthenticator interface has been removed; implement Nette\Security\Authenticator, whose method authenticate() takes the username and the password. The legacy service names user, nette.userStorage, nette.authenticator and nette.authorizator have been removed as well; use security.user, security.userStorage, security.authenticator and security.authorizator, or autowiring.

If you implement Nette\Security\IIdentity yourself, declare the return type string|int of getId() and add the method getData(), which returns the user data as an array. The class Nette\Security\Identity is now just an alias of SimpleIdentity. The method IdentityHandler::getGuestIdentity() is a regular part of the interface, so every handler must have it; return null if you don't use a guest identity.

Reading a key of SimpleIdentity that is not among its data throws Nette\MemberAccessException; previously it returned null. This also concerns adding to an array that doesn't exist yet, such as $identity->cart[] = $item, so create the key first: $identity->cart = [].

It can catch you where you don't expect it: an identity restored from the session may have been created by an older version of your own application, before you added the key. Read keys added later with ??, such as $identity->theme ?? 'light'.

SimpleIdentity stores its ID, roles and data together with the public and protected properties of a subclass. If your subclass has private properties that should survive in the session, extend __serialize() and __unserialize(). Identities saved in sessions by version 3.x are read without problems.

With the cookie storage, the value of the cookie is only a token, which User never takes as the identity of the user. The authenticator must implement IdentityHandler and exchange the token for the identity in wakeupIdentity(), otherwise User throws Nette\InvalidStateException. This has been the case since version 3.2.8.

Keeping the identity after logout is on by default, so code outside voters that decides by it what the user may do must ask $user->getPrincipal(), and where you don't use the retained identity, switch it off with persistIdentity: false (see Identity).

The magic properties $user->id, $user->roles, $user->logoutReason, $user->authenticator and $user->authorizator are deprecated; call the methods getId(), getRoles(), getLogoutReason() and so on. The properties $user->identity and $user->loggedIn stay. The constants User::MANUAL and User::INACTIVITY have been removed; use User::LogoutManual and User::LogoutInactivity. Deprecated constants and methods now trigger E_USER_DEPRECATED when used.

Upgrading to Version 3.1

  • the Nette\Security\Authenticator interface differs from IAuthenticator in the signature of the authenticate(string $user, string $password) method
  • the Nette\Security\UserStorage interface differs from Nette\Security\IUserStorage
  • the class Nette\Security\Identity was renamed to Nette\Security\SimpleIdentity so that in the distant future the interface Nette\Security\IIdentity can be renamed to Identity
  • the interface Nette\Security\UserStorage, which decides where the information about the logged-in user is kept, is brand new and differs from the original Nette\Security\IUserStorage; it is what User::getStorage() returns. Implementations storing the data in a session or in a cookie are available.
  • an authenticator can implement the interface Nette\Security\IdentityHandler and thus influence what happens to the identity when it is saved to and restored from the storage, for example to refresh the roles

How to use IdentityHandler and how to build a website with logging in that needs no sessions at all is shown in the article News in Nette Security 3.1.

Upgrading to Version 3.0

  • the Nette\Security\Passwords class is now used as an object, i.e. the methods are no longer static
version: 4.x 3.x