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.
1 +<?php
2 +
3 +namespace BookStack\Console\Commands;
4 +
5 +use BookStack\Services\RestrictionService;
6 +use Illuminate\Console\Command;
7 +
8 +class RegeneratePermissions extends Command
9 +{
10 + /**
11 + * The name and signature of the console command.
12 + *
13 + * @var string
14 + */
15 + protected $signature = 'permissions:regen';
16 +
17 + /**
18 + * The console command description.
19 + *
20 + * @var string
21 + */
22 + protected $description = 'Regenerate all system permissions';
23 +
24 + /**
25 + * The service to handle the permission system.
26 + *
27 + * @var RestrictionService
28 + */
29 + protected $restrictionService;
30 +
31 + /**
32 + * Create a new command instance.
33 + *
34 + * @param RestrictionService $restrictionService
35 + */
36 + public function __construct(RestrictionService $restrictionService)
37 + {
38 + $this->restrictionService = $restrictionService;
39 + parent::__construct();
40 + }
41 +
42 + /**
43 + * Execute the console command.
44 + *
45 + * @return mixed
46 + */
47 + public function handle()
48 + {
49 + $this->restrictionService->buildEntityPermissions();
50 + }
51 +}
...@@ -15,6 +15,7 @@ class Kernel extends ConsoleKernel ...@@ -15,6 +15,7 @@ class Kernel extends ConsoleKernel
15 protected $commands = [ 15 protected $commands = [
16 \BookStack\Console\Commands\Inspire::class, 16 \BookStack\Console\Commands\Inspire::class,
17 \BookStack\Console\Commands\ResetViews::class, 17 \BookStack\Console\Commands\ResetViews::class,
18 + \BookStack\Console\Commands\RegeneratePermissions::class,
18 ]; 19 ];
19 20
20 /** 21 /**
......
...@@ -74,6 +74,15 @@ abstract class Entity extends Ownable ...@@ -74,6 +74,15 @@ abstract class Entity extends Ownable
74 } 74 }
75 75
76 /** 76 /**
77 + * Get the entity permissions this is connected to.
78 + * @return \Illuminate\Database\Eloquent\Relations\MorphMany
79 + */
80 + public function permissions()
81 + {
82 + return $this->morphMany(EntityPermission::class, 'entity');
83 + }
84 +
85 + /**
77 * Allows checking of the exact class, Used to check entity type. 86 * Allows checking of the exact class, Used to check entity type.
78 * Cleaner method for is_a. 87 * Cleaner method for is_a.
79 * @param $type 88 * @param $type
...@@ -81,7 +90,16 @@ abstract class Entity extends Ownable ...@@ -81,7 +90,16 @@ abstract class Entity extends Ownable
81 */ 90 */
82 public static function isA($type) 91 public static function isA($type)
83 { 92 {
84 - return static::getClassName() === strtolower($type); 93 + return static::getType() === strtolower($type);
94 + }
95 +
96 + /**
97 + * Get entity type.
98 + * @return mixed
99 + */
100 + public static function getType()
101 + {
102 + return strtolower(static::getClassName());
85 } 103 }
86 104
87 /** 105 /**
......
...@@ -19,7 +19,16 @@ class CreateEntityPermissionsTable extends Migration ...@@ -19,7 +19,16 @@ class CreateEntityPermissionsTable extends Migration
19 $table->integer('entity_id'); 19 $table->integer('entity_id');
20 $table->string('action'); 20 $table->string('action');
21 $table->boolean('has_permission')->default(false); 21 $table->boolean('has_permission')->default(false);
22 + $table->boolean('has_permission_own')->default(false);
23 + $table->integer('created_by');
24 + $table->index(['entity_id', 'entity_type']);
25 + $table->index('role_id');
26 + $table->index('action');
27 + $table->index('created_by');
22 }); 28 });
29 +
30 + $restrictionService = app(\BookStack\Services\RestrictionService::class);
31 + $restrictionService->buildEntityPermissions();
23 } 32 }
24 33
25 /** 34 /**
......
...@@ -33,6 +33,7 @@ ...@@ -33,6 +33,7 @@
33 <tr> 33 <tr>
34 <th></th> 34 <th></th>
35 <th>Create</th> 35 <th>Create</th>
36 + <th>View</th>
36 <th>Edit</th> 37 <th>Edit</th>
37 <th>Delete</th> 38 <th>Delete</th>
38 </tr> 39 </tr>
......