83 lines
2.6 KiB
PHP
83 lines
2.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Jinove\Vinoteca\Controller;
|
|
|
|
use Bitrix\Main\Application;
|
|
use Bitrix\Main\Engine\ActionFilter\Csrf;
|
|
use Bitrix\Main\Engine\ActionFilter\HttpMethod;
|
|
use Bitrix\Main\Engine\Controller;
|
|
use Bitrix\Main\Error;
|
|
use Bitrix\Main\UserTable;
|
|
use Jinove\Vinoteca\Controller\Filter\FormGuard;
|
|
use Jinove\Vinoteca\Redirect;
|
|
|
|
class Auth extends Controller
|
|
{
|
|
private const MAX_FAILS = 5;
|
|
private const BLOCK_SECONDS = 600;
|
|
|
|
public function configureActions(): array
|
|
{
|
|
return [
|
|
"login" => [
|
|
"prefilters" => [new HttpMethod([HttpMethod::METHOD_POST]), new Csrf(), new FormGuard(FormGuard::TIMING_ELAPSED)],
|
|
],
|
|
];
|
|
}
|
|
|
|
public function loginAction(
|
|
string $email,
|
|
string $password,
|
|
string $remember = "",
|
|
string $consent = "",
|
|
string $back = "",
|
|
string $lang = "en",
|
|
string $website = "",
|
|
int $elapsed = 0,
|
|
string $altcha = ""
|
|
): ?array {
|
|
$lang = Redirect::language($lang);
|
|
if ($website !== "") {
|
|
return ["ok" => true, "redirect" => Redirect::home($lang)];
|
|
}
|
|
$session = Application::getInstance()->getSession();
|
|
if ((int)($session->get("vino_login_block") ?? 0) > time()) {
|
|
$this->addError(new Error("Too many attempts", "too_often"));
|
|
|
|
return null;
|
|
}
|
|
$email = mb_strtolower(trim($email));
|
|
if ($consent !== "1" || !check_email($email) || $password === "" || mb_strlen($password) > 200) {
|
|
$this->addError(new Error("Invalid credentials", "invalid"));
|
|
|
|
return null;
|
|
}
|
|
$user = UserTable::getRow([
|
|
"select" => ["LOGIN"],
|
|
"filter" => ["=EMAIL" => $email, "=ACTIVE" => "Y"],
|
|
]);
|
|
$logged = false;
|
|
if ($user !== null) {
|
|
global $USER;
|
|
$logged = $USER->Login((string)$user["LOGIN"], $password, $remember === "1" ? "Y" : "N") === true;
|
|
}
|
|
if (!$logged) {
|
|
$fails = (int)($session->get("vino_login_fails") ?? 0) + 1;
|
|
if ($fails >= self::MAX_FAILS) {
|
|
$session->set("vino_login_block", time() + self::BLOCK_SECONDS);
|
|
$fails = 0;
|
|
}
|
|
$session->set("vino_login_fails", $fails);
|
|
$this->addError(new Error("Invalid credentials", "invalid"));
|
|
|
|
return null;
|
|
}
|
|
$session->remove("vino_login_fails");
|
|
$session->remove("vino_login_block");
|
|
|
|
return ["ok" => true, "redirect" => Redirect::back($back, $lang)];
|
|
}
|
|
}
|