Componette

Componette

orisai

orisai / nette-auth

Orisai Auth integration for Nette

download-cloud-line composer require orisai/nette-auth

Orisai
Nette Auth

Orisai Auth integration for Nette

📄 Check out our documentation.

💸 If you like Orisai, please make a donation. Thank you!

namespace App\Admin\Article\View;

use Orisai\Auth\Authentication\Identity;
use Orisai\Auth\Authentication\SimpleFirewall;

final class ArticleEditController
{

	private SimpleFirewall $firewall;

	public function __construct(SimpleFirewall $firewall)
	{
		$this->firewall = $firewall;
	}

	public function run(): void
	{
		if (!$this->firewall->isAllowed('administration.entry')) {
			// Not allowed
		}

		$article = /* get article by ID from request */;

		if (!$this->firewall->isAllowed('article.edit', $article)) {
			// Not allowed
		}

		// Is allowed
	}

}

use App\Core\Article\Article;
use Orisai\Auth\Authorization\Policy;
use Orisai\Auth\Authorization\PolicyContext;

/**
 * @phpstan-implements Policy<Article>
 */
final class ArticleEditPolicy implements Policy
{

	public static function getPrivilege(): string
	{
		return 'article.edit';
	}

	public static function getRequirementsClass(): string
	{
		return Article::class;
	}

	/**
	 * @param Article $requirements
	 */
	public function isAllowed(Identity $identity, object $requirements, PolicyContext $context): bool
	{
		$authorizer = $context->getAuthorizer();

		return $authorizer->hasPrivilege($identity, self::getPrivilege())
			&& $requirements->getAuthor()->getId() === $identity->getId();
	}

}

No release at this moment. Try to create first one.

bar-chart-fill

Statistics

download-cloud-fill
5122
star-fill
2
bug-fill
0
flashlight-fill
102d
price-tag-2-line

Badges

guide-fill

Dependencies

php (>=7.4.0 <8.2.0)
nette/http (^3.1.5)
orisai/auth (^1.0.0)
Componette Componette felix@nette.org