nepada / security-annotations
Security annotations for Nette presenters and components.
Installs: 8 750
Dependents: 1
Suggesters: 0
Security: 0
Stars: 10
Watchers: 3
Forks: 1
Open Issues: 0
Requires
- php: >=8.1.0 <8.5
- nette/application: ^3.1.4@dev
- nette/component-model: ^3.0.2@dev
- nette/security: ^3.1@dev
- nette/utils: ^3.2@dev || ^4.0@dev
Requires (Dev)
- mockery/mockery: 1.6.12
- nepada/coding-standard: 7.14.0
- nepada/phpstan-nette-tester: 1.2.1
- nette/bootstrap: >=3.1@dev
- nette/di: ^3.0.6@dev
- nette/http: >=3.0@dev
- nette/neon: >=3.3.4@dev
- nette/robot-loader: *@dev
- nette/schema: ^1.0.3@dev
- nette/tester: 2.5.4
- php-parallel-lint/php-parallel-lint: 1.4.0
- phpstan/phpstan: 1.12.5
- phpstan/phpstan-mockery: 1.1.3
- phpstan/phpstan-nette: 1.3.8
- phpstan/phpstan-strict-rules: 1.6.1
- shipmonk/phpstan-rules: 3.2.0
- spaze/phpstan-disallowed-calls: 3.4.0
Suggests
- nette/di: for integration with Nette DI container
README
Installation
Via Composer:
$ composer require nepada/security-annotations
Register the extension in config.neon
:
extensions: securityAnnotations: Nepada\Bridges\SecurityAnnotationsDI\SecurityAnnotationsExtension
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 attributes on any of the mentioned methods, or on presenter class.
To enable this feature simple use SecurityAnnotations
trait in any presenter or component and make sure RequirementsChecker
service gets injected via injectRequirementsChecker()
- with default Nette configuration this should work on presenters out of the box, but you need to take care of components, e.g. by enabling inject calls.
Example:
use Nepada\SecurityAnnotations\Annotations\Allowed; use Nepada\SecurityAnnotations\Annotations\LoggedIn; use Nepada\SecurityAnnotations\Annotations\Role; /** * To access this presenter the user must be logged in. */ #[LoggedIn] class SecuredPresenter extends Nette\Application\UI\Presenter { use Nepada\SecurityAnnotations\SecurityAnnotations; #[Role("admin", "superadmin")] public function actionForAdmins(): void { // Only users with role admin or superadmin are allowed here. } #[Allowed(resource: "world", privilege: "destroy")] public function handleDestroyWorld(): void { // Only users with specific permission are allowed to call this signal. } }
The attributes 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:
use Nepada\SecurityAnnotations\Annotations\LoggedIn; class SecuredPresenter extends Nette\Application\UI\Presenter { use Nepada\SecurityAnnotations\SecurityAnnotations; #[LoggedIn] public function actionDefault(): void { // ... } protected function createComponentForm(): Nette\Application\UI\Form { $form = new Nette\Application\UI\Form(); $form->addSubmit('Do something dangerous'); $form->onSuccess[] = function (Nette\Application\UI\Form $form): void { // ... }; return $form; } }
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 SecuredComponents
trait that calls the standard Nette\Application\UI\Component::checkRequirements()
method before calling the component factory (nette/application 3.2.2 and later performs this check natively, making the trait obsolete). Combining it with SecurityAnnotations
it allows you to control access to components via attributes on createComponent<>
methods.
Customizing access validators
- You can disable the default set of validators by
enableDefaultValidators: false
. - You can also define your own validators, i.e. services implementing
Nepada\SecurityAnnotations\AccessValidators\AccessValidator
interface invalidators
configuration section.
securityAnnotations: enableDefaultValidators: false # disable default set of validators validators: - MyRoleAccessValidator # define validator by class name - @fooAccessValidator # define validator by service reference services: fooAccessValidator: FooAccessValidator(%fooParameter%)
How do access validators work?
Every access validator implements Nepada\SecurityAnnotations\AccessValidators\AccessValidator
interface. The access validator specifies which attribute type it supports via its public API.
When checking the requirements PHP attributes are passed one by one to associated access validator for inspection. Based on the attribute value the validator decides either to deny access (throws Nette\Application\BadRequestException
), or grant access (no exception is thrown).