Skip to content

Commit

Permalink
Add security pack
Browse files Browse the repository at this point in the history
  • Loading branch information
loic425 committed Jan 13, 2025
1 parent f5149d7 commit 5e8de99
Show file tree
Hide file tree
Showing 38 changed files with 1,090 additions and 0 deletions.
61 changes: 61 additions & 0 deletions .github/workflows/security_pack.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: Security Pack

on:
push:
branches-ignore:
- main
release:
types: [ created ]
schedule:
-
cron: "0 1 * * 6" # Run at 1am every Saturday
workflow_dispatch:

permissions:
actions: write
contents: write
pull-requests: write

jobs:
cqrs-pack:
runs-on: ubuntu-latest

name: "Tests (PHP ${{ matrix.php }})"

strategy:
fail-fast: false
matrix:
php: ["8.3"]

steps:
- uses: actions/checkout@v4

- name: "Setup PHP"
uses: shivammathur/setup-php@v2
with:
php-version: "${{ matrix.php }}"
ini-values: date.timezone=Europe/Warsaw
extensions: intl, gd, mysql, pdo_mysql, :xdebug
tools: symfony
coverage: none

- name: "Get Composer cache directory"
id: composer-cache
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT

- name: "Setup cache"
uses: actions/cache@v3
with:
path: |
${{ steps.composer-cache.outputs.dir }}
key: ${{ github.run_id }}-${{ runner.os }}-${{ hashFiles('composer.json') }}-symfony-${{ matrix.symfony }}

- name: "Create test application"
run: |
git config --global user.email "[email protected]"
git config --global user.name "Your Name"
(cd tests/Security && make create-test-application)
id: end-of-setup

