Skip to content

Commit fef4e92

Browse files
authored
Added whitelist/blacklist filter option; Updated readme (#86)
1 parent 6cbe4c0 commit fef4e92

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,23 @@ Example filters
104104

105105
This feature uses [fnmatch](http://php.net/manual/en/function.fnmatch.php) syntax to filter displayed commands. `stats:*` will match all Artisan commands that start with `stats:` while `email:daily-reports` will only match the command named `email:daily-reports`.
106106

107+
This filter can be used as either a whitelist or a blacklist. By default it acts as a whitelist but an option flag can be set to instead act as a blacklist.
108+
109+
```php
110+
'artisan' => [
111+
'command_filter' => [
112+
'stats:*',
113+
'email:daily-reports'
114+
],
115+
'whitelist' => true,
116+
],
117+
118+
```
119+
120+
If the value of whitelist is `false` then the filter acts as a blacklist.
121+
122+
`'whitelist' => false`
123+
107124
#### Middleware
108125

109126
`Laravel Totem` uses the default web and api middleware but if customization is required the middleware can be changed by setting the appropriate `.env` value. These values can be found in `config/totem.php`.

config/totem.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,5 +225,6 @@
225225
'table_prefix' => env('TOTEM_TABLE_PREFIX', ''),
226226
'artisan' => [
227227
'command_filter' => [],
228+
'whitelist' => true,
228229
],
229230
];

src/Totem.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Closure;
66
use Illuminate\Support\Facades\Artisan;
7+
use Symfony\Component\Console\Command\Command;
78

89
class Totem
910
{
@@ -17,7 +18,8 @@ class Totem
1718
/**
1819
* Determine if the given request can access the Totem dashboard.
1920
*
20-
* @param \Illuminate\Http\Request $request
21+
* @param \Illuminate\Http\Request $request
22+
*
2123
* @return bool
2224
*/
2325
public static function check($request)
@@ -30,14 +32,15 @@ public static function check($request)
3032
/**
3133
* Set the callback that should be used to authenticate Totem users.
3234
*
33-
* @param \Closure $callback
35+
* @param \Closure $callback
36+
*
3437
* @return static
3538
*/
3639
public static function auth(Closure $callback)
3740
{
3841
static::$authUsing = $callback;
3942

40-
return new static;
43+
return new static();
4144
}
4245

4346
/**
@@ -58,21 +61,22 @@ public static function frequencies()
5861
public static function getCommands()
5962
{
6063
$command_filter = config('totem.artisan.command_filter');
64+
$whitelist = config('totem.artisan.whiltelist', true);
6165
$all_commands = collect(Artisan::all());
6266

6367
if (! empty($command_filter)) {
64-
$all_commands = $all_commands->filter(function ($command) use ($command_filter) {
68+
$all_commands = $all_commands->filter(function (Command $command) use ($command_filter, $whitelist) {
6569
foreach ($command_filter as $filter) {
6670
if (fnmatch($filter, $command->getName())) {
67-
return true;
71+
return $whitelist;
6872
}
6973
}
7074

71-
return false;
75+
return ! $whitelist;
7276
});
7377
}
7478

75-
return $all_commands->sortBy(function ($command) {
79+
return $all_commands->sortBy(function (Command $command) {
7680
return $command->getDescription();
7781
});
7882
}

0 commit comments

Comments
 (0)