From 043a699c6de3ed4e56d34c9f66e36d3450d4c6ce Mon Sep 17 00:00:00 2001 From: Kavalar Date: Wed, 24 Jul 2024 14:07:45 +0300 Subject: [PATCH] add user --- app/controllers/UserController.php | 25 +++----- app/models/User.php | 5 ++ app/models/forms/CreateUserForm.php | 19 ++++++ app/services/UserService.php | 20 +++++++ composer.json | 3 +- composer.lock | 91 ++++++++++++++++++++++++++++- kernel/Controller.php | 5 ++ kernel/FormModel.php | 64 ++++++++++++++++++++ views/user_create.html.twig | 2 +- 9 files changed, 214 insertions(+), 20 deletions(-) create mode 100644 app/models/forms/CreateUserForm.php create mode 100644 app/services/UserService.php create mode 100644 kernel/FormModel.php diff --git a/app/controllers/UserController.php b/app/controllers/UserController.php index 0bcf755..d0eb0e2 100644 --- a/app/controllers/UserController.php +++ b/app/controllers/UserController.php @@ -4,8 +4,10 @@ namespace app\controllers; use app\foo; use app\helpers\Debug; +use app\models\forms\CreateUserForm; use app\models\Question; use app\models\User; +use app\services\UserService; use app\tables\columns\UserViewActionColumn; use Exception; use http\Message; @@ -24,23 +26,14 @@ class UserController extends Controller{ public function actionAdd(): void { - $_REQUEST["password_hash"] = password_hash($_REQUEST["password_hash"], PASSWORD_DEFAULT); - - foreach ($_REQUEST as $key => $value) - { -// Debug::prn(User::where([$key => $value])->get()->toArray()); -// Debug::prn($user); -// Debug::dd($user->toArray()); - if (User::where([$key => $value])->get()->toArray() !== null) - { - header("Location: http://itguild-framework.loc/admin/user/create"); - } - else - { - User::create($_REQUEST); - header("Location: http://itguild-framework.loc/admin/user/"); - } + $userForm = new CreateUserForm(); + $userService = new UserService(); + $userForm->load($_REQUEST); + if ($userForm->validate()){ + $userService->create($userForm); + $this->redirect("/admin/user"); } + $this->redirect("/admin/user/create"); } public function actionQuestionCount($user_id) diff --git a/app/models/User.php b/app/models/User.php index 6f09d5f..aed47c8 100644 --- a/app/models/User.php +++ b/app/models/User.php @@ -2,6 +2,11 @@ namespace app\models; use Illuminate\Database\Eloquent\Model; +/** + * @property string $username + * @property string $email + * @property string $password_hash + */ class User extends Model { protected $table = 'user'; protected $fillable = ['username', 'email', 'password_hash', 'role']; diff --git a/app/models/forms/CreateUserForm.php b/app/models/forms/CreateUserForm.php new file mode 100644 index 0000000..280a83d --- /dev/null +++ b/app/models/forms/CreateUserForm.php @@ -0,0 +1,19 @@ + 'required|min-str-len:5|max-str-len:30', + 'password' => 'required|min-str-len:5|max-str-len:30', + 'email' => 'required|email' + ]; + } + +} \ No newline at end of file diff --git a/app/services/UserService.php b/app/services/UserService.php new file mode 100644 index 0000000..53b82ba --- /dev/null +++ b/app/services/UserService.php @@ -0,0 +1,20 @@ +username = $form_model->getItem('username'); + $model->email = $form_model->getItem('email'); + $model->password_hash = password_hash($form_model->getItem('password'), PASSWORD_DEFAULT); + return $model->save(); + } + +} \ No newline at end of file diff --git a/composer.json b/composer.json index 2490b43..9b8953e 100644 --- a/composer.json +++ b/composer.json @@ -8,7 +8,8 @@ "vlucas/phpdotenv": "^5.6", "twig/twig": "^3.0", "twbs/bootstrap": "5.0.2", - "itguild/tables": "^0.1.3" + "itguild/tables": "^0.1.3", + "madesimple/php-form-validator": "^2.9" }, "autoload": { "psr-4": { diff --git a/composer.lock b/composer.lock index 31c2ea9..3437802 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": "1ab10f0d80bf16426c5a0cbba3924d9a", + "content-hash": "f638e86e2b30657f939e5afa8d66beb9", "packages": [ { "name": "brick/math", @@ -749,6 +749,93 @@ ], "time": "2024-07-23T12:17:16+00:00" }, + { + "name": "madesimple/php-arrays", + "version": "v2.1.0", + "source": { + "type": "git", + "url": "https://github.com/pdscopes/php-arrays.git", + "reference": "e0b921ab295a965dbc125c33bdde864721c59f08" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/pdscopes/php-arrays/zipball/e0b921ab295a965dbc125c33bdde864721c59f08", + "reference": "e0b921ab295a965dbc125c33bdde864721c59f08", + "shasum": "" + }, + "require": { + "php": ">=7.1" + }, + "require-dev": { + "phpunit/phpunit": "^9.5" + }, + "type": "library", + "autoload": { + "psr-4": { + "MadeSimple\\Arrays\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Peter Scopes", + "email": "peter.scopes@gmail.com" + } + ], + "description": "Helper functions for manipulating arrays", + "support": { + "issues": "https://github.com/pdscopes/php-arrays/issues", + "source": "https://github.com/pdscopes/php-arrays/tree/v2.1.0" + }, + "time": "2022-02-25T14:48:55+00:00" + }, + { + "name": "madesimple/php-form-validator", + "version": "v2.9.0", + "source": { + "type": "git", + "url": "https://github.com/pdscopes/php-form-validator.git", + "reference": "d41665ab5cf7f028ab1eae204ab9f2f72dfeab5a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/pdscopes/php-form-validator/zipball/d41665ab5cf7f028ab1eae204ab9f2f72dfeab5a", + "reference": "d41665ab5cf7f028ab1eae204ab9f2f72dfeab5a", + "shasum": "" + }, + "require": { + "madesimple/php-arrays": "^2.0", + "php": ">=7.1" + }, + "require-dev": { + "phpunit/phpunit": "^9.5" + }, + "type": "library", + "autoload": { + "psr-4": { + "MadeSimple\\Validator\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Peter Scopes", + "email": "peter.scopes@gmail.com" + } + ], + "description": "Simple, extendable form validator for multi-dimensional forms", + "support": { + "issues": "https://github.com/pdscopes/php-form-validator/issues", + "source": "https://github.com/pdscopes/php-form-validator/tree/v2.9.0" + }, + "time": "2022-07-09T12:08:20+00:00" + }, { "name": "nesbot/carbon", "version": "3.7.0", @@ -2006,5 +2093,5 @@ "prefer-lowest": false, "platform": [], "platform-dev": [], - "plugin-api-version": "2.6.0" + "plugin-api-version": "2.3.0" } diff --git a/kernel/Controller.php b/kernel/Controller.php index 0d82f01..cdfb5ca 100644 --- a/kernel/Controller.php +++ b/kernel/Controller.php @@ -12,4 +12,9 @@ class Controller $this->loader = new \Twig\Loader\FilesystemLoader(ROOT_DIR . $_ENV['VIEWS_PATH']); $this->twig = new \Twig\Environment($this->loader, ['cache' => ROOT_DIR . $_ENV['VIEWS_CACHE_PATH']]); } + + public function redirect(string $url): void + { + header("Location: " . $url); + } } \ No newline at end of file diff --git a/kernel/FormModel.php b/kernel/FormModel.php new file mode 100644 index 0000000..615abf3 --- /dev/null +++ b/kernel/FormModel.php @@ -0,0 +1,64 @@ +validator = new Validator(); + } + + public function rules(): array + { + return []; + } + + public function load(array $array): void + { + $rules = $this->rules(); + foreach ($array as $key => $item) { + if (isset($rules[$key])) { + $this->data[$key] = $item; + } + } + } + + public function getData(): array + { + return $this->data; + } + + public function getItem(string $name) + { + if (isset($this->data[$name])){ + return $this->data[$name]; + } + + return null; + } + + public function validate(): bool + { + $res = $this->validator->validate($this->data, $this->rules()); + if (!$res) { + return true; + } + + return false; + //return $this->validator->validate($this->data, $this->rules()); + } + + public function getErrors(): array + { + return $this->validator->getProcessedErrors(); + } + +} \ No newline at end of file diff --git a/views/user_create.html.twig b/views/user_create.html.twig index b69a254..45d4e71 100644 --- a/views/user_create.html.twig +++ b/views/user_create.html.twig @@ -9,7 +9,7 @@ Пароль:


Email адрес: