Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Kristories committed Nov 20, 2022
1 parent ceb036a commit 066675e
Show file tree
Hide file tree
Showing 6 changed files with 275 additions and 0 deletions.
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Filament Sanctum

## Installation

You can install the package via composer:

```bash
composer require devtical/filament-sanctum
```

Translations:

```bash
php artisan vendor:publish --tag=filament-sanctum-translations
```

## Testing

```bash
composer test
```

## Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

## Contributing

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

## Security

If you discover any security related issues, please email `w.kristories@gmail.com` instead of using the issue tracker.

## Credits

- [Wahyu Kristianto](https://github.com/kristories)
- [All Contributors](https://github.com/devtical/sanctum/graphs/contributors)

## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
57 changes: 57 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"name": "devtical/filament-sanctum",
"description": "Filament Sanctum",
"keywords": [
"Devtical",
"sanctum",
"laravel"
],
"homepage": "https://github.com/devtical/filament-sanctum",
"license": "MIT",
"authors": [
{
"name": "Wahyu Kristianto",
"email": "w.kristories@gmail.com"
}
],
"require": {
"illuminate/contracts": "^9.0",
"laravel/sanctum": "^3.0",
"filament/filament": "^2.0"
},
"require-dev": {
"laravel/pint": "^1.0",
"pestphp/pest": "^1.20",
"orchestra/testbench": "^7.0"
},
"autoload": {
"psr-4": {
"Devtical\\Sanctum\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Devtical\\Sanctum\\Tests\\": "tests"
}
},
"scripts": {
"test": "vendor/bin/pest",
"format": "vendor/bin/pint"
},
"config": {
"sort-packages": true,
"allow-plugins": {
"pestphp/pest-plugin": true,
"phpstan/extension-installer": true
}
},
"extra": {
"laravel": {
"providers": [
"Devtical\\Sanctum\\SanctumServiceProvider"
]
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
14 changes: 14 additions & 0 deletions resources/lang/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"Sanctum": "Token",
"Name": "Name",
"Abilities": "Abilities",
"Last used at": "Last used at",
"Created at": "Created at",
"Create a new Token": "Create a new Token",
"Saved successfully": "Saved successfully",
"Token was created successfully": "Token was created successfully",
"Token Name": "Token Name",
"Delete": "Delete",
"Attention needed": "Attention needed",
"Make sure to copy your new personal access token now. You won't be able to see it again!": "Make sure to copy your new personal access token now. You won't be able to see it again!"
}
23 changes: 23 additions & 0 deletions resources/views/pages/sanctum.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<x-filament::page>
@if($sanctumToken = session('sanctum-token'))
<div class="rounded-md bg-yellow-50 p-4">
<div class="flex">
<div class="flex-shrink-0">
<!-- Heroicon name: mini/exclamation-triangle -->
<svg class="h-5 w-5 text-yellow-400" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">
<path fill-rule="evenodd" d="M8.485 3.495c.673-1.167 2.357-1.167 3.03 0l6.28 10.875c.673 1.167-.17 2.625-1.516 2.625H3.72c-1.347 0-2.189-1.458-1.515-2.625L8.485 3.495zM10 6a.75.75 0 01.75.75v3.5a.75.75 0 01-1.5 0v-3.5A.75.75 0 0110 6zm0 9a1 1 0 100-2 1 1 0 000 2z" clip-rule="evenodd" />
</svg>
</div>
<div class="ml-3 block w-full">
<h3 class="text-sm font-medium text-yellow-800">{{ trans('Attention needed') }}</h3>
<div class="mt-2 text-sm text-yellow-700">
<p class="mb-4">{{ trans('Make sure to copy your new personal access token now. You won\'t be able to see it again!') }}</p>
<div class="block w-full rounded-md border-transparent bg-yellow-100 text-sm font-mono p-4">{{ $sanctumToken }}</div>
</div>
</div>
</div>
</div>
@endif

{{ $this->table }}
</x-filament::page>
112 changes: 112 additions & 0 deletions src/Pages/Sanctum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

namespace Devtical\Sanctum\Pages;

use Filament\Forms;
use Filament\Notifications\Notification;
use Filament\Pages\Actions\Action;
use Filament\Pages\Page;
use Filament\Tables;
use Filament\Tables\Actions\BulkAction;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;

class Sanctum extends Page implements Tables\Contracts\HasTable
{
use Tables\Concerns\InteractsWithTable;

protected static ?string $navigationIcon = 'heroicon-o-finger-print';

protected static string $view = 'filament-sanctum::pages.sanctum';

protected static ?string $slug = 'sanctum';

protected function getTitle(): string
{
return trans('Sanctum');
}

protected static function getNavigationLabel(): string
{
return trans('Sanctum');
}

protected function getTableQuery(): Builder
{
return Auth::user()->tokens()->getQuery();
}

protected function getDefaultTableSortColumn(): ?string
{
return 'id';
}

protected function getDefaultTableSortDirection(): ?string
{
return 'desc';
}

protected function getTableColumns(): array
{
return [
Tables\Columns\TextColumn::make('name')
->label(trans('Name'))
->sortable()
->searchable(),
Tables\Columns\TagsColumn::make('abilities')
->label(trans('Abilities')),
Tables\Columns\TextColumn::make('last_used_at')
->label(trans('Last used at'))
->dateTime()
->sortable(),
Tables\Columns\TextColumn::make('created_at')
->label(trans('Created at'))
->dateTime()
->sortable(),
];
}

protected function getActions(): array
{
return [
Action::make('new')
->label(trans('Create a new Token'))
->action(function (array $data) {
$user = Auth::user();
$token = $user->createToken($data['name'], $data['abilities'])->plainTextToken;
request()->session()->flash('sanctum-token', $token);
Notification::make()
->title(trans('Saved successfully'))
->success()
->icon('heroicon-o-finger-print')
->title(trans('Token was created successfully'))
->send();

return redirect(config('filament.path').'/sanctum');
})
->form([
Forms\Components\TextInput::make('name')
->label(trans('Token Name'))
->required(),
Forms\Components\CheckboxList::make('abilities')
->label(trans('Abilities'))
->options(config('filament-sanctum.abilities'))
->columns(config('filament-sanctum.columns')),
]),
];
}

protected function getTableBulkActions(): array
{
return [
BulkAction::make('delete')
->label(trans('Delete'))
->action(fn (Collection $records) => $records->each->delete())
->deselectRecordsAfterCompletion()
->requiresConfirmation()
->color('danger')
->icon('heroicon-o-trash'),
];
}
}
27 changes: 27 additions & 0 deletions src/SanctumServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Devtical\Sanctum;

use Devtical\Sanctum\Pages\Sanctum;
use Filament\PluginServiceProvider;
use Spatie\LaravelPackageTools\Package;

class SanctumServiceProvider extends PluginServiceProvider
{
protected array $pages = [
Sanctum::class,
];

protected array $styles = [
'filament-sanctum' => __DIR__.'/../resources/dist/app.css',
];

public function configurePackage(Package $package): void
{
$package
->name('filament-sanctum')
->hasViews()
->hasAssets('filament-sanctum')
->hasTranslations();
}
}

0 comments on commit 066675e

Please sign in to comment.