From 59367b34178739c3aedb4d21e863f3cc58401a2d Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Sat, 30 Apr 2016 17:16:06 +0100 Subject: [PATCH] Improved permission regen performance by factor of 4 Worked around slower eloquent access to speed up permission generation. --- app/Activity.php | 2 -- app/EmailConfirmation.php | 2 -- app/Entity.php | 3 +-- app/EntityPermission.php | 6 +----- app/Model.php | 19 +++++++++++++++++++ app/Ownable.php | 1 - app/Page.php | 5 +---- app/PageRevision.php | 1 - app/Permission.php | 1 - app/Restriction.php | 5 +---- app/Role.php | 16 +++++++++------- app/Services/RestrictionService.php | 26 +++++++++++++------------- app/Setting.php | 6 +----- app/SocialAccount.php | 5 +---- app/User.php | 5 +---- app/View.php | 6 +----- 16 files changed, 49 insertions(+), 60 deletions(-) create mode 100644 app/Model.php diff --git a/app/Activity.php b/app/Activity.php index ac7c1d749..1fd00abea 100644 --- a/app/Activity.php +++ b/app/Activity.php @@ -2,8 +2,6 @@ namespace BookStack; -use Illuminate\Database\Eloquent\Model; - /** * @property string key * @property \User user diff --git a/app/EmailConfirmation.php b/app/EmailConfirmation.php index 46912e733..974cf201c 100644 --- a/app/EmailConfirmation.php +++ b/app/EmailConfirmation.php @@ -2,8 +2,6 @@ namespace BookStack; -use Illuminate\Database\Eloquent\Model; - class EmailConfirmation extends Model { protected $fillable = ['user_id', 'token']; diff --git a/app/Entity.php b/app/Entity.php index c084a2870..eb14780fe 100644 --- a/app/Entity.php +++ b/app/Entity.php @@ -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); } /** diff --git a/app/EntityPermission.php b/app/EntityPermission.php index 6b4ddd212..266930d2c 100644 --- a/app/EntityPermission.php +++ b/app/EntityPermission.php @@ -1,8 +1,4 @@ -permissions->pluck('name')->contains($permission); + $permissions = $this->getRelationValue('permissions'); + foreach ($permissions as $permission) { + if ($permission->getRawAttribute('name') === $permissionName) return true; + } + return false; } /** diff --git a/app/Services/RestrictionService.php b/app/Services/RestrictionService.php index d3394fcd7..40287bf77 100644 --- a/app/Services/RestrictionService.php +++ b/app/Services/RestrictionService.php @@ -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') ]; } diff --git a/app/Setting.php b/app/Setting.php index 05bd2c226..0af3652db 100644 --- a/app/Setting.php +++ b/app/Setting.php @@ -1,8 +1,4 @@ -