Vinoteca: wine shop on 1C-Bitrix (module, template, components, pages)
This commit is contained in:
commit
52795535ae
586 files changed
+27619
No files matched your search
@@ -0,0 +1,152 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Jinove\Vinoteca\Controller;
|
||||
|
||||
use Bitrix\Main\Application;
|
||||
use Bitrix\Main\Data\Cache;
|
||||
use Bitrix\Main\Engine\ActionFilter\Csrf;
|
||||
use Bitrix\Main\Engine\ActionFilter\HttpMethod;
|
||||
use Bitrix\Main\Engine\Controller;
|
||||
use Bitrix\Main\Engine\CurrentUser;
|
||||
use Bitrix\Main\Error;
|
||||
use Bitrix\Main\Security\Password;
|
||||
use Bitrix\Main\UserTable;
|
||||
use Jinove\Vinoteca\Redirect;
|
||||
|
||||
class Profile extends Controller
|
||||
{
|
||||
private const MAX_PER_WINDOW = 5;
|
||||
private const WINDOW_SECONDS = 600;
|
||||
private const NAME_MAX = 50;
|
||||
private const PASSWORD_MIN = 8;
|
||||
private const PASSWORD_MAX = 200;
|
||||
|
||||
public function configureActions(): array
|
||||
{
|
||||
$filters = ["prefilters" => [new HttpMethod([HttpMethod::METHOD_POST]), new Csrf()]];
|
||||
|
||||
return ["save" => $filters, "password" => $filters, "logout" => $filters];
|
||||
}
|
||||
|
||||
public function saveAction(string $name, string $phone = ""): ?array
|
||||
{
|
||||
$userId = $this->userId();
|
||||
if ($userId <= 0) {
|
||||
$this->addError(new Error("Not authorized", "auth"));
|
||||
|
||||
return null;
|
||||
}
|
||||
$name = trim((string)preg_replace('/\s+/u', " ", $name));
|
||||
if ($name === "" || mb_strlen($name) > self::NAME_MAX) {
|
||||
$this->addError(new Error("Invalid name", "name"));
|
||||
|
||||
return null;
|
||||
}
|
||||
if (!mb_check_encoding($name, "UTF-8") || preg_match('/[<>\x00-\x1f\x7f]/', $name)) {
|
||||
$this->addError(new Error("Invalid name characters", "name_chars"));
|
||||
|
||||
return null;
|
||||
}
|
||||
$phone = trim($phone);
|
||||
if ($phone !== "" && !preg_match('/^\+\d{8,15}$/', $phone)) {
|
||||
$this->addError(new Error("Invalid phone", "phone"));
|
||||
|
||||
return null;
|
||||
}
|
||||
$user = new \CUser();
|
||||
if (!$user->Update($userId, ["NAME" => $name, "PERSONAL_PHONE" => $phone])) {
|
||||
$this->addError(new Error("Save failed", "failed"));
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
return ["ok" => true];
|
||||
}
|
||||
|
||||
public function passwordAction(string $current, string $password): ?array
|
||||
{
|
||||
$userId = $this->userId();
|
||||
if ($userId <= 0) {
|
||||
$this->addError(new Error("Not authorized", "auth"));
|
||||
|
||||
return null;
|
||||
}
|
||||
$session = Application::getInstance()->getSession();
|
||||
$now = time();
|
||||
$log = array_values(array_filter((array)($session->get("vino_profile_password_log") ?? []), static fn($at): bool => (int)$at > $now - self::WINDOW_SECONDS));
|
||||
if (count($log) >= self::MAX_PER_WINDOW) {
|
||||
$this->addError(new Error("Too many attempts", "too_often"));
|
||||
|
||||
return null;
|
||||
}
|
||||
$log[] = $now;
|
||||
$session->set("vino_profile_password_log", $log);
|
||||
if (!$this->mayTryPassword($userId)) {
|
||||
$this->addError(new Error("Too many attempts", "too_often"));
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
$row = UserTable::query()->setSelect(["PASSWORD", "EMAIL"])->setFilter(["=ID" => $userId])->enablePrivateFields()->fetch() ?: null;
|
||||
if ($row === null || $current === "" || !Password::equals((string)$row["PASSWORD"], $current)) {
|
||||
$this->addError(new Error("Wrong current password", "current"));
|
||||
|
||||
return null;
|
||||
}
|
||||
$length = mb_strlen($password);
|
||||
if ($length < self::PASSWORD_MIN || $length > self::PASSWORD_MAX || mb_strtolower($password) === mb_strtolower((string)$row["EMAIL"])) {
|
||||
$this->addError(new Error("Weak password", "password"));
|
||||
|
||||
return null;
|
||||
}
|
||||
if ($password === $current) {
|
||||
$this->addError(new Error("Same password", "same"));
|
||||
|
||||
return null;
|
||||
}
|
||||
$user = new \CUser();
|
||||
if (!$user->Update($userId, ["PASSWORD" => $password, "CONFIRM_PASSWORD" => $password])) {
|
||||
$this->addError(new Error("Password rejected", "policy"));
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
return ["ok" => true];
|
||||
}
|
||||
|
||||
public function logoutAction(string $lang = "en"): array
|
||||
{
|
||||
global $USER;
|
||||
$USER->Logout();
|
||||
|
||||
return ["ok" => true, "redirect" => Redirect::home($lang)];
|
||||
}
|
||||
|
||||
private function mayTryPassword(int $userId): bool
|
||||
{
|
||||
$cache = Cache::createInstance();
|
||||
$key = "vino_profile_password_" . $userId;
|
||||
$attempts = [];
|
||||
if ($cache->initCache(self::WINDOW_SECONDS, $key, "/vino/profile")) {
|
||||
$attempts = (array)($cache->getVars()["attempts"] ?? []);
|
||||
}
|
||||
$now = time();
|
||||
$attempts = array_values(array_filter($attempts, static fn($at): bool => (int)$at > $now - self::WINDOW_SECONDS));
|
||||
if (count($attempts) >= self::MAX_PER_WINDOW) {
|
||||
return false;
|
||||
}
|
||||
$attempts[] = $now;
|
||||
$cache->clean($key, "/vino/profile");
|
||||
$cache->startDataCache(self::WINDOW_SECONDS, $key, "/vino/profile");
|
||||
$cache->endDataCache(["attempts" => $attempts]);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private function userId(): int
|
||||
{
|
||||
return (int)CurrentUser::get()->getId();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user