diff --git a/composer.json b/composer.json index 36997721e9dd987783a033143f5611c1ed575c57..58aa1b1947374e2e44eb64881d14e4ed174381c5 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ ], "require": { - "kaa/kaa": "0.0.2" + "kaa/kaa": "0.0.4" }, "require-dev": { "symplify/easy-coding-standard": "^12.0", diff --git a/composer.lock b/composer.lock index e0393bdc751cc761b4444f79821831e036e05abc..8edcf74cc60e2cdfbf59a3909694d7db8dbbd34a 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "0c7a4662da421eb5b2fcca1adee226b0", + "content-hash": "26e27756d0ca58efbf1da65e9865b77c", "packages": [ { "name": "haydenpierce/class-finder", @@ -50,11 +50,11 @@ }, { "name": "kaa/kaa", - "version": "0.0.2", + "version": "0.0.4", "source": { "type": "git", "url": "https://git.miem.hse.ru/kaa-framework/kaa", - "reference": "03b8dacc043a0eb962348fd8280f6c89036e0d0e" + "reference": "f66fa572e7a1f42ad39f61d1ee0384e36047e458" }, "require": { "haydenpierce/class-finder": "0.5.3", @@ -92,7 +92,7 @@ "MIT" ], "description": "Kaa - KPHP compatible Web Framework", - "time": "2024-01-23T22:08:25+00:00" + "time": "2024-02-01T09:54:22+00:00" }, { "name": "nette/php-generator", @@ -312,16 +312,16 @@ }, { "name": "symfony/config", - "version": "v6.4.0", + "version": "v6.4.3", "source": { "type": "git", "url": "https://github.com/symfony/config.git", - "reference": "5d33e0fb707d603330e0edfd4691803a1253572e" + "reference": "206482ff3ed450495b1d5b7bad1bc3a852def96f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/config/zipball/5d33e0fb707d603330e0edfd4691803a1253572e", - "reference": "5d33e0fb707d603330e0edfd4691803a1253572e", + "url": "https://api.github.com/repos/symfony/config/zipball/206482ff3ed450495b1d5b7bad1bc3a852def96f", + "reference": "206482ff3ed450495b1d5b7bad1bc3a852def96f", "shasum": "" }, "require": { @@ -367,7 +367,7 @@ "description": "Helps you find, load, combine, autofill and validate configuration values of any kind", "homepage": "https://symfony.com", "support": { - "source": "https://github.com/symfony/config/tree/v6.4.0" + "source": "https://github.com/symfony/config/tree/v6.4.3" }, "funding": [ { @@ -383,7 +383,7 @@ "type": "tidelift" } ], - "time": "2023-11-09T08:28:32+00:00" + "time": "2024-01-29T13:26:27+00:00" }, { "name": "symfony/deprecation-contracts", @@ -454,16 +454,16 @@ }, { "name": "symfony/filesystem", - "version": "v7.0.0", + "version": "v7.0.3", "source": { "type": "git", "url": "https://github.com/symfony/filesystem.git", - "reference": "7da8ea2362a283771478c5f7729cfcb43a76b8b7" + "reference": "2890e3a825bc0c0558526c04499c13f83e1b6b12" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/filesystem/zipball/7da8ea2362a283771478c5f7729cfcb43a76b8b7", - "reference": "7da8ea2362a283771478c5f7729cfcb43a76b8b7", + "url": "https://api.github.com/repos/symfony/filesystem/zipball/2890e3a825bc0c0558526c04499c13f83e1b6b12", + "reference": "2890e3a825bc0c0558526c04499c13f83e1b6b12", "shasum": "" }, "require": { @@ -497,7 +497,7 @@ "description": "Provides basic utilities for the filesystem", "homepage": "https://symfony.com", "support": { - "source": "https://github.com/symfony/filesystem/tree/v7.0.0" + "source": "https://github.com/symfony/filesystem/tree/v7.0.3" }, "funding": [ { @@ -513,7 +513,7 @@ "type": "tidelift" } ], - "time": "2023-07-27T06:33:22+00:00" + "time": "2024-01-23T15:02:46+00:00" }, { "name": "symfony/finder", @@ -829,16 +829,16 @@ }, { "name": "symfony/yaml", - "version": "v7.0.0", + "version": "v7.0.3", "source": { "type": "git", "url": "https://github.com/symfony/yaml.git", - "reference": "0055b230c408428b9b5cde7c55659555be5c0278" + "reference": "2d4fca631c00700597e9442a0b2451ce234513d3" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/yaml/zipball/0055b230c408428b9b5cde7c55659555be5c0278", - "reference": "0055b230c408428b9b5cde7c55659555be5c0278", + "url": "https://api.github.com/repos/symfony/yaml/zipball/2d4fca631c00700597e9442a0b2451ce234513d3", + "reference": "2d4fca631c00700597e9442a0b2451ce234513d3", "shasum": "" }, "require": { @@ -880,7 +880,7 @@ "description": "Loads and dumps YAML files", "homepage": "https://symfony.com", "support": { - "source": "https://github.com/symfony/yaml/tree/v7.0.0" + "source": "https://github.com/symfony/yaml/tree/v7.0.3" }, "funding": [ { @@ -896,7 +896,7 @@ "type": "tidelift" } ], - "time": "2023-11-07T10:26:03+00:00" + "time": "2024-01-23T15:02:46+00:00" }, { "name": "twig/twig", @@ -1586,16 +1586,16 @@ }, { "name": "symfony/console", - "version": "v7.0.2", + "version": "v7.0.3", "source": { "type": "git", "url": "https://github.com/symfony/console.git", - "reference": "f8587c4cdc5acad67af71c37db34ef03af91e59c" + "reference": "c5010d50f1ee4b25cfa0201d9915cf1b14071456" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/console/zipball/f8587c4cdc5acad67af71c37db34ef03af91e59c", - "reference": "f8587c4cdc5acad67af71c37db34ef03af91e59c", + "url": "https://api.github.com/repos/symfony/console/zipball/c5010d50f1ee4b25cfa0201d9915cf1b14071456", + "reference": "c5010d50f1ee4b25cfa0201d9915cf1b14071456", "shasum": "" }, "require": { @@ -1659,7 +1659,7 @@ "terminal" ], "support": { - "source": "https://github.com/symfony/console/tree/v7.0.2" + "source": "https://github.com/symfony/console/tree/v7.0.3" }, "funding": [ { @@ -1675,20 +1675,20 @@ "type": "tidelift" } ], - "time": "2023-12-10T16:54:46+00:00" + "time": "2024-01-23T15:02:46+00:00" }, { "name": "symfony/event-dispatcher", - "version": "v7.0.2", + "version": "v7.0.3", "source": { "type": "git", "url": "https://github.com/symfony/event-dispatcher.git", - "reference": "098b62ae81fdd6cbf941f355059f617db28f4f9a" + "reference": "834c28d533dd0636f910909d01b9ff45cc094b5e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/event-dispatcher/zipball/098b62ae81fdd6cbf941f355059f617db28f4f9a", - "reference": "098b62ae81fdd6cbf941f355059f617db28f4f9a", + "url": "https://api.github.com/repos/symfony/event-dispatcher/zipball/834c28d533dd0636f910909d01b9ff45cc094b5e", + "reference": "834c28d533dd0636f910909d01b9ff45cc094b5e", "shasum": "" }, "require": { @@ -1739,7 +1739,7 @@ "description": "Provides tools that allow your application components to communicate with each other by dispatching events and listening to them", "homepage": "https://symfony.com", "support": { - "source": "https://github.com/symfony/event-dispatcher/tree/v7.0.2" + "source": "https://github.com/symfony/event-dispatcher/tree/v7.0.3" }, "funding": [ { @@ -1755,7 +1755,7 @@ "type": "tidelift" } ], - "time": "2023-12-27T22:24:19+00:00" + "time": "2024-01-23T15:02:46+00:00" }, { "name": "symfony/event-dispatcher-contracts", @@ -2146,16 +2146,16 @@ }, { "name": "symfony/process", - "version": "v7.0.2", + "version": "v7.0.3", "source": { "type": "git", "url": "https://github.com/symfony/process.git", - "reference": "acd3eb5cb02382c1cb0287ba29b2908cc6ffa83a" + "reference": "937a195147e0c27b2759ade834169ed006d0bc74" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/process/zipball/acd3eb5cb02382c1cb0287ba29b2908cc6ffa83a", - "reference": "acd3eb5cb02382c1cb0287ba29b2908cc6ffa83a", + "url": "https://api.github.com/repos/symfony/process/zipball/937a195147e0c27b2759ade834169ed006d0bc74", + "reference": "937a195147e0c27b2759ade834169ed006d0bc74", "shasum": "" }, "require": { @@ -2187,7 +2187,7 @@ "description": "Executes commands in sub-processes", "homepage": "https://symfony.com", "support": { - "source": "https://github.com/symfony/process/tree/v7.0.2" + "source": "https://github.com/symfony/process/tree/v7.0.3" }, "funding": [ { @@ -2203,7 +2203,7 @@ "type": "tidelift" } ], - "time": "2023-12-24T09:15:37+00:00" + "time": "2024-01-23T15:02:46+00:00" }, { "name": "symfony/service-contracts", @@ -2289,16 +2289,16 @@ }, { "name": "symfony/stopwatch", - "version": "v7.0.0", + "version": "v7.0.3", "source": { "type": "git", "url": "https://github.com/symfony/stopwatch.git", - "reference": "7bbfa3dd564a0ce12eb4acaaa46823c740f9cb7a" + "reference": "983900d6fddf2b0cbaacacbbad07610854bd8112" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/stopwatch/zipball/7bbfa3dd564a0ce12eb4acaaa46823c740f9cb7a", - "reference": "7bbfa3dd564a0ce12eb4acaaa46823c740f9cb7a", + "url": "https://api.github.com/repos/symfony/stopwatch/zipball/983900d6fddf2b0cbaacacbbad07610854bd8112", + "reference": "983900d6fddf2b0cbaacacbbad07610854bd8112", "shasum": "" }, "require": { @@ -2331,7 +2331,7 @@ "description": "Provides a way to profile code", "homepage": "https://symfony.com", "support": { - "source": "https://github.com/symfony/stopwatch/tree/v7.0.0" + "source": "https://github.com/symfony/stopwatch/tree/v7.0.3" }, "funding": [ { @@ -2347,20 +2347,20 @@ "type": "tidelift" } ], - "time": "2023-07-05T13:06:06+00:00" + "time": "2024-01-23T15:02:46+00:00" }, { "name": "symfony/string", - "version": "v7.0.2", + "version": "v7.0.3", "source": { "type": "git", "url": "https://github.com/symfony/string.git", - "reference": "cc78f14f91f5e53b42044d0620961c48028ff9f5" + "reference": "524aac4a280b90a4420d8d6a040718d0586505ac" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/string/zipball/cc78f14f91f5e53b42044d0620961c48028ff9f5", - "reference": "cc78f14f91f5e53b42044d0620961c48028ff9f5", + "url": "https://api.github.com/repos/symfony/string/zipball/524aac4a280b90a4420d8d6a040718d0586505ac", + "reference": "524aac4a280b90a4420d8d6a040718d0586505ac", "shasum": "" }, "require": { @@ -2417,7 +2417,7 @@ "utf8" ], "support": { - "source": "https://github.com/symfony/string/tree/v7.0.2" + "source": "https://github.com/symfony/string/tree/v7.0.3" }, "funding": [ { @@ -2433,7 +2433,7 @@ "type": "tidelift" } ], - "time": "2023-12-10T16:54:46+00:00" + "time": "2024-01-29T15:41:16+00:00" }, { "name": "symplify/easy-coding-standard", diff --git a/config/security.yaml b/config/security.yaml index bf73d10870ab140d3baf263cc5d27c06f66dc0a0..dc3940057834af0a69776ce1b8bc766745f5eeb7 100644 --- a/config/security.yaml +++ b/config/security.yaml @@ -5,6 +5,10 @@ security: user_provider: '@App\Service\UserProvider' firewalls: + register: + path: '^/register' + authenticators: + - { service: App\Authenticator\RegisterAuthenticator } login: path: '^/login' authenticators: diff --git a/creating.sql b/creating.sql index 3144d5f6aba10e96b4a054cd1ee065a8bb893d26..49d0aab14ba061bfce7c919eef331df3709468e8 100644 --- a/creating.sql +++ b/creating.sql @@ -1,74 +1,88 @@ CREATE TABLE IF NOT EXISTS tag( - id INTEGER PRIMARY KEY AUTO_INCREMENT, - title VARCHAR(60) NOT NULL UNIQUE -); + id INTEGER PRIMARY KEY AUTO_INCREMENT, + title VARCHAR(60) NOT NULL UNIQUE + ); CREATE TABLE IF NOT EXISTS user( - id INTEGER PRIMARY KEY AUTO_INCREMENT, - username VARCHAR(120) NOT NULL UNIQUE , + id INTEGER PRIMARY KEY AUTO_INCREMENT, + username VARCHAR(120) NOT NULL UNIQUE , email VARCHAR(120) NOT NULL UNIQUE , - password VARCHAR(60) NOT NULL , - role VARCHAR(10), + password VARCHAR(120) NOT NULL , + roles VARCHAR(10), created DATETIME, changed DATETIME, - CONSTRAINT role_enum CHECK ( role IN ('USER', 'MODERATOR', 'ADMIN') ) -); + CONSTRAINT role_enum CHECK ( roles IN ('USER', 'MODERATOR', 'ADMIN') ) + ); CREATE TABLE IF NOT EXISTS topic( - id INTEGER PRIMARY KEY AUTO_INCREMENT, - author_id INTEGER, - title VARCHAR(365) NOT NULL , + id INTEGER PRIMARY KEY AUTO_INCREMENT, + author_id INTEGER, + title VARCHAR(365) NOT NULL , + type VARCHAR(8) NOT NULL , created DATETIME, CONSTRAINT FOREIGN KEY (author_id) - REFERENCES user(id) - ON DELETE no action -); + REFERENCES user(id) + ON DELETE no action, + CONSTRAINT type_check CHECK ( type IN ('PUBLIC', 'PRIVATE') ) + ); CREATE TABLE IF NOT EXISTS article( - id INTEGER PRIMARY KEY AUTO_INCREMENT, - author_id INTEGER , - topic_id INTEGER , - title VARCHAR(480) NOT NULL , + id INTEGER PRIMARY KEY AUTO_INCREMENT, + author_id INTEGER , + topic_id INTEGER , + title VARCHAR(480) NOT NULL , body VARCHAR(10000) , created DATETIME , changed DATETIME , CONSTRAINT FOREIGN KEY (author_id) - REFERENCES user(id) - ON DELETE no action , + REFERENCES user(id) + ON DELETE no action , CONSTRAINT FOREIGN KEY (topic_id) - REFERENCES topic(id) - ON DELETE set null -); + REFERENCES topic(id) + ON DELETE set null + ); CREATE TABLE IF NOT EXISTS comment( - id INTEGER PRIMARY KEY AUTO_INCREMENT, - author_id INTEGER, - article_id INTEGER, - body VARCHAR(1000), + id INTEGER PRIMARY KEY AUTO_INCREMENT, + author_id INTEGER, + article_id INTEGER, + body VARCHAR(1000), created DATETIME, changed DATETIME, CONSTRAINT FOREIGN KEY (author_id) - REFERENCES user(id) - ON DELETE no action , + REFERENCES user(id) + ON DELETE no action , CONSTRAINT FOREIGN KEY (article_id) - REFERENCES article(id) - ON DELETE CASCADE -); + REFERENCES article(id) + ON DELETE CASCADE + ); CREATE TABLE IF NOT EXISTS article_tag( - id INTEGER PRIMARY KEY AUTO_INCREMENT, - article_id INTEGER, - tag_id INTEGER, - CONSTRAINT FOREIGN KEY (article_id) - REFERENCES article(id) - ON DELETE CASCADE , + id INTEGER PRIMARY KEY AUTO_INCREMENT, + article_id INTEGER, + tag_id INTEGER, + CONSTRAINT FOREIGN KEY (article_id) + REFERENCES article(id) + ON DELETE CASCADE , CONSTRAINT FOREIGN KEY (tag_id) - REFERENCES tag(id) - ON DELETE CASCADE -); + REFERENCES tag(id) + ON DELETE CASCADE + ); + +CREATE TABLE IF NOT EXISTS admission( + id INTEGER PRIMARY KEY AUTO_INCREMENT, + topic_id INTEGER, + user_id INTEGER, + CONSTRAINT FOREIGN KEY (topic_id) + REFERENCES topic(id) + ON DELETE CASCADE , + CONSTRAINT FOREIGN KEY (user_id) + REFERENCES user(id) + ON DELETE CASCADE + ); -INSERT INTO user(username, email, password, role, created, changed) -VALUES ('test', 'test@gmail.com', '12345', 'USER', '2022-12-27 08:26:49.219717', '2022-12-27 08:26:49.219717'); +INSERT INTO user(username, email, password, roles, created, changed) +VALUES ('test', 'test@gmail.com', '5994471ABB01112AFCC18159F6CC74B4F511B99806DA59B3CAF5A9C173CACFC5', 'USER', '2022-12-27 08:26:49.219717', '2022-12-27 08:26:49.219717'); -INSERT INTO user(username, email, password, role, created, changed) -VALUES ('Admin', 'admin@gmail.com', '12345', 'ADMIN', '2022-12-27 08:26:49.219717', '2022-12-27 08:26:49.219717'); +INSERT INTO user(username, email, password, roles, created, changed) +VALUES ('Admin', 'admin@gmail.com', '5994471ABB01112AFCC18159F6CC74B4F511B99806DA59B3CAF5A9C173CACFC5', 'ADMIN', '2022-12-27 08:26:49.219717', '2022-12-27 08:26:49.219717'); diff --git a/demo.http b/demo.http index ea1b91864043303e855f062e95126f59cf45ba99..a93f25e0b785ce15be9796d7bd4c5f6eb9177b54 100644 --- a/demo.http +++ b/demo.http @@ -3,19 +3,17 @@ POST http://localhost:8800/tag Content-Type: application/json { - "id": 0, - "title": "KPHP" + "title": "KPHP2" } ### GET TAG GET http://localhost:8800/tag/1 ### PUT TAG -PUT http://localhost:8800/tag/0 +POST http://localhost:8800/tag/put/1 Content-Type: application/json { - "id": 0, "title": "Test" } @@ -28,12 +26,20 @@ Content-Type: application/json "password": "12345" } +### LOGIN +POST http://localhost:8800/login +Content-Type: application/json + +{ + "username": "test", + "password": "12345" +} + ### REGISTER POST http://localhost:8800/register Content-Type: application/json { - "id": 5, "username": "kphp", "email": "kphp@mail.com", "password": "12355" @@ -68,36 +74,40 @@ Content-Type: application/json ] } +### LOGIN MODER +POST http://localhost:8800/login +Content-Type: application/json + +{ + "username": "Moder", + "password": "qwerty" +} + + ### GET USER GET http://localhost:8800/admin/user/3 ### PUT USER -PUT http://localhost:8800/admin/user/3 +POST http://localhost:8800/put/admin/user/3 Content-Type: application/json { - "id": 3, "username": "Moderator", "email": "moder@mail.com", "password": "qwerty", - "created": "2024-01-22 21:30:00", - "changed": "2024-01-22 21:30:00", "roles": [ "MODERATOR" ] } ### PUT CURRENT USER -PUT http://localhost:8800/user/me +PUT http://localhost:8800/put/user/me Content-Type: application/json { - "id": 1, - "username": "test", - "email": "test@mail.com", + "username": "Admin90", + "email": "testhhaodmin@mail.com", "password": "qwerty", - "created": "2024-01-22 21:30:00", - "changed": "2024-01-22 21:30:00", "roles": [ "USER" ] @@ -108,24 +118,29 @@ POST http://localhost:8800/moderator/topic Content-Type: application/json { - "id": 0, "authorId": 2, "title": "KPHP - not cool!", + "type": "PRIVATE", + "acceptedUsers": [ + 1 + ], "created": "2024-01-22 21:30:00" } ### GET TOPIC -GET http://localhost:8800/topic/1 +GET http://localhost:8800/topic/2 ### PUT TUPIC -PUT http://localhost:8800/moderator/topic/2 +POST http://localhost:8800/put/moderator/topic/2 Content-Type: application/json { - "id": 1, "authorId": 2, "title": "KPHP is very cool!", - "created": "2024-01-22 21:30:00" + "type": "PRIVATE", + "acceptedUsers": [ + 1 + ] } @@ -134,38 +149,30 @@ POST http://localhost:8800/article Content-Type: application/json { - "id": 2, "authorId": 2, - "topicId": 1, + "topicId": 2, "title": "KPHP", "body": "dfdjgkjdfgdfhgopudfhgokvbjncv[qeio[eruoergoierj", "tags": [ 1 - ], - "created": "2024-01-22 21:30:00", - "changed": "2024-01-22 21:30:00" - + ] } ### GET ARTICLE GET http://localhost:8800/article/1 ### CHANGE ARTICLE -PUT http://localhost:8800/article/3 +POST http://localhost:8800/article/put/1 Content-Type: application/json { - "id": 1, "authorId": 2, "topicId": 2, "title": "KPHP", "body": "I very like this framework and KPHP!!!", "tags": [ 1 - ], - "created": "2024-01-22 21:30:00", - "changed": "2024-01-22 21:30:00" - + ] } ### ADD COMMENT @@ -173,28 +180,22 @@ POST http://localhost:8800/comment Content-Type: application/json { - "id": 0, "authorId": 2, "articleId": 1, - "body": "Boring...", - "created": "2024-01-22 21:30:00", - "changed": "2024-01-22 21:30:00" + "body": "Boring..." } ### GET COMMENT GET http://localhost:8800/comment/1 ### UPDATE COMMENT -PUT http://localhost:8800/comment/1 +POST http://localhost:8800/comment/put/1 Content-Type: application/json { - "id": 1, "authorId": 2, "articleId": 1, - "body": "Very interesting!", - "created": "2024-01-22 21:30:00", - "changed": "2024-01-22 21:30:00" + "body": "Very interesting!" } diff --git a/fixer/ecs.php b/fixer/ecs.php index bf3b38912a997471964f19b5ba85c28509da4ce3..1beb95584969e63b226120ae3a38cac1e2b57e45 100755 --- a/fixer/ecs.php +++ b/fixer/ecs.php @@ -3,6 +3,8 @@ declare(strict_types=1); use PhpCsFixer\Fixer\ArrayNotation\ArraySyntaxFixer; +use PhpCsFixer\Fixer\Basic\NoTrailingCommaInSinglelineFixer; +use PhpCsFixer\Fixer\ControlStructure\TrailingCommaInMultilineFixer; use PhpCsFixer\Fixer\Import\NoUnusedImportsFixer; use Symplify\EasyCodingStandard\Config\ECSConfig; @@ -15,6 +17,10 @@ return ECSConfig::configure() // add a single rule ->withRules([ ArraySyntaxFixer::class, - NoUnusedImportsFixer::class + NoUnusedImportsFixer::class, + NoTrailingCommaInSinglelineFixer::class ]) - ->withPreparedSets(psr12: true); + ->withPreparedSets(psr12: true) + ->withSkip([ + TrailingCommaInMultilineFixer::class + ]); diff --git a/src/Authenticator/LoginAuthenticator.php b/src/Authenticator/LoginAuthenticator.php index 744364ac6a7b9c19bb7ec3424b1b6cf8254e95c0..184dde61823f03a139b6d8743221bd8a93fad5fc 100755 --- a/src/Authenticator/LoginAuthenticator.php +++ b/src/Authenticator/LoginAuthenticator.php @@ -15,7 +15,7 @@ use Kaa\Component\Security\AuthenticatorInterface; use Kaa\Component\Security\Exception\SessionException; use Kaa\Component\Security\Session\SessionService; use Random\RandomException; -use Symfony\Component\Filesystem\Exception\RuntimeException; +use RuntimeException; use Throwable; class LoginAuthenticator implements AuthenticatorInterface @@ -43,9 +43,9 @@ class LoginAuthenticator implements AuthenticatorInterface public function supports(Request $request): bool { $content = json_decode($request->getContent(), true); - return $request->getMethod() === 'POST' && - array_key_exists('username', $content) && - array_key_exists('password', $content); + return $request->getMethod() === 'POST' + && array_key_exists('username', $content) + && array_key_exists('password', $content); } /** @@ -55,7 +55,10 @@ class LoginAuthenticator implements AuthenticatorInterface { $data = json_decode($request->getContent(), true); $user = $this->entityManager->findOneBy(User::class, ['username' => $data['username']]); - if (($data['username'] === $user->getUsername()) && ($data['password'] === $user->getPassword())) { + if ( + ($data['username'] === $user->getUsername()) + && (strtoupper(hash('sha256', $data['password'])) === $user->getPassword()) + ) { return fn () => $this->userProvider->getUser($user->getIdentifier()); } throw new RuntimeException('Incorrect username or password'); @@ -77,6 +80,6 @@ class LoginAuthenticator implements AuthenticatorInterface public function onAuthenticationFailure(Request $request, Throwable $throwable): ?Response { - return new JsonResponse('Incorrect login or password', 401); + return new JsonResponse('Incorrect username or password', 401); } } diff --git a/src/Authenticator/RegisterAuthenticator.php b/src/Authenticator/RegisterAuthenticator.php new file mode 100755 index 0000000000000000000000000000000000000000..c758d5222c608a321f8da18f83a78ff93d19ab83 --- /dev/null +++ b/src/Authenticator/RegisterAuthenticator.php @@ -0,0 +1,91 @@ +<?php + +namespace App\Authenticator; + +use App\Model\UserModel; +use App\Service\UserProvider; +use App\Service\UserService; +use Exception; +use Kaa\Component\Database\EntityManager\EntityManagerInterface; +use Kaa\Component\HttpMessage\Exception\BadRequestException; +use Kaa\Component\HttpMessage\Exception\NoContentException; +use Kaa\Component\HttpMessage\Exception\SuspiciousOperationException; +use Kaa\Component\HttpMessage\Request; +use Kaa\Component\HttpMessage\Response\JsonResponse; +use Kaa\Component\HttpMessage\Response\Response; +use Kaa\Component\Security\AuthenticatorInterface; +use Kaa\Component\Security\Exception\SessionException; +use Kaa\Component\Security\Session\SessionService; +use Random\RandomException; +use Throwable; + +class RegisterAuthenticator implements AuthenticatorInterface +{ + private SessionService $sessionService; + private EntityManagerInterface $entityManager; + + private UserProvider $userProvider; + + private UserService $userService; + + public function __construct( + SessionService $sessionService, + EntityManagerInterface $entityManager, + UserProvider $userProvider, + UserService $userService + ) { + $this->sessionService = $sessionService; + $this->entityManager = $entityManager; + $this->userProvider = $userProvider; + $this->userService = $userService; + } + + /** + * @throws BadRequestException + * @throws SuspiciousOperationException + * @throws NoContentException + */ + public function supports(Request $request): bool + { + $content = json_decode($request->getContent(), true); + return $request->getMethod() === 'POST' + && array_key_exists('username', $content) + && array_key_exists('password', $content) + && array_key_exists('email', $content); + } + + /** + * @throws NoContentException + * @throws Exception + */ + public function authenticate(Request $request): callable + { + $data = json_decode($request->getContent(), true); + $id = $this->userService->createUser(new UserModel( + (string)$data['username'], + (string)$data['email'], + (string)$data['password'], + ['USER'] + )); + return fn () => $this->userProvider->getUser((string)$id); + } + + /** + * @throws SessionException + * @throws RandomException + */ + public function onAuthenticationSuccess(Request $request, callable $getUser): ?Response + { + $cookie = $this->sessionService->writeSession($getUser()); + + $response = new Response('Success', 200); + $response->addCookie($cookie); + + return $response; + } + + public function onAuthenticationFailure(Request $request, Throwable $throwable): ?Response + { + return new JsonResponse('Something went wrong...', 400); + } +} diff --git a/src/Controller/ArticleController.php b/src/Controller/ArticleController.php index 5d45bf5a0ad809b47b4a07b728050d55087c8e6e..8abfeac53c07bac3a9086db90ac0b36f42265e82 100755 --- a/src/Controller/ArticleController.php +++ b/src/Controller/ArticleController.php @@ -15,7 +15,6 @@ use Kaa\Component\RequestMapperDecorator\MapJsonPayload; use Kaa\Component\RequestMapperDecorator\MapRouteParameter; use Kaa\Component\Router\Attribute\Get; use Kaa\Component\Router\Attribute\Post; -use Kaa\Component\Router\Attribute\Put; use Kaa\Component\Router\Attribute\Route; use Kaa\Component\SecurityDecorator\CurrentUser; @@ -46,7 +45,7 @@ class ArticleController return $articleService->getArticle($id); } - #[Put('/{id}')] + #[POST('/put/{id}')] public function changeArticle( #[MapRouteParameter] int $id, @@ -58,7 +57,7 @@ class ArticleController User $user ): Response { $articleService->updateArticle($id, $articleModel, $user); - return new JsonResponse('Successful changed', status: 200); + return new JsonResponse('Successful changed', 200); } #[Get('/my')] @@ -69,7 +68,7 @@ class ArticleController #[Inject] ArticleService $articleService ): MultipleArticlesModel { - return $articleService->getUserArticles((string) $user->getId()); + return $articleService->getUserArticles($user->getId()); } #[Get('/user/{id}')] @@ -80,6 +79,6 @@ class ArticleController #[Inject] ArticleService $articleService ): MultipleArticlesModel { - return $articleService->getUserArticles((string) $id); + return $articleService->getUserArticles($id); } } diff --git a/src/Controller/CommentController.php b/src/Controller/CommentController.php index 93c12c70766a4472c43621618b1645c93026f56c..e5c983010a339821f10ea8230498b0519bc07aee 100755 --- a/src/Controller/CommentController.php +++ b/src/Controller/CommentController.php @@ -4,6 +4,7 @@ namespace App\Controller; use App\Entity\User; use App\Model\CommentModel; +use App\Model\CommentResponseModel; use App\Model\MultipleCommentModel; use App\Service\CommentService; use Exception; @@ -15,7 +16,6 @@ use Kaa\Component\RequestMapperDecorator\MapJsonPayload; use Kaa\Component\RequestMapperDecorator\MapRouteParameter; use Kaa\Component\Router\Attribute\Get; use Kaa\Component\Router\Attribute\Post; -use Kaa\Component\Router\Attribute\Put; use Kaa\Component\Router\Attribute\Route; use Kaa\Component\SecurityDecorator\CurrentUser; @@ -45,11 +45,11 @@ class CommentController int $id, #[Inject] CommentService $commentService - ): CommentModel { + ): CommentResponseModel { return $commentService->getComment($id); } - #[Put('/{id}')] + #[Post('/put/{id}')] public function changeComment( #[MapRouteParameter] int $id, @@ -61,7 +61,7 @@ class CommentController User $user ): Response { $commentService->updateComment($id, $commentModel, $user); - return new JsonResponse('Successful changed', status: 200); + return new JsonResponse(); } #[Get('/my')] @@ -72,7 +72,7 @@ class CommentController #[Inject] CommentService $commentService ): MultipleCommentModel { - return $commentService->getUserComments((string) $user->getId()); + return $commentService->getUserComments($user->getId()); } #[Get('/user/{id}')] @@ -83,6 +83,6 @@ class CommentController #[Inject] CommentService $commentService ): MultipleCommentModel { - return $commentService->getUserComments((string) $id); + return $commentService->getUserComments($id); } } diff --git a/src/Controller/ExampleController.php b/src/Controller/ExampleController.php index 803ec31f382439fc4727665698dccf17fcb785da..b7d251e9f5426c5e2ee58535f0dd7acccce543af 100755 --- a/src/Controller/ExampleController.php +++ b/src/Controller/ExampleController.php @@ -11,7 +11,7 @@ class ExampleController #[Get('/test')] public function test(): JsonResponse { - return new JsonResponse('{"hame": "kolaya"}', 200, []); + return new JsonResponse('{"hame": "kolaya"}', 200); } # warning for incorrect json for parsing? #[Get('/test/{id}')] @@ -19,6 +19,6 @@ class ExampleController #[MapRouteParameter] int $id ): JsonResponse { - return new JsonResponse("{'id': {$id}}", 200, []); + return new JsonResponse("{'id': {$id}}", 200); } } diff --git a/src/Controller/RegisterController.php b/src/Controller/RegisterController.php deleted file mode 100755 index 0c3d7dc890d1a79b17adac20e686cc721dbc51f9..0000000000000000000000000000000000000000 --- a/src/Controller/RegisterController.php +++ /dev/null @@ -1,24 +0,0 @@ -<?php - -namespace App\Controller; - -use App\Model\RegisterModel; -use App\Service\UserProvider; -use Kaa\Component\DependencyInjectionDecorator\Inject; -use Kaa\Component\RequestMapperDecorator\AsJsonResponse; -use Kaa\Component\RequestMapperDecorator\MapJsonPayload; -use Kaa\Component\Router\Attribute\Post; - -class RegisterController -{ - #[Post('/register')] - #[AsJsonResponse] - public function register( - #[MapJsonPayload] - RegisterModel $registerModel, - #[Inject] - UserProvider $userProvider - ): int { - return $userProvider->registerUser($registerModel); - } -} diff --git a/src/Controller/TagController.php b/src/Controller/TagController.php index fc90a9be0fc5dac0d2b63401a0f3dc67dc0b8eca..15bed7556ce311ae943296feb574c772b06ccfad 100755 --- a/src/Controller/TagController.php +++ b/src/Controller/TagController.php @@ -3,6 +3,7 @@ namespace App\Controller; use App\Model\TagModel; +use App\Model\TagResponseModel; use App\Service\TagService; use Kaa\Component\DependencyInjectionDecorator\Inject; use Kaa\Component\HttpMessage\Response\JsonResponse; @@ -11,7 +12,6 @@ use Kaa\Component\RequestMapperDecorator\MapJsonPayload; use Kaa\Component\RequestMapperDecorator\MapRouteParameter; use Kaa\Component\Router\Attribute\Get; use Kaa\Component\Router\Attribute\Post; -use Kaa\Component\Router\Attribute\Put; use Kaa\Component\Router\Attribute\Route; #[Route('/tag')] @@ -35,11 +35,11 @@ class TagController TagService $tagService, #[MapRouteParameter] int $id - ): TagModel { + ): TagResponseModel { return $tagService->getTag($id); } - #[Put('/{id}')] + #[Post('/put/{id}')] public function changeTag( #[Inject] TagService $tagService, @@ -49,6 +49,6 @@ class TagController TagModel $tagModel ): JsonResponse { $tagService->updateTag($id, $tagModel); - return new JsonResponse('Successful changed', status: 200); + return new JsonResponse(); } } diff --git a/src/Controller/TopicController.php b/src/Controller/TopicController.php index f9290db877d146da5b2fdd9348de9367387d7b1d..6f0f6dd8deec478134322736a24baddb21df5b9f 100755 --- a/src/Controller/TopicController.php +++ b/src/Controller/TopicController.php @@ -5,6 +5,7 @@ namespace App\Controller; use App\Entity\User; use App\Model\MultipleTopicsModel; use App\Model\TopicModel; +use App\Model\TopicResponseModel; use App\Service\TopicService; use Kaa\Component\DependencyInjectionDecorator\Inject; use Kaa\Component\HttpMessage\Response\JsonResponse; @@ -13,7 +14,6 @@ use Kaa\Component\RequestMapperDecorator\MapJsonPayload; use Kaa\Component\RequestMapperDecorator\MapRouteParameter; use Kaa\Component\Router\Attribute\Get; use Kaa\Component\Router\Attribute\Post; -use Kaa\Component\Router\Attribute\Put; use Kaa\Component\SecurityDecorator\CurrentUser; class TopicController @@ -36,11 +36,11 @@ class TopicController TopicService $topicService, #[MapRouteParameter] int $id - ): TopicModel { + ): TopicResponseModel { return $topicService->getTopic($id); } - #[Put('moderator/topic/{id}')] + #[Post('/put/moderator/topic/{id}')] public function changeTopic( #[MapRouteParameter] int $id, @@ -50,7 +50,7 @@ class TopicController TopicService $topicService ): JsonResponse { $topicService->updateTopic($id, $topicModel); - return new JsonResponse('Successful changed', status: 200); + return new JsonResponse('Successful changed', 200); } #[Get('/topic/my')] @@ -61,7 +61,7 @@ class TopicController #[Inject] TopicService $topicService ): MultipleTopicsModel { - return $topicService->getUserTopics((string) $user->getId()); + return $topicService->getUserTopics($user->getId()); } #[Get('/topic/user/{id}')] @@ -72,6 +72,6 @@ class TopicController #[Inject] TopicService $topicService ): MultipleTopicsModel { - return $topicService->getUserTopics((string) $id); + return $topicService->getUserTopics($id); } } diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php index d0c93215766e0df23816b1552b91fe9c48f99517..814bb1370af30988544e0600b940939e5fb41ccf 100755 --- a/src/Controller/UserController.php +++ b/src/Controller/UserController.php @@ -4,9 +4,8 @@ namespace App\Controller; use App\Entity\User; use App\Model\UserModel; -use App\Service\UserProvider; -use DateTimeImmutable; -use Exception; +use App\Model\UserResponseModel; +use App\Service\UserService; use Kaa\Component\DependencyInjectionDecorator\Inject; use Kaa\Component\HttpMessage\Response\JsonResponse; use Kaa\Component\HttpMessage\Response\Response; @@ -15,7 +14,6 @@ use Kaa\Component\RequestMapperDecorator\MapJsonPayload; use Kaa\Component\RequestMapperDecorator\MapRouteParameter; use Kaa\Component\Router\Attribute\Get; use Kaa\Component\Router\Attribute\Post; -use Kaa\Component\Router\Attribute\Put; use Kaa\Component\SecurityDecorator\CurrentUser; class UserController @@ -26,9 +24,9 @@ class UserController #[MapJsonPayload] UserModel $model, #[Inject] - UserProvider $userProvider + UserService $userService ): int { - return $userProvider->createUser($model); + return $userService->createUser($model); } #[Get('/user/me')] @@ -37,54 +35,46 @@ class UserController #[CurrentUser] User $user, #[Inject] - UserProvider $userProvider, - ): UserModel { - return $userProvider->getCurrentUser($user); + UserService $userService + ): UserResponseModel { + return $userService->getUserByEntity($user); } #[Get('/admin/user/{id}')] #[AsJsonResponse] public function getUser( #[Inject] - UserProvider $userProvider, + UserService $userService, #[MapRouteParameter] int $id - ): UserModel { - return $userProvider->getUserById($id); + ): UserResponseModel { + return $userService->getUserById($id); } - #[Put('/admin/user/{id}')] + #[Post('/put/admin/user/{id}')] public function changeUser( #[Inject] - UserProvider $userProvider, + UserService $userService, #[MapRouteParameter] int $id, #[MapJsonPayload] UserModel $userModel ): JsonResponse { - $userModel->setChangingDate(new DateTimeImmutable()); - try { - $userProvider->updateUser($userModel, $id); - } catch (Exception $exception) { - return new JsonResponse('Nothing changed', status: 200); - } - return new JsonResponse('Successfully changed', status: 200); + $userService->updateUser($userModel, $id); + return new JsonResponse(); } - #[Put('/user/me')] + #[Post('/put/user/me')] public function changeCurrentUser( #[Inject] - UserProvider $userProvider, + UserService $userService, #[CurrentUser] User $user, #[MapJsonPayload] UserModel $userModel ): Response { - $userModel - ->setRoles($user->getRoles()) - ->setCreationDate($user->getCreationTime()) - ->setChangingDate(new DateTimeImmutable()); - $userProvider->updateUser($userModel, $user->getId()); - return new JsonResponse('Successfully changed', status: 200); + $userModel->setRoles($user->getRoles()); + $userService->updateUser($userModel, $user->getId()); + return new JsonResponse('Successfully changed', 200); } } diff --git a/src/Entity/Admission.php b/src/Entity/Admission.php new file mode 100755 index 0000000000000000000000000000000000000000..be4641dc19b238b9561d23b0f306df3b34f82e79 --- /dev/null +++ b/src/Entity/Admission.php @@ -0,0 +1,53 @@ +<?php + +namespace App\Entity; + +use Kaa\Component\Database\Attribute as Db; +use Kaa\Component\Database\EntityInterface; + +#[Db\Entity] +abstract class Admission implements EntityInterface +{ + #[Db\Id] + #[Db\Column] + protected ?int $id = null; + + #[Db\ManyToOne(Topic::class)] + protected Topic $topic; + + #[Db\ManyToOne(User::class)] + protected User $user; + + public function getId(): int + { + return $this->id ?? -1; + } + + public function setId(int $id): Admission + { + $this->id = $id; + return $this; + } + + public function getTopic(): Topic + { + return $this->topic; + } + + public function setTopic(Topic $topic): Admission + { + $this->topic = $topic; + return $this; + } + + public function getUser(): User + { + return $this->user; + } + + public function setUser(User $user): Admission + { + $this->user = $user; + return $this; + } +} diff --git a/src/Entity/Article.php b/src/Entity/Article.php index 74ae8fa04c80fb7df1f3ea8e7c2b91427c69b4e7..b9cbbfbc84123c64137db1354b3ab73a175b9960 100755 --- a/src/Entity/Article.php +++ b/src/Entity/Article.php @@ -34,9 +34,13 @@ abstract class Article implements EntityInterface #[Db\OneToMany(ArticleTag::class, mappedBy: 'article')] protected array $tags = []; + /** @var Comment[] */ + #[Db\OneToMany(Comment::class, mappedBy: 'article')] + protected array$comments = []; + public function getId(): int { - return $this->id; + return $this->id ?? -1; } public function setId(int $id): Article @@ -45,11 +49,6 @@ abstract class Article implements EntityInterface return $this; } - public function getAuthorId(): int - { - return $this->author->getId(); - } - public function getTopicId(): int { return $this->topic->getId(); @@ -135,4 +134,18 @@ abstract class Article implements EntityInterface $this->tags[] = $tag; return $this; } + + /** + * @return Comment[] + */ + public function getComments(): array + { + return $this->comments; + } + + public function addComment(Comment $comment): Article + { + $this->comments[] = $comment; + return $this; + } } diff --git a/src/Entity/ArticleTag.php b/src/Entity/ArticleTag.php index f0fd05da1561806d13f40cf76f7c617eab1882d1..c9c1381637b30cec7935ccd5998f1d41db146e24 100755 --- a/src/Entity/ArticleTag.php +++ b/src/Entity/ArticleTag.php @@ -20,7 +20,7 @@ abstract class ArticleTag implements EntityInterface public function getId(): int { - return $this->id; + return $this->id ?? -1; } public function setId(int $id): ArticleTag @@ -29,15 +29,6 @@ abstract class ArticleTag implements EntityInterface return $this; } - public function getArticleId(): int - { - return $this->article->getId(); - } - - public function getTagId(): int - { - return $this->tag->getId(); - } public function getArticle(): Article { diff --git a/src/Entity/Comment.php b/src/Entity/Comment.php index 77d229efda47371762d8f85147a8533f22d428e8..bac27b9326a1f11260ab5b6422c59f6cf2d93658 100755 --- a/src/Entity/Comment.php +++ b/src/Entity/Comment.php @@ -30,7 +30,7 @@ abstract class Comment implements EntityInterface public function getId(): int { - return $this->id; + return $this->id ?? -1; } public function setId(int $id): Comment @@ -39,11 +39,6 @@ abstract class Comment implements EntityInterface return $this; } - public function getAuthorId(): int - { - return $this->author->getId(); - } - public function getArticleId(): int { return $this->article->getId(); diff --git a/src/Entity/Tag.php b/src/Entity/Tag.php index 3678c134391a86c845410357baa9537c6738d7a4..6c03e30db12289d71a710c4dd8fc43bbc920acf5 100755 --- a/src/Entity/Tag.php +++ b/src/Entity/Tag.php @@ -15,13 +15,13 @@ abstract class Tag implements EntityInterface #[Db\Column] protected string $title; - /** @var Article[] */ + /** @var ArticleTag[] */ #[Db\OneToMany(ArticleTag::class, mappedBy: 'tag')] protected array $articles = []; public function getId(): int { - return $this->id; + return $this->id ?? -1; } public function setId(int $id): Tag diff --git a/src/Entity/Topic.php b/src/Entity/Topic.php index c2aa5d4ce5fa19ccd7af8a064b2f2df168282164..9b35927cab324ecd182456ba9d3dc515b54d5db5 100755 --- a/src/Entity/Topic.php +++ b/src/Entity/Topic.php @@ -19,12 +19,21 @@ abstract class Topic implements EntityInterface #[Db\Column] protected string $title; + #[Db\Column] + protected string $type; + + /** + * @var Admission[] + */ + #[Db\OneToMany(Admission::class, mappedBy: 'topic')] + protected array $acceptedUsers = []; + #[Db\Column(type: Db\Type::DateTimeImmutable)] protected DateTimeImmutable $created; public function getId(): int { - return $this->id; + return $this->id ?? -1; } public function setId(int $id): Topic @@ -33,11 +42,6 @@ abstract class Topic implements EntityInterface return $this; } - public function getAuthorId(): int - { - return $this->author->getId(); - } - public function getTitle(): string { return $this->title; @@ -49,6 +53,30 @@ abstract class Topic implements EntityInterface return $this; } + public function getType(): string + { + return $this->type; + } + + public function setType(string $type): Topic + { + $this->type = $type; + return $this; + } + + /** + * @return Admission[] + */ + public function getAcceptedUsers(): array + { + return $this->acceptedUsers; + } + + public function addAcceptedUser(Admission $user): Topic + { + $this->acceptedUsers[] = $user; + return $this; + } public function getCreationTime(): DateTimeImmutable { diff --git a/src/Entity/User.php b/src/Entity/User.php index bec95740d23ddc8cec4b5ab5191202195f481b68..9749544113125bae9b6b3f55907d86dc6d3e466b 100755 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -25,7 +25,7 @@ abstract class User implements EntityInterface, UserInterface /** @var string[] */ #[Db\Column(type: Db\Type::SimpleArray)] - protected array $role; + protected array $roles; #[Db\Column(type: Db\Type::DateTimeImmutable)] protected DateTimeImmutable $created; @@ -41,7 +41,7 @@ abstract class User implements EntityInterface, UserInterface public function getId(): int { - return $this->id; + return $this->id ?? -1; } public function setId(int $id): User @@ -110,15 +110,15 @@ abstract class User implements EntityInterface, UserInterface */ public function getRoles(): array { - return $this->role; + return $this->roles; } /** * @param string[] $roles */ - public function setRole(array $roles): User + public function setRoles(array $roles): User { - $this->role = $roles; + $this->roles = $roles; return $this; } } diff --git a/src/Model/ArticleModel.php b/src/Model/ArticleModel.php index 095b90dc97b6168942d6103013dc4925b8c68c4f..d0d3d56ffa88c7ea848e18a9c3e4498d3f42f656 100755 --- a/src/Model/ArticleModel.php +++ b/src/Model/ArticleModel.php @@ -2,14 +2,10 @@ namespace App\Model; -use DateTimeImmutable; use Kaa\Component\Validator\Assert; -class ArticleModel +class ArticleModel extends BaseModel { - #[Assert\PositiveOrZero] - private int $id; - #[Assert\PositiveOrZero] private int $topicId; @@ -22,34 +18,20 @@ class ArticleModel /** @var int[] */ private array $tags = []; - private string $created; # maybe something asserting? - - private string $changed; /** * @param int[] $tags */ public function __construct( - int $id, int $topicId, string $title, string $body, - array $tags, - DateTimeImmutable $created, - DateTimeImmutable $changed + array $tags ) { - $this->id = $id; $this->topicId = $topicId; $this->title = $title; $this->body = $body; $this->tags = $tags; - $this->created = $created->format('Y-m-d H:i:s'); - $this->changed = $changed->format('Y-m-d H:i:s'); - } - - public function getId(): int - { - return $this->id; } public function getTopicId(): int @@ -74,14 +56,4 @@ class ArticleModel { return $this->tags; } - - public function getCreationTime(): string - { - return $this->created; - } - - public function getChangedTime(): string - { - return $this->changed; - } } diff --git a/src/Model/ArticleResponseModel.php b/src/Model/ArticleResponseModel.php index df2a26f79175a502da554c9e95aba24e4f2f519b..3db4342fba9482f1f317e7d6bb70c7304430f121 100755 --- a/src/Model/ArticleResponseModel.php +++ b/src/Model/ArticleResponseModel.php @@ -6,7 +6,7 @@ namespace App\Model; use DateTimeImmutable; use Kaa\Component\Validator\Assert; -class ArticleResponseModel +class ArticleResponseModel extends BaseModel { #[Assert\PositiveOrZero] private int $id; @@ -14,7 +14,7 @@ class ArticleResponseModel #[Assert\PositiveOrZero] private int $authorId; - private TopicModel $topic; + private TopicResponseModel $topic; #[Assert\Length(max: 480)] private string $title; @@ -22,10 +22,10 @@ class ArticleResponseModel #[Assert\Length(max: 10000)] private string $body; - /** @var TagModel[] */ + /** @var TagResponseModel[] */ private array $tags = []; - /** @var CommentModel[] */ + /** @var CommentResponseModel[] */ private array $comments = []; private string $created; # maybe something asserting? @@ -33,13 +33,13 @@ class ArticleResponseModel private string $changed; /** - * @param TagModel[] $tags - * @param CommentModel[] $comments + * @param TagResponseModel[] $tags + * @param CommentResponseModel[] $comments */ public function __construct( int $id, int $authorId, - TopicModel $topic, + TopicResponseModel $topic, string $title, string $body, array $tags, @@ -68,7 +68,7 @@ class ArticleResponseModel return $this->authorId; } - public function getTopic(): TopicModel + public function getTopic(): TopicResponseModel { return $this->topic; } @@ -84,7 +84,7 @@ class ArticleResponseModel } /** - * @return TagModel[] + * @return TagResponseModel[] */ public function getTags(): array { @@ -92,7 +92,7 @@ class ArticleResponseModel } /** - * @return CommentModel[] + * @return CommentResponseModel[] */ public function getComments(): array { diff --git a/src/Model/BaseModel.php b/src/Model/BaseModel.php new file mode 100755 index 0000000000000000000000000000000000000000..b24359a87a2af6e53e7c4adc2ac1d99bc1cf53b0 --- /dev/null +++ b/src/Model/BaseModel.php @@ -0,0 +1,7 @@ +<?php + +namespace App\Model; + +class BaseModel +{ +} diff --git a/src/Model/CommentModel.php b/src/Model/CommentModel.php index 538fd777c3d7f7e5d32598d0cdebee83d061a38d..df42686efb1cb08c7cced4790383c13b89591338 100755 --- a/src/Model/CommentModel.php +++ b/src/Model/CommentModel.php @@ -2,51 +2,23 @@ namespace App\Model; -use DateTimeImmutable; use Kaa\Component\Validator\Assert; -class CommentModel +class CommentModel extends BaseModel { - #[Assert\PositiveOrZero] - private int $id; - - #[Assert\PositiveOrZero] - private int $authorId; - #[Assert\PositiveOrZero] private int $articleId; #[Assert\Length(max: 1000)] private string $body; - private string $created; # maybe something asserting? - - private string $changed; public function __construct( - int $id, - int $author_id, int $articleId, - string $body, - DateTimeImmutable $created, - DateTimeImmutable $changed + string $body ) { - $this->id = $id; - $this->authorId = $author_id; $this->articleId = $articleId; $this->body = $body; - $this->created = $created->format('Y-m-d H:i:s'); - $this->changed = $changed->format('Y-m-d H:i:s'); - } - - public function getId(): int - { - return $this->id; - } - - public function getAuthorId(): int - { - return $this->authorId; } public function getArticleId(): int @@ -58,14 +30,4 @@ class CommentModel { return $this->body; } - - public function getCreationTime(): string - { - return $this->created; - } - - public function getChangedTime(): string - { - return $this->changed; - } } diff --git a/src/Model/CommentResponseModel.php b/src/Model/CommentResponseModel.php new file mode 100644 index 0000000000000000000000000000000000000000..75c169823137cec845d31304af7e3133eb5f8cea --- /dev/null +++ b/src/Model/CommentResponseModel.php @@ -0,0 +1,71 @@ +<?php + +namespace App\Model; + +use DateTimeImmutable; +use Kaa\Component\Validator\Assert; + +class CommentResponseModel extends BaseModel +{ + #[Assert\PositiveOrZero] + private int $id; + + #[Assert\PositiveOrZero] + private int $authorId; + + #[Assert\PositiveOrZero] + private int $articleId; + + #[Assert\Length(max: 1000)] + private string $body; + + private string $created; + + private string $changed; + + public function __construct( + int $id, + int $author_id, + int $articleId, + string $body, + DateTimeImmutable $created, + DateTimeImmutable $changed + ) { + $this->id = $id; + $this->authorId = $author_id; + $this->articleId = $articleId; + $this->body = $body; + $this->created = $created->format('Y-m-d H:i:s'); + $this->changed = $changed->format('Y-m-d H:i:s'); + } + + public function getId(): int + { + return $this->id; + } + + public function getAuthorId(): int + { + return $this->authorId; + } + + public function getArticleId(): int + { + return $this->articleId; + } + + public function getBody(): string + { + return $this->body; + } + + public function getCreationTime(): string + { + return $this->created; + } + + public function getChangedTime(): string + { + return $this->changed; + } +} diff --git a/src/Model/MultipleCommentModel.php b/src/Model/MultipleCommentModel.php index 8819a62f65593799744b0d5a4cc93f27a15c85c1..741f814d742fe2d3647a1706bbab033bc776a615 100755 --- a/src/Model/MultipleCommentModel.php +++ b/src/Model/MultipleCommentModel.php @@ -6,11 +6,11 @@ namespace App\Model; /** @kphp-json flatten */ class MultipleCommentModel { - /** @var CommentModel[] */ + /** @var CommentResponseModel[] */ private array $comments; /** - * @param CommentModel[] $comments + * @param CommentResponseModel[] $comments */ public function __construct(array $comments) { @@ -18,7 +18,7 @@ class MultipleCommentModel } /** - * @return CommentModel[] + * @return CommentResponseModel[] */ public function getTopics(): array { diff --git a/src/Model/MultipleTopicsModel.php b/src/Model/MultipleTopicsModel.php index 09669b3caea4a71356a12f5a8fd71ec0ae6fa07a..684642a10ac9bdd75c55af0ef29533ef61d10bb3 100755 --- a/src/Model/MultipleTopicsModel.php +++ b/src/Model/MultipleTopicsModel.php @@ -5,11 +5,11 @@ namespace App\Model; /** @kphp-json flatten */ class MultipleTopicsModel { - /** @var TopicModel[] */ + /** @var TopicResponseModel[] */ private array $topics; /** - * @param TopicModel[] $topics + * @param TopicResponseModel[] $topics */ public function __construct(array $topics) { @@ -17,7 +17,7 @@ class MultipleTopicsModel } /** - * @return TopicModel[] + * @return TopicResponseModel[] */ public function getTopics(): array { diff --git a/src/Model/RegisterModel.php b/src/Model/RegisterModel.php index f0092bccbc6290d6c4075b818edc9fa1b4ef4669..812c0071eaf5b9472fec565a278625559266cbb4 100755 --- a/src/Model/RegisterModel.php +++ b/src/Model/RegisterModel.php @@ -4,7 +4,7 @@ namespace App\Model; use Kaa\Component\Validator\Assert; -class RegisterModel +class RegisterModel extends BaseModel { #[Assert\PositiveOrZero] private int $id; @@ -22,7 +22,7 @@ class RegisterModel int $id, string $username, string $email, - string $password, + string $password ) { $this->id = $id; $this->username = $username; diff --git a/src/Model/TagModel.php b/src/Model/TagModel.php index e64391af0d5520b5ef532d630366eec84d179cd5..340c912e2b07404c1ff2d7e1151dae387e0fe8c5 100755 --- a/src/Model/TagModel.php +++ b/src/Model/TagModel.php @@ -4,23 +4,15 @@ namespace App\Model; use Kaa\Component\Validator\Assert; -class TagModel +class TagModel extends BaseModel { - #[Assert\PositiveOrZero] - private int $id; - #[Assert\Length(max: 60)] private string $title; - public function __construct(int $id, string $title) - { - $this->id = $id; - $this->title = $title; - } - public function getId(): int + public function __construct(string $title) { - return $this->id; + $this->title = $title; } public function getTitle(): string diff --git a/src/Model/TagResponseModel.php b/src/Model/TagResponseModel.php new file mode 100644 index 0000000000000000000000000000000000000000..e051350e8030aed7e63b55c45e6e9cc1884f90c7 --- /dev/null +++ b/src/Model/TagResponseModel.php @@ -0,0 +1,31 @@ +<?php + +namespace App\Model; + +use Kaa\Component\Validator\Assert; + +class TagResponseModel extends BaseModel +{ + #[Assert\PositiveOrZero] + private int $id; + + #[Assert\Length(max: 60)] + private string $title; + + + public function __construct(int $id, string $title) + { + $this->id = $id; + $this->title = $title; + } + + public function getId(): int + { + return $this->id; + } + + public function getTitle(): string + { + return $this->title; + } +} diff --git a/src/Model/TestModel.php b/src/Model/TestModel.php deleted file mode 100755 index bbd959bd8a7caf21ff257c6903769eec47b518d5..0000000000000000000000000000000000000000 --- a/src/Model/TestModel.php +++ /dev/null @@ -1,20 +0,0 @@ -<?php - -namespace App\Model; - -use DateTimeImmutable; - -class TestModel -{ - private DateTimeImmutable $dt; - - public function __construct(DateTimeImmutable $dt) - { - $this->dt = $dt; - } - - public function getDt(): DateTimeImmutable - { - return $this->dt; - } -} diff --git a/src/Model/TopicModel.php b/src/Model/TopicModel.php index 25997b5322ec1a49c359d0ba9429d739de5d9d08..6815727ec3120ce7f72534dc7683d2355e26266b 100755 --- a/src/Model/TopicModel.php +++ b/src/Model/TopicModel.php @@ -2,33 +2,37 @@ namespace App\Model; -use DateTimeImmutable; use Kaa\Component\Validator\Assert; -class TopicModel +class TopicModel extends BaseModel { - #[Assert\PositiveOrZero] - private int $id; - #[Assert\PositiveOrZero] private int $authorId; #[Assert\Length(max: 365)] private string $title; - private string $created; # maybe something asserting? + private string $type; - public function __construct(int $id, int $author_id, string $title, DateTimeImmutable $created) - { - $this->id = $id; + /** + * @var int[] + */ + private array $acceptedUsers; + + /** + * @param int[] $acceptedUsers + */ + + public function __construct( + int $author_id, + string $title, + string $type, + array $acceptedUsers + ) { $this->authorId = $author_id; $this->title = $title; - $this->created = $created->format('Y-m-d H:i:s'); - } - - public function getId(): int - { - return $this->id; + $this->type = $type; + $this->acceptedUsers = $acceptedUsers; } public function getAuthorId(): int @@ -41,8 +45,16 @@ class TopicModel return $this->title; } - public function getCreationTime(): string + public function getType(): string + { + return $this->type; + } + + /** + * @return int[] + */ + public function getAcceptedUsers(): array { - return $this->created; + return $this->acceptedUsers; } } diff --git a/src/Model/TopicResponseModel.php b/src/Model/TopicResponseModel.php new file mode 100755 index 0000000000000000000000000000000000000000..bba2fa6e5d8c42255620b6b0b2754f92c1690918 --- /dev/null +++ b/src/Model/TopicResponseModel.php @@ -0,0 +1,80 @@ +<?php + +namespace App\Model; + +use DateTimeImmutable; +use Kaa\Component\Validator\Assert; + +class TopicResponseModel extends BaseModel +{ + #[Assert\PositiveOrZero] + private int $id; + + #[Assert\PositiveOrZero] + private int $authorId; + + #[Assert\Length(max: 365)] + private string $title; + + private string $type; + + /** + * @var int[] + */ + private array $acceptedUsers; + + private string $created; + + /** + * @param int[] $acceptedUsers + */ + + public function __construct( + int $id, + int $author_id, + string $title, + string $type, + array $acceptedUsers, + DateTimeImmutable $created + ) { + $this->id = $id; + $this->authorId = $author_id; + $this->title = $title; + $this->type = $type; + $this->acceptedUsers = $acceptedUsers; + $this->created = $created->format('Y-m-d H:i:s'); + } + + public function getId(): int + { + return $this->id; + } + + public function getAuthorId(): int + { + return $this->authorId; + } + + public function getTitle(): string + { + return $this->title; + } + + public function getType(): string + { + return $this->type; + } + + /** + * @return int[] + */ + public function getAcceptedUsers(): array + { + return $this->acceptedUsers; + } + + public function getCreationTime(): string + { + return $this->created; + } +} diff --git a/src/Model/UserModel.php b/src/Model/UserModel.php index 8fcf8610c195084921a2978034e3d0dd0238b721..3cc9672a3034b690978ac5be48e3e219de8079f1 100755 --- a/src/Model/UserModel.php +++ b/src/Model/UserModel.php @@ -2,28 +2,19 @@ namespace App\Model; -use DateTimeImmutable; -use Kaa\Component\Security\UserInterface; use Kaa\Component\Validator\Assert; -class UserModel implements UserInterface +class UserModel extends BaseModel { - #[Assert\PositiveOrZero] - private int $id; - #[Assert\Length(max: 120)] private string $username; #[Assert\Email] private string $email; - #[Assert\Length(max: 60)] + #[Assert\Length(max: 120)] private string $password; - private string $created; - - private string $changed; - /** @var string[] */ private array $roles; @@ -31,33 +22,17 @@ class UserModel implements UserInterface * @param string[] $roles */ public function __construct( - int $id, string $username, string $email, string $password, - array $roles, - DateTimeImmutable $created, - DateTimeImmutable $changed + array $roles ) { - $this->id = $id; $this->username = $username; $this->email = $email; $this->password = $password; - $this->created = $created->format('Y-m-d H:i:s'); - $this->changed = $changed->format('Y-m-d H:i:s'); $this->roles = $roles; } - public function getIdentifier(): string - { - return (string) $this->id; - } - - public function getId(): int - { - return $this->id; - } - public function getUsername(): string { return $this->username; @@ -73,28 +48,6 @@ class UserModel implements UserInterface return $this->password; } - public function getCreationDate(): string - { - return $this->created; - } - - public function setCreationDate(DateTimeImmutable $created): UserModel - { - $this->created = $created->format('Y-m-d H:i:s'); - return $this; - } - - public function getChangingDate(): string - { - return $this->changed; - } - - public function setChangingDate(DateTimeImmutable $changed): UserModel - { - $this->changed = $changed->format('Y-m-d H:i:s'); - return $this; - } - /** * @return string[] */ diff --git a/src/Model/UserResponseModel.php b/src/Model/UserResponseModel.php new file mode 100644 index 0000000000000000000000000000000000000000..f1d969e7dd2caa1e55580cd346158a70ac7f0fc9 --- /dev/null +++ b/src/Model/UserResponseModel.php @@ -0,0 +1,83 @@ +<?php + +namespace App\Model; + +use DateTimeImmutable; +use Kaa\Component\Security\UserInterface; +use Kaa\Component\Validator\Assert; + +class UserResponseModel extends BaseModel implements UserInterface +{ + #[Assert\PositiveOrZero] + private int $id; + + #[Assert\Length(max: 120)] + private string $username; + + #[Assert\Email] + private string $email; + + private string $created; + + private string $changed; + + /** @var string[] */ + private array $roles; + + /** + * @param string[] $roles + */ + public function __construct( + int $id, + string $username, + string $email, + array $roles, + DateTimeImmutable $created, + DateTimeImmutable $changed + ) { + $this->id = $id; + $this->username = $username; + $this->email = $email; + $this->created = $created->format('Y-m-d H:i:s'); + $this->changed = $changed->format('Y-m-d H:i:s'); + $this->roles = $roles; + } + + public function getIdentifier(): string + { + return (string) $this->id; + } + + public function getId(): int + { + return $this->id; + } + + public function getUsername(): string + { + return $this->username; + } + + public function getEmail(): string + { + return $this->email; + } + + public function getCreationDate(): string + { + return $this->created; + } + + public function getChangingDate(): string + { + return $this->changed; + } + + /** + * @return string[] + */ + public function getRoles(): array + { + return $this->roles; + } +} diff --git a/src/Service/ArticleService.php b/src/Service/ArticleService.php index bd57eb4caaaa53c873e0d761282e93fb2d9eea4a..9e128d14d13631ae370d78a724bb6509043042e9 100755 --- a/src/Service/ArticleService.php +++ b/src/Service/ArticleService.php @@ -4,16 +4,15 @@ namespace App\Service; use App\Entity\Article; use App\Entity\ArticleTag; -use App\Entity\Comment; use App\Entity\Tag; use App\Entity\Topic; use App\Entity\User; use App\Model\ArticleModel; use App\Model\ArticleResponseModel; -use App\Model\CommentModel; +use App\Model\CommentResponseModel; use App\Model\MultipleArticlesModel; -use App\Model\TagModel; -use App\Model\TopicModel; +use App\Model\TagResponseModel; +use App\Model\TopicResponseModel; use DateTimeImmutable; use Kaa\Component\Database\EntityManager\EntityManagerInterface; use RuntimeException; @@ -35,6 +34,14 @@ class ArticleService throw new RuntimeException("Article contains wrong tag id {$tag}"); } } + $topic = $this->entityManager->find(Topic::class, $articleModel->getTopicId()); + if ( + ($topic->getType() === 'PRIVATE') + && !in_array('MODERATOR', $user->getRoles()) + && !in_array($user->getId(), array_map(fn ($a) => $a->getUser()->getId(), $topic->getAcceptedUsers())) + ) { + throw new ForbiddenOverwriteException("You are not allow to write in this topic!"); + } $article = $this->entityManager->new(Article::class) ->setAuthor($user) ->setTopic($this->entityManager->find(Topic::class, $articleModel->getTopicId())) @@ -59,22 +66,24 @@ class ArticleService } $tags = []; foreach ($article->getTags() as $tag) { - $tagBd = $this->entityManager->find(Tag::class, $tag->getTagId()); - $tags[] = new TagModel($tagBd->getId(), $tagBd->getTitle()); + $tagBd = $this->entityManager->find(Tag::class, $tag->getTag()->getId()); + $tags[] = new TagResponseModel($tagBd->getId(), $tagBd->getTitle()); } - $topicBd = $this->entityManager->find(Topic::class, $article->getTopicId()); - $topic = new TopicModel( + $topicBd = $article->getTopic(); + $topic = new TopicResponseModel( $topicBd->getId(), - $topicBd->getAuthorId(), + $topicBd->getAuthor()->getId(), $topicBd->getTitle(), + $topicBd->getType(), + array_map(fn ($a) => $a->getUser()->getId(), $topicBd->getAcceptedUsers()), $topicBd->getCreationTime() ); $comments = []; - $commentsBd = $this->entityManager->findBy(Comment::class, ['article' => $article->getId()]); + $commentsBd = $article->getComments(); foreach ($commentsBd as $commentBd) { - $comments[] = new CommentModel( + $comments[] = new CommentResponseModel( $commentBd->getId(), - $commentBd->getArticleId(), + $commentBd->getAuthor()->getId(), $commentBd->getArticleId(), $commentBd->getBody(), $commentBd->getCreationTime(), @@ -83,7 +92,7 @@ class ArticleService } return new ArticleResponseModel( $article->getId(), - $article->getAuthorId(), + $article->getAuthor()->getId(), $topic, $article->geTitle(), $article->getBody(), @@ -100,11 +109,11 @@ class ArticleService if ($article === null) { throw new RuntimeException("Article with id=$id not found"); } - if ($user->getId() !== $article->getAuthorId()) { + if ($user->getId() !== $article->getAuthor()->getId()) { throw new ForbiddenOverwriteException('You are not the author'); } $tagIds = array_map(function (ArticleTag $t) { - return $t->getTagId(); + return $t->getTag()->getId(); }, $article->getTags()); $tags = []; foreach ($tagIds as $tag) { @@ -129,7 +138,7 @@ class ArticleService $this->entityManager->flush(); } - public function getUserArticles(string $id): MultipleArticlesModel + public function getUserArticles(int $id): MultipleArticlesModel { $articles = $this->entityManager->findBy(Article::class, ['author' => $id]); $articleModels = []; diff --git a/src/Service/CommentService.php b/src/Service/CommentService.php index 73506a7c44672fdada4c2ffef76735956df8d65e..92ee07221d9cada4f9a63086ab026dc52acdbb2a 100755 --- a/src/Service/CommentService.php +++ b/src/Service/CommentService.php @@ -6,6 +6,7 @@ use App\Entity\Article; use App\Entity\Comment; use App\Entity\User; use App\Model\CommentModel; +use App\Model\CommentResponseModel; use App\Model\MultipleCommentModel; use DateTimeImmutable; use Exception; @@ -31,21 +32,21 @@ class CommentService ->setAuthor($user) ->setArticle($this->entityManager->find(Article::class, $commentModel->getArticleId())) ->setBody($commentModel->getBody()) - ->setCreationTime(new DateTimeImmutable($commentModel->getCreationTime())) + ->setCreationTime(new DateTimeImmutable()) ->setChangingTime(new DateTimeImmutable()); $this->entityManager->flush(); return not_null($comment->getId()); } - public function getComment(int $id): CommentModel + public function getComment(int $id): CommentResponseModel { $comment = $this->entityManager->find(Comment::class, $id); if ($comment === null) { throw new RuntimeException("Comment with id=$id not found"); } - return new CommentModel( + return new CommentResponseModel( $comment->getId(), - $comment->getAuthorId(), + $comment->getAuthor()->getId(), $comment->getArticleId(), $comment->getBody(), $comment->getCreationTime(), @@ -59,7 +60,7 @@ class CommentService if ($comment === null) { throw new RuntimeException("Comment with id=$id not found"); } - if ($user->getId() !== $comment->getAuthorId()) { + if ($user->getId() !== $comment->getAuthor()->getId()) { throw new ForbiddenOverwriteException('You are not the author'); } $comment @@ -68,14 +69,14 @@ class CommentService $this->entityManager->flush(); } - public function getUserComments(string $id) + public function getUserComments(int $id): MultipleCommentModel { $comments = $this->entityManager->findBy(Comment::class, ['author' => $id]); $commentsModels = []; foreach ($comments as $comment) { - $commentsModels[] = new CommentModel( + $commentsModels[] = new CommentResponseModel( $comment->getId(), - $comment->getAuthorId(), + $comment->getAuthor()->getId(), $comment->getArticleId(), $comment->getBody(), $comment->getCreationTime(), diff --git a/src/Service/TagService.php b/src/Service/TagService.php index d753b71a5bca234d48cb260d581e34e6724f23e0..1b749d60b93765de2431cbdfb8032bd57abac97f 100755 --- a/src/Service/TagService.php +++ b/src/Service/TagService.php @@ -4,6 +4,7 @@ namespace App\Service; use App\Entity\Tag; use App\Model\TagModel; +use App\Model\TagResponseModel; use Kaa\Component\Database\EntityManager\EntityManagerInterface; use RuntimeException; @@ -24,13 +25,13 @@ class TagService return not_null($tag->getId()); } - public function getTag(int $tagId): TagModel + public function getTag(int $tagId): TagResponseModel { $tag = $this->entityManager->find(Tag::class, $tagId); if ($tag === null) { throw new RuntimeException("Tag with {$tagId} not found!"); } - return new TagModel( + return new TagResponseModel( $tag->getId(), $tag->getTitle() ); diff --git a/src/Service/TopicService.php b/src/Service/TopicService.php index 7ef3dba2d3422f80557b7e3144820d383eed49c6..401761f81723afd0271863ac5406f945f130882b 100755 --- a/src/Service/TopicService.php +++ b/src/Service/TopicService.php @@ -2,10 +2,12 @@ namespace App\Service; +use App\Entity\Admission; use App\Entity\Topic; use App\Entity\User; use App\Model\MultipleTopicsModel; use App\Model\TopicModel; +use App\Model\TopicResponseModel; use DateTimeImmutable; use Exception; use Kaa\Component\Database\EntityManager\EntityManagerInterface; @@ -25,24 +27,39 @@ class TopicService */ public function createTopic(TopicModel $topicModel): int { + if ($topicModel->getType() === 'PRIVATE') { + foreach ($topicModel->getAcceptedUsers() as $user) { + if (!$this->entityManager->find(User::class, $user)) { + throw new RuntimeException("Topic contains wrong user with id={$user}"); + } + } + } $topic = $this->entityManager->new(Topic::class) ->setAuthor($this->entityManager->find(User::class, $topicModel->getAuthorId())) ->setTitle($topicModel->getTitle()) - ->setCreationTime(new DateTimeImmutable($topicModel->getCreationTime())); + ->setType($topicModel->getType()) + ->setCreationTime(new DateTimeImmutable()); + foreach ($topicModel->getAcceptedUsers() as $user) { + $this->entityManager->new(Admission::class) + ->setTopic($topic) + ->setUser($this->entityManager->find(User::class, $user)); + } $this->entityManager->flush(); return not_null($topic->getId()); } - public function getTopic(int $id): TopicModel + public function getTopic(int $id): TopicResponseModel { $topic = $this->entityManager->find(Topic::class, $id); if ($topic === null) { throw new RuntimeException("Topic with id=$id not found"); } - return new TopicModel( + return new TopicResponseModel( $topic->getId(), - $topic->getAuthorId(), + $topic->getAuthor()->getId(), $topic->getTitle(), + $topic->getType(), + array_map(fn ($a) => $a->getUser()->getId(), $topic->getAcceptedUsers()), $topic->getCreationTime() ); } @@ -56,21 +73,42 @@ class TopicService if ($topic === null) { throw new RuntimeException("Topic with id=$id not found"); } + $userIds = array_map(function (Admission $t) { + return $t->getUser()->getId(); + }, $topic->getAcceptedUsers()); + $users = []; + foreach ($userIds as $userId) { + if (!in_array($userId, $topicModel->getAcceptedUsers())) { + $users[] = $userId; + } + } + foreach ($users as $userId) { + if (!$this->entityManager->find(User::class, $userId)) { + throw new RuntimeException("Topic contains wrong user id {$userId}"); + } + } $topic ->setTitle($topicModel->getTitle()) - ->setCreationTime(new DateTimeImmutable($topicModel->getCreationTime())); + ->setType($topicModel->getType()); + foreach ($users as $user) { + $this->entityManager->new(Admission::class) + ->setUser($this->entityManager->find(User::class, $user)) + ->setTopic($topic); + } $this->entityManager->flush(); } - public function getUserTopics(string $userId): MultipleTopicsModel + public function getUserTopics(int $userId): MultipleTopicsModel { $topics = $this->entityManager->findBy(Topic::class, ['author' => $userId]); $topicsModelArray = []; foreach ($topics as $topic) { - $topicsModelArray[] = new TopicModel( + $topicsModelArray[] = new TopicResponseModel( $topic->getId(), - $topic->getAuthorId(), + $topic->getAuthor()->getId(), $topic->getTitle(), + $topic->getType(), + array_map(fn ($a) => $a->getUser()->getId(), $topic->getAcceptedUsers()), $topic->getCreationTime() ); } diff --git a/src/Service/UserProvider.php b/src/Service/UserProvider.php index 263c6eaa356f6f05df1c9fea4c50c598b6c66909..a6c1cf15068cc53e4f628adb8dfd0be49012a99b 100755 --- a/src/Service/UserProvider.php +++ b/src/Service/UserProvider.php @@ -4,10 +4,6 @@ namespace App\Service; use App\Entity\User; -use App\Model\RegisterModel; -use App\Model\UserModel; -use DateTimeImmutable; -use Exception; use Kaa\Component\Database\EntityManager\EntityManagerInterface; use Kaa\Component\Security\UserInterface; use Kaa\Component\Security\UserProviderInterface; @@ -23,35 +19,6 @@ class UserProvider implements UserProviderInterface $this->entityManager = $entityManager; } - public function registerUser(RegisterModel $registerModel): int - { - $user = $this->entityManager->new(User::class) - ->setUsername($registerModel->getUsername()) - ->setEmail($registerModel->getEmail()) - ->setPassword($registerModel->getPassword()) - ->setCreationTime(new DateTimeImmutable()) - ->setChangingTime(new DateTimeImmutable()) - ->setRole(['USER']); - $this->entityManager->flush(); - return $user->getId(); - } - - /** - * @throws Exception - */ - public function createUser(UserModel $userModel): int - { - $user = $this->entityManager->new(User::class) - ->setUsername($userModel->getUsername()) - ->setEmail($userModel->getEmail()) - ->setPassword($userModel->getPassword()) - ->setCreationTime(new DateTimeImmutable($userModel->getCreationDate())) - ->setChangingTime(new DateTimeImmutable($userModel->getChangingDate())) - ->setRole($userModel->getRoles()); - $this->entityManager->flush(); - return $user->getId(); - } - public function getUser(string $identifier): UserInterface { $user = $this->entityManager->find(User::class, (int) $identifier); @@ -61,54 +28,4 @@ class UserProvider implements UserProviderInterface return $user; } - - public function getUserById(int $id): UserModel - { - $user = $this->entityManager->find(User::class, $id); - if ($user === null) { - throw new RuntimeException("User with id={$id} not found"); - } - - return new UserModel( - $user->getId(), - $user->getUsername(), - $user->getEmail(), - $user->getPassword(), - $user->getRoles(), - $user->getCreationTime(), - $user->getChangingTime() - ); - } - - public function getCurrentUser(User $user): UserModel - { - return new UserModel( - $user->getId(), - $user->getUsername(), - $user->getEmail(), - $user->getPassword(), - $user->getRoles(), - $user->getCreationTime(), - $user->getChangingTime() - ); - } - - /** - * @throws Exception - */ - public function updateUser(UserModel $userModel, int $id): void - { - $user = $this->entityManager->find(User::class, $id); - if ($user === null) { - throw new RuntimeException("User with id={$id} not found"); - } - $user - ->setUsername($userModel->getUsername()) - ->setEmail($userModel->getEmail()) - ->setPassword($userModel->getPassword()) - ->setRole($userModel->getRoles()) - ->setCreationTime(new DateTimeImmutable($userModel->getCreationDate())) - ->setChangingTime(new DateTimeImmutable($userModel->getChangingDate())); - $this->entityManager->flush(); - } } diff --git a/src/Service/UserService.php b/src/Service/UserService.php new file mode 100755 index 0000000000000000000000000000000000000000..9b8618b8bb34cea1bae12b36f5baf32525c38492 --- /dev/null +++ b/src/Service/UserService.php @@ -0,0 +1,85 @@ +<?php + +namespace App\Service; + +use App\Entity\User; +use App\Model\UserModel; +use App\Model\UserResponseModel; +use DateTimeImmutable; +use Exception; +use Kaa\Component\Database\EntityManager\EntityManagerInterface; +use RuntimeException; + +class UserService +{ + private EntityManagerInterface $entityManager; + + public function __construct( + EntityManagerInterface $entityManager + ) { + $this->entityManager = $entityManager; + } + + /** + * @throws Exception + */ + public function createUser(UserModel $userModel): int + { + $user = $this->entityManager->new(User::class) + ->setUsername($userModel->getUsername()) + ->setEmail($userModel->getEmail()) + ->setPassword(strtoupper(hash('sha256', $userModel->getPassword()))) + ->setCreationTime(new DateTimeImmutable()) + ->setChangingTime(new DateTimeImmutable()) + ->setRoles($userModel->getRoles()); + $this->entityManager->flush(); + return $user->getId(); + } + + public function getUserById(int $id): UserResponseModel + { + $user = $this->entityManager->find(User::class, $id); + if ($user === null) { + throw new RuntimeException("User with id={$id} not found"); + } + + return new UserResponseModel( + $user->getId(), + $user->getUsername(), + $user->getEmail(), + $user->getRoles(), + $user->getCreationTime(), + $user->getChangingTime() + ); + } + + public function getUserByEntity(User $user): UserResponseModel + { + return new UserResponseModel( + $user->getId(), + $user->getUsername(), + $user->getEmail(), + $user->getRoles(), + $user->getCreationTime(), + $user->getChangingTime() + ); + } + + /** + * @throws Exception + */ + public function updateUser(UserModel $userModel, int $id): void + { + $user = $this->entityManager->find(User::class, $id); + if ($user === null) { + throw new RuntimeException("User with id={$id} not found"); + } + $user + ->setUsername($userModel->getUsername()) + ->setEmail($userModel->getEmail()) + ->setPassword($userModel->getPassword()) + ->setRoles($userModel->getRoles()) + ->setChangingTime(new DateTimeImmutable()); + $this->entityManager->flush(); + } +}