Download the PHP package zycon42/security without Composer
On this page you can find all versions of the php package zycon42/security. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download zycon42/security
More information about zycon42/security
Files in zycon42/security
Package security
Short Description Security extension for Nette framework
License MIT
Informations about the package security
Security
Overview
Because I wasn't satisfied with current state of nette authorization mechanism I decided to port Symfony/Security
into Nette.
It is largely based on Symfony/Security-Core
. Sadly Nette authentication mechanism and Nette\Security\User
class are incompatible with pure Symfony/Security-Core
, so it was necessary to rewrite it.
Currently this project handles only Authorization for Authentication you have to use Nette classes. Also ACL isn't currently supported.
For more info how it works internally please refer to symfony security documentation.
Requirements
This project requires php 5.4
Installation
The best way to install Zycon42/Security is using the Composer:
and then you have to enable it in your config.neon
Basic Usage
Main entry point for authorizations is SecurityContext
class. Sample usage:
Code above will deny access if current user doesn't have role named ADMIN
. Instead of roles you can use IS_AUTHENTICATED
or IS_ANONYMOUS
that grant access only to authenticated users or anonymous users respectively.
Also you can utilize optional secondary parameter object
of isGranted
method and ask if current user can perform given action on given resource like this:
Voters
Symfony security uses idea of voters that vote if user will be granted or denied. Access decision manager collects these votes and decides based on them. Project ships with three voters. One for roles, second for IS_AUTHENTICATED, IS_ANONYMOUS
tokens and last one for expressions which we will discuss later.
Using voters you can easily extend range of supported attributes and objects. You can for example implement typical use-case of user only allowed to edit own posts.
Create new voter implementing Zycon42\Security\Authorization\Voters\IVoter
interface and then register it in DIC with specific tag
When you tag service with security.voter
tag it will be added into AccessDecisionManager
as voter.
For more information about voters and how to implement new one please refer to symfony documentation only remember that instead of TokenInterface
we use IIdentity
from nette.
Expressions
To be able to write more complex access rules you can use expressions. For parsing it we use symfony/expression-language
.
There are several functions you can use in them:
isAnonymous()
returns true if current user isn't authenticatedisAuthenticated()
returns true if current user is authenticatedhasRole(string $role)
checks if user is in given rolehasPermission($object, $action)
checks if user has permission to perform action on object
Also you can access several variables:
identity
current user identityuser
nette user objectNette\Security\User
object
object that was passed as second parameter intoisGranted
method.roles
array of identity roles
Example usage:
Presenter annotations
To be able to use presenter annotations for granting/denying access use this in your secured presenter, which all your presenters that needs to use this, derive:
Remember not to override checkRequirements
method in your derived presenters.
Now you can annotate your presenters and its action/render/handle
methods with @Security
annotations. Small example:
or on action
method
When using annotations on presenters note that annotations are inherited and are checked in order from base class to derived classes.
Expressions in annotations are same as these on isGranted
but additionally you have access to all current request parameters as variables and object variable contains current request. So if you use something that converts presenter methods parameters from id
to actual objects by adding additional request variables like zycon42/param-converters
you will be able to write:
Configuration
Here you can find possible configuration options and its default values
In roleHierarchy
section you can define how roles inherit from each other
Note that ADMIN inheriting from USER is redundant because ADMIN inherits from USER through MANAGER. But here is list of each role effective list:
- ADMIN: ADMIN, USER, MANAGER, CLIENT, GUEST
- MANAGER: MANAGER, USER, CLIENT, GUEST
- CLIENT: CLIENT, GUEST
All versions of security with dependencies
nette/security Version ~2.2
nette/application Version ~2.2
symfony/expression-language Version ~2.5