add user
This commit is contained in:
parent
094e047a5c
commit
043a699c6d
@ -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)
|
||||
|
@ -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'];
|
||||
|
19
app/models/forms/CreateUserForm.php
Normal file
19
app/models/forms/CreateUserForm.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace app\models\forms;
|
||||
|
||||
use kernel\FormModel;
|
||||
|
||||
class CreateUserForm extends FormModel
|
||||
{
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'username' => 'required|min-str-len:5|max-str-len:30',
|
||||
'password' => 'required|min-str-len:5|max-str-len:30',
|
||||
'email' => 'required|email'
|
||||
];
|
||||
}
|
||||
|
||||
}
|
20
app/services/UserService.php
Normal file
20
app/services/UserService.php
Normal file
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace app\services;
|
||||
|
||||
use app\models\User;
|
||||
use kernel\FormModel;
|
||||
|
||||
class UserService
|
||||
{
|
||||
|
||||
public function create(FormModel $form_model): bool
|
||||
{
|
||||
$model = new User();
|
||||
$model->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();
|
||||
}
|
||||
|
||||
}
|
@ -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": {
|
||||
|
91
composer.lock
generated
91
composer.lock
generated
@ -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"
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
64
kernel/FormModel.php
Normal file
64
kernel/FormModel.php
Normal file
@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace kernel;
|
||||
|
||||
use app\helpers\Debug;
|
||||
use MadeSimple\Validator\Validator;
|
||||
|
||||
class FormModel
|
||||
{
|
||||
protected Validator $validator;
|
||||
|
||||
protected array $data = [];
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->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();
|
||||
}
|
||||
|
||||
}
|
@ -9,7 +9,7 @@
|
||||
|
||||
Пароль:<br>
|
||||
<label>
|
||||
<input type = "text" name = "password_hash" required size="50" placeholder="Пароль">
|
||||
<input type = "text" name = "password" placeholder="Пароль">
|
||||
</label> <br> <br>
|
||||
|
||||
Email адрес: <br>
|
||||
|
Loading…
Reference in New Issue
Block a user