Installation

Usage

This package builds on top of the standard access authorization of Nette components, namely Nette\Application\UI\Component::checkRequirements() method.
This method is called before invoking any of component/presenter signal handlers, and before presenter startup, action<> and render<> methods.

With this package you can specify the access rules via annotations on any of the mentioned methods, or on presenter class.
To enable this feature simple use TSecurityAnnotations trait in any presenter or component.

Example:

/** * @loggedIn * To access this presenter the user must be logged in.*/classSecuredPresenterextendsNette\Application\UI\Presenter{useNepada\SecurityAnnotations\TSecurityAnnotations;/** * @role(admin, superadmin)*/publicfunctionactionForAdmins(): void {// Only users with role admin or superadmin are allowed here. }/** * @allowed(resource=world, privilege=destroy)*/publicfunctionhandleDestroyWorld(): void {// Only users with specific permission are allowed to call this signal. }}

The annotations and rules they enforce are completely customizable (see below), however the default setup comes with three predefined rules:

@loggedIn - checks whether the user is logged in.

@role(admin, superadmin) - checks whether the user has at least one of the specified roles.
If you use Nette\Security\Permission as your authorizator, then role inheritance is taken into account, i.e. users that have at least one role that inherits from at least one of the specified roles are allowed as well.

@allowed(resource=world, privilege=destroy) - checks whether the user has at least one role that is granted the specified privilege on the specified resource.

Securing components

Properly securing components is a tricky business, take a look at the following example:

Securing presenter action<> (or render<>) methods is not sufficient! All it takes is a one general route in your router, e.g. a very common Route('<presenter>/<action>'), and anyone may successfully submit the form by sending POST request to /secured/foo URL.

You should always check user's permissions when creating the component. To make your life easier there is TSecuredComponents trait that calls the standard Nette\Application\UI\Component::checkRequirements() method before calling the component factory. Combining it with TSecurityAnnotations it allows you to control access to components via annotations on createComponent<> methods.

Customizing access validators

All access validators are configured in validators section of the extension configuration:

To disable any of the default rules set the validator to false.

You can also define your own validators, i.e. services implementing Nepada\SecurityAnnotations\AccessValidators\IAccessValidator interface.

How do access validators work?

Annotations are parsed by Nette\Reflection\AnnotationsParser and their value is one by one passed for inspection to the specific validator.
It's the responsibility of the validator to check whether or not the annotation value is of expected type, e.g. the default @loggedIn validator can handle only boolean values.
Based on the annotation value the validator decides either to deny access (throws Nette\Application\BadRequestException), or grant access (no exception is thrown).