Dan Brown

Continued with database work for permissions overhaul

Added to the entity_permissions table with further required fields and indexes.
Wrote the code for checking permissions.
<?php
namespace BookStack\Console\Commands;
use BookStack\Services\RestrictionService;
use Illuminate\Console\Command;
class RegeneratePermissions extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'permissions:regen';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Regenerate all system permissions';
/**
* The service to handle the permission system.
*
* @var RestrictionService
*/
protected $restrictionService;
/**
* Create a new command instance.
*
* @param RestrictionService $restrictionService
*/
public function __construct(RestrictionService $restrictionService)
{
$this->restrictionService = $restrictionService;
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$this->restrictionService->buildEntityPermissions();
}
}
......@@ -15,6 +15,7 @@ class Kernel extends ConsoleKernel
protected $commands = [
\BookStack\Console\Commands\Inspire::class,
\BookStack\Console\Commands\ResetViews::class,
\BookStack\Console\Commands\RegeneratePermissions::class,
];
/**
......
......@@ -74,6 +74,15 @@ abstract class Entity extends Ownable
}
/**
* Get the entity permissions this is connected to.
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
public function permissions()
{
return $this->morphMany(EntityPermission::class, 'entity');
}
/**
* Allows checking of the exact class, Used to check entity type.
* Cleaner method for is_a.
* @param $type
......@@ -81,7 +90,16 @@ abstract class Entity extends Ownable
*/
public static function isA($type)
{
return static::getClassName() === strtolower($type);
return static::getType() === strtolower($type);
}
/**
* Get entity type.
* @return mixed
*/
public static function getType()
{
return strtolower(static::getClassName());
}
/**
......
......@@ -19,7 +19,16 @@ class CreateEntityPermissionsTable extends Migration
$table->integer('entity_id');
$table->string('action');
$table->boolean('has_permission')->default(false);
$table->boolean('has_permission_own')->default(false);
$table->integer('created_by');
$table->index(['entity_id', 'entity_type']);
$table->index('role_id');
$table->index('action');
$table->index('created_by');
});
$restrictionService = app(\BookStack\Services\RestrictionService::class);
$restrictionService->buildEntityPermissions();
}
/**
......
......@@ -33,6 +33,7 @@
<tr>
<th></th>
<th>Create</th>
<th>View</th>
<th>Edit</th>
<th>Delete</th>
</tr>
......