mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-07-31 15:24:31 +03:00
Fixed short editor in firefox and optimised some queries
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.
This commit is contained in:
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all permissions belonging to a the current user.
|
||||
* @param bool $cache
|
||||
* @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
|
||||
*/
|
||||
public function permissions($cache = true)
|
||||
{
|
||||
if (isset($this->permissions) && $cache) {
|
||||
return $this->permissions;
|
||||
}
|
||||
$this->load('roles.permissions');
|
||||
$permissions = $this->roles->map(function ($role) {
|
||||
return $role->permissions;
|
||||
})->flatten()->unique();
|
||||
$this->permissions = $permissions;
|
||||
return $permissions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the user has a particular permission.
|
||||
* @param $permissionName
|
||||
* @return bool
|
||||
*/
|
||||
public function can($permissionName)
|
||||
public function can(string $permissionName): bool
|
||||
{
|
||||
if ($this->email === 'guest') {
|
||||
return false;
|
||||
}
|
||||
return $this->permissions()->pluck('name')->contains($permissionName);
|
||||
|
||||
return $this->permissions()->contains($permissionName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all permissions belonging to a the current user.
|
||||
*/
|
||||
protected function permissions(): Collection
|
||||
{
|
||||
if (isset($this->permissions)) {
|
||||
return $this->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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear any cached permissions on this instance.
|
||||
*/
|
||||
public function clearPermissionCache()
|
||||
{
|
||||
$this->permissions = null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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');
|
||||
|
Reference in New Issue
Block a user