Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added feature: hide specified keys #5

Merged
merged 4 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,17 @@ FilamentEnvEditorPlugin::make()
->slug('env-editor')
```

### Hiding keys

Some keys you may consider to be particularly sensitive and don't wish to expose them, even through this package. You can hide them through this interface:

```php
FilamentEnvEditorPlugin::make()
->hideKeys('APP_KEY', 'BCRYPT_ROUNDS')
```

### Authorization

If you would like to prevent certain users from accessing the logs page, you should add a `authorize` callback in the FilamentLEnvEditorPlugin chain.

```php
Expand Down
22 changes: 22 additions & 0 deletions src/FilamentEnvEditorPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ class FilamentEnvEditorPlugin implements Plugin

protected string|\Closure $slug = 'env-editor';

/**
* @var list<string>
*/
protected array $hideKeys = [];

public function getId(): string
{
return 'filament-env-editor';
Expand Down Expand Up @@ -134,4 +139,21 @@ public function getSlug(): string
{
return $this->evaluate($this->slug);
}

public function hideKeys(string ...$keys): static
{
$this->hideKeys = $keys;

return $this;
}

/**
* Retrieves the list of environment keys that should be hidden from the UI.
*
* @return list<string>
*/
public function getHiddenKeys(): array
{
return $this->hideKeys;
}
}
2 changes: 1 addition & 1 deletion src/Pages/Actions/Backups/UploadBackupAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ protected function setUp(): void
$this->form([
FileUpload::make('file')->saveUploadedFileUsing(static function (
BaseFileUpload $component,
TemporaryUploadedFile $file
TemporaryUploadedFile $file,
): ?string {
$backupsPath = app(EnvEditor::class)->getFilesManager()->getBackupsDir();

Expand Down
35 changes: 22 additions & 13 deletions src/Pages/ViewEnv.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,21 +110,25 @@ private function getFirstTab(): array
->filter(fn (EntryObj $obj) => !$obj->isSeparator())
->groupBy('group')
->map(function (Collection $group) {
$fields = $group->map(function (EntryObj $obj) {
return Forms\Components\Group::make([
Forms\Components\Actions::make([
EditAction::make("edit_{$obj->key}")->setEntry($obj),
DeleteAction::make("delete_{$obj->key}")->setEntry($obj),
])->alignEnd(),
Forms\Components\Placeholder::make($obj->key)
->label('')
->content(new HtmlString("<code>{$obj->getAsEnvLine()}</code>"))
->columnSpan(4),
])->columns(5);
});
$fields = $group
->reject(fn (EntryObj $obj) => $this->shouldHideEnvVariable($obj->key))
->map(function (EntryObj $obj) {
return Forms\Components\Group::make([
Forms\Components\Actions::make([
EditAction::make("edit_{$obj->key}")->setEntry($obj),
DeleteAction::make("delete_{$obj->key}")->setEntry($obj),
])->alignEnd(),
Forms\Components\Placeholder::make($obj->key)
->label('')
->content(new HtmlString("<code>{$obj->getAsEnvLine()}</code>"))
->columnSpan(4),
])->columns(5);
});

return Forms\Components\Section::make()->schema($fields->all())->columns(1);
})->all();
})
->filter(fn (Forms\Components\Section $s) => $s->hasChildComponentContainer(true))
->all();

$header = Forms\Components\Group::make([
Forms\Components\Actions::make([
Expand All @@ -135,6 +139,11 @@ private function getFirstTab(): array
return [$header, ...$envData];
}

private function shouldHideEnvVariable(string $key): bool
{
return in_array($key, FilamentEnvEditorPlugin::get()->getHiddenKeys());
}

/**
* @return list<Component>
*/
Expand Down