]> BookStack Code Mirror - bookstack/commitdiff
Improved permission regen performance by factor of 4
authorDan Brown <redacted>
Sat, 30 Apr 2016 16:16:06 +0000 (17:16 +0100)
committerDan Brown <redacted>
Sat, 30 Apr 2016 16:16:06 +0000 (17:16 +0100)
Worked around slower eloquent access to speed up permission generation.

16 files changed:
app/Activity.php
app/EmailConfirmation.php
app/Entity.php
app/EntityPermission.php
app/Model.php [new file with mode: 0644]
app/Ownable.php
app/Page.php
app/PageRevision.php
app/Permission.php
app/Restriction.php
app/Role.php
app/Services/RestrictionService.php
app/Setting.php
app/SocialAccount.php
app/User.php
app/View.php

index ac7c1d749a560de97483deed10939dc07c2868c4..1fd00abea6de6d0627c386ca39bcc9cc3ae2b38d 100644 (file)
@@ -2,8 +2,6 @@
 
 namespace BookStack;
 
-use Illuminate\Database\Eloquent\Model;
-
 /**
  * @property string  key
  * @property \User   user
index 46912e7338b825fe266ffedad62725403a09f78b..974cf201c78f7887a626858670851bcd00b71eb8 100644 (file)
@@ -2,8 +2,6 @@
 
 namespace BookStack;
 
-use Illuminate\Database\Eloquent\Model;
-
 class EmailConfirmation extends Model
 {
     protected $fillable = ['user_id', 'token'];
index c084a28703cc9b2ec713a0f65e0bba50a7c84a24..eb14780feb181038f8486e294ef72456a966cf35 100644 (file)
@@ -82,8 +82,7 @@ abstract class Entity extends Ownable
      */
     public function hasActiveRestriction($role_id, $action)
     {
-        return $this->restricted && $this->restrictions()
-            ->where('role_id', '=', $role_id)->where('action', '=', $action)->count() > 0;
+        return $this->getRawAttribute('restricted') && $this->hasRestriction($role_id, $action);
     }
 
     /**
index 6b4ddd21271479027a8aebe7672a81296bd23252..266930d2c7124a882c40627f3bdbf80a10899636 100644 (file)
@@ -1,8 +1,4 @@
-<?php
-
-namespace BookStack;
-
-use Illuminate\Database\Eloquent\Model;
+<?php namespace BookStack;
 
 class EntityPermission extends Model
 {
diff --git a/app/Model.php b/app/Model.php
new file mode 100644 (file)
index 0000000..9ec2b73
--- /dev/null
@@ -0,0 +1,19 @@
+<?php namespace BookStack;
+
+use Illuminate\Database\Eloquent\Model as EloquentModel;
+
+class Model extends EloquentModel
+{
+
+    /**
+     * Provides public access to get the raw attribute value from the model.
+     * Used in areas where no mutations are required but performance is critical.
+     * @param $key
+     * @return mixed
+     */
+    public function getRawAttribute($key)
+    {
+        return parent::getAttributeFromArray($key);
+    }
+
+}
\ No newline at end of file
index 28d55c2bb98156cb08a4b6e2e44842270f4bbe99..8890c01bfec41840ad0981384ad90ab1e19f6f81 100644 (file)
@@ -1,6 +1,5 @@
 <?php namespace BookStack;
 
