Skip to content

Commit f0c09b4

Browse files
authored
Merge pull request #189 from codeigniter4/docs
Misc docs updates
2 parents 8c507cf + f99f166 commit f0c09b4

File tree

7 files changed

+150
-99
lines changed

7 files changed

+150
-99
lines changed

docs/0 - install.md

Lines changed: 5 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Installation
22

3+
These instructions assume that you have already [installed the CodeIgniter 4 app starter](https://codeigniter.com/user_guide/installation/installing_composer.html) as the basis for your new project, set up your `.env` file, and created a database that you can access via the Spark CLI script.
4+
35
Installation is done through [Composer](https://getcomposer.org). The example assumes you have it installed globally.
46
If you have it installed as a phar, or othewise you will need to adjust the way you call composer itself.
57

@@ -75,11 +77,12 @@ public $ruleSets = [
7577
7678
## Controller Filters
7779
78-
Shield provides 3 [Controller Filters](https://codeigniter.com/user_guide/incoming/filters.html) you can
80+
Shield provides 4 [Controller Filters](https://codeigniter.com/user_guide/incoming/filters.html) you can
7981
use to protect your routes, `session`, `tokens`, and `chained`. The first two cover the `Session` and
8082
`AccessTokens` authenticators, respectively. The `chained` filter will check both authenticators in sequence
8183
to see if the user is logged in through either of authenticators, allowing a single API endpoint to
82-
work for both an SPA using session auth, and a mobile app using access tokens.
84+
work for both an SPA using session auth, and a mobile app using access tokens. The fourth, `auth-rates`,
85+
provides a good basis for rate limiting of auth-related routes.
8386
8487
These filters are already loaded for you by the registrar class located at `src/Config/Registrar.php`.
8588
@@ -110,54 +113,3 @@ public $filters = [
110113
]
111114
];
112115
```
113-
114-
## Further Customization
115-
116-
### Route Configuration
117-
118-
If you need to customize how any of the auth features are handled, you will likely need to update the routes to point to the correct controllers. You can still use the `service('auth')->routes()` helper, but you will need to pass the `except` option with a list of routes to customize:
119-
120-
```php
121-
service('auth')->routes($routes, ['except' => ['login', 'register']]);
122-
```
123-
124-
Then add the routes to your customized controllers:
125-
126-
```php
127-
$routes->get('login', '\App\Controllers\Auth\LoginController::loginView');
128-
$routes->get('register', '\App\Controllers\Auth\RegisterController::registerView');
129-
```
130-
131-
### Extending the Controllers
132-
133-
Shield has the following controllers that can be extended to handle
134-
various parts of the authentication process:
135-
136-
- **ActionController** handles the after login and after-registration actions that can be ran, like Two Factor Authentication and Email Verification.
137-
138-
- **LoginController** handles the login process.
139-
140-
- **RegisterController** handles the registration process. Overriding this class allows you to customize the User Provider, the User Entity, and the validation rules.
141-
142-
- **MagicLinkController** handles the "lost password" process that allows a user to login with a link sent to their email. Allows you to
143-
override the message that is displayed to a user to describe what is happening, if you'd like to provide more information than simply swapping out the view used.
144-
145-
It is not recommended to copy the entire controller into app and change it's namespace. Instead, you should create a new controller that extends
146-
the existing controller and then only override the methods needed. This allows the other methods to always stay up to date with any security
147-
updates that might happen in the controllers.
148-
149-
```php
150-
<?php
151-
152-
namespace App\Controllers;
153-
154-
use CodeIgniter\Shield\Controllers\LoginController as ShieldLogin;
155-
156-
class LoginController extends ShieldLogin
157-
{
158-
public function logoutAction()
159-
{
160-
// new functionality
161-
}
162-
}
163-
```

docs/1 - concepts.md

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,47 @@ This document covers some of the base concepts used throughout the library.
44

55
## Repository State
66

7-
Shield is designed so that the initial setup of your application can all happen in code with nothing required to be saved in the database. This means you do not have to create large seeder files that need
8-
to run within each environment. Instead, it can be placed under version control, though the Settings library allows those settings to be easily
9-
stored in the database if you create an interface for the user to update those settings at.
7+
Shield is designed so that the initial setup of your application can all happen in code with nothing required to be
8+
saved in the database. This means you do not have to create large seeder files that need to run within each environment.
9+
Instead, it can be placed under version control, though the Settings library allows those settings to be easily stored
10+
in the database if you create an interface for the user to update those settings.
1011

1112
## Settings
1213

13-
In place of the CodeIgniter `config()` helper, Shield uses the official
14-
[Settings](https://github.com/codeigniter4/settings) library. This provides a way to save any Config class values to the database if you want to modify them, but falls back on the standard Config class if nothing is found in the database.
14+
In place of the CodeIgniter `config()` helper, Shield uses the official [Settings](https://github.com/codeigniter4/settings)
15+
library. This provides a way to save any Config class values to the database if you want to modify them, but falls back
16+
on the standard Config class if nothing is found in the database.
1517

1618
## User Providers
1719

18-
To make the system as flexible as possible, you can define which class should be able to interact with your chosen persistence system to get the user records. Typically this is going to be a Model, and one is provided for you, at `CodeIgniter\Shield\Models\UserModel`. This is defined in `Config\Auth->userProvider`.
20+
You can use your own models to handle user persistence. Shield calls this the "User Provider" class. A default model
21+
is provided for you at `CodeIgniter\Shield\Models\UserModel`. You can change this in the `Config\Auth->userProvider` setting.
22+
The only requirement is that your new class MUST extend the provided `UserModel`.
1923

2024
```php
2125
public $userProvider = 'CodeIgniter\Shield\Models\UserModel';
2226
```
2327

2428
## User Identities
2529

26-
User accounts are stored separately from the information needed to identify that user. These identifying pieces of data we call User Identities. By default, the library has two types of identities: one for standard email/password information, and one for access tokens.
30+
User accounts are stored separately from the information needed to identify that user. These identifying pieces of data are
31+
called User Identities. By default, the library has two types of identities: one for standard email/password information,
32+
and one for access tokens.
2733

28-
By keeping the identity information loosely coupled from the user account itself, it frees the system up to more easily integrate third-party sign-in systems, JWT systems, and more, all on a single user. With small overrides you could even allow a single user to have multiple email/password combinations if your needs demands the functionality.
34+
Keeping these identities loosely coupled from the user account itself facilitates integrations with third-party sign-in systems, JWT systems, and more - all on a single user.
2935

30-
While this has the potential to make the system more complex, the `email` and `password` fields are automatically looked up for you when attempting to access from the User entity. Caution should be used to craft queries that will pull in the `email` field when you need to display it to the user, as you could easily run into some n+1 slow queries otherwise.
36+
While this has the potential to make the system more complex, the `email` and `password` fields are automatically
37+
looked up for you when attempting to access them from the User entity. Caution should be used to craft queries that will pull
38+
in the `email` field when you need to display it to the user, as you could easily run into some n+1 slow queries otherwise.
3139

32-
When you `save($user)` a `User` instance in the `UserModel`, the email/password identity will automatically be updated. If no email/password identity exists, you must
33-
pass both the email and the password to the User instance prior to calling `save()`.
40+
When you `save($user)` a `User` instance in the `UserModel`, the email/password identity will automatically be updated.
41+
If no email/password identity exists, you must pass both the email and the password to the User instance prior to calling `save()`.
3442

3543
## Password Validators
3644

37-
When registering a user account, the user's password must be validated to ensure it matches the security requirements of your application. To make the system as flexible as possible Shield uses a pipeline of
38-
Validators to handle the validation. This allows you turn on or off any validation systems that are appropriate for your application. The following Validators are available:
45+
When registering a user account, the user's password must be validated to ensure it matches the security requirements of
46+
your application. Shield uses a pipeline of Validators to handle the validation. This allows you turn on or off any validation
47+
systems that are appropriate for your application. The following Validators are available:
3948

4049
- **CompositionValidator** validates the makeup of the password itself. This used to include things
4150
like ensuring it contained a symbol, a number, etc. According to the current
@@ -50,11 +59,11 @@ Validators to handle the validation. This allows you turn on or off any validati
5059
`Config\Auth->maxSimilarity`. The default value is 50, but see the docblock in the config
5160
file for more details. This is enabled by default.
5261
- **DictionaryValidator** will compare the password against a provided file with about 600,000
53-
frequently used passwords as has been seen in various data dumps over the years. If the
62+
frequently used passwords that have been seen in various data dumps over the years. If the
5463
chosen password matches any found in the file, it will be rejected. This is enabled by default.
5564
- **PwnedValidator** is like the `DictionaryValidator`. Instead of comparing to a local file, it
5665
uses a third-party site, [Have I Been Pwned](https://haveibeenpwned.com/Passwords) to check
57-
against a list of over 500 million leaked passwords from many data dumps across the web.
66+
against a list of over 630 million leaked passwords from many data dumps across the web.
5867
The search is done securely, and provides more information than the simple dictionary version.
5968
However, this does require an API call to a third-party which not every application will
6069
find acceptable. You should use either this validator or the `DictionaryValidator`, not both.

docs/2 - authentication.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
# Authentication
22

3-
Shield provides a flexible, secure, authentication system for your web apps and API's.
3+
Authentication is the process of determining that a visitor actually belongs to your website,
4+
and identifying them. Shield provides a flexible and secure authentication system for your
5+
web apps and APIs.
46

57
## Available Authenticators
68

79
Shield ships with 2 authenticators that will serve several typical situations within web app development: the
810
Session authenticator, which uses username/email/password to authenticate against and stores it in the session,
9-
and Access Tokens authenticator which uses private access tokens passed in the headers.
11+
and the Access Tokens authenticator which uses private access tokens passed in the headers.
1012

1113
The available authenticators are defined in `Config\Auth`:
1214

@@ -37,9 +39,9 @@ helper('auth');
3739
auth()->user();
3840

3941
// get the current user's id
40-
user_id()
41-
// or
4242
auth()->id()
43+
// or
44+
user_id()
4345
```
4446

4547
## Authenticator Responses
@@ -152,6 +154,8 @@ auth()->logout();
152154
The `forget` method will purge all remember-me tokens for the current user, making it so they
153155
will not be remembered on the next visit to the site.
154156

157+
158+
155159
## Access Token Authenticator
156160

157161
The Access Token authenticator supports the use of revoke-able API tokens without using OAuth. These are commonly
@@ -185,6 +189,7 @@ This creates the token using a cryptographically secure random string. The token
185189
is hashed (sha256) before saving it to the database. The method returns an instance of
186190
`CodeIgniters\Shield\Authentication\Entities\AccessToken`. The only time a plain text
187191
version of the token is available is in the `AccessToken` returned immediately after creation.
192+
188193
**The plain text version should be displayed to the user immediately so they can copy it for
189194
their use.** If a user loses it, they cannot see the raw version anymore, but they can generate
190195
a new token to use.
@@ -236,7 +241,7 @@ Tokens will expire after a specified amount of time has passed since they have b
236241
By default, this is set to 1 year. You can change this value by setting the `accessTokenLifetime`
237242
value in the `Auth` config file. This is in seconds so that you can use the
238243
[time constants](https://codeigniter.com/user_guide/general/common_functions.html#time-constants)
239-
CodeIgniter provides.
244+
that CodeIgniter provides.
240245

241246
```php
242247
public $unusedTokenLifetime = YEAR;

docs/3 - auth_actions.md

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,6 @@ Views for all of these pages are defined in the `Auth` config file, with the `$v
5151
];
5252
```
5353

54-
NOTE: a session flag is set with the current action step and the user cannot continue until that
55-
flag has been cleared.
56-
5754
## Defining New Actions
5855

5956
While the provided email-based activation and 2FA will work for many sites, others will have different
@@ -72,9 +69,8 @@ told the user would be happening. For example, in the `Email2FA` class, this met
7269
sends the email to the user, and then displays the form the user should enter the 6 digit code into.
7370

7471
**verify()** is the final step in the action's journey. It verifies the information the user provided
75-
and provides feedback. One important task is to remove the `auth_action` field from the session so
76-
that a user can proceed through the site like normal. In the `Email2FA` class, it verifies the code
77-
against what is saved in the database and either sends them back to the previous form to try again
78-
or redirects the user to the page that a `login` task would have redirected them to anyway.
72+
and provides feedback. In the `Email2FA` class, it verifies the code against what is saved in the
73+
database and either sends them back to the previous form to try again or redirects the user to the
74+
page that a `login` task would have redirected them to anyway.
7975

80-
All methods should return either a RedirectResponse or string of a view, like through the `view()` method.
76+
All methods should return either a `RedirectResponse` or a view string (e.g. using the `view()` function).

docs/4 - authorization.md

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
# Authorization
22

3-
Shield provides a flexible role-based access control that allows users to belong to multiple groups at once.
3+
Authorization happens once a user has been identified through authentication. It is the process of
4+
determining what actions a user is allowed to do within your site.
5+
6+
Shield provides a flexible role-based access control (RBAC) that allows users to belong to multiple groups at once.
47
Groups can be thought of as traditional roles (admin, moderator, user, etc), but can also group people together
5-
around features, like Beta feature access, or used to provide discrete groups of users within a forum, etc.
8+
around features, like Beta feature access, or used to provide discrete groups of users within a forum, etc.
69

710
## Defining Available Groups
811

@@ -19,7 +22,7 @@ public $groups = [
1922
```
2023

2124
The key of the `$groups` array is the common term of the group. This is what you would call when referencing the
22-
group elsewhere, like checking if `$user->inGroup('superadmin')`. By default, the following groups are available:
25+
group elsewhere, like checking if `$user->inGroup('superadmin')`. By default, the following groups are available:
2326
`superadmin`, `admin`, `developer`, `user`, and `beta`.
2427

2528
### Default User Group
@@ -35,7 +38,7 @@ public $defaultGroup = 'users';
3538

3639
All permissions must be added to the `AuthGroups` config file, also. A permission is simply a string consisting of
3740
a scope and action, like `users.create`. The scope would be `users` and the action would be `create`. Each permission
38-
can have a description for display within UIs if needed.
41+
can have a description for display within UIs if needed.
3942

4043
```php
4144
public $permissions = [
@@ -53,15 +56,15 @@ public $permissions = [
5356

5457
In order to grant any permissions to a group, they must have the permission assigned to the group, within the `AuthGroups`
5558
config file, under the `$matrix` property. The matrix is an associative array with the group name as the key,
56-
and array of permissions that should be applied to that group.
59+
and an array of permissions that should be applied to that group.
5760

5861
```php
5962
public $matrix = [
6063
'admin' => ['admin.access', 'users.create', 'users.edit', 'users.delete', 'beta.access'],
6164
];
6265
```
6366

64-
You can use a wildcard within a scope to allow all actions within that scope, by using a '*' in place of the action.
67+
You can use a wildcard within a scope to allow all actions within that scope, by using a `*` in place of the action.
6568

6669
```php
6770
public $matrix = [
@@ -71,17 +74,17 @@ public $matrix = [
7174

7275
## Authorizing Users
7376

74-
When the `Authorization` trait is applied to the user model, it provides the following methods to authorize your users.
77+
The `Authorizable` trait on the `User` entity provides the following methods to authorize your users.
7578

7679
#### can()
7780

78-
Allows you to check if a user is permitted to do a specific action. The only argument is the permission string. Returns
81+
Allows you to check if a user is permitted to do a specific action. The only argument is the permission string. Returns
7982
boolean `true`/`false`. Will check the user's direct permissions first, and then check against all of the user's groups
8083
permissions to determine if they are allowed.
8184

8285
```php
8386
if ($user->can('users.create')) {
84-
//
87+
//
8588
}
8689
```
8790

@@ -97,15 +100,15 @@ if (! $user->inGroup('superadmin', 'admin')) {
97100

98101
## Managing User Permissions
99102

100-
Permissions can be granted on a user level as well as on a group level. Any user-level permissions granted will
103+
Permissions can be granted on a user level as well as on a group level. Any user-level permissions granted will
101104
override the group, so it's possible that a user can perform an action that their groups cannot.
102105

103106
None of the changes are saved on the User entity until you `save()` with the `UserModel`.
104107

105108
#### addPermission()
106109

107-
Adds one or more permissions to the user. If a permission doesn't exist, a `CodeIgniter\Shield\Authorization\AuthorizationException`
108-
is thrown.
110+
Adds one or more permissions to the user. If a permission doesn't exist, a `CodeIgniter\Shield\Authorization\AuthorizationException`
111+
is thrown.
109112

110113
```php
111114
$user->addPermission('users.create', 'users.edit');
@@ -114,7 +117,7 @@ $user->addPermission('users.create', 'users.edit');
114117
#### removePermission()
115118

116119
Removes one or more permissions from a user. If a permission doesn't exist, a `CodeIgniter\Shield\Authorization\AuthorizationException`
117-
is thrown.
120+
is thrown.
118121

119122
```php
120123
$user->removePermission('users.delete');
@@ -134,7 +137,7 @@ $user->syncPermissions(['admin.access', 'beta.access']);
134137
#### addGroup()
135138

136139
Adds one or more groups to a user. If a group doesn't exist, a `CodeIgniter\Shield\Authorization\AuthorizationException`
137-
is thrown.
140+
is thrown.
138141

139142
```php
140143
$user->addGroup('admin', 'beta');

0 commit comments

Comments
 (0)