<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
<title>Nette Framework forum : Development of Nette Framework : Discussion on development : Please test Nette Security 4.0 RC</title>
<link>https://forum.nette.org/en</link>
<description>Nette Framework forum</description>
<language>en</language>
<docs>http://backend.userland.com/rss</docs>
<item>
	<title>Please test Nette Security 4.0 RC (Discussion on development)</title>
	<dc:creator>David Grudl</dc:creator>
	<link>https://forum.nette.org/en/36999#p229158</link>
	<guid isPermaLink="false">229158@http://forum.nette.org/en</guid>
	<description>&lt;p&gt;Hi,&lt;/p&gt;

&lt;p&gt;I&apos;d like to ask you to test &lt;strong&gt;nette/security 4.0.0-RC1&lt;/strong&gt;. It&apos;s a
major version with a new way of deciding permissions, but &lt;strong&gt;for a typical
application it should be fully backward compatible&lt;/strong&gt;. You shouldn&apos;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&apos;s exactly what I need to
verify on your projects.&lt;/p&gt;

&lt;pre
class=&quot;language-bash&quot;&gt;&lt;code&gt;composer require nette/security:^4.0.0-RC1&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It requires PHP 8.4 or newer and &lt;code&gt;nette/utils&lt;/code&gt; 4.1. If you use
it with Nette DI and Nette HTTP, it also needs &lt;code&gt;nette/di&lt;/code&gt; 3.2.4 and
&lt;code&gt;nette/http&lt;/code&gt; 3.4 or newer.&lt;/p&gt;

&lt;h4&gt;What to watch out for&lt;/h4&gt;

&lt;p&gt;You&apos;ll only need to change something in one of these cases:&lt;/p&gt;

&lt;ul&gt;
	&lt;li&gt;&lt;strong&gt;You have an ACL&lt;/strong&gt; (&lt;code&gt;Permission&lt;/code&gt;): it keeps working
	and you call &lt;code&gt;isAllowed()&lt;/code&gt; as before. For a role or resource the ACL
	doesn&apos;t know, though, it now returns &lt;code&gt;false&lt;/code&gt; instead of throwing, so
	a typo in a name no longer shows up as an exception. And
	&lt;code&gt;$user-&amp;gt;getAuthorizator()&lt;/code&gt; now throws; if you need the ACL itself,
	have the &lt;code&gt;Permission&lt;/code&gt; service injected.&lt;/li&gt;

	&lt;li&gt;&lt;strong&gt;You have a custom authorizator&lt;/strong&gt; (a class implementing
	&lt;code&gt;Authorizator&lt;/code&gt;): it keeps working, Nette just reports it as
	deprecated. Voters, described below, replace it.&lt;/li&gt;

	&lt;li&gt;&lt;strong&gt;You have your own identity class&lt;/strong&gt; implementing
	&lt;code&gt;IIdentity&lt;/code&gt;: &lt;code&gt;getId()&lt;/code&gt; needs the return type
	&lt;code&gt;string|int&lt;/code&gt; and the class needs a &lt;code&gt;getData()&lt;/code&gt; method.
	Your own &lt;code&gt;IdentityHandler&lt;/code&gt; needs a &lt;code&gt;getGuestIdentity()&lt;/code&gt;
	method (return &lt;code&gt;null&lt;/code&gt; if you don&apos;t use a guest identity).&lt;/li&gt;

	&lt;li&gt;&lt;strong&gt;You read identity data that may not be there&lt;/strong&gt;: reading a
	&lt;code&gt;SimpleIdentity&lt;/code&gt; key that isn&apos;t set now throws instead of returning
	&lt;code&gt;null&lt;/code&gt;. So &lt;code&gt;$identity-&amp;gt;cart[] = $item&lt;/code&gt; needs
	&lt;code&gt;$identity-&amp;gt;cart = []&lt;/code&gt; first.&lt;/li&gt;

	&lt;li&gt;&lt;strong&gt;You return roles as &lt;code&gt;Role&lt;/code&gt; objects&lt;/strong&gt;: roles are
	plain names now, so &lt;code&gt;getRoles()&lt;/code&gt; throws on an object.&lt;/li&gt;

	&lt;li&gt;&lt;strong&gt;You extend &lt;code&gt;User&lt;/code&gt;&lt;/strong&gt; and override
	&lt;code&gt;isAllowed()&lt;/code&gt; or &lt;code&gt;getRoles()&lt;/code&gt;: both are final.&lt;/li&gt;

	&lt;li&gt;&lt;strong&gt;Your code uses old names&lt;/strong&gt;: removed are the
	&lt;code&gt;IAuthenticator&lt;/code&gt; interface, the constants &lt;code&gt;User::MANUAL&lt;/code&gt;
	and &lt;code&gt;User::INACTIVITY&lt;/code&gt; (use &lt;code&gt;User::LogoutManual&lt;/code&gt; and
	&lt;code&gt;LogoutInactivity&lt;/code&gt;) and the service names &lt;code&gt;@user&lt;/code&gt;,
	&lt;code&gt;@nette.authenticator&lt;/code&gt;, &lt;code&gt;@nette.authorizator&lt;/code&gt;,
	&lt;code&gt;@nette.userStorage&lt;/code&gt; (use &lt;code&gt;@security.*&lt;/code&gt; or autowiring).
	The magic properties &lt;code&gt;$user-&amp;gt;id&lt;/code&gt;, &lt;code&gt;$user-&amp;gt;roles&lt;/code&gt; and
	others are deprecated; call &lt;code&gt;getId()&lt;/code&gt;, &lt;code&gt;getRoles()&lt;/code&gt; and
	so on.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All of it, with examples of how to rewrite each case, is in the &lt;a
href=&quot;https://doc.nette.org/en/security/upgrading&quot;&gt;upgrade guide&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;What&apos;s new&lt;/h4&gt;

&lt;p&gt;For over ten years I&apos;ve felt the ACL was more of a relic. It&apos;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&apos;t sure what to replace it with, because
every alternative had some “but”. In the end I found the solution, and it&apos;s
already described in the &lt;a
href=&quot;https://doc.nette.org/en/security/authorization&quot;&gt;documentation&lt;/a&gt;:
&lt;strong&gt;voters&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;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&apos;s
ID just to feed an ACL assertion:&lt;/p&gt;

&lt;pre
class=&quot;language-php&quot;&gt;&lt;code&gt;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 === &apos;edit&apos; &amp;amp;&amp;amp; $principal-&amp;gt;hasId($resource-&amp;gt;authorId));
	}
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Register it as a service and ask the same way as before:
&lt;code&gt;$user-&amp;gt;isAllowed($article, &apos;edit&apos;)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;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&apos; votes,
and &lt;code&gt;storage: memory&lt;/code&gt; for tests and CLI scripts. There will be a blog
post covering all of it; the documentation has it already.&lt;/p&gt;

&lt;h4&gt;How to test&lt;/h4&gt;

&lt;p&gt;Clear the cache, run the application and go through the places that check
login and permissions. I&apos;m especially interested in whether a user who logged in
under 3.x stays logged in, and whether &lt;code&gt;isAllowed()&lt;/code&gt; gives the same
answers as before.&lt;/p&gt;

&lt;p&gt;If something breaks or you run into unexpected deprecation notices, please
let me know. Thanks!&lt;/p&gt;
</description>
	<pubDate>Thu, 03 Dec 2026 08:23:46 +0100</pubDate>
</item>
</channel>
</rss>
