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

Implement individual table permissions #30

Merged
merged 2 commits into from
Jul 4, 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
7 changes: 5 additions & 2 deletions krait/routes/tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
use Illuminate\Support\Facades\Route;

foreach (app('tables') as $name => $table) {
$tableName = app($table['table'])->name();
Route::get($tableName, $table['controller'])->name($tableName);
$tableObject = app($table['table']);
$tableName = $tableObject->name();
Route::middleware($tableObject->middlewares())
->get($tableName, $table['controller'])
->name($tableName);
}
4 changes: 4 additions & 0 deletions krait/src/Http/Resources/TableCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ public function __construct(

public function toArray(Request $request)
{
if (! $this->table->authorize($request)) {
abort(403);
}

return $this->collection->map(function ($record) {
return array_merge(
[
Expand Down
44 changes: 31 additions & 13 deletions krait/src/KraitServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

namespace MtrDesign\Krait;

use DirectoryIterator;
use FilesystemIterator;
use Illuminate\Contracts\Foundation\CachesRoutes;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
use MtrDesign\Krait\Services\PreviewConfigService;
use MtrDesign\Krait\Tables\BaseTable;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use RuntimeException;

/**
Expand Down Expand Up @@ -90,15 +92,15 @@ protected function configRoutes(): void
'prefix' => config('krait.path', 'krait'),
'as' => 'krait.',
'namespace' => 'MtrDesign\Krait\Http\Controllers',
'middleware' => config('krait.middleware', ['web', 'auth']),
'middleware' => config('krait.global_middlewares', ['web', 'auth']),
], function () {
$this->loadRoutesFrom(__DIR__.'/../routes/web.php');
});

Route::group([
'prefix' => config('krait.tables_path', 'tables'),
'namespace' => 'App\Http\Controllers',
'middleware' => config('krait.middleware', ['web', 'auth']),
'middleware' => config('krait.global_middlewares', ['web', 'auth']),
], function () {
$this->loadRoutesFrom(__DIR__.'/../routes/tables.php');
});
Expand All @@ -125,31 +127,47 @@ protected function configBladeMacros(): void
*/
protected function registerTables(): void
{
$tablesDirectory = config('krait.tables_directory');
Krait::sanityPath($tablesDirectory);

$tables = [];
if (file_exists(config('krait.tables_directory'))) {
$directory = new DirectoryIterator(config('krait.tables_directory'));
$tablesDirectory = config('krait.tables_directory');
Krait::sanityPath($tablesDirectory);

foreach (new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($tablesDirectory,
FilesystemIterator::SKIP_DOTS),
RecursiveIteratorIterator::SELF_FIRST) as $file) {

foreach ($directory as $file) {
if (! $file->isFile()) {
continue;
}

$tableClassName = substr($file, 0, -4);
$tableClass = config('krait.tables_namespace').$tableClassName;
if (! is_subclass_of($tableClass, BaseTable::class)) {
$namespace = Str::replace(app_path().'/', 'App/', $tablesDirectory);
$namespace = Str::replace('/', '\\', $namespace);

$className = explode($tablesDirectory.'/', $file->getPathname())[1];
$className = Str::replace('/', '\\', $className);
$className = substr($className, 0, -4);

$fullClass = sprintf('%s\%s', $namespace, $className);

if (! is_subclass_of($fullClass, BaseTable::class)) {
continue;
}

$this->app->singleton($tableClass, function ($app) use ($tableClass) {
$this->app->singleton($fullClass, function ($app) use ($fullClass) {
$previewConfigService = $app->make(PreviewConfigService::class);
$table = new $tableClass($previewConfigService);
$table = new $fullClass($previewConfigService);
$table->initColumns();

return $table;
});
$tables[$tableClassName] = [
'table' => $tableClass,
'controller' => 'App\\Http\\Controllers\\Tables\\'.$tableClassName.'Controller',

$tables[$className] = [
'table' => $fullClass,
'controller' => 'App\\Http\\Controllers\\Tables\\'.$className.'Controller',
];
}
}
Expand Down
18 changes: 18 additions & 0 deletions krait/src/Tables/BaseTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Exception;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str;
use Laravel\Prompts\Table;
Expand Down Expand Up @@ -45,6 +46,23 @@ abstract public function name(): string;
*/
abstract public function initColumns(): void;

/**
* Flags if the request is authorized to see the table's data.
*/
public function authorize(Request $request): bool
{
return true;
}

/**
* Returns the specific table middlewares that should be
* applied to the request.
*/
public function middlewares(): array
{
return [];
}

/**
* Flags if the columns should be cached.
* Usable for dynamic columns serving (from a third-party services).
Expand Down
2 changes: 1 addition & 1 deletion krait/stubs/krait.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
|
*/

'middleware' => ['web', 'auth'],
'global_middlewares' => ['web', 'auth'],

/*
|--------------------------------------------------------------------------
Expand Down