Skip to content

Commit

Permalink
Merge pull request #19 from MisterPhilip/development
Browse files Browse the repository at this point in the history
Add allow option
  • Loading branch information
MisterPhilip authored Jul 5, 2018
2 parents 19c22e2 + 2ce4684 commit 94bf4be
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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:

Expand Down Expand Up @@ -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.
The original `message`, `time` the app went down, `retry` length, `view`, and `allowed` are all properties available on both events.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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']));
}

/**
Expand All @@ -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;
}

Expand Down Expand Up @@ -87,4 +92,4 @@ protected function getSelectedView()

return $view;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,28 @@ 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
*
* @param int|null $time
* @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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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']);
Expand Down Expand Up @@ -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', []);
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 94bf4be

Please sign in to comment.