diff --git a/README.md b/README.md index a0cbf0e..58dd4d8 100644 --- a/README.md +++ b/README.md @@ -97,7 +97,7 @@ of information. See more in the [events section](#events). This package overwrites the default `artisan down` command, with more options: ```bash -$ php artisan down [--message=MESSAGE] [--retry=RETRY] [--view=VIEW] +$ php artisan down [--message=MESSAGE] [--retry=RETRY] [--view=VIEW] [--allow*=ALLOW] ``` For example, @@ -202,7 +202,12 @@ exemptions that might be useful to your application. ##### IP Whitelist This exemption allows you to check the user's IP address against a whitelist. This is useful for -always including your office IP(s) so that your staff doesn't see the maintenance page. +always including your office IP(s) so that your staff doesn't see the maintenance page. This is similar +to the `allow` option that is offered in Laravel 5.6+ (and is backported to 5.5 with this package), however +the `allow` option requires you to pass the IPs every time, vs. this method allows for you to have IPs stored +in the configuration. Both methods can be used with each other, e.g. a static internal network should always be +allowed via the config, while an additional IP for a vendor or remote employee can temporarily be added via the +`allow` option. Configuration values included with this exemption are: @@ -330,4 +335,4 @@ protected $listen = [ ]; ``` -The original `message`, `time` the app went down, `retry` length, and `view` are all properties available on both events. \ No newline at end of file +The original `message`, `time` the app went down, `retry` length, `view`, and `allowed` are all properties available on both events. \ No newline at end of file diff --git a/src/MisterPhilip/MaintenanceMode/Console/Commands/StartMaintenanceCommand.php b/src/MisterPhilip/MaintenanceMode/Console/Commands/StartMaintenanceCommand.php index e1378c6..78e7e48 100644 --- a/src/MisterPhilip/MaintenanceMode/Console/Commands/StartMaintenanceCommand.php +++ b/src/MisterPhilip/MaintenanceMode/Console/Commands/StartMaintenanceCommand.php @@ -27,7 +27,8 @@ class StartMaintenanceCommand extends DownCommand */ protected $signature = 'down {--message= : The message for the maintenance mode. } {--retry= : The number of seconds after which the request may be retried.} - {--view= : The view to use for this instance of maintenance mode.}'; + {--view= : The view to use for this instance of maintenance mode.} + {--allow=* : IP or networks allowed to access the application while in maintenance mode.}'; /** * Execute the maintenance mode command @@ -46,7 +47,7 @@ public function handle() // Fire an event $payload = $this->getDownFilePayload(); - Event::fire(new MaintenanceModeEnabled($payload['time'], $payload['message'], $payload['view'], $payload['retry'])); + Event::fire(new MaintenanceModeEnabled($payload['time'], $payload['message'], $payload['view'], $payload['retry'], $payload['allowed'])); } /** @@ -59,6 +60,10 @@ protected function getDownFilePayload() // Get the Laravel file data & add ours (selected view) $data = parent::getDownFilePayload(); $data['view'] = $this->getSelectedView(); + + if(!isset($data['allowed'])) { + $data['allowed'] = $this->option('allow'); + } return $data; } @@ -87,4 +92,4 @@ protected function getSelectedView() return $view; } -} \ No newline at end of file +} diff --git a/src/MisterPhilip/MaintenanceMode/Events/MaintenanceModeChanged.php b/src/MisterPhilip/MaintenanceMode/Events/MaintenanceModeChanged.php index 6f84845..7a345c3 100644 --- a/src/MisterPhilip/MaintenanceMode/Events/MaintenanceModeChanged.php +++ b/src/MisterPhilip/MaintenanceMode/Events/MaintenanceModeChanged.php @@ -35,6 +35,13 @@ abstract class MaintenanceModeChanged */ public $retry; + /** + * Any allowed IPs or networks that can access the site during maintenance mode + * + * @var array + */ + public $allowed; + /** * Build a new event when Maintenance Mode is disabled * @@ -42,12 +49,14 @@ abstract class MaintenanceModeChanged * @param string|null $message * @param string|null $view * @param int|null $retry + * @param array $allowed */ - public function __construct($time = null, $message = null, $view = null, $retry = null) + public function __construct($time = null, $message = null, $view = null, $retry = null, $allowed = []) { $this->time = Carbon::createFromTimestamp($time); $this->message = $message; $this->view = $view; $this->retry = $retry; + $this->allowed = $allowed; } } diff --git a/src/MisterPhilip/MaintenanceMode/Http/Middleware/CheckForMaintenanceMode.php b/src/MisterPhilip/MaintenanceMode/Http/Middleware/CheckForMaintenanceMode.php index 4e2ed6a..d1419d2 100644 --- a/src/MisterPhilip/MaintenanceMode/Http/Middleware/CheckForMaintenanceMode.php +++ b/src/MisterPhilip/MaintenanceMode/Http/Middleware/CheckForMaintenanceMode.php @@ -5,6 +5,7 @@ use Carbon\Carbon; use Closure; use Illuminate\Http\Response; +use Symfony\Component\HttpFoundation\IpUtils; use Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode as LaravelMaintenanceMode; @@ -97,7 +98,7 @@ public function handle($request, Closure $next) // Inject the information into the views before the exception $this->injectIntoViews($info); - if(!$this->isExempt()) + if(!$this->isExempt($data, $request)) { // The user isn't exempt, so show them the error page throw new MaintenanceModeException($data['time'], $data['retry'], $data['message'], $data['view']); @@ -133,11 +134,14 @@ protected function injectIntoViews($info) /** * Check if a user is exempt from the maintenance mode page * + * @param $data + * @param $request + * * @return bool * @throws ExemptionDoesNotExist * @throws InvalidExemption */ - protected function isExempt() + protected function isExempt($data, $request) { // Grab all of the exemption classes to create/execute against $exemptions = $this->app['config']->get('maintenancemode.exemptions', []); @@ -166,6 +170,12 @@ protected function isExempt() throw new ExemptionDoesNotExist($this->app['translator']->get($this->language . '.exceptions.missing', ['class' => $className])); } } + + // Check for IP via the "allow" option + if (isset($data['allowed']) && IpUtils::checkIp($request->ip(), (array) $data['allowed'])) { + return true; + } + return false; } } \ No newline at end of file