Skip to content

Commit d4b64ab

Browse files
authored
Merge pull request #8 from dotkernel/composer
updated intro, how to
2 parents 6e1d8d9 + fd31b43 commit d4b64ab

17 files changed

+450
-72
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 provides configuration to restrict access to certain actions based on the permissions defined in `authorization.global.php` so basically we have to add the permissions in the dot-rbac configuration file first to specify the action restriction permissions.
29+
30+
```php
31+
// configuration example to restrict certain actions of some routes based on the permissions specified in the dot-rbac configuration file
32+
'rules' => [
33+
[
34+
'route' => 'account',
35+
'actions' => [//list of actions to apply , or empty array for all actions
36+
'unregister',
37+
'avatar',
38+
'details',
39+
'changePassword'
40+
],
41+
'permissions' => ['authenticated']
42+
],
43+
[
44+
'route' => 'admin',
45+
'actions' => [
46+
'deleteAccount'
47+
],
48+
'permissions' => [
49+
'delete'
50+
//list of roles to allow
51+
]
52+
]
53+
]
54+
```
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 `data/doctrine/fixtures/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 `/data/doctrine/migrations/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+
```
File renamed without changes.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Dependency Injection
2+
3+
Dependency injection is a design pattern used in software development to implement inversion of control.
4+
In simpler terms, it's the act of providing dependencies for an object during instantiation.
5+
6+
In PHP, dependency injection can be implemented in various ways, including through constructor injection, setter injection and property injection.
7+
8+
Dotkernel Admin, through its [dot-dependency-injection](https://github.com/dotkernel/dot-dependency-injection) package focuses only on constructor injection.
9+
10+
## Usage
11+
12+
**Dotkernel Admin** comes out of the box with the [dot-dependency-injection](https://github.com/dotkernel/dot-dependency-injection) package, which provides all the functionality injecting dependencies into any object you want.
13+
14+
`dot-dependency-injection` determines the dependencies by looking at the `#[Inject]` attribute, added to the constructor of a class.
15+
Each dependency is specified as a separate parameter of the `#[Inject]` attribute.
16+
17+
For our example we will inject `AdminService` and `config` dependencies into a `AdminController`.
18+
19+
```php
20+
use Dot\DependencyInjection\Attribute\Inject;
21+
22+
class AdminController implements RequestHandlerInterface
23+
{
24+
#[Inject(
25+
AdminService::class,
26+
"config",
27+
)]
28+
public function __construct(
29+
protected AdminServiceInterface $adminService,
30+
protected array $config,
31+
) {
32+
}
33+
}
34+
```
35+
36+
> If your class needs the value of a specific configuration key, you can specify the path using dot notation: `config.example`
37+
38+
The next step is to register the class in the `ConfigProvider` under `factories` using
39+
`Dot\DependencyInjection\Factory\AttributedServiceFactory::class`.
40+
41+
```php
42+
public function getDependencies(): array
43+
{
44+
return [
45+
'factories' => [
46+
AdminController::class => AttributedServiceFactory::class
47+
]
48+
];
49+
}
50+
```
51+
52+
That's it.
53+
When your object is instantiated from the container, it will automatically have its dependencies resolved.
54+
55+
> Dependencies injection is available to any object within Dotkernel Admin.
56+
> For example, you can inject dependencies in a service, a controller and so on, simply by registering them in the `ConfigProvider`.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# NPM Commands
2+
3+
To install dependencies into the `node_modules` directory run this command.
4+
5+
```shell
6+
npm install
7+
```
8+
9+
> If `npm install` fails, this could be caused by user permissions of npm.
10+
> The recommended way to install npm is through `Node Version Manager`.
11+
12+
The watch command compiles the components then monitors the files for changes and recompiles them.
13+
14+
```shell
15+
npm run watch
16+
```
17+
18+
After all updates are done, this command compiles the assets locally, minifies them and makes them ready for production.
19+
20+
```shell
21+
npm run prod
22+
```

docs/book/v5/installation/composer.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Composer Installation of Packages
22

