Access Control (Authorization)
Authorization determines whether a user has sufficient privileges, for example, to access a specific resource or to perform an action. Authorization assumes previous successful authentication, ie that the user is logged in.
→ Installation and requirements
In the examples, we will use an object of class Nette\Security\User, which represents the current user and
which you get by passing it using dependency injection. In presenters
simply call $user = $this->getUser()
.
For very simple websites with administration, where user rights are not distinguished, it is possible to use the already known
method as an authorization criterion isLoggedIn()
. In other words: once a user is logged in, he has permissions to
all actions and vice versa.
Roles
The purpose of roles is to offer a more precise permission management and remain independent on the user name. As soon as user
logs in, he is assigned one or more roles. Roles themselves may be simple strings, for example, admin
,
member
, guest
, etc. They are specified in the second argument of SimpleIdentity
constructor, either as a string or an array.
As an authorization criterion, we will now use the method isInRole()
, which checks whether the user is in the
given role:
As you already know, logging the user out does not erase his identity. Thus, method getIdentity()
still returns
object SimpleIdentity
, including all granted roles. The Nette Framework adheres to the principle of „less code,
more security“, so when you are checking roles, you do not have to check whether the user is logged in too. Method
isInRole()
works with effective roles, ie if the user is logged in, roles assigned to identity are used, if he
is not logged in, an automatic special role guest
is used instead.
Authorizator
In addition to roles, we will introduce the terms resource and operation:
- role is a user attribute – for example moderator, editor, visitor, registered user, administrator, …
- resource is a logical unit of the application – article, page, user, menu item, poll, presenter, …
- operation is a specific activity, which user may or may not do with resource – view, edit, delete, vote, …
An authorizer is an object that decides whether a given role has permission to perform a certain operation with
specific resource. It is an object implementing the Nette\Security\Authorizator interface with only one
method isAllowed()
:
We add the authorizator to the configuration as a service of the DI container:
And the following is an example of use. Note that this time we call the method Nette\Security\User::isAllowed()
,
not the authorizator's one, so there is not first parameter $role
. This method calls
MyAuthorizator::isAllowed()
sequentially for all user roles and returns true if at least one of them has
permission.
Both arguments are optional and their default value means everything.
Permission ACL
Nette comes with a built-in implementation of the authorizer, the Nette\Security\Permission class, which offers a lightweight and flexible ACL (Access Control List) layer for permission and access control. When we work with this class, we define roles, resources, and individual permissions. And roles and resources may form hierarchies. To explain, we will show an example of a web application:
guest
: visitor that is not logged in, allowed to read and browse public part of the web, ie. read articles, comment and vote in pollsregistered
: logged-in user, which may on top of that post commentsadmin
: can manage articles, comments and polls
So we have defined certain roles (guest
, registered
and admin
) and mentioned resources
(article
, comments
, poll
), which the users may access or take actions on
(view
, vote
, add
, edit
).
We create an instance of the Permission class and define roles. It is possible to use the inheritance of roles, which
ensures that, for example, a user with a role admin
can do what an ordinary website visitor can do (and of
course more).
We will now define a list of resources that users can access:
Resources can also use inheritance, for example, we can add $acl->addResource('perex', 'article')
.
And now the most important thing. We will define between them rules determining who can do what:
What if we want to prevent someone from accessing a resource?
Now when we have created the set of rules, we may simply ask the authorization queries:
The same applies to a registered user, but he can also comment:
The administrator can edit everything except polls:
Permissions can also be evaluated dynamically and we can leave the decision to our own callback, to which all parameters are passed:
But how to solve a situation where the names of roles and resources are not enough, ie we would like to define that, for
example, a role registered
can edit a resource article
only if it is its author? We will use objects
instead of strings, the role will be the object Nette\Security\Role and the source Nette\Security\Resource. Their methods
getRoleId()
resp. getResourceId()
will return the original strings:
And now let's create a rule:
The ACL is queried by passing objects:
A role may inherit form one or more other roles. But what happens, if one ancestor has certain action allowed and the other one has it denied? Then the role weight comes into play – the last role in the array of roles to inherit has the greatest weight, first one the lowest:
Roles and resources can also be removed (removeRole()
, removeResource()
), rules can also be reverted
(removeAllow()
, removeDeny()
). The array of all direct parent roles returns
getRoleParents()
. Whether two entities inherit from each other returns roleInheritsFrom()
and
resourceInheritsFrom()
.
Add as a Service
We need to add the ACL created by us to the configuration as a service so that it can be used by the object $user
,
ie so that we can use in code for example $user->isAllowed('article', 'view')
. For this purpose, we will write a
factory for it:
And we will add it to the configuration:
In presenters, you can then verify permissions in the startup()
method, for example: