You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: readme.md
+12-12Lines changed: 12 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -72,7 +72,7 @@ $user->setExpiration(null);
72
72
73
73
Expiration must be set to value equal or lower than the expiration of sessions.
74
74
75
-
The reason of the last logout can be obtained by method `$user->getLogoutReason()`, which returns either the constant `Nette\Security\IUserStorage::INACTIVITY` if the time expired or `IUserStorage::MANUAL` when the `logout()` method was called.
75
+
The reason of the last logout can be obtained by method `$user->getLogoutReason()`, which returns either the constant `Nette\Security\UserStorage::LOGOUT_INACTIVITY` if the time expired or `UserStorage::LOGOUT_MANUAL` when the `logout()` method was called.
76
76
77
77
In presenters, you can verify login in the `startup()` method:
78
78
@@ -102,12 +102,12 @@ $authenticator = new Nette\Security\SimpleAuthenticator([
102
102
103
103
This solution is more suitable for testing purposes. We will show you how to create an authenticator that will verify credentials against a database table.
104
104
105
-
An authenticator is an object that implements the [Nette\Security\IAuthenticator](https://api.nette.org/3.0/Nette/Security/IAuthenticator.html) interface with method `authenticate()`. Its task is either to return the so-called [identity](#Identity) or to throw an exception `Nette\Security\AuthenticationException`. It would also be possible to provide an fine-grain error code `IAuthenticator::IDENTITY_NOT_FOUND` or `IAuthenticator::INVALID_CREDENTIAL`.
105
+
An authenticator is an object that implements the [Nette\Security\Authenticator](https://api.nette.org/3.0/Nette/Security/Authenticator.html) interface with method `authenticate()`. Its task is either to return the so-called [identity](#Identity) or to throw an exception `Nette\Security\AuthenticationException`. It would also be possible to provide an fine-grain error code `Authenticator::IDENTITY_NOT_FOUND` or `Authenticator::INVALID_CREDENTIAL`.
106
106
107
107
```php
108
108
use Nette;
109
109
110
-
class MyAuthenticator implements Nette\Security\IAuthenticator
110
+
class MyAuthenticator implements Nette\Security\Authenticator
111
111
{
112
112
private $database;
113
113
private $passwords;
@@ -118,10 +118,8 @@ class MyAuthenticator implements Nette\Security\IAuthenticator
118
118
$this->passwords = $passwords;
119
119
}
120
120
121
-
public function authenticate(array $credentials): Nette\Security\IIdentity
121
+
public function authenticate($username, $password): Nette\Security\IIdentity
122
122
{
123
-
[$username, $password] = $credentials;
124
-
125
123
$row = $this->database->table('users')
126
124
->where('username', $username)
127
125
->fetch();
@@ -134,7 +132,7 @@ class MyAuthenticator implements Nette\Security\IAuthenticator
134
132
throw new Nette\Security\AuthenticationException('Invalid password.');
135
133
}
136
134
137
-
return new Nette\Security\Identity(
135
+
return new Nette\Security\SimpleIdentity(
138
136
$row->id,
139
137
$row->role, // or array of roles
140
138
['name' => $row->username]
@@ -180,7 +178,7 @@ Importantly, **when user logs out, identity is not deleted** and is still availa
180
178
181
179
Thanks to this, you can still assume which user is at the computer and, for example, display personalized offers in the e-shop, however, you can only display his personal data after logging in.
182
180
183
-
Identity is an object that implements the [Nette\Security\IIdentity](https://api.nette.org/3.0/Nette/Security/IIdentity.html) interface, the default implementation is [Nette\Security\Identity](https://api.nette.org/3.0/Nette/Security/Identity.html). And as mentioned, identity is stored in the session, so if, for example, we change the role of some of the logged-in users, old data will be kept in the identity until he logs in again.
181
+
Identity is an object that implements the [Nette\Security\IIdentity](https://api.nette.org/3.0/Nette/Security/IIdentity.html) interface, the default implementation is [Nette\Security\SimpleIdentity](https://api.nette.org/3.0/Nette/Security/SimpleIdentity.html). And as mentioned, identity is stored in the session, so if, for example, we change the role of some of the logged-in users, old data will be kept in the identity until he logs in again.
184
182
185
183
186
184
@@ -201,7 +199,7 @@ if ($user->isLoggedIn()) { // is user logged in?
201
199
Roles
202
200
-----
203
201
204
-
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 `Identity` constructor, either as a string or an array.
202
+
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.
205
203
206
204
As an authorization criterion, we will now use the method `isInRole()`, which checks whether the user is in the given role:
207
205
@@ -211,7 +209,7 @@ if ($user->isInRole('admin')) { // is the admin role assigned to the user?
211
209
}
212
210
```
213
211
214
-
As you already know, logging the user out does not erase his identity. Thus, method `getIdentity()` still returns object `Identity`, 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.
212
+
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.
215
213
216
214
217
215
Authorizator
@@ -223,10 +221,10 @@ In addition to roles, we will introduce the terms resource and operation:
223
221
-**resource** is a logical unit of the application - article, page, user, menu item, poll, presenter, ...
224
222
-**operation** is a specific activity, which user may or may not do with *resource* - view, edit, delete, vote, ...
225
223
226
-
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\IAuthorizator](https://api.nette.org/3.0/Nette/Security/IAuthorizator.html) interface with only one method `isAllowed()`:
224
+
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](https://api.nette.org/3.0/Nette/Security/Authorizator.html) interface with only one method `isAllowed()`:
227
225
228
226
```php
229
-
class MyAuthorizator implements Nette\Security\IAuthorizator
227
+
class MyAuthorizator implements Nette\Security\Authorizator
230
228
{
231
229
public function isAllowed($role, $resource, $operation): bool
232
230
{
@@ -434,3 +432,5 @@ It is possible to have several independent logged users within one site and one
0 commit comments