Skip to content

Commit ffc3312

Browse files
authored
Merge pull request #28 from dotkernel/issue-27
Issue #27: Dotkernel Admin v7 documentation
2 parents fe16aa4 + 52a5d0f commit ffc3312

27 files changed

+1899
-11
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Dotkernel Admin Documentation

docs/book/index.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

docs/book/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../README.md

docs/book/v5/introduction/file-structure.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ Other classes the `src` folder may include are `InputFilter`, `EventListener`, `
108108
The `src` folder in each Module folder normally also contains these files:
109109

110110
* `ConfigProvider.php` - Configuration data for the module
111-
* `OpenAPI.php` - Detailed descriptions for each endpoint in the OpenAPI format
112111
* `RoutesDelegator.php` - Module specific route registrations
113112

114113
### `templates` folder for modules

docs/book/v6/introduction/file-structure.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ Other classes the `src` folder may include are `Adapter`, `Factory`, `Form`, `De
110110
The `src` folder in each Module folder normally also contains these files:
111111

112112
* `ConfigProvider.php` - Configuration data for the module
113-
* `OpenAPI.php` - Detailed descriptions for each endpoint in the OpenAPI format
114113
* `RoutesDelegator.php` - Module specific route registrations
115114

116115
### `templates` folder for modules

docs/book/v6/tutorials/create-book-module-via-dot-maker.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Implementing a book module in Dotkernel Admin using DotMaker
22

