Skip to content

Commit a58c5e3

Browse files
committed
readme: updated [WIP]
1 parent d47059b commit a58c5e3

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

readme.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ $user->setExpiration(null);
7272

7373
Expiration must be set to value equal or lower than the expiration of sessions.
7474

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.
7676

7777
In presenters, you can verify login in the `startup()` method:
7878

@@ -102,12 +102,12 @@ $authenticator = new Nette\Security\SimpleAuthenticator([
102102

103103
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.
104104

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`.
106106

107107
```php
108108
use Nette;
109109

110-
class MyAuthenticator implements Nette\Security\IAuthenticator
110+
class MyAuthenticator implements Nette\Security\Authenticator
111111
{
112112
private $database;
113113
private $passwords;
@@ -118,10 +118,8 @@ class MyAuthenticator implements Nette\Security\IAuthenticator
118118
$this->passwords = $passwords;
119119
}
120120

121-
public function authenticate(array $credentials): Nette\Security\IIdentity
121+
public function authenticate($username, $password): Nette\Security\IIdentity
122122
{
123-
[$username, $password] = $credentials;
124-
125123
$row = $this->database->table('users')
126124
->where('username', $username)
127125
->fetch();
@@ -134,7 +132,7 @@ class MyAuthenticator implements Nette\Security\IAuthenticator
134132
throw new Nette\Security\AuthenticationException('Invalid password.');
135133
}
136134

137-
return new Nette\Security\Identity(
135+
return new Nette\Security\SimpleIdentity(
138136
$row->id,
139137
$row->role, // or array of roles
140138
['name' => $row->username]
@@ -180,7 +178,7 @@ Importantly, **when user logs out, identity is not deleted** and is still availa
180178

181179
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.
182180

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.
184182

185183

186184

@@ -201,7 +199,7 @@ if ($user->isLoggedIn()) { // is user logged in?
201199
Roles
202200
-----
203201

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.
205203

206204
As an authorization criterion, we will now use the method `isInRole()`, which checks whether the user is in the given role:
207205

@@ -211,7 +209,7 @@ if ($user->isInRole('admin')) { // is the admin role assigned to the user?
211209
}
212210
```
213211

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.
215213

216214

217215
Authorizator
@@ -223,10 +221,10 @@ In addition to roles, we will introduce the terms resource and operation:
223221
- **resource** is a logical unit of the application - article, page, user, menu item, poll, presenter, ...
224222
- **operation** is a specific activity, which user may or may not do with *resource* - view, edit, delete, vote, ...
225223

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()`:
227225

228226
```php
229-
class MyAuthorizator implements Nette\Security\IAuthorizator
227+
class MyAuthorizator implements Nette\Security\Authorizator
230228
{
231229
public function isAllowed($role, $resource, $operation): bool
232230
{
@@ -434,3 +432,5 @@ It is possible to have several independent logged users within one site and one
434432
```php
435433
$user->getStorage()->setNamespace('forum');
436434
```
435+
436+
[Continue...](https://doc.nette.org/en/3.0/access-control)

0 commit comments

Comments
 (0)