Search by

drago-ex / permission

accgit

Lightweight ACL and role management.

Package info

github.com/drago-ex/permission

pkg:composer/drago-ex/permission

Statistics

Installs: 440

Dependents: 12

Suggesters: 1

Stars: 0

Open Issues: 0

v2.0.0 2026-11-15 01:50 UTC

This package is auto-updated.

Last update: 2026-12-05 11:07:51 UTC


README

Lightweight ACL and role management. The package provides the base roles, ACL written in neon next to the code it protects, and automatic authorization checks in presenters.

License: MIT PHP version Tests Coding Style

Requirements

  • PHP >= 8.3, < 9
  • Nette Framework
  • Composer

Installation

composer require drago-ex/permission

Features

  • Default roles: guest, user, admin
  • ACL in neon files, one per module
  • Automatic presenter authorization
  • The same privileges in every module: view, add, edit, delete, set by attributes, and privileges of their own for other operations (publish, export)
  • One form for adding and editing, as Nette recommends
  • Latte function isAllowed() for menus and links
  • Role switch panel for Tracy

Roles

The package ships role.acl.neon with the default roles; the administrator is allowed everything:

  • guest
  • user (inherits from guest)
  • admin (inherits from user)

drago-ex/project-tools copies it into the project as app/Core/Security/role.acl.neon, where Nette loads it with the other configuration files of app/. A later update keeps your changes. Without project-tools, copy resources/app/Core/Security/role.acl.neon of this package there.

The role names are also available as constants of Drago\Permission\Role.

ACL of Modules

The rules of a presenter are in acl.neon in its directory, next to its conf.neon, in the security section of Nette. Nette merges the files of all modules into one Nette\Security\Permission service, so no PHP code is needed.

The resource is the presenter name. A rule is needed only for roles other than the administrator, for example public pages for guests:

# app/Presentation/Backend/Blog/acl.neon
security:
	resources:
		Backend:Blog:
# app/Presentation/Front/Blog/acl.neon
security:
	resources:
		Front:Blog:

	rules:
		allow:
			- [guest, Front:Blog]

A new presenter needs its resource (and a guest rule when it is public) in the acl.neon next to it. A presenter without a resource is an error (Nette\InvalidStateException, as in Nette), so a forgotten resource shows at once.

Presenter Authorization

Authorization is handled by the Authorization trait. Include it in the base presenter:

use Drago\Permission\Authorization;

class BasePresenter extends Nette\Application\UI\Presenter
{
	use Authorization;
}

As in the Nette ACL, the resource is what is protected (the presenter) and the privilege is the operation:

