]> BookStack Code Mirror - bookstack/commitdiff
Fixed short editor in firefox and optimised some queries
authorDan Brown <redacted>
Sat, 2 Jan 2021 01:22:41 +0000 (01:22 +0000)
committerDan Brown <redacted>
Sat, 2 Jan 2021 01:22:41 +0000 (01:22 +0000)
Optimised permission fetching so that it won't initialise a bunch
of models for the role permissions and instead does a manual
query to get the data directly.

app/Auth/User.php
app/Http/Controllers/HomeController.php
resources/sass/_layout.scss
resources/views/pages/edit.blade.php
tests/SharedTestHelpers.php

index 32179a1fbb8d1e97300d4a5445975014705a4631..fdfd9e616567628ae7f04de837350e714f33cea4 100644 (file)
@@ -15,6 +15,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsToMany;
 use Illuminate\Database\Eloquent\Relations\HasMany;
 use Illuminate\Database\Eloquent\Relations\HasOne;
 use Illuminate\Notifications\Notifiable;
+use Illuminate\Support\Collection;
 
 /**
  * Class User
@@ -56,7 +57,7 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon
 
     /**
      * This holds the user's permissions when loaded.
-     * @var array
+     * @var ?Collection
      */
     protected $permissions;
 
@@ -130,35 +131,44 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon
         }
     }
 
+    /**
+     * Check if the user has a particular permission.
+     */
+    public function can(string $permissionName): bool
+    {
+        if ($this->email === 'guest') {
+            return false;
+        }
+
+        return $this->permissions()->contains($permissionName);
+    }
+
     /**
      * Get all permissions belonging to a the current user.
-     * @param bool $cache
-     * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
      */
-    public function permissions($cache = true)
+    protected function permissions(): Collection
     {
-        if (isset($this->permissions) && $cache) {
+        if (isset($this->permissions)) {
             return $this->permissions;
         }
-        $this->load('roles.permissions');
-        $permissions = $this->roles->map(function ($role) {
-            return $role->permissions;
-        })->flatten()->unique();
-        $this->permissions = $permissions;
-        return $permissions;
+
+        $this->permissions = $this->newQuery()->getConnection()->table('role_user', 'ru')
+            ->select('role_permissions.name as name')->distinct()
+            ->leftJoin('permission_role', 'ru.role_id', '=', 'permission_role.role_id')
+            ->leftJoin('role_permissions', 'permission_role.permission_id', '=', 'role_permissions.id')
+            ->where('ru.user_id', '=', $this->id)
+            ->get()
+            ->pluck('name');
+
+        return $this->permissions;
     }
 
     /**
-     * Check if the user has a particular permission.
-     * @param $permissionName
-     * @return bool
+     * Clear any cached permissions on this instance.
      */
-    public function can($permissionName)
+    public function clearPermissionCache()
     {
-        if ($this->email === 'guest') {
-            return false;
-        }
-        return $this->permissions()->pluck('name')->contains($permissionName);
+        $this->permissions = null;
     }
 
     /**
index c6856d33963c4fb72a88357819935b9d15e10c0c..d97740d2725f01d8bedda10e687fd915d5e08deb 100644 (file)
@@ -25,6 +25,7 @@ class HomeController extends Controller
                 ->where('draft', '=', true)
                 ->where('created_by', '=', user()->id)
                 ->orderBy('updated_at', 'desc')
+                ->with('book')
                 ->take(6)
                 ->get();
         }
@@ -33,8 +34,11 @@ class HomeController extends Controller
         $recents = $this->isSignedIn() ?
               Views::getUserRecentlyViewed(12*$recentFactor, 1)
             : Book::visible()->orderBy('created_at', 'desc')->take(12 * $recentFactor)->get();
-        $recentlyUpdatedPages = Page::visible()->where('draft', false)
-            ->orderBy('updated_at', 'desc')->take(12)->get();
+        $recentlyUpdatedPages = Page::visible()->with('book')
+            ->where('draft', false)
+            ->orderBy('updated_at', 'desc')
+            ->take(12)
+            ->get();
 
         $homepageOptions = ['default', 'books', 'bookshelves', 'page'];
         $homepageOption = setting('app-homepage-type', 'default');
index e5ed608eb94bcbb975dda6c7cd03e55fac2efbfa..a6a74afed42b6ab92e04ae201d5b85392e219c91 100644 (file)
@@ -178,6 +178,10 @@ body.flexbox {
   display: none !important;
 }
 
+.fill-height {
+  height: 100%;
+}
+
 .float {
   float: left;
   &.right {
index 5acd11af491c4c61ecb434cb08966378ddbd2b97..f580b06cf7cd1caeee8a0bb624e5ba20a8411d10 100644 (file)
@@ -8,7 +8,7 @@
 
 @section('content')
 
-    <div class="flex-fill flex">
+    <div class="flex-fill flex fill-height">
         <form action="{{ $page->getUrl() }}" autocomplete="off" data-page-id="{{ $page->id }}" method="POST" class="flex flex-fill">
             {{ csrf_field() }}
 
index ffcc6f40ca3f0b55ee20e72ded30feaac211f24f..02f7caae1cb859b4c14d105eab864a78eb4ec0fc 100644 (file)
@@ -179,15 +179,13 @@ trait SharedTestHelpers
 
     /**
      * Give the given user some permissions.
-     * @param User $user
-     * @param array $permissions
      */
-    protected function giveUserPermissions(User $user, $permissions = [])
+    protected function giveUserPermissions(User $user, array $permissions = [])
     {
         $newRole = $this->createNewRole($permissions);
         $user->attachRole($newRole);
         $user->load('roles');
-        $user->permissions(false);
+        $user->clearPermissionCache();
     }
 
     /**