-use Illuminate\Database\Eloquent\Model;
 
 abstract class Ownable extends Model
 {
index d2a303f619662cfd2fcbd197c05e510f288b24aa..3dc3b0256cebdcb5a595136704ba4ac825fda6ab 100644 (file)
@@ -1,8 +1,5 @@
-<?php
+<?php namespace BookStack;
 
-namespace BookStack;
-
-use Illuminate\Database\Eloquent\Model;
 
 class Page extends Entity
 {
index c258913ff23e6cb102e8549404bdcef32bfc48b1..49e53400e615bc6c1e7986bb1d52f99be60e7d98 100644 (file)
@@ -1,6 +1,5 @@
 <?php namespace BookStack;
 
-use Illuminate\Database\Eloquent\Model;
 
 class PageRevision extends Model
 {
index e3f391562fd06443fda705e0c83e789d1577407a..0ce326e06dd39c3c7a29d1db2a9fe7f18628e4c4 100644 (file)
@@ -1,6 +1,5 @@
 <?php namespace BookStack;
 
-use Illuminate\Database\Eloquent\Model;
 
 class Permission extends Model
 {
index 58d1179979eaad4a6fd974c1114d481e7f782040..c9dd705d5790fc3b2ab264ca0beda4fd5dfe2006 100644 (file)
@@ -1,8 +1,5 @@
-<?php
+<?php namespace BookStack;
 
-namespace BookStack;
-
-use Illuminate\Database\Eloquent\Model;
 
 class Restriction extends Model
 {
index 45d160cfebec93d6d215986dac5e66753c0dc81d..3b930d113259af4105b5e1e68b6cdedb28c12e17 100644 (file)
@@ -1,8 +1,5 @@
-<?php
+<?php namespace BookStack;
 
-namespace BookStack;
-
-use Illuminate\Database\Eloquent\Model;
 
 class Role extends Model
 {
@@ -36,11 +33,16 @@ class Role extends Model
 
     /**
      * Check if this role has a permission.
-     * @param $permission
+     * @param $permissionName
+     * @return bool
      */
-    public function hasPermission($permission)
+    public function hasPermission($permissionName)
     {
-        return $this->permissions->pluck('name')->contains($permission);
+        $permissions = $this->getRelationValue('permissions');
+        foreach ($permissions as $permission) {
+            if ($permission->getRawAttribute('name') === $permissionName) return true;
+        }
+        return false;
     }
 
     /**
index d3394fcd7d1cbdf031fd33eca9f880b6889c316f..40287bf77c426f1a741309ef4ec0f32ec079548b 100644 (file)
@@ -54,21 +54,21 @@ class RestrictionService
         $this->entityPermission->truncate();
 
         // Get all roles (Should be the most limited dimension)
-        $roles = $this->role->load('permissions')->all();
+        $roles = $this->role->with('permissions')->get();
 
         // Chunk through all books
-        $this->book->chunk(500, function ($books) use ($roles) {
+        $this->book->with('restrictions')->chunk(500, function ($books) use ($roles) {
             $this->createManyEntityPermissions($books, $roles);
         });
 
         // Chunk through all chapters
-        $this->chapter->with('book')->chunk(500, function ($books) use ($roles) {
-            $this->createManyEntityPermissions($books, $roles);
+        $this->chapter->with('book', 'restrictions')->chunk(500, function ($chapters) use ($roles) {
+            $this->createManyEntityPermissions($chapters, $roles);
         });
 
         // Chunk through all pages
-        $this->page->with('book', 'chapter')->chunk(500, function ($books) use ($roles) {
-            $this->createManyEntityPermissions($books, $roles);
+        $this->page->with('book', 'chapter', 'restrictions')->chunk(500, function ($pages) use ($roles) {
+            $this->createManyEntityPermissions($pages, $roles);
         });
     }
 
@@ -78,7 +78,7 @@ class RestrictionService
      */
     public function buildEntityPermissionsForEntity(Entity $entity)
     {
-        $roles = $this->role->load('permissions')->all();
+        $roles = $this->role->with('permissions')->get();
         $entities = collect([$entity]);
 
         if ($entity->isA('book')) {
@@ -103,17 +103,17 @@ class RestrictionService
         $this->deleteManyEntityPermissionsForRoles($roles);
 
         // Chunk through all books
-        $this->book->chunk(500, function ($books) use ($roles) {
+        $this->book->with('restrictions')->chunk(500, function ($books) use ($roles) {
             $this->createManyEntityPermissions($books, $roles);
         });
 
         // Chunk through all chapters
-        $this->chapter->with('book')->chunk(500, function ($books) use ($roles) {
+        $this->chapter->with('book', 'restrictions')->chunk(500, function ($books) use ($roles) {
             $this->createManyEntityPermissions($books, $roles);
         });
 
         // Chunk through all pages
-        $this->page->with('book', 'chapter')->chunk(500, function ($books) use ($roles) {
+        $this->page->with('book', 'chapter', 'restrictions')->chunk(500, function ($books) use ($roles) {
             $this->createManyEntityPermissions($books, $roles);
         });
     }
@@ -272,13 +272,13 @@ class RestrictionService
     {
         $entityClass = get_class($entity);
         return [
-            'role_id'            => $role->id,
-            'entity_id'          => $entity->id,
+            'role_id'            => $role->getRawAttribute('id'),
+            'entity_id'          => $entity->getRawAttribute('id'),
             'entity_type'        => $entityClass,
             'action'             => $action,
             'has_permission'     => $permissionAll,
             'has_permission_own' => $permissionOwn,
-            'created_by'         => $entity->created_by
+            'created_by'         => $entity->getRawAttribute('created_by')
         ];
     }
 
index 05bd2c2265e45e65d4ee5f5d03860b1b153ea335..0af3652db628dec19cff86439e5ffa0544aff5bd 100644 (file)
@@ -1,8 +1,4 @@
-<?php
-
-namespace BookStack;
-
-use Illuminate\Database\Eloquent\Model;
+<?php namespace BookStack;
 
 class Setting extends Model
 {
index 2d63b519807b832469a068a202ba62c1b9613493..127b1e2293a4790524dd337c792d356b21938c8b 100644 (file)
@@ -1,8 +1,5 @@
-<?php
+<?php namespace BookStack;
 
-namespace BookStack;
-
-use Illuminate\Database\Eloquent\Model;
 
 class SocialAccount extends Model
 {
index a16eab972ac60a34aaae852e4d127be2f0ecdb2f..1ba5b90f344b4618d72d839da08c46b8229df999 100644 (file)
@@ -1,9 +1,6 @@
-<?php
-
-namespace BookStack;
+<?php namespace BookStack;
 
 use Illuminate\Auth\Authenticatable;
-use Illuminate\Database\Eloquent\Model;
 use Illuminate\Auth\Passwords\CanResetPassword;
 use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
 use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
index 50dd06012525928f92f6d92e99fd7ccb518d9d3c..c02550c7caa8e9d68157656e26c2dc6cd2ecc100 100644 (file)
@@ -1,8 +1,4 @@
-<?php
-
-namespace BookStack;
-
-use Illuminate\Database\Eloquent\Model;
+<?php namespace BookStack;
 
 class View extends Model
 {