Skip to content

NOW MANAGED ON CODEBERG

Improved permission regen performance by factor of 4

Dan Brown59367b341787
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 @@
-<?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
index 000000000..9ec2b7362
--- /dev/null
+++ b/app/Model.php
@@ -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
diff --git a/app/Ownable.php b/app/Ownable.php
index 28d55c2bb..8890c01bf 100644
--- a/app/Ownable.php
+++ b/app/Ownable.php
@@ -1,6 +1,5 @@
<?php namespace BookStack;
-use Illuminate\Database\Eloquent\Model;
abstract class Ownable extends Model
{
diff --git a/app/Page.php b/app/Page.php
index d2a303f61..3dc3b0256 100644
--- a/app/Page.php
+++ b/app/Page.php
@@ -1,8 +1,5 @@
-<?php
+<?php namespace BookStack;
-namespace BookStack;
-
-use Illuminate\Database\Eloquent\Model;
class Page extends Entity
{
diff --git a/app/PageRevision.php b/app/PageRevision.php
index c258913ff..49e53400e 100644
--- a/app/PageRevision.php
+++ b/app/PageRevision.php
@@ -1,6 +1,5 @@
<?php namespace BookStack;
-use Illuminate\Database\Eloquent\Model;
class PageRevision extends Model
{
diff --git a/app/Permission.php b/app/Permission.php
index e3f391562..0ce326e06 100644
--- a/app/Permission.php
+++ b/app/Permission.php
@@ -1,6 +1,5 @@
<?php namespace BookStack;
-use Illuminate\Database\Eloquent\Model;
class Permission extends Model
{
diff --git a/app/Restriction.php b/app/Restriction.php
index 58d117997..c9dd705d5 100644
--- a/app/Restriction.php
+++ b/app/Restriction.php
@@ -1,8 +1,5 @@
-<?php
+<?php namespace BookStack;
-namespace BookStack;
-
-use Illuminate\Database\Eloquent\Model;
class Restriction extends Model
{
diff --git a/app/Role.php b/app/Role.php
index 45d160cfe..3b930d113 100644
--- a/app/Role.php
+++ b/app/Role.php
@@ -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;
}
/**
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 @@
-<?php
-
-namespace BookStack;
-
-use Illuminate\Database\Eloquent\Model;
+<?php namespace BookStack;
class Setting extends Model
{
diff --git a/app/SocialAccount.php b/app/SocialAccount.php
index 2d63b5198..127b1e229 100644
--- a/app/SocialAccount.php
+++ b/app/SocialAccount.php
@@ -1,8 +1,5 @@
-<?php
+<?php namespace BookStack;
-namespace BookStack;
-
-use Illuminate\Database\Eloquent\Model;
class SocialAccount extends Model
{
diff --git a/app/User.php b/app/User.php
index a16eab972..1ba5b90f3 100644
--- a/app/User.php
+++ b/app/User.php
@@ -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;
diff --git a/app/View.php b/app/View.php
index 50dd06012..c02550c7c 100644
--- a/app/View.php
+++ b/app/View.php
@@ -1,8 +1,4 @@
-<?php
-
-namespace BookStack;
-
-use Illuminate\Database\Eloquent\Model;
+<?php namespace BookStack;
class View extends Model
{