Configuring Access Control
Overview of configuration options for Nette Security.
If you are not using the entire framework, but only this library, read how to load the configuration.
You can define a list of users in the configuration to create a simple authenticator
(Nette\Security\SimpleAuthenticator). Because passwords are listed in plain text in the configuration, this solution
is suitable for testing purposes only.
security:
# show the user panel in Tracy Bar?
debugger: ... # (bool) default is auto (on when Tracy is active)
users:
# name: password
johndoe: 'secret123'
# name, password, roles, and other data available in the identity
janedoe:
password: 'secret123'
roles: [admin]
data: ...
A user listed without roles, like johndoe, has an empty list of roles. How to grant rights to anyone
who is logged in is shown in Access
Control.
Instead of a plain-text password, you can enter its hash; it is recognized automatically. Because both passwords and hashes may contain characters with special meaning in NEON, we recommend always enclosing them in quotes.
security:
users:
johndoe: '$2y$12$8aeofxuRALu4iNeaBHyLpuxf3xxKQdEreSaNLkQUiIfUmAv6cRHLa'
Furthermore, you can define roles and resources to create a basis for the ACL (Nette\Security\Permission):
security:
roles:
guest:
registered: [guest] # registered inherits from guest
admin: [registered] # and admin inherits from registered
resources:
article:
comment: [article] # resource inherits from article
poll:
And finally the access rules. Each rule is a triple of role, resource, and privilege; an omitted or null item
means “all”, and a bare string allows or denies the role everything.
security:
rules:
allow:
- [guest, article, view] # role, resource, privilege
- [registered, comment, [add, edit]] # a list means multiple values
- [admin] # admin may do everything
deny:
- [banned, comment, add]
The ACL created this way is one of the voters of the current user. Rules
that depend on a particular object, such as an article that only its author may edit, don't belong here; write your own voter for
them. If you need conditions verified by a callback (assertions), build the ACL in PHP as shown in ACL. When you do, define the roles, resources, and rules
there rather than here, otherwise you would end up with two competing Nette\Security\Permission services.
User Storage
You can configure how to store information about the logged-in user:
security:
authentication:
# period of inactivity after which the user will be logged out
expiration: 30 minutes # (string) default is not set
# where to store information about the logged-in user
storage: session # (session|cookie) default is session
# keep the identity available after logout and expiration
persistIdentity: true # (bool) defaults to true
If you choose cookie as the storage, you can also set these options:
security:
authentication:
# name of the cookie
cookieName: userId # (string) defaults to userid
# domains that can receive the cookie
cookieDomain: 'example.com' # (string|domain)
# restriction for cross-origin access
cookieSamesite: None # (Strict|Lax|None) defaults to Lax
DI Services
These services are added to the DI container:
| Name | Type | Description |
|---|---|---|
security.authenticator |
Nette\Security\Authenticator | authenticator |
security.authorizator |
Nette\Security\Permission | ACL from the configuration |
security.passwords |
Nette\Security\Passwords | password hashing |
security.user |
Nette\Security\User | current user |
security.userStorage |
Nette\Security\UserStorage | storage |
Every service implementing Nette\Security\Voter
is added to the security.user service as a voter.