|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * SuiteCRM is a customer relationship management program developed by SalesAgility Ltd. |
| 4 | + * Copyright (C) 2023 SalesAgility Ltd. |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or modify it under |
| 7 | + * the terms of the GNU Affero General Public License version 3 as published by the |
| 8 | + * Free Software Foundation with the addition of the following permission added |
| 9 | + * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK |
| 10 | + * IN WHICH THE COPYRIGHT IS OWNED BY SALESAGILITY, SALESAGILITY DISCLAIMS THE |
| 11 | + * WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS. |
| 12 | + * |
| 13 | + * This program is distributed in the hope that it will be useful, but WITHOUT |
| 14 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 15 | + * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more |
| 16 | + * details. |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU Affero General Public License |
| 19 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | + * |
| 21 | + * In accordance with Section 7(b) of the GNU Affero General Public License |
| 22 | + * version 3, these Appropriate Legal Notices must retain the display of the |
| 23 | + * "Supercharged by SuiteCRM" logo. If the display of the logos is not reasonably |
| 24 | + * feasible for technical reasons, the Appropriate Legal Notices must display |
| 25 | + * the words "Supercharged by SuiteCRM". |
| 26 | + */ |
| 27 | + |
| 28 | +declare(strict_types=1); |
| 29 | + |
| 30 | +namespace App\Migrations; |
| 31 | + |
| 32 | +use Doctrine\DBAL\Schema\Schema; |
| 33 | +use Psr\Log\LoggerInterface; |
| 34 | +use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
| 35 | +use Symfony\Component\DependencyInjection\ContainerInterface; |
| 36 | + |
| 37 | +final class Version20230420135520 extends BaseMigration implements ContainerAwareInterface |
| 38 | +{ |
| 39 | + use EnvHandlingMigrationTrait; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var ContainerInterface |
| 43 | + */ |
| 44 | + protected $container; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var LoggerInterface |
| 48 | + */ |
| 49 | + protected $upgradeLogger; |
| 50 | + |
| 51 | + public function getDescription(): string |
| 52 | + { |
| 53 | + return 'Add login throttling interval to .env'; |
| 54 | + } |
| 55 | + |
| 56 | + public function isTransactional(): bool |
| 57 | + { |
| 58 | + return false; |
| 59 | + } |
| 60 | + |
| 61 | + public function up(Schema $schema): void |
| 62 | + { |
| 63 | + $envFile = $this->getProjectDir() . "/.env"; |
| 64 | + |
| 65 | + if (!file_exists($envFile)) { |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + $envContents = file_get_contents($envFile); |
| 70 | + |
| 71 | + $this->addLoginThrottlingConfig($envContents, $envFile); |
| 72 | + |
| 73 | + } |
| 74 | + |
| 75 | + public function down(Schema $schema): void |
| 76 | + { |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Check and add missing login throttling config |
| 81 | + * @param $envContents |
| 82 | + * @param string $envFile |
| 83 | + */ |
| 84 | + protected function addLoginThrottlingConfig(&$envContents, string $envFile): void |
| 85 | + { |
| 86 | + $properties = [ |
| 87 | + 'LOGIN_THROTTLING_IP_LOGIN_MAX_ATTEMPTS' => '50', |
| 88 | + 'LOGIN_THROTTLING_INTERVAL' => '"30 minutes"' |
| 89 | + ]; |
| 90 | + |
| 91 | + $wrapperStart = '###> login throttling ###'; |
| 92 | + $wrapperEnd = '###< login throttling ###'; |
| 93 | + |
| 94 | + $propertiesToAdd = $this->getContentToAdd($envContents, $properties, $wrapperStart, $wrapperEnd); |
| 95 | + if (!empty($propertiesToAdd)) { |
| 96 | + $envContents .= $propertiesToAdd; |
| 97 | + file_put_contents($envFile, $envContents); |
| 98 | + $this->log('Added LOGIN_THROTTLING_INTERVAL to .env.'); |
| 99 | + |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + $this->log('LOGIN_THROTTLING_INTERVAL already in .env, skipping.'); |
| 104 | + } |
| 105 | +} |
0 commit comments