When Privilege
on startup, every request the action: default → view, other actions by their name (users)
a signal, right before its handler runs its attribute (#[Add] → add), else its name: approve! → approve

Every module uses the same base privileges view, add, edit and delete, so a role may add without editing. Any other operation has a privilege of its own (publish, approveComment, export), never folded into edit. A signal also needs its page: deleting on Blog:default needs view and delete. The rules then read like the application:

security:
	roles:
		author: [user]
	rules:
		allow:
			- [author, Backend:Blog, [view, add, edit]]   # writes articles, does not delete or publish them

Unauthorized access:

  • not logged in: redirect to :Sign:in with a backlink (override signInDestination() to change the target); only a plain GET page is stored, a signal or a form is not replayed after signing in
  • logged in but forbidden: HTTP 403
  • a presenter without a registered ACL resource throws Nette\InvalidStateException, as User::isAllowed() of Nette does: a forgotten resource is an error you see, it never opens a page

Attributes

A signal without an attribute needs the privilege named like it, a form the privilege named like the form (the delete form → delete). An attribute sets it on the handler, or on the factory createComponent<Name>() or the class of a component for all its signals and subcomponents (e.g. its form):

Attribute Privilege
#[View] none of its own, the page is enough (sorting, preview, dialogs)
#[Add], #[Edit], #[Delete] add, edit, delete
#[Privilege('publish')] a privilege of its own
#[Privilege(parameter: 'name')] the value of the signal parameter, else the signal name
use Drago\Permission\Delete;
use Drago\Permission\Edit;
use Drago\Permission\Privilege;
use Drago\Permission\View;

class ArticleControl extends Nette\Application\UI\Control
{
	#[Delete]
	public function handleDelete(int $id): void      // delete
	{
	}


	#[Edit]
	public function handleRemoveImage(int $id): void // edit, a part of editing
	{
	}


	#[Privilege('publish')]
	public function handlePublish(int $id): void     // publish
	{
	}


	#[View]
	public function handlePreview(): void            // the page is enough
	{
	}
}

The signal is checked when its component already exists, so the attribute of the real class decides, whatever the component is named. drago-ex/datagrid marks sorting, filters, paging and the page size with #[View], and a row action with #[Privilege(parameter: 'name')], so it needs the privilege named like the action (addActionEdit() → edit, addActionDelete() → delete).

One Form for Adding and Editing

As in Nette, one form adds and edits a record, with a handler for each. In a component (e.g. in an offcanvas opened by AJAX) the id of the edited record is a persistent parameter, so the submit knows it; the factory only builds the fields and attaches the handler by the id. The submit itself needs only the page (#[View]), each handler checks its privilege with requireAllowed() of AccessCheck (below), and the edit also that the record exists, because a URL can be forged:

use Drago\Permission\Add;
use Drago\Permission\Edit;
use Drago\Permission\View;
use Nette\Application\Attributes\Persistent;

class ArticleControl extends Nette\Application\UI\Control
{
	use Drago\Permission\AccessCheck;

	#[Persistent]
	public ?int $id = null;


	#[Add]
	public function handleAdd(): void
	{
		$this->id = null;
		// open the form
	}


	#[Edit]
	public function handleEdit(int $id): void
	{
		$article = $this->repository->get($id) ?? $this->error();
		$this->id = $id;
		$this['article']->setDefaults($article);
		// open the form
	}


	#[View]
	protected function createComponentArticle(): Form
	{
		$form = $this->formFactory->create();
		// fields
		$form->onSuccess[] = $this->id === null ? $this->addSucceeded(...) : $this->editSucceeded(...);
		return $form;
	}


	private function addSucceeded(Form $form, ArticleValues $values): void
	{
		$this->requireAllowed('add');
		// insert
	}


	private function editSucceeded(Form $form, ArticleValues $values): void
	{
		$this->requireAllowed('edit');
		$this->repository->get($this->id) ?? $this->error();
		// update
		$this->id = null;
	}
}

The attribute needs nothing at runtime: PHP ignores an attribute whose class is not installed, so any Nette component or package can mark its signals and still work without this package.

Buttons

The AccessCheck trait lets a component show its buttons only to those who may use them. isAllowed() asks for the privilege on the presenter of the component, the same one the Authorization trait checks for the signal:

use Drago\Permission\AccessCheck;

class ArticleControl extends Nette\Application\UI\Control
{
	use AccessCheck;
}
$grid->addActionDelete(fn(int $id) => $this->handleDelete($id))
	->setVisible($this->isAllowed('delete'));
<a n:if="$control->isAllowed('add')" n:href="add!" class="btn btn-primary btn-sm">Add</a>

Hiding a button is only for comfort; the signal itself is still checked by the Authorization trait. Where no signal is checked, requireAllowed('edit') ends the request with 403 (ForbiddenRequestException), e.g. in the handlers of one form for adding and editing.

Checking Other Places

For a menu item or a link to another presenter, PrivilegeResolver::isAllowed() is true when the user has at least one of the privileges on the resource. A resource or role the ACL does not know throws, as User::isAllowed() of Nette. PrivilegeResolver::forAction() gives the privilege of a page (default → view):

use Drago\Permission\PrivilegeResolver;

PrivilegeResolver::isAllowed($this->getUser(), 'Backend:Permission', 'users', 'roles');

In templates, register the Latte extension (requires latte/latte):

latte:
	extensions:
		- Drago\Permission\Latte\PermissionExtension

and use the function isAllowed() with the same arguments:

<li n:if="isAllowed('Backend:Permission', 'users', 'roles')">
	<a n:href=":Backend:Permission:">Permissions</a>
</li>

Privileges of a Presenter

PrivilegeCatalog reads the privileges of a presenter from its code by the same rules: the pages (action*(), render*() and the templates next to it, view for default) and the signals and forms of the presenter and its components. Signals with #[View] are left out. An administration of permissions offers them without any list kept by hand:

$catalog = new Drago\Permission\PrivilegeCatalog($presenterFactory);
$catalog->getPrivileges('Backend:Blog'); // ['view', 'add', 'edit', 'delete', 'approveComment', …]

getSources() also tells which places of the code need each privilege, by kind (PrivilegeCatalog::Page, Signal, Form), e.g. for labels in the administration:

$catalog->getSources('Backend:Blog')['delete'];
// ['signal' => ['ArticleControl::handleDelete'], 'form' => ['delete']]

Role Switch in Tracy

While developing, the Tracy bar can switch the roles of the signed-in user, so you can try the application as another role without a test account. Project Permission registers it by itself; otherwise register the panel (requires tracy/tracy):

tracy:
	bar:
		- Drago\Permission\Tracy\RoleSwitchPanel

The tab shows the current roles (red while they are switched). The panel lists the roles of the ACL, from neon as well as from the database; My roles brings the real ones back, and signing out forgets the switch. Nette adds bar panels only in debug mode, so the panel never runs in production.

Optional add-ons

The package works without them. Set them up by hand as described below when the project needs them.

Related Package: Dynamic Role Management

For dynamic role and access management, use:

This package is built on top of drago-ex/permission and provides:

  • role creation
  • assigning roles to users
  • allowing or denying access per role