<?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 : Uživatelé Nette Framework : Autentizace a autorizace : User::isInRole nefunguje s IRole</title>
<link>https://forum.nette.org/cs</link>
<description>Nette Framework forum</description>
<language>cs</language>
<docs>http://backend.userland.com/rss</docs>
<item>
	<title>User::isInRole nefunguje s IRole (Autentizace a autorizace)</title>
	<dc:creator>Kori</dc:creator>
	<link>https://forum.nette.org/cs/33873#p212968</link>
	<guid isPermaLink="false">212968@http://forum.nette.org/cs</guid>
	<description>&lt;p&gt;&lt;span class=&quot;mention&quot;&gt;@DavidGrudl&lt;/span&gt; PR jsem poslal. Jak moc zavazny je
to BC break je otazka do pranice, ale vzhledem k tomu, ze
&lt;code&gt;isAllowed&lt;/code&gt; s Irole funguje, tak je to spis napraveni stavajiciho
stavu :-)&lt;/p&gt;
</description>
	<pubDate>Wed, 11 Nov 2020 13:07:38 +0100</pubDate>
</item>
<item>
	<title>User::isInRole nefunguje s IRole (Autentizace a autorizace)</title>
	<dc:creator>David Grudl</dc:creator>
	<link>https://forum.nette.org/cs/33873#p212625</link>
	<guid isPermaLink="false">212625@http://forum.nette.org/cs</guid>
	<description>&lt;p&gt;Klidně pošli PR&lt;/p&gt;
</description>
	<pubDate>Sun, 25 Oct 2020 17:52:46 +0100</pubDate>
</item>
<item>
	<title>User::isInRole nefunguje s IRole (Autentizace a autorizace)</title>
	<dc:creator>Kori</dc:creator>
	<link>https://forum.nette.org/cs/33873#p212577</link>
	<guid isPermaLink="false">212577@http://forum.nette.org/cs</guid>
	<description>&lt;p&gt;&lt;span class=&quot;mention&quot;&gt;@DavidGrudl&lt;/span&gt; ?&lt;/p&gt;
</description>
	<pubDate>Wed, 21 Oct 2020 21:08:33 +0200</pubDate>
</item>
<item>
	<title>User::isInRole nefunguje s IRole (Autentizace a autorizace)</title>
	<dc:creator>Kori</dc:creator>
	<link>https://forum.nette.org/cs/33873#p212506</link>
	<guid isPermaLink="false">212506@http://forum.nette.org/cs</guid>
	<description>&lt;p&gt;Ahoj,&lt;br&gt;
existuje nejaky duvod proc metoda &lt;a
href=&quot;https://api.nette.org/security/master/Nette/Security/User.html#method_isInRole&quot;&gt;User::isInRole()&lt;/a&gt;
nepracuje s rozhranim &lt;a
href=&quot;https://api.nette.org/security/master/Nette/Security/IRole.html&quot;&gt;IRole&lt;/a&gt;?&lt;/p&gt;

&lt;p&gt;Ted jsem na to po delsi dobe opet narazil. Drive to slo vyresit vlastni
implementaci metody isInRole(), ale ta je ted z nejakeho duvodu od
v.3 &lt;strong&gt;final&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Priklad:&lt;/p&gt;

&lt;pre
class=&quot;language-php&quot;&gt;&lt;code&gt;class AdminRole implements \Nette\Security\IRole
{
	public function getRoleId(): string
	{
		return &apos;admin&apos;;
	}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;pre
class=&quot;language-php&quot;&gt;&lt;code&gt;class Authenticator implements Nette\Security\IAuthenticator
{
	public function authenticate(array $credentials): Nette\Security\IIdentity
	{
		.....

		$roles[] = new AdminRole();

		return new Nette\Security\Identity($id, $roles, $data);
	}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;pre
class=&quot;language-php&quot;&gt;&lt;code&gt;$this-&amp;gt;user-&amp;gt;isInRole(&apos;admin&apos;); //false&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Jednoducha implementace by mohla vypadat nejak takhle&lt;/p&gt;

&lt;pre
class=&quot;language-php&quot;&gt;&lt;code&gt;class User extends \Nette\Security\User
{
	public function isInRole($role): bool
	{
		$roles = [];

		foreach ($this-&amp;gt;getRoles() as $role)
		{
			if ($role instanceof \Nette\Security\IRole) {
				$roles[] = $role-&amp;gt;getRoleId();
			} else {
				$roles[] = $role;
			}
		}

		return in_array($role, $roles, true);
	}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Diky za info&lt;/p&gt;
</description>
	<pubDate>Fri, 16 Oct 2020 23:47:55 +0200</pubDate>
</item>
</channel>
</rss>