33
The `dotkernel/dot-maker` library can be used to programmatically generate project files and directories.
4-
It can be added to your API installation by following the [official documentation](https://docs.dotkernel.org/dot-maker/).
4+
It can be added to your Dotkernel Admin installation by following the [official documentation](https://docs.dotkernel.org/dot-maker/).
55

66
## Folder and files structure
77

@@ -72,10 +72,6 @@ you can have multiple components such as event listeners, wrappers, etc.
7272
* `src/Core/src/Book/src/Repository/BookRepository.php` – a repository is a class responsible for querying and retrieving entities from the database
7373
* `src/Core/src/Book/src/ConfigProvider.php` – is a class that provides configuration for Doctrine ORM
7474

75-
> Note that while this tutorial covers a standalone case, the `Core` module generated by default has the same structure as the one described in the
76-
> [Dotkernel API "Book" module](https://docs.dotkernel.org/api-documentation/v6/tutorials/create-book-module-via-dot-maker/)
77-
> allowing use as part of the [Dotkernel Headless Platform](https://docs.dotkernel.org/headless-documentation/)
78-
7975
## File creation and contents
8076

8177
After successfully installing `dot-maker`, it can be used to generate the Book module.
@@ -90,7 +86,7 @@ Type `book` when prompted to enter the module name.
9086

9187
Next you will be prompted to add the relevant components of a module, accepting `y(es)`, `n(o)` and `Enter` (defaults to `yes`):
9288

93-
> Note that `dot-maker` will automatically split the files into the described `Api` and `Core` structure without a further input needed.
89+
> Note that `dot-maker` will automatically split the files into the described `Admin` and `Core` structure without a further input needed.
9490
9591
* `Entity and repository` (Y): will generate the `Book.php` entity and the associated `BookRepository.php`.
9692
* `Service` and `service interface` (Y): will generate the `BookService` and the `BookServiceInterface`.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Authorization Guards
2+
3+
The packages responsible for restricting access to certain parts of the application are [dot-rbac-guard](https://github.com/dotkernel/dot-rbac-guard) and [dot-rbac](https://github.com/dotkernel/dot-rbac).
4+
These packages work together to create an infrastructure that is customizable and diversified to manage user access to the platform by specifying the type of role the user has.
5+
6+
The `authorization.global.php` file provides multiple configurations specifying multiple roles as well as the types of permissions to which these roles have access.
7+
8+
```php
9+
//example of a flat RBAC model that specifies two types of roles as well as their permission
10+
'roles' => [
11+
'admin' => [
12+
'permissions' => [
13+
'authenticated',
14+
'edit',
15+
'delete',
16+
//etc..
17+
]
18+
],
19+
'user' => [
20+
'permissions' => [
21+
'authenticated',
22+
//etc..
23+
]
24+
]
25+
]
26+
```
27+
28+
The `authorization-guards.global.php` file defines which permissions are required to access specific route handlers.
29+
These permissions must first be declared in the `authorization.global.php` (dot-rbac) configuration file.
30+
31+
```php
32+
// Example configuration granting access to route handlers based on permissions.
33+
'rules' => [
34+
'admin::admin-login-form' => [],
35+
'admin::admin-login' => [],
36+
'admin::admin-create-form' => ['authenticated'],
37+
'admin::admin-create' => ['authenticated'],
38+
'admin::admin-delete-form' => ['authenticated'],
39+
'admin::admin-delete' => ['authenticated'],
40+
'admin::admin-edit-form' => ['authenticated'],
41+
'admin::admin-edit' => ['authenticated'],
42+
]
43+
```
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Fixtures
2+
3+
> Fixtures are used to seed the database with initial values and should only be executed ONCE each, after migrating the database.
4+
5+
Seeding the database is done with the help of our custom package `dotkernel/dot-data-fixtures` built on top of `doctrine/data-fixtures`.
6+
See below on how to use our CLI command for listing and executing Doctrine data fixtures.
7+
8+
## Working with fixtures
9+
10+
You can find an example of a fixtures class in `src/Core/src/App/src/Fixture/AdminLoader.php`.
11+
12+
To list all the available fixtures by order of execution, run:
13+
14+
```shell
15+
php ./bin/doctrine fixtures:list
16+
```
17+
18+
To execute all fixtures, run:
19+
20+
```shell
21+
php ./bin/doctrine fixtures:execute
22+
```
23+
24+
To execute a specific fixture, use its class name, like in this example:
25+
26+
```shell
27+
php ./bin/doctrine fixtures:execute --class=AdminLoader
28+
```
29+
30+
Fixtures can and should be ordered to ensure database consistency.
31+
More on ordering fixtures can be found here:
32+
https://www.doctrine-project.org/projects/doctrine-data-fixtures/en/latest/how-to/fixture-ordering.html#fixture-ordering
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Creating migrations
2+
3+
Migrations are used to create and/or edit the database structure.
4+
To generate a new migration file, use this command:
5+
6+
```shell
7+
php ./vendor/bin/doctrine-migrations migrations:generate
8+
```
9+
10+
It creates a PHP file like this one `src/Core/src/App/src/Migration/Version20240627134952.php` that can then be edited in the IDE.
11+
You can add new queries in:
12+
13+
- `public function up` - these are executed when the migration is run.
14+
- `public function down` - these are optional queries that undo the above changes.
15+
16+
## Example
17+
18+
This example creates a new column named `test`.
19+
Add this in `public function up`:
20+
21+
```shell
22+
$this->addSql('ALTER TABLE admin ADD test VARCHAR(255) NOT NULL');
23+
```
24+
25+
And its opposite in `public function down`:
26+
27+
```shell
28+
$this->addSql('ALTER TABLE admin DROP test');
29+
```

docs/book/v7/how-to/csrf.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# CSRF protection in forms
2+
3+
A Cross-Site Request Forgery (CSRF) attack is a type of security vulnerability that tricks a user into performing actions on a web application in which they are authenticated, without their knowledge or consent.
4+
5+
Web applications can protect users against these types of attacks by implementing CSRF tokens in their forms, which are known only to the application that generated them and must be included when submitting forms.
6+
With each visit, a new CSRF token is added to the form, so tokens are not reusable between forms.
7+
Missing to provide a valid CSRF token will result in a form validation error.
8+
9+
## Implement CSRF protection
10+
11+
Implementing CSRF protection requires three steps:
12+
13+
- create a new field using [laminas/laminas-form](https://github.com/laminas/laminas-form)'s [CSRF](https://github.com/laminas/laminas-form/blob/3.21.x/src/Element/Csrf.php) element
14+
- validate new field using [laminas/laminas-session](https://github.com/laminas/laminas-session)'s [CSRF](https://github.com/laminas/laminas-session/blob/2.22.x/src/Validator/Csrf.php) validator
15+
- render field using [laminas/laminas-form](https://github.com/laminas/laminas-form)'s [FormElement](https://github.com/laminas/laminas-form/blob/3.21.x/src/View/Helper/FormElement.php) helper
16+
17+
### Create field
18+
19+
Open the form's PHP class and append the following code to the method that initializes the fields (usually `init`):
20+
21+
```php
22+
$this->add(
23+
(new \Laminas\Form\Element\Csrf('exampleCsrf'))
24+
->setOptions([
25+
'csrf_options' => ['timeout' => 3600, 'session' => new Container()],
26+
])
27+
->setAttribute('required', true)
28+
);
29+
```
30+
31+
where `exampleCsrf` should be a suggestive name that describes the purpose of the field (example: `forgotPasswordCsrf`).
32+
33+
### Validate field
34+
35+
Open the InputFilter that validates the form fields and append the following code to the method that initializes the fields (usually `init`):
36+
37+
```php
38+
$this->add(new \Admin\App\InputFilter\Input\CsrfInput('exampleCsrf'));
39+
```
40+
41+
where `exampleCsrf` must match the CSRF field's name in the form.
42+
43+
> Remember to modify both occurrences in this file.
44+
45+
> Make sure that you validate the form using its `isValid` method in the handler/controller where it is submitted.
46+
47+
### Render field
48+
49+
Open the template that renders your form and add the following code somewhere between the form's opening and closing tags:
50+
51+
```text
52+
{{ formElement(form.get('exampleCsrf')) }}
53+
```
54+
55+
## Test the implementation
56+
57+
Access your form from the browser and view its source. You should see a new hidden field, called `exampleCsrf` (or however you named it).
58+
After filling out the form, submitting it should work as before.
59+
60+
To make sure that the new CSRF field works as expected, you can inspect the form using your browser's `Developer tools` and modify its value in any way.
61+
Submitting a filled-out form should result in a validation error:
62+
63+
> This field is required and cannot be empty.
64+
65+
### Timeout
66+
67+
Note the `timeout` option in your PHP form's `exampleCsrf` field, with its default value set to **3600**.
68+
This represents the value in seconds for how long the token is valid.
69+
Submitting a form that has been rendered for longer than this value will result in a validation error:
70+
71+
> Invalid CSRF.
72+
73+
You can modify the value of `timeout` in each form, but the default value should work in most cases.

0 commit comments

Comments
 (0)