diff --git a/bootstrap/secure.php b/bootstrap/secure.php index 58f17bd..4ee1ec7 100644 --- a/bootstrap/secure.php +++ b/bootstrap/secure.php @@ -1,7 +1,7 @@ 'login_password', // login_password, email_code + 'web_auth_type' => 'email_code', // login_password, email_code 'token_type' => 'JWT', // random_bytes, md5, crypt, hash, JWT 'token_expired_time' => "+30 days", // +1 day ]; diff --git a/kernel/modules/module_shop_client/views/login_at_module_shop.php b/kernel/modules/module_shop_client/views/login_at_module_shop.php index c704b5e..b635740 100644 --- a/kernel/modules/module_shop_client/views/login_at_module_shop.php +++ b/kernel/modules/module_shop_client/views/login_at_module_shop.php @@ -4,7 +4,7 @@ use itguild\forms\ActiveForm; \kernel\widgets\ModuleTabsWidget::create()->run(); -echo \kernel\helpers\Html::h(2, "Форма авторизации"); +echo \kernel\helpers\Html::h(2, "Форма авторизации/регистрации"); $form = new ActiveForm(); $form->beginForm("/admin/module_shop_client/auth/"); diff --git a/kernel/modules/secure/controllers/SecureController.php b/kernel/modules/secure/controllers/SecureController.php index bdea8ab..dac13b8 100644 --- a/kernel/modules/secure/controllers/SecureController.php +++ b/kernel/modules/secure/controllers/SecureController.php @@ -4,11 +4,18 @@ namespace kernel\modules\secure\controllers; use JetBrains\PhpStorm\NoReturn; use kernel\AdminController; +use kernel\App; use kernel\Flash; use kernel\helpers\Debug; +use kernel\Mailing; +use kernel\modules\secure\models\forms\LoginEmailForm; use kernel\modules\secure\models\forms\LoginForm; use kernel\modules\secure\models\forms\RegisterForm; +use kernel\modules\user\models\User; use kernel\modules\user\service\UserService; +use kernel\Request; +use PHPMailer\PHPMailer\Exception; +use Random\RandomException; class SecureController extends AdminController { @@ -24,7 +31,12 @@ class SecureController extends AdminController public function actionLogin(): void { - $this->cgView->render('login.php'); + $this->cgView->render(match (App::$secure['web_auth_type']) { + "login_password" => "login.php", + "email_code" => "email_login.php", + }); + +// $this->cgView->render('login.php'); } #[NoReturn] public function actionAuth(): void @@ -54,14 +66,78 @@ class SecureController extends AdminController } } + /** + * @throws RandomException + * @throws Exception + */ + public function actionEmailAuth(): void + { + $loginForm = new LoginEmailForm(); + $loginForm->load($_REQUEST); + $email = $loginForm->getItem("email"); + + $user = $this->userService->getByField('email', $email); + if (!$user){ + $newUser = new User(); + $code = mt_rand(100000, 999999); + $newUser->email = $email; + $newUser->username = substr($email, 0, strpos($email, "@")); + $newUser->password_hash = password_hash(md5(microtime() . bin2hex(random_bytes(10)) . time()), PASSWORD_DEFAULT); + $newUser->auth_code = $code; + $newUser->auth_code_expires_at = date("Y-m-d H:i:s", strtotime("+5 minutes")); + $newUser->save(); + } else { + $code = mt_rand(100000, 999999); + $user->auth_code = $code; + $user->auth_code_expires_at = date("Y-m-d H:i:s", strtotime("+5 minutes")); + $user->save(); + } + + $mailing = new Mailing(); + $mailing->send_html("login_by_code.php", ['code' => $code], [ + 'address' => $email, + 'subject' => "Код авторизации", + "from_name" => $_ENV['APP_NAME'] + ]); + + setcookie('user_email', $email, time()+60*15, '/', $_SERVER['SERVER_NAME'], false); + + $this->cgView->render("enter_code.php", ['email' => $email]); + } + + /** + * @throws Exception + */ + public function actionCodeCheck(): void + { + $request = new Request(); + + if (isset($_COOKIE['user_email'])) { + $user = User::where('email', $_COOKIE["user_email"])->first(); + if (!$user) { + throw new exception("User not found."); + } + $code = $request->post("code"); + if ($user->auth_code == $code && time() <= strtotime($user->auth_code_expires_at)) { + setcookie('user_id', $user->id, time() + 60 * 60 * 24, '/', $_SERVER['SERVER_NAME'], false); + $this->redirect("/admin", code: 302); + } else { + Flash::setMessage("error", "Wrong code."); + $this->redirect("/admin/login", code: 302); + } + + } + } + #[NoReturn] public function actionLogout(): void { unset($_COOKIE['user_id']); setcookie('user_id', "", -1, '/', ".".$_SERVER['SERVER_NAME'], false); + setcookie('user_email', "", -1, '/', ".".$_SERVER['SERVER_NAME'], false); $this->redirect("/", code: 302); } - public function actionRegister() + public function actionRegister(): void { $this->cgView->render('register.php'); } diff --git a/kernel/modules/secure/models/forms/LoginEmailForm.php b/kernel/modules/secure/models/forms/LoginEmailForm.php new file mode 100644 index 0000000..201817a --- /dev/null +++ b/kernel/modules/secure/models/forms/LoginEmailForm.php @@ -0,0 +1,17 @@ + 'required|string|email|max255', + ]; + } + +} \ No newline at end of file diff --git a/kernel/modules/secure/routs/secure.php b/kernel/modules/secure/routs/secure.php index 0377475..c1fa0fa 100644 --- a/kernel/modules/secure/routs/secure.php +++ b/kernel/modules/secure/routs/secure.php @@ -14,8 +14,10 @@ App::$collector->group(["prefix" => "admin"], function (RouteCollector $router){ App::$collector->get('/login', [\kernel\modules\secure\controllers\SecureController::class, 'actionLogin']); App::$collector->get('/logout', [\kernel\modules\secure\controllers\SecureController::class, 'actionLogout']); App::$collector->post('/auth', [\kernel\modules\secure\controllers\SecureController::class, 'actionAuth']); + App::$collector->post('/email_auth', [\kernel\modules\secure\controllers\SecureController::class, 'actionEmailAuth']); App::$collector->get('/register', [\kernel\modules\secure\controllers\SecureController::class, 'actionRegister']); App::$collector->post('/registration', [\kernel\modules\secure\controllers\SecureController::class, 'actionRegistration']); + App::$collector->post('/code_check', [\kernel\modules\secure\controllers\SecureController::class, 'actionCodeCheck']); }); App::$collector->group(["prefix" => "api"], function (CgRouteCollector $router){ diff --git a/kernel/modules/secure/views/email_login.php b/kernel/modules/secure/views/email_login.php new file mode 100644 index 0000000..eadc71b --- /dev/null +++ b/kernel/modules/secure/views/email_login.php @@ -0,0 +1,48 @@ + + + + + + + + + + + + Форма авторизации/регистрации + + + + + + + Email + + + + Отправить + + + + + + + + + \ No newline at end of file diff --git a/kernel/modules/secure/views/enter_code.php b/kernel/modules/secure/views/enter_code.php new file mode 100644 index 0000000..57aed3a --- /dev/null +++ b/kernel/modules/secure/views/enter_code.php @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + Введите код, отправленный на почту "" + + + + + + + Код подтверждения + + + + + Подтвердить + + + + Отправить код еще раз + + + + + + + + + + \ No newline at end of file diff --git a/kernel/modules/user/migrations/2024_09_23_125827_create_user_table.php b/kernel/modules/user/migrations/2024_09_23_125827_create_user_table.php index 774213a..4d2b7f5 100644 --- a/kernel/modules/user/migrations/2024_09_23_125827_create_user_table.php +++ b/kernel/modules/user/migrations/2024_09_23_125827_create_user_table.php @@ -19,6 +19,8 @@ return new class extends Migration $table->string('email', 255); $table->string('password_hash', 255); $table->integer('role')->default(1); + $table->integer('auth_code')->default(1); + $table->dateTime('auth_code_expires_at')->nullable(true); $table->string('access_token', 255)->nullable(true); $table->dateTime('access_token_expires_at')->nullable(true); $table->timestamps(); diff --git a/kernel/modules/user/models/User.php b/kernel/modules/user/models/User.php index 3dd03c1..4c9da2b 100644 --- a/kernel/modules/user/models/User.php +++ b/kernel/modules/user/models/User.php @@ -7,6 +7,8 @@ use Illuminate\Database\Eloquent\Model; * @property string $username * @property string $email * @property string $password_hash + * @property int $auth_code + * @property string $auth_code_expires_at * @property string $access_token * @property string $access_token_expires_at * @method static find($id) @@ -17,7 +19,7 @@ class User extends Model { const ADMIN_USER_ROLE = 9; protected $table = 'user'; - protected $fillable = ['username', 'email', 'password_hash', 'role', 'access_token', 'access_token_expires_at']; + protected $fillable = ['username', 'email', 'password_hash', 'role', 'auth_code', 'auth_code_expires_at', 'access_token', 'access_token_expires_at']; protected array $dates = ['deleted at']; public static function labels(): array