MaintenanceAllowIpsCommand.php 3.78 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Magento\Setup\Console\Command;

use Magento\Framework\App\MaintenanceMode;
use Magento\Framework\Module\ModuleList;
use Magento\Setup\Validator\IpValidator;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
 * Command for setting allowed IPs in maintenance mode
 */
class MaintenanceAllowIpsCommand extends AbstractSetupCommand
{
    /**
     * Names of input arguments or options
     */
    const INPUT_KEY_IP = 'ip';
    const INPUT_KEY_NONE = 'none';
    const INPUT_KEY_ADD = 'add';

    /**
     * @var MaintenanceMode
     */
    private $maintenanceMode;

    /**
     * @var IpValidator
     */
    private $ipValidator;

    /**
     * Constructor
     *
     * @param MaintenanceMode $maintenanceMode
     * @param IpValidator $ipValidator
     */
    public function __construct(MaintenanceMode $maintenanceMode, IpValidator $ipValidator)
    {
        $this->maintenanceMode = $maintenanceMode;
        $this->ipValidator = $ipValidator;
        parent::__construct();
    }

    /**
     * Initialization of the command
     *
     * @return void
     */
    protected function configure()
    {
        $arguments = [
            new InputArgument(
                self::INPUT_KEY_IP,
                InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
                'Allowed IP addresses'
            ),
        ];
        $options = [
            new InputOption(
                self::INPUT_KEY_NONE,
                null,
                InputOption::VALUE_NONE,
                'Clear allowed IP addresses'
            ),
            new InputOption(
                self::INPUT_KEY_ADD,
                null,
                InputOption::VALUE_NONE,
                'Add the IP address to existing list'
            ),
        ];
        $this->setName('maintenance:allow-ips')
            ->setDescription('Sets maintenance mode exempt IPs')
            ->setDefinition(array_merge($arguments, $options));
        parent::configure();
    }

    /**
     * {@inheritdoc}
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        if (!$input->getOption(self::INPUT_KEY_NONE)) {
            $addresses = $input->getArgument(self::INPUT_KEY_IP);
            $messages = $this->validate($addresses);
            if (!empty($messages)) {
                $output->writeln('<error>' . implode('</error>' . PHP_EOL . '<error>', $messages));
                // we must have an exit code higher than zero to indicate something was wrong
                return \Magento\Framework\Console\Cli::RETURN_FAILURE;
            }

            if (!empty($addresses)) {
                if ($input->getOption(self::INPUT_KEY_ADD)) {
                    $addresses = array_unique(array_merge($this->maintenanceMode->getAddressInfo(), $addresses));
                }
                $this->maintenanceMode->setAddresses(implode(',', $addresses));
                $output->writeln(
                    '<info>Set exempt IP-addresses: ' . implode(' ', $this->maintenanceMode->getAddressInfo()) .
                    '</info>'
                );
            }
        } else {
            $this->maintenanceMode->setAddresses('');
            $output->writeln('<info>Set exempt IP-addresses: none</info>');
        }
        return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
    }

    /**
     * Validates IP addresses and return error messages
     *
     * @param string[] $addresses
     * @return string[]
     */
    protected function validate(array $addresses)
    {
        return $this->ipValidator->validateIps($addresses, false);
    }
}