- name: "Lint container"
run: (cd tests/Security/app && bin/console lint:container)
5 changes: 5 additions & 0 deletions monofony/security-application-meta/0.1/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"copy-from-recipe": {
"src/": "%SRC_DIR%/"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace App\Security\Application\AdminUser\Command;

use App\Security\Domain\ValueObject\User\AdminUserEmail;
use App\Security\Domain\ValueObject\User\AdminUserPassword;
use App\Shared\Application\Command\CommandInterface;

final class CreateAdminUserCommand implements CommandInterface
{
public function __construct(
public AdminUserEmail $email,
public AdminUserPassword $password,
) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace App\Security\Application\AdminUser\Command;

use App\Security\Domain\Model\User\AdminUser;
use App\Security\Domain\Repository\AdminUserRepositoryInterface;
use App\Security\Infrastructure\Identity\AdminUserIdGenerator;
use App\Shared\Application\Command\CommandHandlerInterface;

final readonly class CreateAdminUserCommandHandler implements CommandHandlerInterface
{
public function __construct(
private AdminUserIdGenerator $generator,
private AdminUserRepositoryInterface $adminUserRepository,
) {
}

public function __invoke(CreateAdminUserCommand $command): AdminUser
{
$adminUser = new AdminUser(
$this->generator->nextIdentity(),
$command->email,
$command->password,
);

$this->adminUserRepository->save($adminUser);

return $adminUser;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace App\Security\Application\AdminUser\Command;

use App\Shared\Application\Command\CommandInterface;

final class DeleteAdminUserCommand implements CommandInterface
{
public function __construct(
public string $id,
) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace App\Security\Application\AdminUser\Command;

use App\Security\Domain\Repository\AdminUserRepositoryInterface;
use App\Shared\Application\Command\CommandHandlerInterface;

final class DeleteAdminUserCommandHandler implements CommandHandlerInterface
{
public function __construct(
private AdminUserRepositoryInterface $adminUserRepository
) {
}

public function __invoke(DeleteAdminUserCommand $command): void
{
if (null === $boardGame = $this->adminUserRepository->ofId($command->id)) {
return;
}

$this->adminUserRepository->remove($boardGame);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace App\Security\Application\AdminUser\Command;

use App\Security\Domain\ValueObject\User\AdminUserEmail;
use App\Security\Domain\ValueObject\User\AdminUserPassword;
use App\Shared\Application\Command\CommandInterface;

final readonly class UpdateAdminUserCommand implements CommandInterface
{
public function __construct(
public string $id,
public AdminUserEmail $email,
public AdminUserPassword|null $password,
) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace App\Security\Application\AdminUser\Command;

use App\Security\Domain\Model\User\AdminUser;
use App\Security\Domain\Repository\AdminUserRepositoryInterface;
use App\Security\Domain\ValueObject\User\AdminUserId;
use App\Shared\Application\Command\CommandHandlerInterface;

final readonly class UpdateAdminUserCommandHandler implements CommandHandlerInterface
{
public function __construct(
private AdminUserRepositoryInterface $adminUserRepository,
) {
}

public function __invoke(UpdateAdminUserCommand $command): AdminUser
{
$adminUser = new AdminUser(
new AdminUserId($command->id),
$command->email,
$command->password,
);

$this->adminUserRepository->save($adminUser);

return $adminUser;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace App\Security\Application\AdminUser\Query;

use App\Shared\Application\Query\QueryInterface;

class FindAdminUserQuery implements QueryInterface
{
public function __construct(
public string $id,
) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace App\Security\Application\AdminUser\Query;

use App\Security\Domain\Model\User\AdminUser;
use App\Security\Domain\Repository\AdminUserRepositoryInterface;
use App\Shared\Application\Query\QueryHandlerInterface;

final class FindAdminUserQueryHandler implements QueryHandlerInterface
{
public function __construct(
private AdminUserRepositoryInterface $repository
) {
}

public function __invoke(FindAdminUserQuery $query): AdminUser|null
{
return $this->repository->ofId($query->id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace App\Security\Application\AdminUser\Query;

use App\Shared\Application\Query\QueryInterface;

final readonly class FindAdminUsersQuery implements QueryInterface
{
public function __construct(
public int|null $page = null,
public int|null $itemsPerPage = null,
public bool|null $emailSortingAsc = null,
public bool|null $emailSortingDesc = null,
public string|null $searchQuery = null,
) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace App\Security\Application\AdminUser\Query;

use App\Security\Domain\Repository\AdminUserRepositoryInterface;
use App\Shared\Application\Query\QueryHandlerInterface;

final readonly class FindAdminUsersQueryHandler implements QueryHandlerInterface
{
public function __construct(
private AdminUserRepositoryInterface $adminUserRepository,
) {
}

public function __invoke(FindAdminUsersQuery $query): AdminUserRepositoryInterface
{
$adminUserRepository = $this->adminUserRepository;

if (null !== $query->page && null !== $query->itemsPerPage) {
$adminUserRepository = $adminUserRepository->withPagination($query->page, $query->itemsPerPage);
}

if ($query->emailSortingAsc) {
$adminUserRepository = $adminUserRepository->withAscendingEmailSorting();
}

if ($query->emailSortingDesc) {
$adminUserRepository = $adminUserRepository->withDescendingEmailSorting();
}

if (null !== $query->searchQuery) {
$adminUserRepository = $adminUserRepository->withSearchQuery($query->searchQuery);
}

return $adminUserRepository;
}
}
5 changes: 5 additions & 0 deletions monofony/security-domain-meta/0.1/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"copy-from-recipe": {
"src/": "%SRC_DIR%/"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace App\Security\Domain\Model\User;

use App\Security\Domain\ValueObject\User\AdminUserEmail;
use App\Security\Domain\ValueObject\User\AdminUserId;
use App\Security\Domain\ValueObject\User\AdminUserPassword;

final readonly class AdminUser
{
public function __construct(
public AdminUserId $id,
public AdminUserEmail $email,
public AdminUserPassword|null $password = null,
) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace App\Security\Domain\Repository;

use App\Security\Domain\Model\User\AdminUser;
use App\Shared\Domain\Repository\RepositoryInterface;

/**
* @extends RepositoryInterface<AdminUser>
*/
interface AdminUserRepositoryInterface extends RepositoryInterface
{
public function save(AdminUser $adminUser): void;

public function remove(AdminUser $adminUser): void;

public function ofId(string $id): AdminUser|null;

public function withAscendingEmailSorting(): static;

public function withDescendingEmailSorting(): static;

public function withSearchQuery(string $query): static;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace App\Security\Domain\ValueObject\User;

final readonly class AdminUserEmail
{
public function __construct(
public string $value,
) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace App\Security\Domain\ValueObject\User;

final readonly class AdminUserId
{
public function __construct(
public string $value,
) {
}
}
Loading

0 comments on commit 5e8de99

Please sign in to comment.