3-
Composer is required to install Dotkernel `admin`. You can install Composer from the [official site](https://getcomposer.org/).
3+
Composer is required to install Dotkernel Admin. You can install Composer from the [official site](https://getcomposer.org/).
44

55
> First make sure that you have navigated your command prompt to the folder where you copied the files in the previous step.
66

docs/book/v5/installation/configuration-files.md

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,31 @@
22

33
## Prepare config files
44

5-
* duplicate `config/autoload/local.php.dist` as `config/autoload/local.php`
5+
- duplicate `config/autoload/local.php.dist` as `config/autoload/local.php`
6+
- duplicate `config/autoload/mail.local.php.dist` as `config/autoload/mail.local.php`
67

7-
* duplicate `config/autoload/mail.local.php.dist` as `config/autoload/mail.local.php`
8+
> If you intend to send emails from your Frontend, make sure to fill in SMTP connection params.
9+
> This will be covered in the next section.
810
9-
### Note
11+
> **optional**: in order to run/create tests, duplicate `config/autoload/local.test.php.dist` as `config/autoload/local.test.php`
12+
> this creates a new in-memory database that your tests will run on.
1013
11-
> if your Admin will send emails, make sure to fill in SMTP connection params
14+
## Mail
1215

13-
* **optional**: in order to run/create tests, duplicate `config/autoload/local.test.php.dist` as `config/autoload/local.test.php`
16+
If you want your application to send mail, add valid credentials to the following keys in `config/autoload/mail.local.php`
1417

15-
### Note
18+
Under `message_options` key:
1619

17-
> this creates a new in-memory database that your tests will run on.
20+
- `from` - email address that will send emails (required)
21+
- `from_name` - organization name for signing sent emails (optional)
22+
23+
Under `smtp_options` key:
24+
25+
- `host` - hostname or IP address of the mail server (required)
26+
- `connection_config` - add the `username` and `password` keys (required)
27+
28+
In `config/autoload/local.php` edit the key `contact` => `message_receivers` => `to` with *string* values for emails that should receive contact messages.
29+
30+
> **Please add at least 1 email address in order for contact message to reach someone**
31+
32+
Also feel free to add as many CCs as you require under the `contact` => `message_receivers` => `cc` key.

docs/book/v5/installation/doctrine-orm.md

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,85 @@
11
# Doctrine ORM
22

3+
This step saves the database connection credentials in an Admin configuration file.
4+
We do not cover the creation steps of the database itself.
5+
36
## Setup database
47

5-
Make sure you fill out the database credentials in `config/autoload/local.php` under `$databases['default']`.
8+
Create a new MySQL database and set its collation to `utf8mb4_general_ci`.
69

7-
Create a new MySQL database - set collation to `utf8mb4_general_ci`
10+
Make sure you fill out the database credentials in `config/autoload/local.php` under `$databases['default']`.
11+
Below is the item you need to focus on.
12+
13+
> `my_database`, `my_user`, `my_password` are provided only as an example.
14+
15+
```php
16+
$databases = [
17+
'default' => [
18+
'host' => 'localhost',
19+
'dbname' => 'my_database',
20+
'user' => 'my_user',
21+
'password' => 'my_password',
22+
'port' => 3306,
23+
'driver' => 'pdo_mysql',
24+
'charset' => 'utf8mb4',
25+
'collate' => 'utf8mb4_general_ci',
26+
],
27+
// you can add more database connections into this array
28+
];
29+
```
830

931
## Running migrations
1032

1133
Run the database migrations by using the following command:
1234

1335
```shell
14-
php bin/doctrine-migrations migrate
36+
php vendor/bin/doctrine-migrations migrate
1537
```
1638

17-
This command will prompt you to confirm that you want to run it.
39+
Note: If you have already run the migrations, you may get this message.
40+
You should double-check to make sure the new migrations are ok to run.
1841

19-
> WARNING! You are about to execute a migration in database "..." that could result in schema changes and data loss. Are you sure you wish to continue? (yes/no) [yes]:
42+
```shell
43+
WARNING! You have x previously executed migrations in the database that are not registered migrations.
44+
{migration list}
45+
Are you sure you wish to continue? (y/n)
46+
```
2047

21-
Hit `Enter` to confirm the operation.
48+
When using an empty database, you will get this confirmation message instead.
2249

23-
## Executing fixtures
50+
```shell
51+
WARNING! You are about to execute a database migration that could result in schema changes and data loss. Are you sure you wish to continue? (y/n)
52+
```
2453

25-
**Fixtures are used to seed the database with initial values and should be executed after migrating the database.**
54+
Again, submit `y` to run all the migrations in chronological order.
55+
Each migration will be logged in the `migrations` table to prevent running the same migration more than once, which is often not desirable.
2656

27-
To list all the fixtures, run:
57+
If everything ran correctly, you will get this confirmation.
2858

2959
```shell
30-
php bin/doctrine fixtures:list
60+
[OK] Successfully migrated to version: Admin\Migrations\Version20240627134952
3161
```
3262

33-
This will output all the fixtures in the order of execution.
63+
> The migration name `Version20240627134952` may differ in future Admin updates.
3464
35-
To execute all fixtures, run:
65+
## Fixtures
66+
67+
Run this command to populate the admin tables with the default values:
3668

3769
```shell
3870
php bin/doctrine fixtures:execute
3971
```
4072

41-
To execute a specific fixture, run:
73+
You should see our galloping horse in the command line.
4274

4375
```shell
44-
php bin/doctrine fixtures:execute --class=FixtureClassName
76+
Executing Admin\Fixtures\AdminRoleLoader
77+
Executing Admin\Fixtures\AdminLoader
78+
Fixtures have been loaded.
79+
.''
80+
._.-.___.' (`\
81+
//( ( `'
82+
'/ )\ ).__. )
83+
' <' `\ ._/'\
84+
` \ \
4585
```
46-
47-
More details on how fixtures work can be found here: https://github.com/dotkernel/dot-data-fixtures#creating-fixtures

docs/book/v5/installation/getting-started.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,24 @@
55
> If you are using Windows as OS on your machine, you can use WSL2 as development environment.
66
> Read more here: [PHP-Mariadb-on-WLS2](https://www.dotkernel.com/php-development/almalinux-9-in-wsl2-install-php-apache-mariadb-composer-phpmyadmin/)
77
8-
Using your terminal, navigate inside the directory you want to download the project files into. Make sure that the
9-
directory is empty before proceeding to the download process. Once there, run the following command:
8+
Using your terminal, navigate inside the directory you want to download the project files into.
9+
Make sure that the directory is empty before proceeding to the download process.
10+
Once there, run the following command:
1011

1112
```shell
1213
git clone https://github.com/dotkernel/admin.git .
1314
```
15+
16+
If everything ran correctly, you can expect to see an output like this, though the numbers may differ.
17+
18+
```shell
19+
Cloning into '.'...
20+
remote: Enumerating objects: 6538, done.
21+
remote: Counting objects: 100% (1652/1652), done.
22+
remote: Compressing objects: 100% (785/785), done.
23+
remote: Total 6538 (delta 804), reused 1417 (delta 748), pack-reused 4886 (from 1)
24+
Receiving objects: 100% (6538/6538), 11.84 MiB | 16.52 MiB/s, done.
25+
Resolving deltas: 100% (3359/3359), done.
26+
```
27+
28+
You can already open the project in your preferred IDE to double-check the files were copied correctly.

0 commit comments

Comments
 (0)