]> BookStack Code Mirror - bookstack/commitdiff
Started creation of intermediate permission table
authorDan Brown <redacted>
Wed, 20 Apr 2016 20:37:57 +0000 (21:37 +0100)
committerDan Brown <redacted>
Wed, 20 Apr 2016 20:37:57 +0000 (21:37 +0100)
app/EntityPermission.php [new file with mode: 0644]
app/Services/RestrictionService.php
database/migrations/2016_04_20_192649_create_entity_permissions_table.php [new file with mode: 0644]

diff --git a/app/EntityPermission.php b/app/EntityPermission.php
new file mode 100644 (file)
index 0000000..6b4ddd2
--- /dev/null
@@ -0,0 +1,28 @@
+<?php
+
+namespace BookStack;
+
+use Illuminate\Database\Eloquent\Model;
+
+class EntityPermission extends Model
+{
+    public $timestamps = false;
+
+    /**
+     * Get the role that this points to.
+     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
+     */
+    public function role()
+    {
+        return $this->belongsTo(Role::class);
+    }
+
+    /**
+     * Get the entity this points to.
+     * @return \Illuminate\Database\Eloquent\Relations\MorphOne
+     */
+    public function entity()
+    {
+        return $this->morphOne(Entity::class, 'entity');
+    }
+}
index 50cbe4a51df5eda06f257f3e991f045617a23336..8d57b9edca65f3d30bdab9107973e1daeac46936 100644 (file)
@@ -1,6 +1,13 @@
 <?php namespace BookStack\Services;
 
+use BookStack\Book;
+use BookStack\Chapter;
 use BookStack\Entity;
+use BookStack\EntityPermission;
+use BookStack\Page;
+use BookStack\Permission;
+use BookStack\Role;
+use Illuminate\Database\Eloquent\Collection;
 
 class RestrictionService
 {
@@ -10,14 +17,84 @@ class RestrictionService
     protected $currentAction;
     protected $currentUser;
 
+    public $book;
+    public $chapter;
+    public $page;
+
+    protected $entityPermission;
+    protected $role;
+    protected $permission;
+
     /**
      * RestrictionService constructor.
+     * @param EntityPermission $entityPermission
+     * @param Book $book
+     * @param Chapter $chapter
+     * @param Page $page
+     * @param Role $role
+     * @param Permission $permission
      */
-    public function __construct()
+    public function __construct(EntityPermission $entityPermission, Book $book, Chapter $chapter, Page $page, Role $role, Permission $permission)
     {
         $this->currentUser = auth()->user();
         $this->userRoles = $this->currentUser ? $this->currentUser->roles->pluck('id') : [];
         $this->isAdmin = $this->currentUser ? $this->currentUser->hasRole('admin') : false;
+
+        $this->entityPermission = $entityPermission;
+        $this->role = $role;
+        $this->permission = $permission;
+        $this->book = $book;
+        $this->chapter = $chapter;
+        $this->page = $page;
+    }
+
+
+    /**
+     * Re-generate all entity permission from scratch.
+     */
+    public function buildEntityPermissions()
+    {
+        $this->entityPermission->truncate();
+
+        // Get all roles (Should be the most limited dimension)
+        $roles = $this->role->load('permissions')->all();
+
+        // Chunk through all books
+        $this->book->chunk(500, function ($books) use ($roles) {
+            $this->createManyEntityPermissions($books, $roles);
+        });
+
+        // Chunk through all chapters
+        $this->chapter->chunk(500, function ($books) use ($roles) {
+            $this->createManyEntityPermissions($books, $roles);
+        });
+
+        // Chunk through all pages
+        $this->page->chunk(500, function ($books) use ($roles) {
+            $this->createManyEntityPermissions($books, $roles);
+        });
+    }
+
+    /**
+     * Create & Save entity permissions for many entities and permissions.
+     * @param Collection $entities
+     * @param Collection $roles
+     */
+    protected function createManyEntityPermissions($entities, $roles)
+    {
+        $entityPermissions = [];
+        foreach ($entities as $entity) {
+            foreach ($roles as $role) {
+                $entityPermissions[] = $this->createEntityPermission($entity, $role);
+            }
+        }
+        $this->entityPermission->insert($entityPermissions);
+    }
+
+
+    protected function createEntityPermissionData(Entity $entity, Role $role)
+    {
+        // TODO - Check the permission values and return an EntityPermission
     }
 
     /**
diff --git a/database/migrations/2016_04_20_192649_create_entity_permissions_table.php b/database/migrations/2016_04_20_192649_create_entity_permissions_table.php
new file mode 100644 (file)
index 0000000..359f25d
--- /dev/null
@@ -0,0 +1,34 @@
+<?php
+
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class CreateEntityPermissionsTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('entity_permissions', function (Blueprint $table) {
+            $table->increments('id');
+            $table->integer('role_id');
+            $table->string('entity_type');
+            $table->integer('entity_id');
+            $table->string('action');
+            $table->boolean('has_permission')->default(false);
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::drop('entity_permissions');
+    }
+}