diff --git a/kernel/modules/secure/controllers/SecureController.php b/kernel/modules/secure/controllers/SecureController.php index 1086058..7466afc 100644 --- a/kernel/modules/secure/controllers/SecureController.php +++ b/kernel/modules/secure/controllers/SecureController.php @@ -72,35 +72,37 @@ class SecureController extends AdminController */ public function actionEmailAuth(): void { + $mailing = new Mailing(); + $loginForm = new LoginEmailForm(); $loginForm->load($_REQUEST); - $email = $loginForm->getItem("email"); - $code = mt_rand(100000, 999999); + $email = $loginForm->getItem("email"); $user = $this->userService->getByField('email', $email); + if (!$user){ - $newUser = new User(); - $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(); + $password = md5(microtime() . bin2hex(random_bytes(10)) . time()); + UserService::createUserByEmailAndPassword($email, $password); + $user = $this->userService->getByField('email', $email); + + $mailing->send_html("register_by_code.php", ['code' => $user->auth_code, 'password' => $password], [ + 'address' => $email, + 'subject' => "Код регистрации", + "from_name" => $_ENV['APP_NAME'] + ]); } else { - $user->auth_code = $code; + $user->auth_code = mt_rand(100000, 999999);; $user->auth_code_expires_at = date("Y-m-d H:i:s", strtotime("+5 minutes")); $user->save(); + + $mailing->send_html("login_by_code.php", ['code' => $user->auth_code], [ + 'address' => $email, + 'subject' => "Код авторизации", + "from_name" => $_ENV['APP_NAME'] + ]); } - $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]); } diff --git a/kernel/modules/user/service/UserService.php b/kernel/modules/user/service/UserService.php index 077edab..a504ade 100644 --- a/kernel/modules/user/service/UserService.php +++ b/kernel/modules/user/service/UserService.php @@ -85,4 +85,15 @@ class UserService return $this->getByField("access_token", $token); } + public static function createUserByEmailAndPassword(string $email, string $password): void + { + $user = new User(); + $user->email = $email; + $user->username = $email; + $user->password_hash = password_hash($password, PASSWORD_DEFAULT); + $user->auth_code = mt_rand(100000, 999999); + $user->auth_code_expires_at = date("Y-m-d H:i:s", strtotime("+5 minutes")); + $user->save(); + } + } \ No newline at end of file diff --git a/kernel/views/mailing/register_by_code.php b/kernel/views/mailing/register_by_code.php new file mode 100644 index 0000000..4dc6a54 --- /dev/null +++ b/kernel/views/mailing/register_by_code.php @@ -0,0 +1,15 @@ + + +

+ Код подтверждения:

+ Ваш пароль: +

+ +

+ Если вы не запрашивали код, проигнорируйте данное письмо. +

\ No newline at end of file