Merge branch 'master' into release

This commit is contained in:
Dan Brown 2021-01-03 21:52:00 +00:00
commit 359c067279
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9
388 changed files with 8572 additions and 3811 deletions

View File

@ -255,6 +255,14 @@ APP_VIEWS_BOOKSHELVES=grid
# If set to 'false' a limit will not be enforced. # If set to 'false' a limit will not be enforced.
REVISION_LIMIT=50 REVISION_LIMIT=50
# Recycle Bin Lifetime
# The number of days that content will remain in the recycle bin before
# being considered for auto-removal. It is not a guarantee that content will
# be removed after this time.
# Set to 0 for no recycle bin functionality.
# Set to -1 for unlimited recycle bin lifetime.
RECYCLE_BIN_LIFETIME=30
# Allow <script> tags in page content # Allow <script> tags in page content
# Note, if set to 'true' the page editor may still escape scripts. # Note, if set to 'true' the page editor may still escape scripts.
ALLOW_CONTENT_SCRIPTS=false ALLOW_CONTENT_SCRIPTS=false
@ -265,6 +273,12 @@ ALLOW_CONTENT_SCRIPTS=false
# Contents of the robots.txt file can be overridden, making this option obsolete. # Contents of the robots.txt file can be overridden, making this option obsolete.
ALLOW_ROBOTS=null ALLOW_ROBOTS=null
# A list of hosts that BookStack can be iframed within.
# Space separated if multiple. BookStack host domain is auto-inferred.
# For Example: ALLOWED_IFRAME_HOSTS="https://example.com https://a.example.com"
# Setting this option will also auto-adjust cookies to be SameSite=None.
ALLOWED_IFRAME_HOSTS=null
# The default and maximum item-counts for listing API requests. # The default and maximum item-counts for listing API requests.
API_DEFAULT_ITEM_COUNT=100 API_DEFAULT_ITEM_COUNT=100
API_MAX_ITEM_COUNT=500 API_MAX_ITEM_COUNT=500

3
.github/FUNDING.yml vendored Normal file
View File

@ -0,0 +1,3 @@
# These are supported funding model platforms
github: [ssddanbrown]

17
.github/ISSUE_TEMPLATE/api_request.md vendored Normal file
View File

@ -0,0 +1,17 @@
---
name: New API Endpoint or Feature
about: Request a new endpoint or API feature be added
labels: ":nut_and_bolt: API Request"
---
#### API Endpoint or Feature
Clearly describe what you'd like to have added to the API.
#### Use-Case
Explain the use-case that you're working-on that requires the above request.
#### Additional Context
If required, add any other context about the feature request here.

View File

@ -123,3 +123,12 @@ Jakub Bouček (jakubboucek) :: Czech
Marco (cdrfun) :: German Marco (cdrfun) :: German
10935336 :: Chinese Simplified 10935336 :: Chinese Simplified
孟繁阳 (FanyangMeng) :: Chinese Simplified 孟繁阳 (FanyangMeng) :: Chinese Simplified
Andrej Močan (andrejm) :: Slovenian
gilane9_ :: Arabic
Raed alnahdi (raednahdi) :: Arabic
Xiphoseer :: German
MerlinSVK (merlinsvk) :: Slovak
Kauê Sena (kaue.sena.ks) :: Portuguese, Brazilian
MatthieuParis :: French
Douradinho :: Portuguese, Brazilian
Gaku Yaguchi (tama11) :: Japanese

View File

@ -3,18 +3,19 @@
namespace BookStack\Actions; namespace BookStack\Actions;
use BookStack\Auth\User; use BookStack\Auth\User;
use BookStack\Entities\Entity; use BookStack\Entities\Models\Entity;
use BookStack\Model; use BookStack\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Str;
/** /**
* @property string $key * @property string $type
* @property User $user * @property User $user
* @property Entity $entity * @property Entity $entity
* @property string $extra * @property string $detail
* @property string $entity_type * @property string $entity_type
* @property int $entity_id * @property int $entity_id
* @property int $user_id * @property int $user_id
* @property int $book_id
*/ */
class Activity extends Model class Activity extends Model
{ {
@ -32,20 +33,28 @@ class Activity extends Model
/** /**
* Get the user this activity relates to. * Get the user this activity relates to.
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/ */
public function user() public function user(): BelongsTo
{ {
return $this->belongsTo(User::class); return $this->belongsTo(User::class);
} }
/** /**
* Returns text from the language files, Looks up by using the * Returns text from the language files, Looks up by using the activity key.
* activity key.
*/ */
public function getText() public function getText(): string
{ {
return trans('activities.' . $this->key); return trans('activities.' . $this->type);
}
/**
* Check if this activity is intended to be for an entity.
*/
public function isForEntity(): bool
{
return Str::startsWith($this->type, [
'page_', 'chapter_', 'book_', 'bookshelf_'
]);
} }
/** /**
@ -53,6 +62,6 @@ class Activity extends Model
*/ */
public function isSimilarTo(Activity $activityB): bool public function isSimilarTo(Activity $activityB): bool
{ {
return [$this->key, $this->entity_type, $this->entity_id] === [$activityB->key, $activityB->entity_type, $activityB->entity_id]; return [$this->type, $this->entity_type, $this->entity_id] === [$activityB->type, $activityB->entity_type, $activityB->entity_id];
} }
} }

View File

@ -2,57 +2,59 @@
use BookStack\Auth\Permissions\PermissionService; use BookStack\Auth\Permissions\PermissionService;
use BookStack\Auth\User; use BookStack\Auth\User;
use BookStack\Entities\Entity; use BookStack\Entities\Models\Chapter;
use Illuminate\Support\Collection; use BookStack\Entities\Models\Entity;
use BookStack\Entities\Models\Page;
use BookStack\Interfaces\Loggable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
class ActivityService class ActivityService
{ {
protected $activity; protected $activity;
protected $user;
protected $permissionService; protected $permissionService;
/**
* ActivityService constructor.
*/
public function __construct(Activity $activity, PermissionService $permissionService) public function __construct(Activity $activity, PermissionService $permissionService)
{ {
$this->activity = $activity; $this->activity = $activity;
$this->permissionService = $permissionService; $this->permissionService = $permissionService;
$this->user = user();
} }
/** /**
* Add activity data to database. * Add activity data to database for an entity.
*/ */
public function add(Entity $entity, string $activityKey, ?int $bookId = null) public function addForEntity(Entity $entity, string $type)
{ {
$activity = $this->newActivityForUser($activityKey, $bookId); $activity = $this->newActivityForUser($type);
$entity->activity()->save($activity); $entity->activity()->save($activity);
$this->setNotification($activityKey); $this->setNotification($type);
} }
/** /**
* Adds a activity history with a message, without binding to a entity. * Add a generic activity event to the database.
* @param string|Loggable $detail
*/ */
public function addMessage(string $activityKey, string $message, ?int $bookId = null) public function add(string $type, $detail = '')
{ {
$this->newActivityForUser($activityKey, $bookId)->forceFill([ if ($detail instanceof Loggable) {
'extra' => $message $detail = $detail->logDescriptor();
])->save(); }
$this->setNotification($activityKey); $activity = $this->newActivityForUser($type);
$activity->detail = $detail;
$activity->save();
$this->setNotification($type);
} }
/** /**
* Get a new activity instance for the current user. * Get a new activity instance for the current user.
*/ */
protected function newActivityForUser(string $key, ?int $bookId = null): Activity protected function newActivityForUser(string $type): Activity
{ {
return $this->activity->newInstance()->forceFill([ return $this->activity->newInstance()->forceFill([
'key' => strtolower($key), 'type' => strtolower($type),
'user_id' => $this->user->id, 'user_id' => user()->id,
'book_id' => $bookId ?? 0,
]); ]);
} }
@ -61,15 +63,13 @@ class ActivityService
* and instead uses the 'extra' field with the entities name. * and instead uses the 'extra' field with the entities name.
* Used when an entity is deleted. * Used when an entity is deleted.
*/ */
public function removeEntity(Entity $entity): Collection public function removeEntity(Entity $entity)
{ {
$activities = $entity->activity()->get();
$entity->activity()->update([ $entity->activity()->update([
'extra' => $entity->name, 'detail' => $entity->name,
'entity_id' => 0, 'entity_id' => null,
'entity_type' => '', 'entity_type' => null,
]); ]);
return $activities;
} }
/** /**
@ -94,17 +94,30 @@ class ActivityService
*/ */
public function entityActivity(Entity $entity, int $count = 20, int $page = 1): array public function entityActivity(Entity $entity, int $count = 20, int $page = 1): array
{ {
/** @var [string => int[]] $queryIds */
$queryIds = [$entity->getMorphClass() => [$entity->id]];
if ($entity->isA('book')) { if ($entity->isA('book')) {
$query = $this->activity->newQuery()->where('book_id', '=', $entity->id); $queryIds[(new Chapter)->getMorphClass()] = $entity->chapters()->visible()->pluck('id');
} else { }
$query = $this->activity->newQuery()->where('entity_type', '=', $entity->getMorphClass()) if ($entity->isA('book') || $entity->isA('chapter')) {
->where('entity_id', '=', $entity->id); $queryIds[(new Page)->getMorphClass()] = $entity->pages()->visible()->pluck('id');
} }
$activity = $this->permissionService $query = $this->activity->newQuery();
->filterRestrictedEntityRelations($query, 'activities', 'entity_id', 'entity_type') $query->where(function (Builder $query) use ($queryIds) {
->orderBy('created_at', 'desc') foreach ($queryIds as $morphClass => $idArr) {
->with(['entity', 'user.avatar']) $query->orWhere(function (Builder $innerQuery) use ($morphClass, $idArr) {
$innerQuery->where('entity_type', '=', $morphClass)
->whereIn('entity_id', $idArr);
});
}
});
$activity = $query->orderBy('created_at', 'desc')
->with(['entity' => function (Relation $query) {
$query->withTrashed();
}, 'user.avatar'])
->skip($count * ($page - 1)) ->skip($count * ($page - 1))
->take($count) ->take($count)
->get(); ->get();
@ -152,9 +165,9 @@ class ActivityService
/** /**
* Flashes a notification message to the session if an appropriate message is available. * Flashes a notification message to the session if an appropriate message is available.
*/ */
protected function setNotification(string $activityKey) protected function setNotification(string $type)
{ {
$notificationTextKey = 'activities.' . $activityKey . '_notification'; $notificationTextKey = 'activities.' . $type . '_notification';
if (trans()->has($notificationTextKey)) { if (trans()->has($notificationTextKey)) {
$message = trans($notificationTextKey); $message = trans($notificationTextKey);
session()->flash('success', $message); session()->flash('success', $message);

View File

@ -0,0 +1,51 @@
<?php namespace BookStack\Actions;
class ActivityType
{
const PAGE_CREATE = 'page_create';
const PAGE_UPDATE = 'page_update';
const PAGE_DELETE = 'page_delete';
const PAGE_RESTORE = 'page_restore';
const PAGE_MOVE = 'page_move';
const CHAPTER_CREATE = 'chapter_create';
const CHAPTER_UPDATE = 'chapter_update';
const CHAPTER_DELETE = 'chapter_delete';
const CHAPTER_MOVE = 'chapter_move';
const BOOK_CREATE = 'book_create';
const BOOK_UPDATE = 'book_update';
const BOOK_DELETE = 'book_delete';
const BOOK_SORT = 'book_sort';
const BOOKSHELF_CREATE = 'bookshelf_create';
const BOOKSHELF_UPDATE = 'bookshelf_update';
const BOOKSHELF_DELETE = 'bookshelf_delete';
const COMMENTED_ON = 'commented_on';
const PERMISSIONS_UPDATE = 'permissions_update';
const SETTINGS_UPDATE = 'settings_update';
const MAINTENANCE_ACTION_RUN = 'maintenance_action_run';
const RECYCLE_BIN_EMPTY = 'recycle_bin_empty';
const RECYCLE_BIN_RESTORE = 'recycle_bin_restore';
const RECYCLE_BIN_DESTROY = 'recycle_bin_destroy';
const USER_CREATE = 'user_create';
const USER_UPDATE = 'user_update';
const USER_DELETE = 'user_delete';
const API_TOKEN_CREATE = 'api_token_create';
const API_TOKEN_UPDATE = 'api_token_update';
const API_TOKEN_DELETE = 'api_token_delete';
const ROLE_CREATE = 'role_create';
const ROLE_UPDATE = 'role_update';
const ROLE_DELETE = 'role_delete';
const AUTH_PASSWORD_RESET = 'auth_password_reset_request';
const AUTH_PASSWORD_RESET_UPDATE = 'auth_password_reset_update';
const AUTH_LOGIN = 'auth_login';
const AUTH_REGISTER = 'auth_register';
}

View File

@ -1,6 +1,8 @@
<?php namespace BookStack\Actions; <?php namespace BookStack\Actions;
use BookStack\Ownable; use BookStack\Model;
use BookStack\Traits\HasCreatorAndUpdater;
use Illuminate\Database\Eloquent\Relations\MorphTo;
/** /**
* @property string text * @property string text
@ -8,25 +10,25 @@ use BookStack\Ownable;
* @property int|null parent_id * @property int|null parent_id
* @property int local_id * @property int local_id
*/ */
class Comment extends Ownable class Comment extends Model
{ {
use HasCreatorAndUpdater;
protected $fillable = ['text', 'parent_id']; protected $fillable = ['text', 'parent_id'];
protected $appends = ['created', 'updated']; protected $appends = ['created', 'updated'];
/** /**
* Get the entity that this comment belongs to * Get the entity that this comment belongs to
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
*/ */
public function entity() public function entity(): MorphTo
{ {
return $this->morphTo('entity'); return $this->morphTo('entity');
} }
/** /**
* Check if a comment has been updated since creation. * Check if a comment has been updated since creation.
* @return bool
*/ */
public function isUpdated() public function isUpdated(): bool
{ {
return $this->updated_at->timestamp > $this->created_at->timestamp; return $this->updated_at->timestamp > $this->created_at->timestamp;
} }

View File

@ -1,7 +1,8 @@
<?php namespace BookStack\Actions; <?php namespace BookStack\Actions;
use BookStack\Entities\Entity; use BookStack\Entities\Models\Entity;
use League\CommonMark\CommonMarkConverter; use League\CommonMark\CommonMarkConverter;
use BookStack\Facades\Activity as ActivityService;
/** /**
* Class CommentRepo * Class CommentRepo
@ -44,6 +45,7 @@ class CommentRepo
$comment->parent_id = $parent_id; $comment->parent_id = $parent_id;
$entity->comments()->save($comment); $entity->comments()->save($comment);
ActivityService::addForEntity($entity, ActivityType::COMMENTED_ON);
return $comment; return $comment;
} }

View File

@ -2,14 +2,10 @@
use BookStack\Model; use BookStack\Model;
/**
* Class Attribute
* @package BookStack
*/
class Tag extends Model class Tag extends Model
{ {
protected $fillable = ['name', 'value', 'order']; protected $fillable = ['name', 'value', 'order'];
protected $hidden = ['id', 'entity_id', 'entity_type']; protected $hidden = ['id', 'entity_id', 'entity_type', 'created_at', 'updated_at'];
/** /**
* Get the entity that this tag belongs to * Get the entity that this tag belongs to

View File

@ -1,7 +1,7 @@
<?php namespace BookStack\Actions; <?php namespace BookStack\Actions;
use BookStack\Auth\Permissions\PermissionService; use BookStack\Auth\Permissions\PermissionService;
use BookStack\Entities\Entity; use BookStack\Entities\Models\Entity;
use DB; use DB;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;

View File

@ -1,8 +1,8 @@
<?php namespace BookStack\Actions; <?php namespace BookStack\Actions;
use BookStack\Auth\Permissions\PermissionService; use BookStack\Auth\Permissions\PermissionService;
use BookStack\Entities\Book; use BookStack\Entities\Models\Book;
use BookStack\Entities\Entity; use BookStack\Entities\Models\Entity;
use BookStack\Entities\EntityProvider; use BookStack\Entities\EntityProvider;
use DB; use DB;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
@ -28,7 +28,7 @@ class ViewService
/** /**
* Add a view to the given entity. * Add a view to the given entity.
* @param \BookStack\Entities\Entity $entity * @param \BookStack\Entities\Models\Entity $entity
* @return int * @return int
*/ */
public function add(Entity $entity) public function add(Entity $entity)
@ -74,34 +74,36 @@ class ViewService
$query->whereIn('viewable_type', $this->entityProvider->getMorphClasses($filterModels)); $query->whereIn('viewable_type', $this->entityProvider->getMorphClasses($filterModels));
} }
return $query->with('viewable')->skip($skipCount)->take($count)->get()->pluck('viewable'); return $query->with('viewable')
->skip($skipCount)
->take($count)
->get()
->pluck('viewable')
->filter();
} }
/** /**
* Get all recently viewed entities for the current user. * Get all recently viewed entities for the current user.
* @param int $count
* @param int $page
* @param Entity|bool $filterModel
* @return mixed
*/ */
public function getUserRecentlyViewed($count = 10, $page = 0, $filterModel = false) public function getUserRecentlyViewed(int $count = 10, int $page = 1)
{ {
$user = user(); $user = user();
if ($user === null || $user->isDefault()) { if ($user === null || $user->isDefault()) {
return collect(); return collect();
} }
$query = $this->permissionService $all = collect();
->filterRestrictedEntityRelations($this->view, 'views', 'viewable_id', 'viewable_type'); /** @var Entity $instance */
foreach ($this->entityProvider->all() as $name => $instance) {
if ($filterModel) { $items = $instance::visible()->withLastView()
$query = $query->where('viewable_type', '=', $filterModel->getMorphClass()); ->orderBy('last_viewed_at', 'desc')
->skip($count * ($page - 1))
->take($count)
->get();
$all = $all->concat($items);
} }
$query = $query->where('user_id', '=', $user->id);
$viewables = $query->with('viewable')->orderBy('updated_at', 'desc') return $all->sortByDesc('last_viewed_at')->slice(0, $count);
->skip($count * $page)->take($count)->get()->pluck('viewable');
return $viewables;
} }
/** /**

View File

@ -1,7 +1,9 @@
<?php namespace BookStack\Api; <?php namespace BookStack\Api;
use BookStack\Http\Controllers\Api\ApiController; use BookStack\Http\Controllers\Api\ApiController;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Route; use Illuminate\Support\Facades\Route;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use ReflectionClass; use ReflectionClass;
@ -14,10 +16,27 @@ class ApiDocsGenerator
protected $reflectionClasses = []; protected $reflectionClasses = [];
protected $controllerClasses = []; protected $controllerClasses = [];
/**
* Load the docs form the cache if existing
* otherwise generate and store in the cache.
*/
public static function generateConsideringCache(): Collection
{
$appVersion = trim(file_get_contents(base_path('version')));
$cacheKey = 'api-docs::' . $appVersion;
if (Cache::has($cacheKey) && config('app.env') === 'production') {
$docs = Cache::get($cacheKey);
} else {
$docs = (new static())->generate();
Cache::put($cacheKey, $docs, 60 * 24);
}
return $docs;
}
/** /**
* Generate API documentation. * Generate API documentation.
*/ */
public function generate(): Collection protected function generate(): Collection
{ {
$apiRoutes = $this->getFlatApiRoutes(); $apiRoutes = $this->getFlatApiRoutes();
$apiRoutes = $this->loadDetailsFromControllers($apiRoutes); $apiRoutes = $this->loadDetailsFromControllers($apiRoutes);
@ -58,7 +77,7 @@ class ApiDocsGenerator
/** /**
* Load body params and their rules by inspecting the given class and method name. * Load body params and their rules by inspecting the given class and method name.
* @throws \Illuminate\Contracts\Container\BindingResolutionException * @throws BindingResolutionException
*/ */
protected function getBodyParamsFromClass(string $className, string $methodName): ?array protected function getBodyParamsFromClass(string $className, string $methodName): ?array
{ {

View File

@ -1,11 +1,21 @@
<?php namespace BookStack\Api; <?php namespace BookStack\Api;
use BookStack\Auth\User; use BookStack\Auth\User;
use BookStack\Interfaces\Loggable;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Carbon; use Illuminate\Support\Carbon;
class ApiToken extends Model /**
* Class ApiToken
* @property int $id
* @property string $token_id
* @property string $secret
* @property string $name
* @property Carbon $expires_at
* @property User $user
*/
class ApiToken extends Model implements Loggable
{ {
protected $fillable = ['name', 'expires_at']; protected $fillable = ['name', 'expires_at'];
protected $casts = [ protected $casts = [
@ -28,4 +38,12 @@ class ApiToken extends Model
{ {
return Carbon::now()->addYears(100)->format('Y-m-d'); return Carbon::now()->addYears(100)->format('Y-m-d');
} }
/**
* @inheritdoc
*/
public function logDescriptor(): string
{
return "({$this->id}) {$this->name}; User: {$this->user->logDescriptor()}";
}
} }

View File

@ -15,8 +15,6 @@ use Illuminate\Contracts\Session\Session;
* guard with 'remember' functionality removed. Basic auth and event emission * guard with 'remember' functionality removed. Basic auth and event emission
* has also been removed to keep this simple. Designed to be extended by external * has also been removed to keep this simple. Designed to be extended by external
* Auth Guards. * Auth Guards.
*
* @package Illuminate\Auth
*/ */
class ExternalBaseSessionGuard implements StatefulGuard class ExternalBaseSessionGuard implements StatefulGuard
{ {

View File

@ -9,8 +9,6 @@ namespace BookStack\Auth\Access\Guards;
* into the default laravel 'Guard' auth flow. Instead most of the logic is done * into the default laravel 'Guard' auth flow. Instead most of the logic is done
* via the Saml2 controller & Saml2Service. This class provides a safer, thin * via the Saml2 controller & Saml2Service. This class provides a safer, thin
* version of SessionGuard. * version of SessionGuard.
*
* @package BookStack\Auth\Access\Guards
*/ */
class Saml2SessionGuard extends ExternalBaseSessionGuard class Saml2SessionGuard extends ExternalBaseSessionGuard
{ {

View File

@ -4,7 +4,6 @@
* Class Ldap * Class Ldap
* An object-orientated thin abstraction wrapper for common PHP LDAP functions. * An object-orientated thin abstraction wrapper for common PHP LDAP functions.
* Allows the standard LDAP functions to be mocked for testing. * Allows the standard LDAP functions to be mocked for testing.
* @package BookStack\Services
*/ */
class Ldap class Ldap
{ {

View File

@ -1,9 +1,11 @@
<?php namespace BookStack\Auth\Access; <?php namespace BookStack\Auth\Access;
use BookStack\Actions\ActivityType;
use BookStack\Auth\SocialAccount; use BookStack\Auth\SocialAccount;
use BookStack\Auth\User; use BookStack\Auth\User;
use BookStack\Auth\UserRepo; use BookStack\Auth\UserRepo;
use BookStack\Exceptions\UserRegistrationException; use BookStack\Exceptions\UserRegistrationException;
use BookStack\Facades\Activity;
use Exception; use Exception;
class RegistrationService class RegistrationService
@ -68,6 +70,8 @@ class RegistrationService
$newUser->socialAccounts()->save($socialAccount); $newUser->socialAccounts()->save($socialAccount);
} }
Activity::add(ActivityType::AUTH_REGISTER, $socialAccount ?? $newUser);
// Start email confirmation flow if required // Start email confirmation flow if required
if ($this->emailConfirmationService->confirmationRequired() && !$emailConfirmed) { if ($this->emailConfirmationService->confirmationRequired() && !$emailConfirmed) {
$newUser->save(); $newUser->save();

View File

@ -1,9 +1,11 @@
<?php namespace BookStack\Auth\Access; <?php namespace BookStack\Auth\Access;
use BookStack\Actions\ActivityType;
use BookStack\Auth\User; use BookStack\Auth\User;
use BookStack\Exceptions\JsonDebugException; use BookStack\Exceptions\JsonDebugException;
use BookStack\Exceptions\SamlException; use BookStack\Exceptions\SamlException;
use BookStack\Exceptions\UserRegistrationException; use BookStack\Exceptions\UserRegistrationException;
use BookStack\Facades\Activity;
use Exception; use Exception;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use OneLogin\Saml2\Auth; use OneLogin\Saml2\Auth;
@ -372,6 +374,7 @@ class Saml2Service extends ExternalAuthService
} }
auth()->login($user); auth()->login($user);
Activity::add(ActivityType::AUTH_LOGIN, "saml2; {$user->logDescriptor()}");
return $user; return $user;
} }
} }

View File

@ -1,10 +1,12 @@
<?php namespace BookStack\Auth\Access; <?php namespace BookStack\Auth\Access;
use BookStack\Actions\ActivityType;
use BookStack\Auth\SocialAccount; use BookStack\Auth\SocialAccount;
use BookStack\Auth\UserRepo; use BookStack\Auth\UserRepo;
use BookStack\Exceptions\SocialDriverNotConfigured; use BookStack\Exceptions\SocialDriverNotConfigured;
use BookStack\Exceptions\SocialSignInAccountNotUsed; use BookStack\Exceptions\SocialSignInAccountNotUsed;
use BookStack\Exceptions\UserRegistrationException; use BookStack\Exceptions\UserRegistrationException;
use BookStack\Facades\Activity;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Laravel\Socialite\Contracts\Factory as Socialite; use Laravel\Socialite\Contracts\Factory as Socialite;
use Laravel\Socialite\Contracts\Provider; use Laravel\Socialite\Contracts\Provider;
@ -98,6 +100,7 @@ class SocialAuthService
// Simply log the user into the application. // Simply log the user into the application.
if (!$isLoggedIn && $socialAccount !== null) { if (!$isLoggedIn && $socialAccount !== null) {
auth()->login($socialAccount->user); auth()->login($socialAccount->user);
Activity::add(ActivityType::AUTH_LOGIN, $socialAccount);
return redirect()->intended('/'); return redirect()->intended('/');
} }

View File

@ -1,7 +1,7 @@
<?php namespace BookStack\Auth\Permissions; <?php namespace BookStack\Auth\Permissions;
use BookStack\Auth\Role; use BookStack\Auth\Role;
use BookStack\Entities\Entity; use BookStack\Entities\Models\Entity;
use BookStack\Model; use BookStack\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphOne; use Illuminate\Database\Eloquent\Relations\MorphOne;

View File

@ -2,13 +2,12 @@
use BookStack\Auth\Permissions; use BookStack\Auth\Permissions;
use BookStack\Auth\Role; use BookStack\Auth\Role;
use BookStack\Entities\Book; use BookStack\Entities\Models\Book;
use BookStack\Entities\Bookshelf; use BookStack\Entities\Models\Entity;
use BookStack\Entities\Chapter;
use BookStack\Entities\Entity;
use BookStack\Entities\EntityProvider; use BookStack\Entities\EntityProvider;
use BookStack\Entities\Page; use BookStack\Model;
use BookStack\Ownable; use BookStack\Traits\HasCreatorAndUpdater;
use BookStack\Traits\HasOwner;
use Illuminate\Database\Connection; use Illuminate\Database\Connection;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Query\Builder as QueryBuilder; use Illuminate\Database\Query\Builder as QueryBuilder;
@ -51,11 +50,6 @@ class PermissionService
/** /**
* PermissionService constructor. * PermissionService constructor.
* @param JointPermission $jointPermission
* @param EntityPermission $entityPermission
* @param Role $role
* @param Connection $db
* @param EntityProvider $entityProvider
*/ */
public function __construct( public function __construct(
JointPermission $jointPermission, JointPermission $jointPermission,
@ -82,7 +76,7 @@ class PermissionService
/** /**
* Prepare the local entity cache and ensure it's empty * Prepare the local entity cache and ensure it's empty
* @param \BookStack\Entities\Entity[] $entities * @param \BookStack\Entities\Models\Entity[] $entities
*/ */
protected function readyEntityCache($entities = []) protected function readyEntityCache($entities = [])
{ {
@ -119,7 +113,7 @@ class PermissionService
/** /**
* Get a chapter via ID, Checks local cache * Get a chapter via ID, Checks local cache
* @param $chapterId * @param $chapterId
* @return \BookStack\Entities\Book * @return \BookStack\Entities\Models\Book
*/ */
protected function getChapter($chapterId) protected function getChapter($chapterId)
{ {
@ -176,7 +170,7 @@ class PermissionService
}); });
// Chunk through all bookshelves // Chunk through all bookshelves
$this->entityProvider->bookshelf->newQuery()->select(['id', 'restricted', 'created_by']) $this->entityProvider->bookshelf->newQuery()->withTrashed()->select(['id', 'restricted', 'owned_by'])
->chunk(50, function ($shelves) use ($roles) { ->chunk(50, function ($shelves) use ($roles) {
$this->buildJointPermissionsForShelves($shelves, $roles); $this->buildJointPermissionsForShelves($shelves, $roles);
}); });
@ -188,11 +182,11 @@ class PermissionService
*/ */
protected function bookFetchQuery() protected function bookFetchQuery()
{ {
return $this->entityProvider->book->newQuery() return $this->entityProvider->book->withTrashed()->newQuery()
->select(['id', 'restricted', 'created_by'])->with(['chapters' => function ($query) { ->select(['id', 'restricted', 'owned_by'])->with(['chapters' => function ($query) {
$query->select(['id', 'restricted', 'created_by', 'book_id']); $query->withTrashed()->select(['id', 'restricted', 'owned_by', 'book_id']);
}, 'pages' => function ($query) { }, 'pages' => function ($query) {
$query->select(['id', 'restricted', 'created_by', 'book_id', 'chapter_id']); $query->withTrashed()->select(['id', 'restricted', 'owned_by', 'book_id', 'chapter_id']);
}]); }]);
} }
@ -238,7 +232,7 @@ class PermissionService
/** /**
* Rebuild the entity jointPermissions for a particular entity. * Rebuild the entity jointPermissions for a particular entity.
* @param \BookStack\Entities\Entity $entity * @param \BookStack\Entities\Models\Entity $entity
* @throws \Throwable * @throws \Throwable
*/ */
public function buildJointPermissionsForEntity(Entity $entity) public function buildJointPermissionsForEntity(Entity $entity)
@ -294,7 +288,7 @@ class PermissionService
}); });
// Chunk through all bookshelves // Chunk through all bookshelves
$this->entityProvider->bookshelf->newQuery()->select(['id', 'restricted', 'created_by']) $this->entityProvider->bookshelf->newQuery()->select(['id', 'restricted', 'owned_by'])
->chunk(50, function ($shelves) use ($roles) { ->chunk(50, function ($shelves) use ($roles) {
$this->buildJointPermissionsForShelves($shelves, $roles); $this->buildJointPermissionsForShelves($shelves, $roles);
}); });
@ -333,7 +327,7 @@ class PermissionService
/** /**
* Delete all of the entity jointPermissions for a list of entities. * Delete all of the entity jointPermissions for a list of entities.
* @param \BookStack\Entities\Entity[] $entities * @param \BookStack\Entities\Models\Entity[] $entities
* @throws \Throwable * @throws \Throwable
*/ */
protected function deleteManyJointPermissionsForEntities($entities) protected function deleteManyJointPermissionsForEntities($entities)
@ -414,7 +408,7 @@ class PermissionService
/** /**
* Get the actions related to an entity. * Get the actions related to an entity.
* @param \BookStack\Entities\Entity $entity * @param \BookStack\Entities\Models\Entity $entity
* @return array * @return array
*/ */
protected function getActions(Entity $entity) protected function getActions(Entity $entity)
@ -500,7 +494,7 @@ class PermissionService
/** /**
* Create an array of data with the information of an entity jointPermissions. * Create an array of data with the information of an entity jointPermissions.
* Used to build data for bulk insertion. * Used to build data for bulk insertion.
* @param \BookStack\Entities\Entity $entity * @param \BookStack\Entities\Models\Entity $entity
* @param Role $role * @param Role $role
* @param $action * @param $action
* @param $permissionAll * @param $permissionAll
@ -516,21 +510,19 @@ class PermissionService
'action' => $action, 'action' => $action,
'has_permission' => $permissionAll, 'has_permission' => $permissionAll,
'has_permission_own' => $permissionOwn, 'has_permission_own' => $permissionOwn,
'created_by' => $entity->getRawAttribute('created_by') 'owned_by' => $entity->getRawAttribute('owned_by')
]; ];
} }
/** /**
* Checks if an entity has a restriction set upon it. * Checks if an entity has a restriction set upon it.
* @param Ownable $ownable * @param HasCreatorAndUpdater|HasOwner $ownable
* @param $permission
* @return bool
*/ */
public function checkOwnableUserAccess(Ownable $ownable, $permission) public function checkOwnableUserAccess(Model $ownable, string $permission): bool
{ {
$explodedPermission = explode('-', $permission); $explodedPermission = explode('-', $permission);
$baseQuery = $ownable->where('id', '=', $ownable->id); $baseQuery = $ownable->newQuery()->where('id', '=', $ownable->id);
$action = end($explodedPermission); $action = end($explodedPermission);
$this->currentAction = $action; $this->currentAction = $action;
@ -574,7 +566,7 @@ class PermissionService
$query->where('has_permission', '=', 1) $query->where('has_permission', '=', 1)
->orWhere(function ($query2) use ($userId) { ->orWhere(function ($query2) use ($userId) {
$query2->where('has_permission_own', '=', 1) $query2->where('has_permission_own', '=', 1)
->where('created_by', '=', $userId); ->where('owned_by', '=', $userId);
}); });
}); });
@ -591,7 +583,7 @@ class PermissionService
/** /**
* Check if an entity has restrictions set on itself or its * Check if an entity has restrictions set on itself or its
* parent tree. * parent tree.
* @param \BookStack\Entities\Entity $entity * @param \BookStack\Entities\Models\Entity $entity
* @param $action * @param $action
* @return bool|mixed * @return bool|mixed
*/ */
@ -623,7 +615,7 @@ class PermissionService
$query->where('has_permission', '=', true) $query->where('has_permission', '=', true)
->orWhere(function ($query) { ->orWhere(function ($query) {
$query->where('has_permission_own', '=', true) $query->where('has_permission_own', '=', true)
->where('created_by', '=', $this->currentUser()->id); ->where('owned_by', '=', $this->currentUser()->id);
}); });
}); });
}); });
@ -647,7 +639,7 @@ class PermissionService
$query->where('has_permission', '=', true) $query->where('has_permission', '=', true)
->orWhere(function (Builder $query) { ->orWhere(function (Builder $query) {
$query->where('has_permission_own', '=', true) $query->where('has_permission_own', '=', true)
->where('created_by', '=', $this->currentUser()->id); ->where('owned_by', '=', $this->currentUser()->id);
}); });
}); });
}); });
@ -664,7 +656,7 @@ class PermissionService
$query->where('draft', '=', false) $query->where('draft', '=', false)
->orWhere(function (Builder $query) { ->orWhere(function (Builder $query) {
$query->where('draft', '=', true) $query->where('draft', '=', true)
->where('created_by', '=', $this->currentUser()->id); ->where('owned_by', '=', $this->currentUser()->id);
}); });
}); });
} }
@ -672,7 +664,7 @@ class PermissionService
/** /**
* Add restrictions for a generic entity * Add restrictions for a generic entity
* @param string $entityType * @param string $entityType
* @param Builder|\BookStack\Entities\Entity $query * @param Builder|\BookStack\Entities\Models\Entity $query
* @param string $action * @param string $action
* @return Builder * @return Builder
*/ */
@ -684,7 +676,7 @@ class PermissionService
$query->where('draft', '=', false) $query->where('draft', '=', false)
->orWhere(function ($query) { ->orWhere(function ($query) {
$query->where('draft', '=', true) $query->where('draft', '=', true)
->where('created_by', '=', $this->currentUser()->id); ->where('owned_by', '=', $this->currentUser()->id);
}); });
}); });
} }
@ -718,7 +710,7 @@ class PermissionService
->where(function ($query) { ->where(function ($query) {
$query->where('has_permission', '=', true)->orWhere(function ($query) { $query->where('has_permission', '=', true)->orWhere(function ($query) {
$query->where('has_permission_own', '=', true) $query->where('has_permission_own', '=', true)
->where('created_by', '=', $this->currentUser()->id); ->where('owned_by', '=', $this->currentUser()->id);
}); });
}); });
}); });
@ -754,7 +746,7 @@ class PermissionService
->where(function ($query) { ->where(function ($query) {
$query->where('has_permission', '=', true)->orWhere(function ($query) { $query->where('has_permission', '=', true)->orWhere(function ($query) {
$query->where('has_permission_own', '=', true) $query->where('has_permission_own', '=', true)
->where('created_by', '=', $this->currentUser()->id); ->where('owned_by', '=', $this->currentUser()->id);
}); });
}); });
}); });

View File

@ -1,10 +1,11 @@
<?php namespace BookStack\Auth\Permissions; <?php namespace BookStack\Auth\Permissions;
use BookStack\Actions\ActivityType;
use BookStack\Auth\Role; use BookStack\Auth\Role;
use BookStack\Exceptions\PermissionsException; use BookStack\Exceptions\PermissionsException;
use BookStack\Facades\Activity;
use Exception; use Exception;
use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Str;
class PermissionsRepo class PermissionsRepo
{ {
@ -60,6 +61,7 @@ class PermissionsRepo
$permissions = isset($roleData['permissions']) ? array_keys($roleData['permissions']) : []; $permissions = isset($roleData['permissions']) ? array_keys($roleData['permissions']) : [];
$this->assignRolePermissions($role, $permissions); $this->assignRolePermissions($role, $permissions);
$this->permissionService->buildJointPermissionForRole($role); $this->permissionService->buildJointPermissionForRole($role);
Activity::add(ActivityType::ROLE_CREATE, $role);
return $role; return $role;
} }
@ -88,12 +90,13 @@ class PermissionsRepo
$role->fill($roleData); $role->fill($roleData);
$role->save(); $role->save();
$this->permissionService->buildJointPermissionForRole($role); $this->permissionService->buildJointPermissionForRole($role);
Activity::add(ActivityType::ROLE_UPDATE, $role);
} }
/** /**
* Assign an list of permission names to an role. * Assign an list of permission names to an role.
*/ */
public function assignRolePermissions(Role $role, array $permissionNameArray = []) protected function assignRolePermissions(Role $role, array $permissionNameArray = [])
{ {
$permissions = []; $permissions = [];
$permissionNameArray = array_values($permissionNameArray); $permissionNameArray = array_values($permissionNameArray);
@ -137,6 +140,7 @@ class PermissionsRepo
} }
$this->permissionService->deleteJointPermissionsForRole($role); $this->permissionService->deleteJointPermissionsForRole($role);
Activity::add(ActivityType::ROLE_DELETE, $role);
$role->delete(); $role->delete();
} }
} }

View File

@ -2,8 +2,10 @@
use BookStack\Auth\Permissions\JointPermission; use BookStack\Auth\Permissions\JointPermission;
use BookStack\Auth\Permissions\RolePermission; use BookStack\Auth\Permissions\RolePermission;
use BookStack\Interfaces\Loggable;
use BookStack\Model; use BookStack\Model;
use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasMany;
/** /**
@ -14,7 +16,7 @@ use Illuminate\Database\Eloquent\Relations\HasMany;
* @property string $external_auth_id * @property string $external_auth_id
* @property string $system_name * @property string $system_name
*/ */
class Role extends Model class Role extends Model implements Loggable
{ {
protected $fillable = ['display_name', 'description', 'external_auth_id']; protected $fillable = ['display_name', 'description', 'external_auth_id'];
@ -22,7 +24,7 @@ class Role extends Model
/** /**
* The roles that belong to the role. * The roles that belong to the role.
*/ */
public function users() public function users(): BelongsToMany
{ {
return $this->belongsToMany(User::class)->orderBy('name', 'asc'); return $this->belongsToMany(User::class)->orderBy('name', 'asc');
} }
@ -38,7 +40,7 @@ class Role extends Model
/** /**
* The RolePermissions that belong to the role. * The RolePermissions that belong to the role.
*/ */
public function permissions() public function permissions(): BelongsToMany
{ {
return $this->belongsToMany(RolePermission::class, 'permission_role', 'role_id', 'permission_id'); return $this->belongsToMany(RolePermission::class, 'permission_role', 'role_id', 'permission_id');
} }
@ -104,4 +106,12 @@ class Role extends Model
{ {
return static::query()->where('system_name', '!=', 'admin')->get(); return static::query()->where('system_name', '!=', 'admin')->get();
} }
/**
* @inheritdoc
*/
public function logDescriptor(): string
{
return "({$this->id}) {$this->display_name}";
}
} }

View File

@ -1,8 +1,14 @@
<?php namespace BookStack\Auth; <?php namespace BookStack\Auth;
use BookStack\Interfaces\Loggable;
use BookStack\Model; use BookStack\Model;
class SocialAccount extends Model /**
* Class SocialAccount
* @property string $driver
* @property User $user
*/
class SocialAccount extends Model implements Loggable
{ {
protected $fillable = ['user_id', 'driver', 'driver_id', 'timestamps']; protected $fillable = ['user_id', 'driver', 'driver_id', 'timestamps'];
@ -11,4 +17,12 @@ class SocialAccount extends Model
{ {
return $this->belongsTo(User::class); return $this->belongsTo(User::class);
} }
/**
* @inheritDoc
*/
public function logDescriptor(): string
{
return "{$this->driver}; {$this->user->logDescriptor()}";
}
} }

View File

@ -1,6 +1,8 @@
<?php namespace BookStack\Auth; <?php namespace BookStack\Auth;
use BookStack\Actions\Activity;
use BookStack\Api\ApiToken; use BookStack\Api\ApiToken;
use BookStack\Interfaces\Loggable;
use BookStack\Model; use BookStack\Model;
use BookStack\Notifications\ResetPassword; use BookStack\Notifications\ResetPassword;
use BookStack\Uploads\Image; use BookStack\Uploads\Image;
@ -11,11 +13,12 @@ use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract; use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Notifications\Notifiable; use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Collection;
/** /**
* Class User * Class User
* @package BookStack\Auth
* @property string $id * @property string $id
* @property string $name * @property string $name
* @property string $email * @property string $email
@ -27,7 +30,7 @@ use Illuminate\Notifications\Notifiable;
* @property string $external_auth_id * @property string $external_auth_id
* @property string $system_name * @property string $system_name
*/ */
class User extends Model implements AuthenticatableContract, CanResetPasswordContract class User extends Model implements AuthenticatableContract, CanResetPasswordContract, Loggable
{ {
use Authenticatable, CanResetPassword, Notifiable; use Authenticatable, CanResetPassword, Notifiable;
@ -54,7 +57,7 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon
/** /**
* This holds the user's permissions when loaded. * This holds the user's permissions when loaded.
* @var array * @var ?Collection
*/ */
protected $permissions; protected $permissions;
@ -128,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. * 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') { if ($this->email === 'guest') {
return false; 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;
} }
/** /**
@ -229,6 +241,14 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon
return $this->hasMany(ApiToken::class); return $this->hasMany(ApiToken::class);
} }
/**
* Get the latest activity instance for this user.
*/
public function latestActivity(): HasOne
{
return $this->hasOne(Activity::class)->latest();
}
/** /**
* Get the url for editing this user. * Get the url for editing this user.
*/ */
@ -274,4 +294,12 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon
{ {
$this->notify(new ResetPassword($token)); $this->notify(new ResetPassword($token));
} }
/**
* @inheritdoc
*/
public function logDescriptor(): string
{
return "({$this->id}) {$this->name}";
}
} }

View File

@ -1,31 +1,32 @@
<?php namespace BookStack\Auth; <?php namespace BookStack\Auth;
use Activity; use Activity;
use BookStack\Entities\Book; use BookStack\Entities\EntityProvider;
use BookStack\Entities\Bookshelf; use BookStack\Entities\Models\Book;
use BookStack\Entities\Chapter; use BookStack\Entities\Models\Bookshelf;
use BookStack\Entities\Page; use BookStack\Entities\Models\Chapter;
use BookStack\Entities\Models\Page;
use BookStack\Exceptions\NotFoundException; use BookStack\Exceptions\NotFoundException;
use BookStack\Exceptions\UserUpdateException; use BookStack\Exceptions\UserUpdateException;
use BookStack\Uploads\Image; use BookStack\Uploads\Image;
use BookStack\Uploads\UserAvatars;
use Exception; use Exception;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Pagination\LengthAwarePaginator;
use Images; use Images;
use Log; use Log;
class UserRepo class UserRepo
{ {
protected $userAvatar;
protected $user;
protected $role;
/** /**
* UserRepo constructor. * UserRepo constructor.
*/ */
public function __construct(User $user, Role $role) public function __construct(UserAvatars $userAvatar)
{ {
$this->user = $user; $this->userAvatar = $userAvatar;
$this->role = $role;
} }
/** /**
@ -33,36 +34,40 @@ class UserRepo
*/ */
public function getByEmail(string $email): ?User public function getByEmail(string $email): ?User
{ {
return $this->user->where('email', '=', $email)->first(); return User::query()->where('email', '=', $email)->first();
} }
/** /**
* @param int $id * Get a user by their ID.
* @return User
*/ */
public function getById($id) public function getById(int $id): User
{ {
return $this->user->newQuery()->findOrFail($id); return User::query()->findOrFail($id);
} }
/** /**
* Get all the users with their permissions. * Get all the users with their permissions.
* @return Builder|static
*/ */
public function getAllUsers() public function getAllUsers(): Collection
{ {
return $this->user->with('roles', 'avatar')->orderBy('name', 'asc')->get(); return User::query()->with('roles', 'avatar')->orderBy('name', 'asc')->get();
} }
/** /**
* Get all the users with their permissions in a paginated format. * Get all the users with their permissions in a paginated format.
* @param int $count
* @param $sortData
* @return Builder|static
*/ */
public function getAllUsersPaginatedAndSorted($count, $sortData) public function getAllUsersPaginatedAndSorted(int $count, array $sortData): LengthAwarePaginator
{ {
$query = $this->user->with('roles', 'avatar')->orderBy($sortData['sort'], $sortData['order']); $sort = $sortData['sort'];
if ($sort === 'latest_activity') {
$sort = \BookStack\Actions\Activity::query()->select('created_at')
->whereColumn('activities.user_id', 'users.id')
->latest()
->take(1);
}
$query = User::query()->with(['roles', 'avatar', 'latestActivity'])
->orderBy($sort, $sortData['order']);
if ($sortData['search']) { if ($sortData['search']) {
$term = '%' . $sortData['search'] . '%'; $term = '%' . $sortData['search'] . '%';
@ -89,14 +94,12 @@ class UserRepo
/** /**
* Assign a user to a system-level role. * Assign a user to a system-level role.
* @param User $user
* @param $systemRoleName
* @throws NotFoundException * @throws NotFoundException
*/ */
public function attachSystemRole(User $user, $systemRoleName) public function attachSystemRole(User $user, string $systemRoleName)
{ {
$role = $this->role->newQuery()->where('system_name', '=', $systemRoleName)->first(); $role = Role::getSystemRole($systemRoleName);
if ($role === null) { if (is_null($role)) {
throw new NotFoundException("Role '{$systemRoleName}' not found"); throw new NotFoundException("Role '{$systemRoleName}' not found");
} }
$user->attachRole($role); $user->attachRole($role);
@ -104,26 +107,23 @@ class UserRepo
/** /**
* Checks if the give user is the only admin. * Checks if the give user is the only admin.
* @param User $user
* @return bool
*/ */
public function isOnlyAdmin(User $user) public function isOnlyAdmin(User $user): bool
{ {
if (!$user->hasSystemRole('admin')) { if (!$user->hasSystemRole('admin')) {
return false; return false;
} }
$adminRole = $this->role->getSystemRole('admin'); $adminRole = Role::getSystemRole('admin');
if ($adminRole->users->count() > 1) { if ($adminRole->users()->count() > 1) {
return false; return false;
} }
return true; return true;
} }
/** /**
* Set the assigned user roles via an array of role IDs. * Set the assigned user roles via an array of role IDs.
* @param User $user
* @param array $roles
* @throws UserUpdateException * @throws UserUpdateException
*/ */
public function setUserRoles(User $user, array $roles) public function setUserRoles(User $user, array $roles)
@ -138,14 +138,11 @@ class UserRepo
/** /**
* Check if the given user is the last admin and their new roles no longer * Check if the given user is the last admin and their new roles no longer
* contains the admin role. * contains the admin role.
* @param User $user
* @param array $newRoles
* @return bool
*/ */
protected function demotingLastAdmin(User $user, array $newRoles) : bool protected function demotingLastAdmin(User $user, array $newRoles) : bool
{ {
if ($this->isOnlyAdmin($user)) { if ($this->isOnlyAdmin($user)) {
$adminRole = $this->role->getSystemRole('admin'); $adminRole = Role::getSystemRole('admin');
if (!in_array(strval($adminRole->id), $newRoles)) { if (!in_array(strval($adminRole->id), $newRoles)) {
return true; return true;
} }
@ -159,41 +156,59 @@ class UserRepo
*/ */
public function create(array $data, bool $emailConfirmed = false): User public function create(array $data, bool $emailConfirmed = false): User
{ {
return $this->user->forceCreate([ $details = [
'name' => $data['name'], 'name' => $data['name'],
'email' => $data['email'], 'email' => $data['email'],
'password' => bcrypt($data['password']), 'password' => bcrypt($data['password']),
'email_confirmed' => $emailConfirmed, 'email_confirmed' => $emailConfirmed,
'external_auth_id' => $data['external_auth_id'] ?? '', 'external_auth_id' => $data['external_auth_id'] ?? '',
]); ];
return User::query()->forceCreate($details);
} }
/** /**
* Remove the given user from storage, Delete all related content. * Remove the given user from storage, Delete all related content.
* @param User $user
* @throws Exception * @throws Exception
*/ */
public function destroy(User $user) public function destroy(User $user, ?int $newOwnerId = null)
{ {
$user->socialAccounts()->delete(); $user->socialAccounts()->delete();
$user->apiTokens()->delete(); $user->apiTokens()->delete();
$user->delete(); $user->delete();
// Delete user profile images // Delete user profile images
$profileImages = Image::where('type', '=', 'user')->where('uploaded_to', '=', $user->id)->get(); $profileImages = Image::query()->where('type', '=', 'user')
->where('uploaded_to', '=', $user->id)
->get();
foreach ($profileImages as $image) { foreach ($profileImages as $image) {
Images::destroy($image); Images::destroy($image);
} }
if (!empty($newOwnerId)) {
$newOwner = User::query()->find($newOwnerId);
if (!is_null($newOwner)) {
$this->migrateOwnership($user, $newOwner);
}
}
}
/**
* Migrate ownership of items in the system from one user to another.
*/
protected function migrateOwnership(User $fromUser, User $toUser)
{
$entities = (new EntityProvider)->all();
foreach ($entities as $instance) {
$instance->newQuery()->where('owned_by', '=', $fromUser->id)
->update(['owned_by' => $toUser->id]);
}
} }
/** /**
* Get the latest activity for a user. * Get the latest activity for a user.
* @param User $user
* @param int $count
* @param int $page
* @return array
*/ */
public function getActivity(User $user, $count = 20, $page = 0) public function getActivity(User $user, int $count = 20, int $page = 0): array
{ {
return Activity::userActivity($user, $count, $page); return Activity::userActivity($user, $count, $page);
} }
@ -234,33 +249,22 @@ class UserRepo
/** /**
* Get the roles in the system that are assignable to a user. * Get the roles in the system that are assignable to a user.
* @return mixed
*/ */
public function getAllRoles() public function getAllRoles(): Collection
{ {
return $this->role->newQuery()->orderBy('display_name', 'asc')->get(); return Role::query()->orderBy('display_name', 'asc')->get();
} }
/** /**
* Get an avatar image for a user and set it as their avatar. * Get an avatar image for a user and set it as their avatar.
* Returns early if avatars disabled or not set in config. * Returns early if avatars disabled or not set in config.
* @param User $user
* @return bool
*/ */
public function downloadAndAssignUserAvatar(User $user) public function downloadAndAssignUserAvatar(User $user): void
{ {
if (!Images::avatarFetchEnabled()) {
return false;
}
try { try {
$avatar = Images::saveUserAvatar($user); $this->userAvatar->fetchAndAssignToUser($user);
$user->avatar()->associate($avatar);
$user->save();
return true;
} catch (Exception $e) { } catch (Exception $e) {
Log::error('Failed to save user avatar image'); Log::error('Failed to save user avatar image');
return false;
} }
} }
} }

View File

@ -31,6 +31,13 @@ return [
// If set to false then a limit will not be enforced. // If set to false then a limit will not be enforced.
'revision_limit' => env('REVISION_LIMIT', 50), 'revision_limit' => env('REVISION_LIMIT', 50),
// The number of days that content will remain in the recycle bin before
// being considered for auto-removal. It is not a guarantee that content will
// be removed after this time.
// Set to 0 for no recycle bin functionality.
// Set to -1 for unlimited recycle bin lifetime.
'recycle_bin_lifetime' => env('RECYCLE_BIN_LIFETIME', 30),
// Allow <script> tags to entered within page content. // Allow <script> tags to entered within page content.
// <script> tags are escaped by default. // <script> tags are escaped by default.
// Even when overridden the WYSIWYG editor may still escape script content. // Even when overridden the WYSIWYG editor may still escape script content.
@ -45,6 +52,10 @@ return [
// and used by BookStack in URL generation. // and used by BookStack in URL generation.
'url' => env('APP_URL', '') === 'http://bookstack.dev' ? '' : env('APP_URL', ''), 'url' => env('APP_URL', '') === 'http://bookstack.dev' ? '' : env('APP_URL', ''),
// A list of hosts that BookStack can be iframed within.
// Space separated if multiple. BookStack host domain is auto-inferred.
'iframe_hosts' => env('ALLOWED_IFRAME_HOSTS', null),
// Application timezone for back-end date functions. // Application timezone for back-end date functions.
'timezone' => env('APP_TIMEZONE', 'UTC'), 'timezone' => env('APP_TIMEZONE', 'UTC'),
@ -52,7 +63,7 @@ return [
'locale' => env('APP_LANG', 'en'), 'locale' => env('APP_LANG', 'en'),
// Locales available // Locales available
'locales' => ['en', 'ar', 'bg', 'cs', 'da', 'de', 'de_informal', 'es', 'es_AR', 'fa', 'fr', 'he', 'hu', 'it', 'ja', 'ko', 'nl', 'pt', 'pt_BR', 'sk', 'sl', 'sv', 'pl', 'ru', 'th', 'tr', 'uk', 'vi', 'zh_CN', 'zh_TW',], 'locales' => ['en', 'ar', 'bg', 'cs', 'da', 'de', 'de_informal', 'es', 'es_AR', 'fa', 'fr', 'he', 'hu', 'it', 'ja', 'ko', 'nl', 'nb', 'pt', 'pt_BR', 'sk', 'sl', 'sv', 'pl', 'ru', 'th', 'tr', 'uk', 'vi', 'zh_CN', 'zh_TW',],
// Application Fallback Locale // Application Fallback Locale
'fallback_locale' => 'en', 'fallback_locale' => 'en',
@ -117,6 +128,7 @@ return [
BookStack\Providers\EventServiceProvider::class, BookStack\Providers\EventServiceProvider::class,
BookStack\Providers\RouteServiceProvider::class, BookStack\Providers\RouteServiceProvider::class,
BookStack\Providers\CustomFacadeProvider::class, BookStack\Providers\CustomFacadeProvider::class,
BookStack\Providers\CustomValidationServiceProvider::class,
], ],
/* /*

View File

@ -1,5 +1,7 @@
<?php <?php
use \Illuminate\Support\Str;
/** /**
* Session configuration options. * Session configuration options.
* *
@ -69,7 +71,8 @@ return [
// By setting this option to true, session cookies will only be sent back // By setting this option to true, session cookies will only be sent back
// to the server if the browser has a HTTPS connection. This will keep // to the server if the browser has a HTTPS connection. This will keep
// the cookie from being sent to you if it can not be done securely. // the cookie from being sent to you if it can not be done securely.
'secure' => env('SESSION_SECURE_COOKIE', false), 'secure' => env('SESSION_SECURE_COOKIE', null)
?? Str::startsWith(env('APP_URL'), 'https:'),
// HTTP Access Only // HTTP Access Only
// Setting this value to true will prevent JavaScript from accessing the // Setting this value to true will prevent JavaScript from accessing the
@ -80,6 +83,6 @@ return [
// This option determines how your cookies behave when cross-site requests // This option determines how your cookies behave when cross-site requests
// take place, and can be used to mitigate CSRF attacks. By default, we // take place, and can be used to mitigate CSRF attacks. By default, we
// do not enable this as other CSRF protection services are in place. // do not enable this as other CSRF protection services are in place.
// Options: lax, strict // Options: lax, strict, none
'same_site' => null, 'same_site' => 'lax',
]; ];

View File

@ -14,8 +14,8 @@ class CleanupImages extends Command
* @var string * @var string
*/ */
protected $signature = 'bookstack:cleanup-images protected $signature = 'bookstack:cleanup-images
{--a|all : Include images that are used in page revisions} {--a|all : Also delete images that are only used in old revisions}
{--f|force : Actually run the deletions} {--f|force : Actually run the deletions, Defaults to a dry-run}
'; ';
/** /**

View File

@ -2,7 +2,7 @@
namespace BookStack\Console\Commands; namespace BookStack\Console\Commands;
use BookStack\Entities\PageRevision; use BookStack\Entities\Models\PageRevision;
use Illuminate\Console\Command; use Illuminate\Console\Command;
class ClearRevisions extends Command class ClearRevisions extends Command

View File

@ -2,7 +2,7 @@
namespace BookStack\Console\Commands; namespace BookStack\Console\Commands;
use BookStack\Entities\Bookshelf; use BookStack\Entities\Models\Bookshelf;
use BookStack\Entities\Repos\BookshelfRepo; use BookStack\Entities\Repos\BookshelfRepo;
use Illuminate\Console\Command; use Illuminate\Console\Command;

View File

@ -28,8 +28,6 @@ class CreateAdmin extends Command
/** /**
* Create a new command instance. * Create a new command instance.
*
* @param UserRepo $userRepo
*/ */
public function __construct(UserRepo $userRepo) public function __construct(UserRepo $userRepo)
{ {

View File

@ -2,7 +2,7 @@
namespace BookStack\Console\Commands; namespace BookStack\Console\Commands;
use BookStack\Entities\SearchService; use BookStack\Entities\Tools\SearchIndex;
use DB; use DB;
use Illuminate\Console\Command; use Illuminate\Console\Command;
@ -22,17 +22,15 @@ class RegenerateSearch extends Command
*/ */
protected $description = 'Re-index all content for searching'; protected $description = 'Re-index all content for searching';
protected $searchService; protected $searchIndex;
/** /**
* Create a new command instance. * Create a new command instance.
*
* @param SearchService $searchService
*/ */
public function __construct(SearchService $searchService) public function __construct(SearchIndex $searchIndex)
{ {
parent::__construct(); parent::__construct();
$this->searchService = $searchService; $this->searchIndex = $searchIndex;
} }
/** /**
@ -45,10 +43,9 @@ class RegenerateSearch extends Command
$connection = DB::getDefaultConnection(); $connection = DB::getDefaultConnection();
if ($this->option('database') !== null) { if ($this->option('database') !== null) {
DB::setDefaultConnection($this->option('database')); DB::setDefaultConnection($this->option('database'));
$this->searchService->setConnection(DB::connection($this->option('database')));
} }
$this->searchService->indexAllEntities(); $this->searchIndex->indexAllEntities();
DB::setDefaultConnection($connection); DB::setDefaultConnection($connection);
$this->comment('Search index regenerated'); $this->comment('Search index regenerated');
} }

View File

@ -1,6 +1,7 @@
<?php namespace BookStack\Entities; <?php namespace BookStack\Entities;
use BookStack\Entities\Managers\EntityContext; use BookStack\Entities\Models\Book;
use BookStack\Entities\Tools\ShelfContext;
use Illuminate\View\View; use Illuminate\View\View;
class BreadcrumbsViewComposer class BreadcrumbsViewComposer
@ -10,9 +11,9 @@ class BreadcrumbsViewComposer
/** /**
* BreadcrumbsViewComposer constructor. * BreadcrumbsViewComposer constructor.
* @param EntityContext $entityContextManager * @param ShelfContext $entityContextManager
*/ */
public function __construct(EntityContext $entityContextManager) public function __construct(ShelfContext $entityContextManager)
{ {
$this->entityContextManager = $entityContextManager; $this->entityContextManager = $entityContextManager;
} }

View File

@ -1,64 +0,0 @@
<?php namespace BookStack\Entities;
use Illuminate\Support\Collection;
/**
* Class Chapter
* @property Collection<Page> $pages
*/
class Chapter extends BookChild
{
public $searchFactor = 1.3;
protected $fillable = ['name', 'description', 'priority', 'book_id'];
protected $hidden = ['restricted', 'pivot'];
/**
* Get the pages that this chapter contains.
* @param string $dir
* @return mixed
*/
public function pages($dir = 'ASC')
{
return $this->hasMany(Page::class)->orderBy('priority', $dir);
}
/**
* Get the url of this chapter.
* @param string|bool $path
* @return string
*/
public function getUrl($path = false)
{
$bookSlug = $this->getAttribute('bookSlug') ? $this->getAttribute('bookSlug') : $this->book->slug;
$fullPath = '/books/' . urlencode($bookSlug) . '/chapter/' . urlencode($this->slug);
if ($path !== false) {
$fullPath .= '/' . trim($path, '/');
}
return url($fullPath);
}
/**
* Get an excerpt of this chapter's description to the specified length or less.
* @param int $length
* @return string
*/
public function getExcerpt(int $length = 100)
{
$description = $this->text ?? $this->description;
return mb_strlen($description) > $length ? mb_substr($description, 0, $length-3) . '...' : $description;
}
/**
* Get the visible pages in this chapter.
*/
public function getVisiblePages(): Collection
{
return $this->pages()->visible()
->orderBy('draft', 'desc')
->orderBy('priority', 'asc')
->get();
}
}

View File

@ -1,13 +1,18 @@
<?php namespace BookStack\Entities; <?php namespace BookStack\Entities;
use BookStack\Entities\Models\Book;
use BookStack\Entities\Models\Bookshelf;
use BookStack\Entities\Models\Chapter;
use BookStack\Entities\Models\Entity;
use BookStack\Entities\Models\Page;
use BookStack\Entities\Models\PageRevision;
/** /**
* Class EntityProvider * Class EntityProvider
* *
* Provides access to the core entity models. * Provides access to the core entity models.
* Wrapped up in this provider since they are often used together * Wrapped up in this provider since they are often used together
* so this is a neater alternative to injecting all in individually. * so this is a neater alternative to injecting all in individually.
*
* @package BookStack\Entities
*/ */
class EntityProvider class EntityProvider
{ {
@ -37,26 +42,20 @@ class EntityProvider
*/ */
public $pageRevision; public $pageRevision;
/**
* EntityProvider constructor. public function __construct()
*/ {
public function __construct( $this->bookshelf = new Bookshelf();
Bookshelf $bookshelf, $this->book = new Book();
Book $book, $this->chapter = new Chapter();
Chapter $chapter, $this->page = new Page();
Page $page, $this->pageRevision = new PageRevision();
PageRevision $pageRevision
) {
$this->bookshelf = $bookshelf;
$this->book = $book;
$this->chapter = $chapter;
$this->page = $page;
$this->pageRevision = $pageRevision;
} }
/** /**
* Fetch all core entity types as an associated array * Fetch all core entity types as an associated array
* with their basic names as the keys. * with their basic names as the keys.
* @return array<Entity>
*/ */
public function all(): array public function all(): array
{ {

View File

@ -1,109 +0,0 @@
<?php namespace BookStack\Entities\Managers;
use BookStack\Entities\Book;
use BookStack\Entities\Bookshelf;
use BookStack\Entities\Chapter;
use BookStack\Entities\Entity;
use BookStack\Entities\HasCoverImage;
use BookStack\Entities\Page;
use BookStack\Exceptions\NotifyException;
use BookStack\Facades\Activity;
use BookStack\Uploads\AttachmentService;
use BookStack\Uploads\ImageService;
use Exception;
use Illuminate\Contracts\Container\BindingResolutionException;
class TrashCan
{
/**
* Remove a bookshelf from the system.
* @throws Exception
*/
public function destroyShelf(Bookshelf $shelf)
{
$this->destroyCommonRelations($shelf);
$shelf->delete();
}
/**
* Remove a book from the system.
* @throws NotifyException
* @throws BindingResolutionException
*/
public function destroyBook(Book $book)
{
foreach ($book->pages as $page) {
$this->destroyPage($page);
}
foreach ($book->chapters as $chapter) {
$this->destroyChapter($chapter);
}
$this->destroyCommonRelations($book);
$book->delete();
}
/**
* Remove a page from the system.
* @throws NotifyException
*/
public function destroyPage(Page $page)
{
// Check if set as custom homepage & remove setting if not used or throw error if active
$customHome = setting('app-homepage', '0:');
if (intval($page->id) === intval(explode(':', $customHome)[0])) {
if (setting('app-homepage-type') === 'page') {
throw new NotifyException(trans('errors.page_custom_home_deletion'), $page->getUrl());
}
setting()->remove('app-homepage');
}
$this->destroyCommonRelations($page);
// Delete Attached Files
$attachmentService = app(AttachmentService::class);
foreach ($page->attachments as $attachment) {
$attachmentService->deleteFile($attachment);
}
$page->delete();
}
/**
* Remove a chapter from the system.
* @throws Exception
*/
public function destroyChapter(Chapter $chapter)
{
if (count($chapter->pages) > 0) {
foreach ($chapter->pages as $page) {
$page->chapter_id = 0;
$page->save();
}
}
$this->destroyCommonRelations($chapter);
$chapter->delete();
}
/**
* Update entity relations to remove or update outstanding connections.
*/
protected function destroyCommonRelations(Entity $entity)
{
Activity::removeEntity($entity);
$entity->views()->delete();
$entity->permissions()->delete();
$entity->tags()->delete();
$entity->comments()->delete();
$entity->jointPermissions()->delete();
$entity->searchTerms()->delete();
if ($entity instanceof HasCoverImage && $entity->cover) {
$imageService = app()->make(ImageService::class);
$imageService->destroy($entity->cover);
}
}
}

View File

@ -1,4 +1,4 @@
<?php namespace BookStack\Entities; <?php namespace BookStack\Entities\Models;
use BookStack\Uploads\Image; use BookStack\Uploads\Image;
use Exception; use Exception;
@ -12,26 +12,20 @@ use Illuminate\Support\Collection;
* @property string $description * @property string $description
* @property int $image_id * @property int $image_id
* @property Image|null $cover * @property Image|null $cover
* @package BookStack\Entities
*/ */
class Book extends Entity implements HasCoverImage class Book extends Entity implements HasCoverImage
{ {
public $searchFactor = 2; public $searchFactor = 2;
protected $fillable = ['name', 'description']; protected $fillable = ['name', 'description'];
protected $hidden = ['restricted', 'pivot', 'image_id']; protected $hidden = ['restricted', 'pivot', 'image_id', 'deleted_at'];
/** /**
* Get the url for this book. * Get the url for this book.
* @param string|bool $path
* @return string
*/ */
public function getUrl($path = false) public function getUrl(string $path = ''): string
{ {
if ($path !== false) { return url('/books/' . implode('/', [urlencode($this->slug), trim($path, '/')]));
return url('/books/' . urlencode($this->slug) . '/' . trim($path, '/'));
}
return url('/books/' . urlencode($this->slug));
} }
/** /**
@ -117,15 +111,4 @@ class Book extends Entity implements HasCoverImage
$chapters = $this->chapters()->visible()->get(); $chapters = $this->chapters()->visible()->get();
return $pages->concat($chapters)->sortBy('priority')->sortByDesc('draft'); return $pages->concat($chapters)->sortBy('priority')->sortByDesc('draft');
} }
/**
* Get an excerpt of this book's description to the specified length or less.
* @param int $length
* @return string
*/
public function getExcerpt(int $length = 100)
{
$description = $this->description;
return mb_strlen($description) > $length ? mb_substr($description, 0, $length-3) . '...' : $description;
}
} }

View File

@ -1,5 +1,8 @@
<?php namespace BookStack\Entities; <?php namespace BookStack\Entities\Models;
use BookStack\Entities\Models\Chapter;
use BookStack\Entities\Models\Entity;
use BookStack\Entities\Models\Book;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsTo;
@ -10,7 +13,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
* @property Book $book * @property Book $book
* @method Builder whereSlugs(string $bookSlug, string $childSlug) * @method Builder whereSlugs(string $bookSlug, string $childSlug)
*/ */
class BookChild extends Entity abstract class BookChild extends Entity
{ {
/** /**
@ -45,9 +48,6 @@ class BookChild extends Entity
$this->save(); $this->save();
$this->refresh(); $this->refresh();
// Update related activity
$this->activity()->update(['book_id' => $newBookId]);
// Update all child pages if a chapter // Update all child pages if a chapter
if ($this instanceof Chapter) { if ($this instanceof Chapter) {
foreach ($this->pages as $page) { foreach ($this->pages as $page) {

View File

@ -1,4 +1,4 @@
<?php namespace BookStack\Entities; <?php namespace BookStack\Entities\Models;
use BookStack\Uploads\Image; use BookStack\Uploads\Image;
use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsTo;
@ -12,7 +12,7 @@ class Bookshelf extends Entity implements HasCoverImage
protected $fillable = ['name', 'description', 'image_id']; protected $fillable = ['name', 'description', 'image_id'];
protected $hidden = ['restricted', 'image_id']; protected $hidden = ['restricted', 'image_id', 'deleted_at'];
/** /**
* Get the books in this shelf. * Get the books in this shelf.
@ -36,15 +36,10 @@ class Bookshelf extends Entity implements HasCoverImage
/** /**
* Get the url for this bookshelf. * Get the url for this bookshelf.
* @param string|bool $path
* @return string
*/ */
public function getUrl($path = false) public function getUrl(string $path = ''): string
{ {
if ($path !== false) { return url('/shelves/' . implode('/', [urlencode($this->slug), trim($path, '/')]));
return url('/shelves/' . urlencode($this->slug) . '/' . trim($path, '/'));
}
return url('/shelves/' . urlencode($this->slug));
} }
/** /**
@ -85,17 +80,6 @@ class Bookshelf extends Entity implements HasCoverImage
return 'cover_shelf'; return 'cover_shelf';
} }
/**
* Get an excerpt of this book's description to the specified length or less.
* @param int $length
* @return string
*/
public function getExcerpt(int $length = 100)
{
$description = $this->description;
return mb_strlen($description) > $length ? mb_substr($description, 0, $length-3) . '...' : $description;
}
/** /**
* Check if this shelf contains the given book. * Check if this shelf contains the given book.
* @param Book $book * @param Book $book

View File

@ -0,0 +1,52 @@
<?php namespace BookStack\Entities\Models;
use Illuminate\Support\Collection;
/**
* Class Chapter
* @property Collection<Page> $pages
*/
class Chapter extends BookChild
{
public $searchFactor = 1.3;
protected $fillable = ['name', 'description', 'priority', 'book_id'];
protected $hidden = ['restricted', 'pivot', 'deleted_at'];
/**
* Get the pages that this chapter contains.
* @param string $dir
* @return mixed
*/
public function pages($dir = 'ASC')
{
return $this->hasMany(Page::class)->orderBy('priority', $dir);
}
/**
* Get the url of this chapter.
*/
public function getUrl($path = ''): string
{
$parts = [
'books',
urlencode($this->getAttribute('bookSlug') ?? $this->book->slug),
'chapter',
urlencode($this->slug),
trim($path, '/'),
];
return url('/' . implode('/', $parts));
}
/**
* Get the visible pages in this chapter.
*/
public function getVisiblePages(): Collection
{
return $this->pages()->visible()
->orderBy('draft', 'desc')
->orderBy('priority', 'asc')
->get();
}
}

View File

@ -0,0 +1,48 @@
<?php namespace BookStack\Entities\Models;
use BookStack\Auth\User;
use BookStack\Entities\Models\Entity;
use BookStack\Interfaces\Loggable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphTo;
class Deletion extends Model implements Loggable
{
/**
* Get the related deletable record.
*/
public function deletable(): MorphTo
{
return $this->morphTo('deletable')->withTrashed();
}
/**
* The the user that performed the deletion.
*/
public function deleter(): BelongsTo
{
return $this->belongsTo(User::class, 'deleted_by');
}
/**
* Create a new deletion record for the provided entity.
*/
public static function createForEntity(Entity $entity): Deletion
{
$record = (new self())->forceFill([
'deleted_by' => user()->id,
'deletable_type' => $entity->getMorphClass(),
'deletable_id' => $entity->id,
]);
$record->save();
return $record;
}
public function logDescriptor(): string
{
$deletable = $this->deletable()->first();
return "Deletion ({$this->id}) for {$deletable->getType()} ({$deletable->id}) {$deletable->name}";
}
}

View File

@ -1,4 +1,4 @@
<?php namespace BookStack\Entities; <?php namespace BookStack\Entities\Models;
use BookStack\Actions\Activity; use BookStack\Actions\Activity;
use BookStack\Actions\Comment; use BookStack\Actions\Comment;
@ -6,12 +6,17 @@ use BookStack\Actions\Tag;
use BookStack\Actions\View; use BookStack\Actions\View;
use BookStack\Auth\Permissions\EntityPermission; use BookStack\Auth\Permissions\EntityPermission;
use BookStack\Auth\Permissions\JointPermission; use BookStack\Auth\Permissions\JointPermission;
use BookStack\Entities\Tools\SearchIndex;
use BookStack\Entities\Tools\SlugGenerator;
use BookStack\Facades\Permissions; use BookStack\Facades\Permissions;
use BookStack\Ownable; use BookStack\Model;
use BookStack\Traits\HasCreatorAndUpdater;
use BookStack\Traits\HasOwner;
use Carbon\Carbon; use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Relations\MorphMany; use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\SoftDeletes;
/** /**
* Class Entity * Class Entity
@ -31,11 +36,12 @@ use Illuminate\Database\Eloquent\Relations\MorphMany;
* @method static Entity|Builder hasPermission(string $permission) * @method static Entity|Builder hasPermission(string $permission)
* @method static Builder withLastView() * @method static Builder withLastView()
* @method static Builder withViewCount() * @method static Builder withViewCount()
*
* @package BookStack\Entities
*/ */
class Entity extends Ownable abstract class Entity extends Model
{ {
use SoftDeletes;
use HasCreatorAndUpdater;
use HasOwner;
/** /**
* @var string - Name of property where the main text content is found * @var string - Name of property where the main text content is found
@ -50,7 +56,7 @@ class Entity extends Ownable
/** /**
* Get the entities that are visible to the current user. * Get the entities that are visible to the current user.
*/ */
public function scopeVisible(Builder $query) public function scopeVisible(Builder $query): Builder
{ {
return $this->scopeHasPermission($query, 'view'); return $this->scopeHasPermission($query, 'view');
} }
@ -92,24 +98,18 @@ class Entity extends Ownable
/** /**
* Compares this entity to another given entity. * Compares this entity to another given entity.
* Matches by comparing class and id. * Matches by comparing class and id.
* @param $entity
* @return bool
*/ */
public function matches($entity) public function matches(Entity $entity): bool
{ {
return [get_class($this), $this->id] === [get_class($entity), $entity->id]; return [get_class($this), $this->id] === [get_class($entity), $entity->id];
} }
/** /**
* Checks if an entity matches or contains another given entity. * Checks if the current entity matches or contains the given.
* @param Entity $entity
* @return bool
*/ */
public function matchesOrContains(Entity $entity) public function matchesOrContains(Entity $entity): bool
{ {
$matches = [get_class($this), $this->id] === [get_class($entity), $entity->id]; if ($this->matches($entity)) {
if ($matches) {
return true; return true;
} }
@ -126,9 +126,8 @@ class Entity extends Ownable
/** /**
* Gets the activity objects for this entity. * Gets the activity objects for this entity.
* @return MorphMany
*/ */
public function activity() public function activity(): MorphMany
{ {
return $this->morphMany(Activity::class, 'entity') return $this->morphMany(Activity::class, 'entity')
->orderBy('created_at', 'desc'); ->orderBy('created_at', 'desc');
@ -137,26 +136,23 @@ class Entity extends Ownable
/** /**
* Get View objects for this entity. * Get View objects for this entity.
*/ */
public function views() public function views(): MorphMany
{ {
return $this->morphMany(View::class, 'viewable'); return $this->morphMany(View::class, 'viewable');
} }
/** /**
* Get the Tag models that have been user assigned to this entity. * Get the Tag models that have been user assigned to this entity.
* @return MorphMany
*/ */
public function tags() public function tags(): MorphMany
{ {
return $this->morphMany(Tag::class, 'entity')->orderBy('order', 'asc'); return $this->morphMany(Tag::class, 'entity')->orderBy('order', 'asc');
} }
/** /**
* Get the comments for an entity * Get the comments for an entity
* @param bool $orderByCreated
* @return MorphMany
*/ */
public function comments($orderByCreated = true) public function comments(bool $orderByCreated = true): MorphMany
{ {
$query = $this->morphMany(Comment::class, 'entity'); $query = $this->morphMany(Comment::class, 'entity');
return $orderByCreated ? $query->orderBy('created_at', 'asc') : $query; return $orderByCreated ? $query->orderBy('created_at', 'asc') : $query;
@ -164,9 +160,8 @@ class Entity extends Ownable
/** /**
* Get the related search terms. * Get the related search terms.
* @return MorphMany
*/ */
public function searchTerms() public function searchTerms(): MorphMany
{ {
return $this->morphMany(SearchTerm::class, 'entity'); return $this->morphMany(SearchTerm::class, 'entity');
} }
@ -174,18 +169,15 @@ class Entity extends Ownable
/** /**
* Get this entities restrictions. * Get this entities restrictions.
*/ */
public function permissions() public function permissions(): MorphMany
{ {
return $this->morphMany(EntityPermission::class, 'restrictable'); return $this->morphMany(EntityPermission::class, 'restrictable');
} }
/** /**
* Check if this entity has a specific restriction set against it. * Check if this entity has a specific restriction set against it.
* @param $role_id
* @param $action
* @return bool
*/ */
public function hasRestriction($role_id, $action) public function hasRestriction(int $role_id, string $action): bool
{ {
return $this->permissions()->where('role_id', '=', $role_id) return $this->permissions()->where('role_id', '=', $role_id)
->where('action', '=', $action)->count() > 0; ->where('action', '=', $action)->count() > 0;
@ -193,13 +185,20 @@ class Entity extends Ownable
/** /**
* Get the entity jointPermissions this is connected to. * Get the entity jointPermissions this is connected to.
* @return MorphMany
*/ */
public function jointPermissions() public function jointPermissions(): MorphMany
{ {
return $this->morphMany(JointPermission::class, 'entity'); return $this->morphMany(JointPermission::class, 'entity');
} }
/**
* Get the related delete records for this entity.
*/
public function deletions(): MorphMany
{
return $this->morphMany(Deletion::class, 'deletable');
}
/** /**
* Check if this instance or class is a certain type of entity. * Check if this instance or class is a certain type of entity.
* Examples of $type are 'page', 'book', 'chapter' * Examples of $type are 'page', 'book', 'chapter'
@ -210,28 +209,12 @@ class Entity extends Ownable
} }
/** /**
* Get entity type. * Get the entity type as a simple lowercase word.
* @return mixed
*/ */
public static function getType() public static function getType(): string
{ {
return strtolower(static::getClassName()); $className = array_slice(explode('\\', static::class), -1, 1)[0];
} return strtolower($className);
/**
* Get an instance of an entity of the given type.
* @param $type
* @return Entity
*/
public static function getEntityInstance($type)
{
$types = ['Page', 'Book', 'Chapter', 'Bookshelf'];
$className = str_replace([' ', '-', '_'], '', ucwords($type));
if (!in_array($className, $types)) {
return null;
}
return app('BookStack\\Entities\\' . $className);
} }
/** /**
@ -247,35 +230,45 @@ class Entity extends Ownable
/** /**
* Get the body text of this entity. * Get the body text of this entity.
* @return mixed
*/ */
public function getText() public function getText(): string
{ {
return $this->{$this->textField}; return $this->{$this->textField} ?? '';
} }
/** /**
* Get an excerpt of this entity's descriptive content to the specified length. * Get an excerpt of this entity's descriptive content to the specified length.
* @param int $length
* @return mixed
*/ */
public function getExcerpt(int $length = 100) public function getExcerpt(int $length = 100): string
{ {
$text = $this->getText(); $text = $this->getText();
if (mb_strlen($text) > $length) { if (mb_strlen($text) > $length) {
$text = mb_substr($text, 0, $length-3) . '...'; $text = mb_substr($text, 0, $length-3) . '...';
} }
return trim($text); return trim($text);
} }
/** /**
* Get the url of this entity * Get the url of this entity
* @param $path
* @return string
*/ */
public function getUrl($path = '/') abstract public function getUrl(string $path = '/'): string;
/**
* Get the parent entity if existing.
* This is the "static" parent and does not include dynamic
* relations such as shelves to books.
*/
public function getParent(): ?Entity
{ {
return $path; if ($this->isA('page')) {
return $this->chapter_id ? $this->chapter()->withTrashed()->first() : $this->book()->withTrashed()->first();
}
if ($this->isA('chapter')) {
return $this->book()->withTrashed()->first();
}
return null;
} }
/** /**
@ -292,8 +285,7 @@ class Entity extends Ownable
*/ */
public function indexForSearch() public function indexForSearch()
{ {
$searchService = app()->make(SearchService::class); app(SearchIndex::class)->indexEntity(clone $this);
$searchService->indexEntity(clone $this);
} }
/** /**
@ -301,8 +293,7 @@ class Entity extends Ownable
*/ */
public function refreshSlug(): string public function refreshSlug(): string
{ {
$generator = new SlugGenerator($this); $this->slug = (new SlugGenerator)->generate($this);
$this->slug = $generator->generate();
return $this->slug; return $this->slug;
} }
} }

View File

@ -1,7 +1,7 @@
<?php <?php
namespace BookStack\Entities; namespace BookStack\Entities\Models;
use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsTo;

View File

@ -1,5 +1,6 @@
<?php namespace BookStack\Entities; <?php namespace BookStack\Entities\Models;
use BookStack\Entities\Tools\PageContent;
use BookStack\Uploads\Attachment; use BookStack\Uploads\Attachment;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Collection;
@ -27,12 +28,17 @@ class Page extends BookChild
public $textField = 'text'; public $textField = 'text';
protected $hidden = ['html', 'markdown', 'text', 'restricted', 'pivot']; protected $hidden = ['html', 'markdown', 'text', 'restricted', 'pivot', 'deleted_at'];
protected $casts = [
'draft' => 'boolean',
'template' => 'boolean',
];
/** /**
* Get the entities that are visible to the current user. * Get the entities that are visible to the current user.
*/ */
public function scopeVisible(Builder $query) public function scopeVisible(Builder $query): Builder
{ {
$query = Permissions::enforceDraftVisiblityOnQuery($query); $query = Permissions::enforceDraftVisiblityOnQuery($query);
return parent::scopeVisible($query); return parent::scopeVisible($query);
@ -49,14 +55,6 @@ class Page extends BookChild
return $array; return $array;
} }
/**
* Get the parent item
*/
public function parent(): Entity
{
return $this->chapter_id ? $this->chapter : $this->book;
}
/** /**
* Get the chapter that this page is in, If applicable. * Get the chapter that this page is in, If applicable.
* @return BelongsTo * @return BelongsTo
@ -94,22 +92,19 @@ class Page extends BookChild
} }
/** /**
* Get the url for this page. * Get the url of this page.
* @param string|bool $path
* @return string
*/ */
public function getUrl($path = false) public function getUrl($path = ''): string
{ {
$bookSlug = $this->getAttribute('bookSlug') ? $this->getAttribute('bookSlug') : $this->book->slug; $parts = [
$midText = $this->draft ? '/draft/' : '/page/'; 'books',
$idComponent = $this->draft ? $this->id : urlencode($this->slug); urlencode($this->getAttribute('bookSlug') ?? $this->book->slug),
$this->draft ? 'draft' : 'page',
$this->draft ? $this->id : urlencode($this->slug),
trim($path, '/'),
];
$url = '/books/' . urlencode($bookSlug) . $midText . $idComponent; return url('/' . implode('/', $parts));
if ($path !== false) {
$url .= '/' . trim($path, '/');
}
return url($url);
} }
/** /**
@ -120,4 +115,15 @@ class Page extends BookChild
{ {
return $this->revisions()->first(); return $this->revisions()->first();
} }
/**
* Get this page for JSON display.
*/
public function forJsonDisplay(): Page
{
$refreshed = $this->refresh()->unsetRelations()->load(['tags', 'createdBy', 'updatedBy']);
$refreshed->setHidden(array_diff($refreshed->getHidden(), ['html', 'markdown']));
$refreshed->html = (new PageContent($refreshed))->render();
return $refreshed;
}
} }

View File

@ -1,6 +1,7 @@
<?php namespace BookStack\Entities; <?php namespace BookStack\Entities\Models;
use BookStack\Auth\User; use BookStack\Auth\User;
use BookStack\Entities\Models\Page;
use BookStack\Model; use BookStack\Model;
use Carbon\Carbon; use Carbon\Carbon;

View File

@ -1,4 +1,4 @@
<?php namespace BookStack\Entities; <?php namespace BookStack\Entities\Models;
use BookStack\Model; use BookStack\Model;

View File

@ -2,11 +2,13 @@
namespace BookStack\Entities\Repos; namespace BookStack\Entities\Repos;
use BookStack\Actions\ActivityType;
use BookStack\Actions\TagRepo; use BookStack\Actions\TagRepo;
use BookStack\Entities\Book; use BookStack\Auth\User;
use BookStack\Entities\Entity; use BookStack\Entities\Models\Entity;
use BookStack\Entities\HasCoverImage; use BookStack\Entities\Models\HasCoverImage;
use BookStack\Exceptions\ImageUploadException; use BookStack\Exceptions\ImageUploadException;
use BookStack\Facades\Activity;
use BookStack\Uploads\ImageRepo; use BookStack\Uploads\ImageRepo;
use Illuminate\Http\UploadedFile; use Illuminate\Http\UploadedFile;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
@ -18,10 +20,6 @@ class BaseRepo
protected $imageRepo; protected $imageRepo;
/**
* BaseRepo constructor.
* @param $tagRepo
*/
public function __construct(TagRepo $tagRepo, ImageRepo $imageRepo) public function __construct(TagRepo $tagRepo, ImageRepo $imageRepo)
{ {
$this->tagRepo = $tagRepo; $this->tagRepo = $tagRepo;
@ -37,6 +35,7 @@ class BaseRepo
$entity->forceFill([ $entity->forceFill([
'created_by' => user()->id, 'created_by' => user()->id,
'updated_by' => user()->id, 'updated_by' => user()->id,
'owned_by' => user()->id,
]); ]);
$entity->refreshSlug(); $entity->refreshSlug();
$entity->save(); $entity->save();
@ -91,29 +90,4 @@ class BaseRepo
$entity->save(); $entity->save();
} }
} }
/**
* Update the permissions of an entity.
*/
public function updatePermissions(Entity $entity, bool $restricted, Collection $permissions = null)
{
$entity->restricted = $restricted;
$entity->permissions()->delete();
if (!is_null($permissions)) {
$entityPermissionData = $permissions->flatMap(function ($restrictions, $roleId) {
return collect($restrictions)->keys()->map(function ($action) use ($roleId) {
return [
'role_id' => $roleId,
'action' => strtolower($action),
] ;
});
});
$entity->permissions()->createMany($entityPermissionData);
}
$entity->save();
$entity->rebuildPermissions();
}
} }

View File

@ -1,14 +1,14 @@
<?php namespace BookStack\Entities\Repos; <?php namespace BookStack\Entities\Repos;
use BookStack\Actions\ActivityType;
use BookStack\Actions\TagRepo; use BookStack\Actions\TagRepo;
use BookStack\Entities\Book; use BookStack\Entities\Models\Book;
use BookStack\Entities\Managers\TrashCan; use BookStack\Entities\Tools\TrashCan;
use BookStack\Exceptions\ImageUploadException; use BookStack\Exceptions\ImageUploadException;
use BookStack\Exceptions\NotFoundException; use BookStack\Exceptions\NotFoundException;
use BookStack\Exceptions\NotifyException; use BookStack\Facades\Activity;
use BookStack\Uploads\ImageRepo; use BookStack\Uploads\ImageRepo;
use Exception; use Exception;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Contracts\Pagination\LengthAwarePaginator; use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Http\UploadedFile; use Illuminate\Http\UploadedFile;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
@ -22,7 +22,6 @@ class BookRepo
/** /**
* BookRepo constructor. * BookRepo constructor.
* @param $tagRepo
*/ */
public function __construct(BaseRepo $baseRepo, TagRepo $tagRepo, ImageRepo $imageRepo) public function __construct(BaseRepo $baseRepo, TagRepo $tagRepo, ImageRepo $imageRepo)
{ {
@ -91,6 +90,7 @@ class BookRepo
{ {
$book = new Book(); $book = new Book();
$this->baseRepo->create($book, $input); $this->baseRepo->create($book, $input);
Activity::addForEntity($book, ActivityType::BOOK_CREATE);
return $book; return $book;
} }
@ -100,6 +100,7 @@ class BookRepo
public function update(Book $book, array $input): Book public function update(Book $book, array $input): Book
{ {
$this->baseRepo->update($book, $input); $this->baseRepo->update($book, $input);
Activity::addForEntity($book, ActivityType::BOOK_UPDATE);
return $book; return $book;
} }
@ -113,22 +114,16 @@ class BookRepo
$this->baseRepo->updateCoverImage($book, $coverImage, $removeImage); $this->baseRepo->updateCoverImage($book, $coverImage, $removeImage);
} }
/**
* Update the permissions of a book.
*/
public function updatePermissions(Book $book, bool $restricted, Collection $permissions = null)
{
$this->baseRepo->updatePermissions($book, $restricted, $permissions);
}
/** /**
* Remove a book from the system. * Remove a book from the system.
* @throws NotifyException * @throws Exception
* @throws BindingResolutionException
*/ */
public function destroy(Book $book) public function destroy(Book $book)
{ {
$trashCan = new TrashCan(); $trashCan = new TrashCan();
$trashCan->destroyBook($book); $trashCan->softDestroyBook($book);
Activity::addForEntity($book, ActivityType::BOOK_DELETE);
$trashCan->autoClearOld();
} }
} }

View File

@ -1,10 +1,12 @@
<?php namespace BookStack\Entities\Repos; <?php namespace BookStack\Entities\Repos;
use BookStack\Entities\Book; use BookStack\Actions\ActivityType;
use BookStack\Entities\Bookshelf; use BookStack\Entities\Models\Book;
use BookStack\Entities\Managers\TrashCan; use BookStack\Entities\Models\Bookshelf;
use BookStack\Entities\Tools\TrashCan;
use BookStack\Exceptions\ImageUploadException; use BookStack\Exceptions\ImageUploadException;
use BookStack\Exceptions\NotFoundException; use BookStack\Exceptions\NotFoundException;
use BookStack\Facades\Activity;
use Exception; use Exception;
use Illuminate\Contracts\Pagination\LengthAwarePaginator; use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Http\UploadedFile; use Illuminate\Http\UploadedFile;
@ -16,7 +18,6 @@ class BookshelfRepo
/** /**
* BookshelfRepo constructor. * BookshelfRepo constructor.
* @param $baseRepo
*/ */
public function __construct(BaseRepo $baseRepo) public function __construct(BaseRepo $baseRepo)
{ {
@ -87,11 +88,12 @@ class BookshelfRepo
$shelf = new Bookshelf(); $shelf = new Bookshelf();
$this->baseRepo->create($shelf, $input); $this->baseRepo->create($shelf, $input);
$this->updateBooks($shelf, $bookIds); $this->updateBooks($shelf, $bookIds);
Activity::addForEntity($shelf, ActivityType::BOOKSHELF_CREATE);
return $shelf; return $shelf;
} }
/** /**
* Create a new shelf in the system. * Update an existing shelf in the system using the given input.
*/ */
public function update(Bookshelf $shelf, array $input, ?array $bookIds): Bookshelf public function update(Bookshelf $shelf, array $input, ?array $bookIds): Bookshelf
{ {
@ -101,6 +103,7 @@ class BookshelfRepo
$this->updateBooks($shelf, $bookIds); $this->updateBooks($shelf, $bookIds);
} }
Activity::addForEntity($shelf, ActivityType::BOOKSHELF_UPDATE);
return $shelf; return $shelf;
} }
@ -134,14 +137,6 @@ class BookshelfRepo
$this->baseRepo->updateCoverImage($shelf, $coverImage, $removeImage); $this->baseRepo->updateCoverImage($shelf, $coverImage, $removeImage);
} }
/**
* Update the permissions of a bookshelf.
*/
public function updatePermissions(Bookshelf $shelf, bool $restricted, Collection $permissions = null)
{
$this->baseRepo->updatePermissions($shelf, $restricted, $permissions);
}
/** /**
* Copy down the permissions of the given shelf to all child books. * Copy down the permissions of the given shelf to all child books.
*/ */
@ -174,6 +169,8 @@ class BookshelfRepo
public function destroy(Bookshelf $shelf) public function destroy(Bookshelf $shelf)
{ {
$trashCan = new TrashCan(); $trashCan = new TrashCan();
$trashCan->destroyShelf($shelf); $trashCan->softDestroyShelf($shelf);
Activity::addForEntity($shelf, ActivityType::BOOKSHELF_DELETE);
$trashCan->autoClearOld();
} }
} }

View File

@ -1,15 +1,14 @@
<?php namespace BookStack\Entities\Repos; <?php namespace BookStack\Entities\Repos;
use BookStack\Entities\Book; use BookStack\Actions\ActivityType;
use BookStack\Entities\Chapter; use BookStack\Entities\Models\Book;
use BookStack\Entities\Managers\BookContents; use BookStack\Entities\Models\Chapter;
use BookStack\Entities\Managers\TrashCan; use BookStack\Entities\Tools\BookContents;
use BookStack\Entities\Tools\TrashCan;
use BookStack\Exceptions\MoveOperationException; use BookStack\Exceptions\MoveOperationException;
use BookStack\Exceptions\NotFoundException; use BookStack\Exceptions\NotFoundException;
use BookStack\Exceptions\NotifyException; use BookStack\Facades\Activity;
use Exception; use Exception;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
class ChapterRepo class ChapterRepo
@ -19,7 +18,6 @@ class ChapterRepo
/** /**
* ChapterRepo constructor. * ChapterRepo constructor.
* @param $baseRepo
*/ */
public function __construct(BaseRepo $baseRepo) public function __construct(BaseRepo $baseRepo)
{ {
@ -50,6 +48,7 @@ class ChapterRepo
$chapter->book_id = $parentBook->id; $chapter->book_id = $parentBook->id;
$chapter->priority = (new BookContents($parentBook))->getLastPriority() + 1; $chapter->priority = (new BookContents($parentBook))->getLastPriority() + 1;
$this->baseRepo->create($chapter, $input); $this->baseRepo->create($chapter, $input);
Activity::addForEntity($chapter, ActivityType::CHAPTER_CREATE);
return $chapter; return $chapter;
} }
@ -59,17 +58,10 @@ class ChapterRepo
public function update(Chapter $chapter, array $input): Chapter public function update(Chapter $chapter, array $input): Chapter
{ {
$this->baseRepo->update($chapter, $input); $this->baseRepo->update($chapter, $input);
Activity::addForEntity($chapter, ActivityType::CHAPTER_UPDATE);
return $chapter; return $chapter;
} }
/**
* Update the permissions of a chapter.
*/
public function updatePermissions(Chapter $chapter, bool $restricted, Collection $permissions = null)
{
$this->baseRepo->updatePermissions($chapter, $restricted, $permissions);
}
/** /**
* Remove a chapter from the system. * Remove a chapter from the system.
* @throws Exception * @throws Exception
@ -77,7 +69,9 @@ class ChapterRepo
public function destroy(Chapter $chapter) public function destroy(Chapter $chapter)
{ {
$trashCan = new TrashCan(); $trashCan = new TrashCan();
$trashCan->destroyChapter($chapter); $trashCan->softDestroyChapter($chapter);
Activity::addForEntity($chapter, ActivityType::CHAPTER_DELETE);
$trashCan->autoClearOld();
} }
/** /**
@ -96,6 +90,7 @@ class ChapterRepo
throw new MoveOperationException('Chapters can only be moved into books'); throw new MoveOperationException('Chapters can only be moved into books');
} }
/** @var Book $parent */
$parent = Book::visible()->where('id', '=', $entityId)->first(); $parent = Book::visible()->where('id', '=', $entityId)->first();
if ($parent === null) { if ($parent === null) {
throw new MoveOperationException('Book to move chapter into not found'); throw new MoveOperationException('Book to move chapter into not found');
@ -103,6 +98,8 @@ class ChapterRepo
$chapter->changeBook($parent->id); $chapter->changeBook($parent->id);
$chapter->rebuildPermissions(); $chapter->rebuildPermissions();
Activity::addForEntity($chapter, ActivityType::CHAPTER_MOVE);
return $parent; return $parent;
} }
} }

View File

@ -1,17 +1,19 @@
<?php namespace BookStack\Entities\Repos; <?php namespace BookStack\Entities\Repos;
use BookStack\Entities\Book; use BookStack\Actions\ActivityType;
use BookStack\Entities\Chapter; use BookStack\Entities\Models\Book;
use BookStack\Entities\Entity; use BookStack\Entities\Models\Chapter;
use BookStack\Entities\Managers\BookContents; use BookStack\Entities\Models\Entity;
use BookStack\Entities\Managers\PageContent; use BookStack\Entities\Tools\BookContents;
use BookStack\Entities\Managers\TrashCan; use BookStack\Entities\Tools\PageContent;
use BookStack\Entities\Page; use BookStack\Entities\Tools\TrashCan;
use BookStack\Entities\PageRevision; use BookStack\Entities\Models\Page;
use BookStack\Entities\Models\PageRevision;
use BookStack\Exceptions\MoveOperationException; use BookStack\Exceptions\MoveOperationException;
use BookStack\Exceptions\NotFoundException; use BookStack\Exceptions\NotFoundException;
use BookStack\Exceptions\NotifyException;
use BookStack\Exceptions\PermissionsException; use BookStack\Exceptions\PermissionsException;
use BookStack\Facades\Activity;
use Exception;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
@ -33,9 +35,9 @@ class PageRepo
* Get a page by ID. * Get a page by ID.
* @throws NotFoundException * @throws NotFoundException
*/ */
public function getById(int $id): Page public function getById(int $id, array $relations = ['book']): Page
{ {
$page = Page::visible()->with(['book'])->find($id); $page = Page::visible()->with($relations)->find($id);
if (!$page) { if (!$page) {
throw new NotFoundException(trans('errors.page_not_found')); throw new NotFoundException(trans('errors.page_not_found'));
@ -128,6 +130,7 @@ class PageRepo
$page = (new Page())->forceFill([ $page = (new Page())->forceFill([
'name' => trans('entities.pages_initial_name'), 'name' => trans('entities.pages_initial_name'),
'created_by' => user()->id, 'created_by' => user()->id,
'owned_by' => user()->id,
'updated_by' => user()->id, 'updated_by' => user()->id,
'draft' => true, 'draft' => true,
]); ]);
@ -150,12 +153,8 @@ class PageRepo
public function publishDraft(Page $draft, array $input): Page public function publishDraft(Page $draft, array $input): Page
{ {
$this->baseRepo->update($draft, $input); $this->baseRepo->update($draft, $input);
if (isset($input['template']) && userCan('templates-manage')) { $this->updateTemplateStatusAndContentFromInput($draft, $input);
$draft->template = ($input['template'] === 'true');
}
$pageContent = new PageContent($draft);
$pageContent->setNewHTML($input['html']);
$draft->draft = false; $draft->draft = false;
$draft->revision_count = 1; $draft->revision_count = 1;
$draft->priority = $this->getNewPriority($draft); $draft->priority = $this->getNewPriority($draft);
@ -164,7 +163,10 @@ class PageRepo
$this->savePageRevision($draft, trans('entities.pages_initial_revision')); $this->savePageRevision($draft, trans('entities.pages_initial_revision'));
$draft->indexForSearch(); $draft->indexForSearch();
return $draft->refresh(); $draft->refresh();
Activity::addForEntity($draft, ActivityType::PAGE_CREATE);
return $draft;
} }
/** /**
@ -176,12 +178,7 @@ class PageRepo
$oldHtml = $page->html; $oldHtml = $page->html;
$oldName = $page->name; $oldName = $page->name;
if (isset($input['template']) && userCan('templates-manage')) { $this->updateTemplateStatusAndContentFromInput($page, $input);
$page->template = ($input['template'] === 'true');
}
$pageContent = new PageContent($page);
$pageContent->setNewHTML($input['html']);
$this->baseRepo->update($page, $input); $this->baseRepo->update($page, $input);
// Update with new details // Update with new details
@ -202,13 +199,28 @@ class PageRepo
$this->savePageRevision($page, $summary); $this->savePageRevision($page, $summary);
} }
Activity::addForEntity($page, ActivityType::PAGE_UPDATE);
return $page; return $page;
} }
protected function updateTemplateStatusAndContentFromInput(Page $page, array $input)
{
if (isset($input['template']) && userCan('templates-manage')) {
$page->template = ($input['template'] === 'true');
}
$pageContent = new PageContent($page);
if (isset($input['html'])) {
$pageContent->setNewHTML($input['html']);
} else {
$pageContent->setNewMarkdown($input['markdown']);
}
}
/** /**
* Saves a page revision into the system. * Saves a page revision into the system.
*/ */
protected function savePageRevision(Page $page, string $summary = null) protected function savePageRevision(Page $page, string $summary = null): PageRevision
{ {
$revision = new PageRevision($page->getAttributes()); $revision = new PageRevision($page->getAttributes());
@ -237,11 +249,10 @@ class PageRepo
{ {
// If the page itself is a draft simply update that // If the page itself is a draft simply update that
if ($page->draft) { if ($page->draft) {
$page->fill($input);
if (isset($input['html'])) { if (isset($input['html'])) {
$content = new PageContent($page); (new PageContent($page))->setNewHTML($input['html']);
$content->setNewHTML($input['html']);
} }
$page->fill($input);
$page->save(); $page->save();
return $page; return $page;
} }
@ -259,12 +270,14 @@ class PageRepo
/** /**
* Destroy a page from the system. * Destroy a page from the system.
* @throws NotifyException * @throws Exception
*/ */
public function destroy(Page $page) public function destroy(Page $page)
{ {
$trashCan = new TrashCan(); $trashCan = new TrashCan();
$trashCan->destroyPage($page); $trashCan->softDestroyPage($page);
Activity::addForEntity($page, ActivityType::PAGE_DELETE);
$trashCan->autoClearOld();
} }
/** /**
@ -273,17 +286,20 @@ class PageRepo
public function restoreRevision(Page $page, int $revisionId): Page public function restoreRevision(Page $page, int $revisionId): Page
{ {
$page->revision_count++; $page->revision_count++;
$this->savePageRevision($page);
$revision = $page->revisions()->where('id', '=', $revisionId)->first(); $revision = $page->revisions()->where('id', '=', $revisionId)->first();
$page->fill($revision->toArray()); $page->fill($revision->toArray());
$content = new PageContent($page); $content = new PageContent($page);
$content->setNewHTML($revision->html); $content->setNewHTML($revision->html);
$page->updated_by = user()->id; $page->updated_by = user()->id;
$page->refreshSlug(); $page->refreshSlug();
$page->save(); $page->save();
$page->indexForSearch(); $page->indexForSearch();
$summary = trans('entities.pages_revision_restored_from', ['id' => strval($revisionId), 'summary' => $revision->summary]);
$this->savePageRevision($page, $summary);
Activity::addForEntity($page, ActivityType::PAGE_RESTORE);
return $page; return $page;
} }
@ -294,7 +310,7 @@ class PageRepo
* @throws MoveOperationException * @throws MoveOperationException
* @throws PermissionsException * @throws PermissionsException
*/ */
public function move(Page $page, string $parentIdentifier): Book public function move(Page $page, string $parentIdentifier): Entity
{ {
$parent = $this->findParentByIdentifier($parentIdentifier); $parent = $this->findParentByIdentifier($parentIdentifier);
if ($parent === null) { if ($parent === null) {
@ -309,7 +325,8 @@ class PageRepo
$page->changeBook($parent instanceof Book ? $parent->id : $parent->book->id); $page->changeBook($parent instanceof Book ? $parent->id : $parent->book->id);
$page->rebuildPermissions(); $page->rebuildPermissions();
return ($parent instanceof Book ? $parent : $parent->book); Activity::addForEntity($page, ActivityType::PAGE_MOVE);
return $parent;
} }
/** /**
@ -320,7 +337,7 @@ class PageRepo
*/ */
public function copy(Page $page, string $parentIdentifier = null, string $newName = null): Page public function copy(Page $page, string $parentIdentifier = null, string $newName = null): Page
{ {
$parent = $parentIdentifier ? $this->findParentByIdentifier($parentIdentifier) : $page->parent(); $parent = $parentIdentifier ? $this->findParentByIdentifier($parentIdentifier) : $page->getParent();
if ($parent === null) { if ($parent === null) {
throw new MoveOperationException('Book or chapter to move page into not found'); throw new MoveOperationException('Book or chapter to move page into not found');
} }
@ -368,14 +385,6 @@ class PageRepo
return $parentClass::visible()->where('id', '=', $entityId)->first(); return $parentClass::visible()->where('id', '=', $entityId)->first();
} }
/**
* Update the permissions of a page.
*/
public function updatePermissions(Page $page, bool $restricted, Collection $permissions = null)
{
$this->baseRepo->updatePermissions($page, $restricted, $permissions);
}
/** /**
* Change the page's parent to the given entity. * Change the page's parent to the given entity.
*/ */
@ -439,8 +448,9 @@ class PageRepo
*/ */
protected function getNewPriority(Page $page): int protected function getNewPriority(Page $page): int
{ {
if ($page->parent() instanceof Chapter) { $parent = $page->getParent();
$lastPage = $page->parent()->pages('desc')->first(); if ($parent instanceof Chapter) {
$lastPage = $parent->pages('desc')->first();
return $lastPage ? $lastPage->priority + 1 : 0; return $lastPage ? $lastPage->priority + 1 : 0;
} }

View File

@ -1,10 +1,10 @@
<?php namespace BookStack\Entities\Managers; <?php namespace BookStack\Entities\Tools;
use BookStack\Entities\Book; use BookStack\Entities\Models\Book;
use BookStack\Entities\BookChild; use BookStack\Entities\Models\BookChild;
use BookStack\Entities\Chapter; use BookStack\Entities\Models\Chapter;
use BookStack\Entities\Entity; use BookStack\Entities\Models\Entity;
use BookStack\Entities\Page; use BookStack\Entities\Models\Page;
use BookStack\Exceptions\SortOperationException; use BookStack\Exceptions\SortOperationException;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
@ -18,7 +18,6 @@ class BookContents
/** /**
* BookContents constructor. * BookContents constructor.
* @param $book
*/ */
public function __construct(Book $book) public function __construct(Book $book)
{ {

View File

@ -1,14 +1,15 @@
<?php namespace BookStack\Entities; <?php namespace BookStack\Entities\Tools;
use BookStack\Entities\Managers\BookContents; use BookStack\Entities\Models\Book;
use BookStack\Entities\Managers\PageContent; use BookStack\Entities\Models\Chapter;
use BookStack\Entities\Models\Page;
use BookStack\Uploads\ImageService; use BookStack\Uploads\ImageService;
use DomPDF; use DomPDF;
use Exception; use Exception;
use SnappyPDF; use SnappyPDF;
use Throwable; use Throwable;
class ExportService class ExportFormatter
{ {
protected $imageService; protected $imageService;
@ -142,7 +143,7 @@ class ExportService
protected function containHtml(string $htmlContent): string protected function containHtml(string $htmlContent): string
{ {
$imageTagsOutput = []; $imageTagsOutput = [];
preg_match_all("/\<img.*src\=(\'|\")(.*?)(\'|\").*?\>/i", $htmlContent, $imageTagsOutput); preg_match_all("/\<img.*?src\=(\'|\")(.*?)(\'|\").*?\>/i", $htmlContent, $imageTagsOutput);
// Replace image src with base64 encoded image strings // Replace image src with base64 encoded image strings
if (isset($imageTagsOutput[0]) && count($imageTagsOutput[0]) > 0) { if (isset($imageTagsOutput[0]) && count($imageTagsOutput[0]) > 0) {

View File

@ -1,9 +1,10 @@
<?php namespace BookStack\Entities\Managers; <?php namespace BookStack\Entities\Tools;
use BookStack\Entities\Page; use BookStack\Entities\Models\Page;
use DOMDocument; use DOMDocument;
use DOMNodeList; use DOMNodeList;
use DOMXPath; use DOMXPath;
use League\CommonMark\CommonMarkConverter;
class PageContent class PageContent
{ {
@ -25,6 +26,27 @@ class PageContent
{ {
$this->page->html = $this->formatHtml($html); $this->page->html = $this->formatHtml($html);
$this->page->text = $this->toPlainText(); $this->page->text = $this->toPlainText();
$this->page->markdown = '';
}
/**
* Update the content of the page with new provided Markdown content.
*/
public function setNewMarkdown(string $markdown)
{
$this->page->markdown = $markdown;
$html = $this->markdownToHtml($markdown);
$this->page->html = $this->formatHtml($html);
$this->page->text = $this->toPlainText();
}
/**
* Convert the given Markdown content to a HTML string.
*/
protected function markdownToHtml(string $markdown): string
{
$converter = new CommonMarkConverter();
return $converter->convertToHtml($markdown);
} }
/** /**

View File

@ -1,7 +1,7 @@
<?php namespace BookStack\Entities\Managers; <?php namespace BookStack\Entities\Tools;
use BookStack\Entities\Page; use BookStack\Entities\Models\Page;
use BookStack\Entities\PageRevision; use BookStack\Entities\Models\PageRevision;
use Carbon\Carbon; use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;

View File

@ -0,0 +1,68 @@
<?php namespace BookStack\Entities\Tools;
use BookStack\Actions\ActivityType;
use BookStack\Auth\User;
use BookStack\Entities\Models\Entity;
use BookStack\Facades\Activity;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
class PermissionsUpdater
{
/**
* Update an entities permissions from a permission form submit request.
*/
public function updateFromPermissionsForm(Entity $entity, Request $request)
{
$restricted = $request->get('restricted') === 'true';
$permissions = $request->get('restrictions', null);
$ownerId = $request->get('owned_by', null);
$entity->restricted = $restricted;
$entity->permissions()->delete();
if (!is_null($permissions)) {
$entityPermissionData = $this->formatPermissionsFromRequestToEntityPermissions($permissions);
$entity->permissions()->createMany($entityPermissionData);
}
if (!is_null($ownerId)) {
$this->updateOwnerFromId($entity, intval($ownerId));
}
$entity->save();
$entity->rebuildPermissions();
Activity::addForEntity($entity, ActivityType::PERMISSIONS_UPDATE);
}
/**
* Update the owner of the given entity.
* Checks the user exists in the system first.
* Does not save the model, just updates it.
*/
protected function updateOwnerFromId(Entity $entity, int $newOwnerId)
{
$newOwner = User::query()->find($newOwnerId);
if (!is_null($newOwner)) {
$entity->owned_by = $newOwner->id;
}
}
/**
* Format permissions provided from a permission form to be
* EntityPermission data.
*/
protected function formatPermissionsFromRequestToEntityPermissions(array $permissions): Collection
{
return collect($permissions)->flatMap(function ($restrictions, $roleId) {
return collect($restrictions)->keys()->map(function ($action) use ($roleId) {
return [
'role_id' => $roleId,
'action' => strtolower($action),
] ;
});
});
}
}

View File

@ -0,0 +1,120 @@
<?php namespace BookStack\Entities\Tools;
use BookStack\Entities\EntityProvider;
use BookStack\Entities\Models\Entity;
use BookStack\Entities\Models\SearchTerm;
use Illuminate\Support\Collection;
class SearchIndex
{
/**
* @var SearchTerm
*/
protected $searchTerm;
/**
* @var EntityProvider
*/
protected $entityProvider;
public function __construct(SearchTerm $searchTerm, EntityProvider $entityProvider)
{
$this->searchTerm = $searchTerm;
$this->entityProvider = $entityProvider;
}
/**
* Index the given entity.
*/
public function indexEntity(Entity $entity)
{
$this->deleteEntityTerms($entity);
$nameTerms = $this->generateTermArrayFromText($entity->name, 5 * $entity->searchFactor);
$bodyTerms = $this->generateTermArrayFromText($entity->getText(), 1 * $entity->searchFactor);
$terms = array_merge($nameTerms, $bodyTerms);
foreach ($terms as $index => $term) {
$terms[$index]['entity_type'] = $entity->getMorphClass();
$terms[$index]['entity_id'] = $entity->id;
}
$this->searchTerm->newQuery()->insert($terms);
}
/**
* Index multiple Entities at once
* @param Entity[] $entities
*/
protected function indexEntities(array $entities)
{
$terms = [];
foreach ($entities as $entity) {
$nameTerms = $this->generateTermArrayFromText($entity->name, 5 * $entity->searchFactor);
$bodyTerms = $this->generateTermArrayFromText($entity->getText(), 1 * $entity->searchFactor);
foreach (array_merge($nameTerms, $bodyTerms) as $term) {
$term['entity_id'] = $entity->id;
$term['entity_type'] = $entity->getMorphClass();
$terms[] = $term;
}
}
$chunkedTerms = array_chunk($terms, 500);
foreach ($chunkedTerms as $termChunk) {
$this->searchTerm->newQuery()->insert($termChunk);
}
}
/**
* Delete and re-index the terms for all entities in the system.
*/
public function indexAllEntities()
{
$this->searchTerm->newQuery()->truncate();
foreach ($this->entityProvider->all() as $entityModel) {
$selectFields = ['id', 'name', $entityModel->textField];
$entityModel->newQuery()
->withTrashed()
->select($selectFields)
->chunk(1000, function (Collection $entities) {
$this->indexEntities($entities->all());
});
}
}
/**
* Delete related Entity search terms.
*/
public function deleteEntityTerms(Entity $entity)
{
$entity->searchTerms()->delete();
}
/**
* Create a scored term array from the given text.
*/
protected function generateTermArrayFromText(string $text, int $scoreAdjustment = 1): array
{
$tokenMap = []; // {TextToken => OccurrenceCount}
$splitChars = " \n\t.,!?:;()[]{}<>`'\"";
$token = strtok($text, $splitChars);
while ($token !== false) {
if (!isset($tokenMap[$token])) {
$tokenMap[$token] = 0;
}
$tokenMap[$token]++;
$token = strtok($splitChars);
}
$terms = [];
foreach ($tokenMap as $token => $count) {
$terms[] = [
'term' => $token,
'score' => $count * $scoreAdjustment
];
}
return $terms;
}
}

View File

@ -1,4 +1,4 @@
<?php namespace BookStack\Entities; <?php namespace BookStack\Entities\Tools;
use Illuminate\Http\Request; use Illuminate\Http\Request;

View File

@ -1,6 +1,8 @@
<?php namespace BookStack\Entities; <?php namespace BookStack\Entities\Tools;
use BookStack\Auth\Permissions\PermissionService; use BookStack\Auth\Permissions\PermissionService;
use BookStack\Entities\EntityProvider;
use BookStack\Entities\Models\Entity;
use Illuminate\Database\Connection; use Illuminate\Database\Connection;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder; use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Query\Builder; use Illuminate\Database\Query\Builder;
@ -8,12 +10,8 @@ use Illuminate\Database\Query\JoinClause;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Illuminate\Support\Str; use Illuminate\Support\Str;
class SearchService class SearchRunner
{ {
/**
* @var SearchTerm
*/
protected $searchTerm;
/** /**
* @var EntityProvider * @var EntityProvider
@ -37,25 +35,14 @@ class SearchService
*/ */
protected $queryOperators = ['<=', '>=', '=', '<', '>', 'like', '!=']; protected $queryOperators = ['<=', '>=', '=', '<', '>', 'like', '!='];
/**
* SearchService constructor. public function __construct(EntityProvider $entityProvider, Connection $db, PermissionService $permissionService)
*/
public function __construct(SearchTerm $searchTerm, EntityProvider $entityProvider, Connection $db, PermissionService $permissionService)
{ {
$this->searchTerm = $searchTerm;
$this->entityProvider = $entityProvider; $this->entityProvider = $entityProvider;
$this->db = $db; $this->db = $db;
$this->permissionService = $permissionService; $this->permissionService = $permissionService;
} }
/**
* Set the database connection
*/
public function setConnection(Connection $connection)
{
$this->db = $connection;
}
/** /**
* Search all entities in the system. * Search all entities in the system.
* The provided count is for each entity to search, * The provided count is for each entity to search,
@ -115,11 +102,12 @@ class SearchService
$search = $this->buildEntitySearchQuery($opts, $entityType)->where('book_id', '=', $bookId)->take(20)->get(); $search = $this->buildEntitySearchQuery($opts, $entityType)->where('book_id', '=', $bookId)->take(20)->get();
$results = $results->merge($search); $results = $results->merge($search);
} }
return $results->sortByDesc('score')->take(20); return $results->sortByDesc('score')->take(20);
} }
/** /**
* Search a book for entities * Search a chapter for entities
*/ */
public function searchChapter(int $chapterId, string $searchString): Collection public function searchChapter(int $chapterId, string $searchString): Collection
{ {
@ -134,7 +122,7 @@ class SearchService
* matching instead of the items themselves. * matching instead of the items themselves.
* @return \Illuminate\Database\Eloquent\Collection|int|static[] * @return \Illuminate\Database\Eloquent\Collection|int|static[]
*/ */
public function searchEntityTable(SearchOptions $searchOpts, string $entityType = 'page', int $page = 1, int $count = 20, string $action = 'view', bool $getCount = false) protected function searchEntityTable(SearchOptions $searchOpts, string $entityType = 'page', int $page = 1, int $count = 20, string $action = 'view', bool $getCount = false)
{ {
$query = $this->buildEntitySearchQuery($searchOpts, $entityType, $action); $query = $this->buildEntitySearchQuery($searchOpts, $entityType, $action);
if ($getCount) { if ($getCount) {
@ -155,30 +143,27 @@ class SearchService
// Handle normal search terms // Handle normal search terms
if (count($searchOpts->searches) > 0) { if (count($searchOpts->searches) > 0) {
$subQuery = $this->db->table('search_terms')->select('entity_id', 'entity_type', \DB::raw('SUM(score) as score')); $rawScoreSum = $this->db->raw('SUM(score) as score');
$subQuery = $this->db->table('search_terms')->select('entity_id', 'entity_type', $rawScoreSum);
$subQuery->where('entity_type', '=', $entity->getMorphClass()); $subQuery->where('entity_type', '=', $entity->getMorphClass());
$subQuery->where(function (Builder $query) use ($searchOpts) { $subQuery->where(function (Builder $query) use ($searchOpts) {
foreach ($searchOpts->searches as $inputTerm) { foreach ($searchOpts->searches as $inputTerm) {
$query->orWhere('term', 'like', $inputTerm .'%'); $query->orWhere('term', 'like', $inputTerm .'%');
} }
})->groupBy('entity_type', 'entity_id'); })->groupBy('entity_type', 'entity_id');
$entitySelect->join(\DB::raw('(' . $subQuery->toSql() . ') as s'), function (JoinClause $join) { $entitySelect->join($this->db->raw('(' . $subQuery->toSql() . ') as s'), function (JoinClause $join) {
$join->on('id', '=', 'entity_id'); $join->on('id', '=', 'entity_id');
})->selectRaw($entity->getTable().'.*, s.score')->orderBy('score', 'desc'); })->selectRaw($entity->getTable().'.*, s.score')->orderBy('score', 'desc');
$entitySelect->mergeBindings($subQuery); $entitySelect->mergeBindings($subQuery);
} }
// Handle exact term matching // Handle exact term matching
if (count($searchOpts->exacts) > 0) {
$entitySelect->where(function (EloquentBuilder $query) use ($searchOpts, $entity) {
foreach ($searchOpts->exacts as $inputTerm) { foreach ($searchOpts->exacts as $inputTerm) {
$query->where(function (EloquentBuilder $query) use ($inputTerm, $entity) { $entitySelect->where(function (EloquentBuilder $query) use ($inputTerm, $entity) {
$query->where('name', 'like', '%'.$inputTerm .'%') $query->where('name', 'like', '%'.$inputTerm .'%')
->orWhere($entity->textField, 'like', '%'.$inputTerm .'%'); ->orWhere($entity->textField, 'like', '%'.$inputTerm .'%');
}); });
} }
});
}
// Handle tag searches // Handle tag searches
foreach ($searchOpts->tags as $inputTerm) { foreach ($searchOpts->tags as $inputTerm) {
@ -239,102 +224,6 @@ class SearchService
return $query; return $query;
} }
/**
* Index the given entity.
*/
public function indexEntity(Entity $entity)
{
$this->deleteEntityTerms($entity);
$nameTerms = $this->generateTermArrayFromText($entity->name, 5 * $entity->searchFactor);
$bodyTerms = $this->generateTermArrayFromText($entity->getText(), 1 * $entity->searchFactor);
$terms = array_merge($nameTerms, $bodyTerms);
foreach ($terms as $index => $term) {
$terms[$index]['entity_type'] = $entity->getMorphClass();
$terms[$index]['entity_id'] = $entity->id;
}
$this->searchTerm->newQuery()->insert($terms);
}
/**
* Index multiple Entities at once
* @param \BookStack\Entities\Entity[] $entities
*/
protected function indexEntities($entities)
{
$terms = [];
foreach ($entities as $entity) {
$nameTerms = $this->generateTermArrayFromText($entity->name, 5 * $entity->searchFactor);
$bodyTerms = $this->generateTermArrayFromText($entity->getText(), 1 * $entity->searchFactor);
foreach (array_merge($nameTerms, $bodyTerms) as $term) {
$term['entity_id'] = $entity->id;
$term['entity_type'] = $entity->getMorphClass();
$terms[] = $term;
}
}
$chunkedTerms = array_chunk($terms, 500);
foreach ($chunkedTerms as $termChunk) {
$this->searchTerm->newQuery()->insert($termChunk);
}
}
/**
* Delete and re-index the terms for all entities in the system.
*/
public function indexAllEntities()
{
$this->searchTerm->truncate();
foreach ($this->entityProvider->all() as $entityModel) {
$selectFields = ['id', 'name', $entityModel->textField];
$entityModel->newQuery()->select($selectFields)->chunk(1000, function ($entities) {
$this->indexEntities($entities);
});
}
}
/**
* Delete related Entity search terms.
* @param Entity $entity
*/
public function deleteEntityTerms(Entity $entity)
{
$entity->searchTerms()->delete();
}
/**
* Create a scored term array from the given text.
* @param $text
* @param float|int $scoreAdjustment
* @return array
*/
protected function generateTermArrayFromText($text, $scoreAdjustment = 1)
{
$tokenMap = []; // {TextToken => OccurrenceCount}
$splitChars = " \n\t.,!?:;()[]{}<>`'\"";
$token = strtok($text, $splitChars);
while ($token !== false) {
if (!isset($tokenMap[$token])) {
$tokenMap[$token] = 0;
}
$tokenMap[$token]++;
$token = strtok($splitChars);
}
$terms = [];
foreach ($tokenMap as $token => $count) {
$terms[] = [
'term' => $token,
'score' => $count * $scoreAdjustment
];
}
return $terms;
}
/** /**
* Custom entity search filters * Custom entity search filters
*/ */

View File

@ -1,29 +1,18 @@
<?php namespace BookStack\Entities\Managers; <?php namespace BookStack\Entities\Tools;
use BookStack\Entities\Book; use BookStack\Entities\Models\Book;
use BookStack\Entities\Bookshelf; use BookStack\Entities\Models\Bookshelf;
use Illuminate\Session\Store;
class EntityContext class ShelfContext
{ {
protected $session;
protected $KEY_SHELF_CONTEXT_ID = 'context_bookshelf_id'; protected $KEY_SHELF_CONTEXT_ID = 'context_bookshelf_id';
/**
* EntityContextManager constructor.
*/
public function __construct(Store $session)
{
$this->session = $session;
}
/** /**
* Get the current bookshelf context for the given book. * Get the current bookshelf context for the given book.
*/ */
public function getContextualShelfForBook(Book $book): ?Bookshelf public function getContextualShelfForBook(Book $book): ?Bookshelf
{ {
$contextBookshelfId = $this->session->get($this->KEY_SHELF_CONTEXT_ID, null); $contextBookshelfId = session()->get($this->KEY_SHELF_CONTEXT_ID, null);
if (!is_int($contextBookshelfId)) { if (!is_int($contextBookshelfId)) {
return null; return null;
@ -37,11 +26,10 @@ class EntityContext
/** /**
* Store the current contextual shelf ID. * Store the current contextual shelf ID.
* @param int $shelfId
*/ */
public function setShelfContext(int $shelfId) public function setShelfContext(int $shelfId)
{ {
$this->session->put($this->KEY_SHELF_CONTEXT_ID, $shelfId); session()->put($this->KEY_SHELF_CONTEXT_ID, $shelfId);
} }
/** /**
@ -49,6 +37,6 @@ class EntityContext
*/ */
public function clearShelfContext() public function clearShelfContext()
{ {
$this->session->forget($this->KEY_SHELF_CONTEXT_ID); session()->forget($this->KEY_SHELF_CONTEXT_ID);
} }
} }

View File

@ -0,0 +1,47 @@
<?php namespace BookStack\Entities\Tools;
use BookStack\Entities\EntityProvider;
use BookStack\Entities\Models\Book;
use BookStack\Entities\Models\Bookshelf;
use Illuminate\Support\Collection;
class SiblingFetcher
{
/**
* Search among the siblings of the entity of given type and id.
*/
public function fetch(string $entityType, int $entityId): Collection
{
$entity = (new EntityProvider)->get($entityType)->visible()->findOrFail($entityId);
$entities = [];
// Page in chapter
if ($entity->isA('page') && $entity->chapter) {
$entities = $entity->chapter->getVisiblePages();
}
// Page in book or chapter
if (($entity->isA('page') && !$entity->chapter) || $entity->isA('chapter')) {
$entities = $entity->book->getDirectChildren();
}
// Book
// Gets just the books in a shelf if shelf is in context
if ($entity->isA('book')) {
$contextShelf = (new ShelfContext)->getContextualShelfForBook($entity);
if ($contextShelf) {
$entities = $contextShelf->visibleBooks()->get();
} else {
$entities = Book::visible()->get();
}
}
// Shelve
if ($entity->isA('bookshelf')) {
$entities = Bookshelf::visible()->get();
}
return $entities;
}
}

View File

@ -1,29 +1,19 @@
<?php namespace BookStack\Entities; <?php namespace BookStack\Entities\Tools;
use BookStack\Entities\Models\Entity;
use Illuminate\Support\Str; use Illuminate\Support\Str;
class SlugGenerator class SlugGenerator
{ {
protected $entity;
/**
* SlugGenerator constructor.
* @param $entity
*/
public function __construct(Entity $entity)
{
$this->entity = $entity;
}
/** /**
* Generate a fresh slug for the given entity. * Generate a fresh slug for the given entity.
* The slug will generated so it does not conflict within the same parent item. * The slug will generated so it does not conflict within the same parent item.
*/ */
public function generate(): string public function generate(Entity $entity): string
{ {
$slug = $this->formatNameAsSlug($this->entity->name); $slug = $this->formatNameAsSlug($entity->name);
while ($this->slugInUse($slug)) { while ($this->slugInUse($slug, $entity)) {
$slug .= '-' . substr(md5(rand(1, 500)), 0, 3); $slug .= '-' . substr(md5(rand(1, 500)), 0, 3);
} }
return $slug; return $slug;
@ -45,16 +35,16 @@ class SlugGenerator
* Check if a slug is already in-use for this * Check if a slug is already in-use for this
* type of model within the same parent. * type of model within the same parent.
*/ */
protected function slugInUse(string $slug): bool protected function slugInUse(string $slug, Entity $entity): bool
{ {
$query = $this->entity->newQuery()->where('slug', '=', $slug); $query = $entity->newQuery()->where('slug', '=', $slug);
if ($this->entity instanceof BookChild) { if ($entity instanceof BookChild) {
$query->where('book_id', '=', $this->entity->book_id); $query->where('book_id', '=', $entity->book_id);
} }
if ($this->entity->id) { if ($entity->id) {
$query->where('id', '!=', $this->entity->id); $query->where('id', '!=', $entity->id);
} }
return $query->count() > 0; return $query->count() > 0;

View File

@ -0,0 +1,325 @@
<?php namespace BookStack\Entities\Tools;
use BookStack\Entities\Models\Book;
use BookStack\Entities\Models\Bookshelf;
use BookStack\Entities\Models\Chapter;
use BookStack\Entities\Models\Deletion;
use BookStack\Entities\Models\Entity;
use BookStack\Entities\EntityProvider;
use BookStack\Entities\Models\HasCoverImage;
use BookStack\Entities\Models\Page;
use BookStack\Exceptions\NotifyException;
use BookStack\Facades\Activity;
use BookStack\Uploads\AttachmentService;
use BookStack\Uploads\ImageService;
use Exception;
use Illuminate\Support\Carbon;
class TrashCan
{
/**
* Send a shelf to the recycle bin.
*/
public function softDestroyShelf(Bookshelf $shelf)
{
Deletion::createForEntity($shelf);
$shelf->delete();
}
/**
* Send a book to the recycle bin.
* @throws Exception
*/
public function softDestroyBook(Book $book)
{
Deletion::createForEntity($book);
foreach ($book->pages as $page) {
$this->softDestroyPage($page, false);
}
foreach ($book->chapters as $chapter) {
$this->softDestroyChapter($chapter, false);
}
$book->delete();
}
/**
* Send a chapter to the recycle bin.
* @throws Exception
*/
public function softDestroyChapter(Chapter $chapter, bool $recordDelete = true)
{
if ($recordDelete) {
Deletion::createForEntity($chapter);
}
if (count($chapter->pages) > 0) {
foreach ($chapter->pages as $page) {
$this->softDestroyPage($page, false);
}
}
$chapter->delete();
}
/**
* Send a page to the recycle bin.
* @throws Exception
*/
public function softDestroyPage(Page $page, bool $recordDelete = true)
{
if ($recordDelete) {
Deletion::createForEntity($page);
}
// Check if set as custom homepage & remove setting if not used or throw error if active
$customHome = setting('app-homepage', '0:');
if (intval($page->id) === intval(explode(':', $customHome)[0])) {
if (setting('app-homepage-type') === 'page') {
throw new NotifyException(trans('errors.page_custom_home_deletion'), $page->getUrl());
}
setting()->remove('app-homepage');
}
$page->delete();
}
/**
* Remove a bookshelf from the system.
* @throws Exception
*/
protected function destroyShelf(Bookshelf $shelf): int
{
$this->destroyCommonRelations($shelf);
$shelf->forceDelete();
return 1;
}
/**
* Remove a book from the system.
* Destroys any child chapters and pages.
* @throws Exception
*/
protected function destroyBook(Book $book): int
{
$count = 0;
$pages = $book->pages()->withTrashed()->get();
foreach ($pages as $page) {
$this->destroyPage($page);
$count++;
}
$chapters = $book->chapters()->withTrashed()->get();
foreach ($chapters as $chapter) {
$this->destroyChapter($chapter);
$count++;
}
$this->destroyCommonRelations($book);
$book->forceDelete();
return $count + 1;
}
/**
* Remove a chapter from the system.
* Destroys all pages within.
* @throws Exception
*/
protected function destroyChapter(Chapter $chapter): int
{
$count = 0;
$pages = $chapter->pages()->withTrashed()->get();
if (count($pages)) {
foreach ($pages as $page) {
$this->destroyPage($page);
$count++;
}
}
$this->destroyCommonRelations($chapter);
$chapter->forceDelete();
return $count + 1;
}
/**
* Remove a page from the system.
* @throws Exception
*/
protected function destroyPage(Page $page): int
{
$this->destroyCommonRelations($page);
// Delete Attached Files
$attachmentService = app(AttachmentService::class);
foreach ($page->attachments as $attachment) {
$attachmentService->deleteFile($attachment);
}
$page->forceDelete();
return 1;
}
/**
* Get the total counts of those that have been trashed
* but not yet fully deleted (In recycle bin).
*/
public function getTrashedCounts(): array
{
$counts = [];
/** @var Entity $instance */
foreach ((new EntityProvider)->all() as $key => $instance) {
$counts[$key] = $instance->newQuery()->onlyTrashed()->count();
}
return $counts;
}
/**
* Destroy all items that have pending deletions.
* @throws Exception
*/
public function empty(): int
{
$deletions = Deletion::all();
$deleteCount = 0;
foreach ($deletions as $deletion) {
$deleteCount += $this->destroyFromDeletion($deletion);
}
return $deleteCount;
}
/**
* Destroy an element from the given deletion model.
* @throws Exception
*/
public function destroyFromDeletion(Deletion $deletion): int
{
// We directly load the deletable element here just to ensure it still
// exists in the event it has already been destroyed during this request.
$entity = $deletion->deletable()->first();
$count = 0;
if ($entity) {
$count = $this->destroyEntity($deletion->deletable);
}
$deletion->delete();
return $count;
}
/**
* Restore the content within the given deletion.
* @throws Exception
*/
public function restoreFromDeletion(Deletion $deletion): int
{
$shouldRestore = true;
$restoreCount = 0;
$parent = $deletion->deletable->getParent();
if ($parent && $parent->trashed()) {
$shouldRestore = false;
}
if ($shouldRestore) {
$restoreCount = $this->restoreEntity($deletion->deletable);
}
$deletion->delete();
return $restoreCount;
}
/**
* Automatically clear old content from the recycle bin
* depending on the configured lifetime.
* Returns the total number of deleted elements.
* @throws Exception
*/
public function autoClearOld(): int
{
$lifetime = intval(config('app.recycle_bin_lifetime'));
if ($lifetime < 0) {
return 0;
}
$clearBeforeDate = Carbon::now()->addSeconds(10)->subDays($lifetime);
$deleteCount = 0;
$deletionsToRemove = Deletion::query()->where('created_at', '<', $clearBeforeDate)->get();
foreach ($deletionsToRemove as $deletion) {
$deleteCount += $this->destroyFromDeletion($deletion);
}
return $deleteCount;
}
/**
* Restore an entity so it is essentially un-deleted.
* Deletions on restored child elements will be removed during this restoration.
*/
protected function restoreEntity(Entity $entity): int
{
$count = 1;
$entity->restore();
$restoreAction = function ($entity) use (&$count) {
if ($entity->deletions_count > 0) {
$entity->deletions()->delete();
}
$entity->restore();
$count++;
};
if ($entity->isA('chapter') || $entity->isA('book')) {
$entity->pages()->withTrashed()->withCount('deletions')->get()->each($restoreAction);
}
if ($entity->isA('book')) {
$entity->chapters()->withTrashed()->withCount('deletions')->get()->each($restoreAction);
}
return $count;
}
/**
* Destroy the given entity.
*/
protected function destroyEntity(Entity $entity): int
{
if ($entity->isA('page')) {
return $this->destroyPage($entity);
}
if ($entity->isA('chapter')) {
return $this->destroyChapter($entity);
}
if ($entity->isA('book')) {
return $this->destroyBook($entity);
}
if ($entity->isA('shelf')) {
return $this->destroyShelf($entity);
}
}
/**
* Update entity relations to remove or update outstanding connections.
*/
protected function destroyCommonRelations(Entity $entity)
{
Activity::removeEntity($entity);
$entity->views()->delete();
$entity->permissions()->delete();
$entity->tags()->delete();
$entity->comments()->delete();
$entity->jointPermissions()->delete();
$entity->searchTerms()->delete();
$entity->deletions()->delete();
if ($entity instanceof HasCoverImage && $entity->cover) {
$imageService = app()->make(ImageService::class);
$imageService->destroy($entity->cover);
}
}
}

View File

@ -5,7 +5,7 @@ use BookStack\Http\Controllers\Controller;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\JsonResponse; use Illuminate\Http\JsonResponse;
class ApiController extends Controller abstract class ApiController extends Controller
{ {
protected $rules = []; protected $rules = [];

View File

@ -1,8 +1,6 @@
<?php namespace BookStack\Http\Controllers\Api; <?php namespace BookStack\Http\Controllers\Api;
use BookStack\Api\ApiDocsGenerator; use BookStack\Api\ApiDocsGenerator;
use Cache;
use Illuminate\Support\Collection;
class ApiDocsController extends ApiController class ApiDocsController extends ApiController
{ {
@ -12,7 +10,8 @@ class ApiDocsController extends ApiController
*/ */
public function display() public function display()
{ {
$docs = $this->getDocs(); $docs = ApiDocsGenerator::generateConsideringCache();
$this->setPageTitle(trans('settings.users_api_tokens_docs'));
return view('api-docs.index', [ return view('api-docs.index', [
'docs' => $docs, 'docs' => $docs,
]); ]);
@ -21,27 +20,10 @@ class ApiDocsController extends ApiController
/** /**
* Show a JSON view of the API docs data. * Show a JSON view of the API docs data.
*/ */
public function json() { public function json()
$docs = $this->getDocs(); {
$docs = ApiDocsGenerator::generateConsideringCache();
return response()->json($docs); return response()->json($docs);
} }
/**
* Get the base docs data.
* Checks and uses the system cache for quick re-fetching.
*/
protected function getDocs(): Collection
{
$appVersion = trim(file_get_contents(base_path('version')));
$cacheKey = 'api-docs::' . $appVersion;
if (Cache::has($cacheKey) && config('app.env') === 'production') {
$docs = Cache::get($cacheKey);
} else {
$docs = (new ApiDocsGenerator())->generate();
Cache::put($cacheKey, $docs, 60*24);
}
return $docs;
}
} }

View File

@ -1,9 +1,8 @@
<?php namespace BookStack\Http\Controllers\Api; <?php namespace BookStack\Http\Controllers\Api;
use BookStack\Entities\Book; use BookStack\Entities\Models\Book;
use BookStack\Entities\Repos\BookRepo; use BookStack\Entities\Repos\BookRepo;
use BookStack\Exceptions\NotifyException; use BookStack\Exceptions\NotifyException;
use BookStack\Facades\Activity;
use Illuminate\Contracts\Container\BindingResolutionException; use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException; use Illuminate\Validation\ValidationException;
@ -26,9 +25,6 @@ class BookApiController extends ApiController
], ],
]; ];
/**
* BooksApiController constructor.
*/
public function __construct(BookRepo $bookRepo) public function __construct(BookRepo $bookRepo)
{ {
$this->bookRepo = $bookRepo; $this->bookRepo = $bookRepo;
@ -55,8 +51,6 @@ class BookApiController extends ApiController
$requestData = $this->validate($request, $this->rules['create']); $requestData = $this->validate($request, $this->rules['create']);
$book = $this->bookRepo->create($requestData); $book = $this->bookRepo->create($requestData);
Activity::add($book, 'book_create', $book->id);
return response()->json($book); return response()->json($book);
} }
@ -80,15 +74,14 @@ class BookApiController extends ApiController
$requestData = $this->validate($request, $this->rules['update']); $requestData = $this->validate($request, $this->rules['update']);
$book = $this->bookRepo->update($book, $requestData); $book = $this->bookRepo->update($book, $requestData);
Activity::add($book, 'book_update', $book->id);
return response()->json($book); return response()->json($book);
} }
/** /**
* Delete a single book from the system. * Delete a single book.
* @throws NotifyException * This will typically send the book to the recycle bin.
* @throws BindingResolutionException * @throws \Exception
*/ */
public function delete(string $id) public function delete(string $id)
{ {
@ -96,8 +89,6 @@ class BookApiController extends ApiController
$this->checkOwnablePermission('book-delete', $book); $this->checkOwnablePermission('book-delete', $book);
$this->bookRepo->destroy($book); $this->bookRepo->destroy($book);
Activity::addMessage('book_delete', $book->name);
return response('', 204); return response('', 204);
} }
} }

View File

@ -1,23 +1,16 @@
<?php namespace BookStack\Http\Controllers\Api; <?php namespace BookStack\Http\Controllers\Api;
use BookStack\Entities\Book; use BookStack\Entities\Models\Book;
use BookStack\Entities\ExportService; use BookStack\Entities\Tools\ExportFormatter;
use BookStack\Entities\Repos\BookRepo;
use Throwable; use Throwable;
class BookExportApiController extends ApiController class BookExportApiController extends ApiController
{ {
protected $bookRepo; protected $exportFormatter;
protected $exportService;
/** public function __construct(ExportFormatter $exportFormatter)
* BookExportController constructor.
*/
public function __construct(BookRepo $bookRepo, ExportService $exportService)
{ {
$this->bookRepo = $bookRepo; $this->exportFormatter = $exportFormatter;
$this->exportService = $exportService;
parent::__construct();
} }
/** /**
@ -27,7 +20,7 @@ class BookExportApiController extends ApiController
public function exportPdf(int $id) public function exportPdf(int $id)
{ {
$book = Book::visible()->findOrFail($id); $book = Book::visible()->findOrFail($id);
$pdfContent = $this->exportService->bookToPdf($book); $pdfContent = $this->exportFormatter->bookToPdf($book);
return $this->downloadResponse($pdfContent, $book->slug . '.pdf'); return $this->downloadResponse($pdfContent, $book->slug . '.pdf');
} }
@ -38,7 +31,7 @@ class BookExportApiController extends ApiController
public function exportHtml(int $id) public function exportHtml(int $id)
{ {
$book = Book::visible()->findOrFail($id); $book = Book::visible()->findOrFail($id);
$htmlContent = $this->exportService->bookToContainedHtml($book); $htmlContent = $this->exportFormatter->bookToContainedHtml($book);
return $this->downloadResponse($htmlContent, $book->slug . '.html'); return $this->downloadResponse($htmlContent, $book->slug . '.html');
} }
@ -48,7 +41,7 @@ class BookExportApiController extends ApiController
public function exportPlainText(int $id) public function exportPlainText(int $id)
{ {
$book = Book::visible()->findOrFail($id); $book = Book::visible()->findOrFail($id);
$textContent = $this->exportService->bookToPlainText($book); $textContent = $this->exportFormatter->bookToPlainText($book);
return $this->downloadResponse($textContent, $book->slug . '.txt'); return $this->downloadResponse($textContent, $book->slug . '.txt');
} }
} }

View File

@ -1,8 +1,7 @@
<?php namespace BookStack\Http\Controllers\Api; <?php namespace BookStack\Http\Controllers\Api;
use BookStack\Facades\Activity;
use BookStack\Entities\Repos\BookshelfRepo; use BookStack\Entities\Repos\BookshelfRepo;
use BookStack\Entities\Bookshelf; use BookStack\Entities\Models\Bookshelf;
use Exception; use Exception;
use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Http\Request; use Illuminate\Http\Request;
@ -31,7 +30,6 @@ class BookshelfApiController extends ApiController
/** /**
* BookshelfApiController constructor. * BookshelfApiController constructor.
* @param BookshelfRepo $bookshelfRepo
*/ */
public function __construct(BookshelfRepo $bookshelfRepo) public function __construct(BookshelfRepo $bookshelfRepo)
{ {
@ -63,7 +61,6 @@ class BookshelfApiController extends ApiController
$bookIds = $request->get('books', []); $bookIds = $request->get('books', []);
$shelf = $this->bookshelfRepo->create($requestData, $bookIds); $shelf = $this->bookshelfRepo->create($requestData, $bookIds);
Activity::add($shelf, 'bookshelf_create', $shelf->id);
return response()->json($shelf); return response()->json($shelf);
} }
@ -94,19 +91,17 @@ class BookshelfApiController extends ApiController
$this->checkOwnablePermission('bookshelf-update', $shelf); $this->checkOwnablePermission('bookshelf-update', $shelf);
$requestData = $this->validate($request, $this->rules['update']); $requestData = $this->validate($request, $this->rules['update']);
$bookIds = $request->get('books', null); $bookIds = $request->get('books', null);
$shelf = $this->bookshelfRepo->update($shelf, $requestData, $bookIds); $shelf = $this->bookshelfRepo->update($shelf, $requestData, $bookIds);
Activity::add($shelf, 'bookshelf_update', $shelf->id);
return response()->json($shelf); return response()->json($shelf);
} }
/** /**
* Delete a single shelf from the system. * Delete a single shelf.
* This will typically send the shelf to the recycle bin.
* @throws Exception * @throws Exception
*/ */
public function delete(string $id) public function delete(string $id)
@ -115,8 +110,6 @@ class BookshelfApiController extends ApiController
$this->checkOwnablePermission('bookshelf-delete', $shelf); $this->checkOwnablePermission('bookshelf-delete', $shelf);
$this->bookshelfRepo->destroy($shelf); $this->bookshelfRepo->destroy($shelf);
Activity::addMessage('bookshelf_delete', $shelf->name);
return response('', 204); return response('', 204);
} }
} }

View File

@ -1,7 +1,8 @@
<?php namespace BookStack\Http\Controllers\Api; <?php namespace BookStack\Http\Controllers\Api;
use BookStack\Entities\Book; use BookStack\Actions\ActivityType;
use BookStack\Entities\Chapter; use BookStack\Entities\Models\Book;
use BookStack\Entities\Models\Chapter;
use BookStack\Entities\Repos\ChapterRepo; use BookStack\Entities\Repos\ChapterRepo;
use BookStack\Facades\Activity; use BookStack\Facades\Activity;
use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasMany;
@ -58,8 +59,6 @@ class ChapterApiController extends ApiController
$this->checkOwnablePermission('chapter-create', $book); $this->checkOwnablePermission('chapter-create', $book);
$chapter = $this->chapterRepo->create($request->all(), $book); $chapter = $this->chapterRepo->create($request->all(), $book);
Activity::add($chapter, 'chapter_create', $book->id);
return response()->json($chapter->load(['tags'])); return response()->json($chapter->load(['tags']));
} }
@ -83,13 +82,12 @@ class ChapterApiController extends ApiController
$this->checkOwnablePermission('chapter-update', $chapter); $this->checkOwnablePermission('chapter-update', $chapter);
$updatedChapter = $this->chapterRepo->update($chapter, $request->all()); $updatedChapter = $this->chapterRepo->update($chapter, $request->all());
Activity::add($chapter, 'chapter_update', $chapter->book->id);
return response()->json($updatedChapter->load(['tags'])); return response()->json($updatedChapter->load(['tags']));
} }
/** /**
* Delete a chapter from the system. * Delete a chapter.
* This will typically send the chapter to the recycle bin.
*/ */
public function delete(string $id) public function delete(string $id)
{ {
@ -97,8 +95,6 @@ class ChapterApiController extends ApiController
$this->checkOwnablePermission('chapter-delete', $chapter); $this->checkOwnablePermission('chapter-delete', $chapter);
$this->chapterRepo->destroy($chapter); $this->chapterRepo->destroy($chapter);
Activity::addMessage('chapter_delete', $chapter->name, $chapter->book->id);
return response('', 204); return response('', 204);
} }
} }

View File

@ -1,23 +1,20 @@
<?php namespace BookStack\Http\Controllers\Api; <?php namespace BookStack\Http\Controllers\Api;
use BookStack\Entities\Chapter; use BookStack\Entities\Models\Chapter;
use BookStack\Entities\ExportService; use BookStack\Entities\Tools\ExportFormatter;
use BookStack\Entities\Repos\BookRepo; use BookStack\Entities\Repos\BookRepo;
use Throwable; use Throwable;
class ChapterExportApiController extends ApiController class ChapterExportApiController extends ApiController
{ {
protected $chapterRepo; protected $exportFormatter;
protected $exportService;
/** /**
* ChapterExportController constructor. * ChapterExportController constructor.
*/ */
public function __construct(BookRepo $chapterRepo, ExportService $exportService) public function __construct(ExportFormatter $exportFormatter)
{ {
$this->chapterRepo = $chapterRepo; $this->exportFormatter = $exportFormatter;
$this->exportService = $exportService;
parent::__construct();
} }
/** /**
@ -27,7 +24,7 @@ class ChapterExportApiController extends ApiController
public function exportPdf(int $id) public function exportPdf(int $id)
{ {
$chapter = Chapter::visible()->findOrFail($id); $chapter = Chapter::visible()->findOrFail($id);
$pdfContent = $this->exportService->chapterToPdf($chapter); $pdfContent = $this->exportFormatter->chapterToPdf($chapter);
return $this->downloadResponse($pdfContent, $chapter->slug . '.pdf'); return $this->downloadResponse($pdfContent, $chapter->slug . '.pdf');
} }
@ -38,7 +35,7 @@ class ChapterExportApiController extends ApiController
public function exportHtml(int $id) public function exportHtml(int $id)
{ {
$chapter = Chapter::visible()->findOrFail($id); $chapter = Chapter::visible()->findOrFail($id);
$htmlContent = $this->exportService->chapterToContainedHtml($chapter); $htmlContent = $this->exportFormatter->chapterToContainedHtml($chapter);
return $this->downloadResponse($htmlContent, $chapter->slug . '.html'); return $this->downloadResponse($htmlContent, $chapter->slug . '.html');
} }
@ -48,7 +45,7 @@ class ChapterExportApiController extends ApiController
public function exportPlainText(int $id) public function exportPlainText(int $id)
{ {
$chapter = Chapter::visible()->findOrFail($id); $chapter = Chapter::visible()->findOrFail($id);
$textContent = $this->exportService->chapterToPlainText($chapter); $textContent = $this->exportFormatter->chapterToPlainText($chapter);
return $this->downloadResponse($textContent, $chapter->slug . '.txt'); return $this->downloadResponse($textContent, $chapter->slug . '.txt');
} }
} }

View File

@ -0,0 +1,140 @@
<?php
namespace BookStack\Http\Controllers\Api;
use BookStack\Entities\Models\Book;
use BookStack\Entities\Models\Chapter;
use BookStack\Entities\Models\Page;
use BookStack\Entities\Repos\PageRepo;
use BookStack\Exceptions\PermissionsException;
use Exception;
use Illuminate\Http\Request;
class PageApiController extends ApiController
{
protected $pageRepo;
protected $rules = [
'create' => [
'book_id' => 'required_without:chapter_id|integer',
'chapter_id' => 'required_without:book_id|integer',
'name' => 'required|string|max:255',
'html' => 'required_without:markdown|string',
'markdown' => 'required_without:html|string',
'tags' => 'array',
],
'update' => [
'book_id' => 'required|integer',
'chapter_id' => 'required|integer',
'name' => 'string|min:1|max:255',
'html' => 'string',
'markdown' => 'string',
'tags' => 'array',
],
];
public function __construct(PageRepo $pageRepo)
{
$this->pageRepo = $pageRepo;
}
/**
* Get a listing of pages visible to the user.
*/
public function list()
{
$pages = Page::visible();
return $this->apiListingResponse($pages, [
'id', 'book_id', 'chapter_id', 'name', 'slug', 'priority',
'draft', 'template',
'created_at', 'updated_at', 'created_by', 'updated_by',
]);
}
/**
* Create a new page in the system.
*
* The ID of a parent book or chapter is required to indicate
* where this page should be located.
*
* Any HTML content provided should be kept to a single-block depth of plain HTML
* elements to remain compatible with the BookStack front-end and editors.
*/
public function create(Request $request)
{
$this->validate($request, $this->rules['create']);
if ($request->has('chapter_id')) {
$parent = Chapter::visible()->findOrFail($request->get('chapter_id'));
} else {
$parent = Book::visible()->findOrFail($request->get('book_id'));
}
$this->checkOwnablePermission('page-create', $parent);
$draft = $this->pageRepo->getNewDraftPage($parent);
$this->pageRepo->publishDraft($draft, $request->only(array_keys($this->rules['create'])));
return response()->json($draft->forJsonDisplay());
}
/**
* View the details of a single page.
*
* Pages will always have HTML content. They may have markdown content
* if the markdown editor was used to last update the page.
*/
public function read(string $id)
{
$page = $this->pageRepo->getById($id, []);
return response()->json($page->forJsonDisplay());
}
/**
* Update the details of a single page.
*
* See the 'create' action for details on the provided HTML/Markdown.
* Providing a 'book_id' or 'chapter_id' property will essentially move
* the page into that parent element if you have permissions to do so.
*/
public function update(Request $request, string $id)
{
$page = $this->pageRepo->getById($id, []);
$this->checkOwnablePermission('page-update', $page);
$parent = null;
if ($request->has('chapter_id')) {
$parent = Chapter::visible()->findOrFail($request->get('chapter_id'));
} else if ($request->has('book_id')) {
$parent = Book::visible()->findOrFail($request->get('book_id'));
}
if ($parent && !$parent->matches($page->getParent())) {
$this->checkOwnablePermission('page-delete', $page);
try {
$this->pageRepo->move($page, $parent->getType() . ':' . $parent->id);
} catch (Exception $exception) {
if ($exception instanceof PermissionsException) {
$this->showPermissionError();
}
return $this->jsonError(trans('errors.selected_book_chapter_not_found'));
}
}
$updatedPage = $this->pageRepo->update($page, $request->all());
return response()->json($updatedPage->forJsonDisplay());
}
/**
* Delete a page.
* This will typically send the page to the recycle bin.
*/
public function delete(string $id)
{
$page = $this->pageRepo->getById($id, []);
$this->checkOwnablePermission('page-delete', $page);
$this->pageRepo->destroy($page);
return response('', 204);
}
}

View File

@ -0,0 +1,47 @@
<?php namespace BookStack\Http\Controllers\Api;
use BookStack\Entities\Models\Page;
use BookStack\Entities\Tools\ExportFormatter;
use Throwable;
class PageExportApiController extends ApiController
{
protected $exportFormatter;
public function __construct(ExportFormatter $exportFormatter)
{
$this->exportFormatter = $exportFormatter;
}
/**
* Export a page as a PDF file.
* @throws Throwable
*/
public function exportPdf(int $id)
{
$page = Page::visible()->findOrFail($id);
$pdfContent = $this->exportFormatter->pageToPdf($page);
return $this->downloadResponse($pdfContent, $page->slug . '.pdf');
}
/**
* Export a page as a contained HTML file.
* @throws Throwable
*/
public function exportHtml(int $id)
{
$page = Page::visible()->findOrFail($id);
$htmlContent = $this->exportFormatter->pageToContainedHtml($page);
return $this->downloadResponse($htmlContent, $page->slug . '.html');
}
/**
* Export a page as a plain text file.
*/
public function exportPlainText(int $id)
{
$page = Page::visible()->findOrFail($id);
$textContent = $this->exportFormatter->pageToPlainText($page);
return $this->downloadResponse($textContent, $page->slug . '.txt');
}
}

View File

@ -25,7 +25,6 @@ class AttachmentController extends Controller
$this->attachmentService = $attachmentService; $this->attachmentService = $attachmentService;
$this->attachment = $attachment; $this->attachment = $attachment;
$this->pageRepo = $pageRepo; $this->pageRepo = $pageRepo;
parent::__construct();
} }

View File

@ -23,11 +23,16 @@ class AuditLogController extends Controller
]; ];
$query = Activity::query() $query = Activity::query()
->with(['entity', 'user']) ->with([
'entity' => function ($query) {
$query->withTrashed();
},
'user'
])
->orderBy($listDetails['sort'], $listDetails['order']); ->orderBy($listDetails['sort'], $listDetails['order']);
if ($listDetails['event']) { if ($listDetails['event']) {
$query->where('key', '=', $listDetails['event']); $query->where('type', '=', $listDetails['event']);
} }
if ($listDetails['date_from']) { if ($listDetails['date_from']) {
@ -40,12 +45,12 @@ class AuditLogController extends Controller
$activities = $query->paginate(100); $activities = $query->paginate(100);
$activities->appends($listDetails); $activities->appends($listDetails);
$keys = DB::table('activities')->select('key')->distinct()->pluck('key'); $types = DB::table('activities')->select('type')->distinct()->pluck('type');
$this->setPageTitle(trans('settings.audit')); $this->setPageTitle(trans('settings.audit'));
return view('settings.audit', [ return view('settings.audit', [
'activities' => $activities, 'activities' => $activities,
'listDetails' => $listDetails, 'listDetails' => $listDetails,
'activityKeys' => $keys, 'activityTypes' => $types,
]); ]);
} }
} }

View File

@ -21,15 +21,11 @@ class ConfirmEmailController extends Controller
/** /**
* Create a new controller instance. * Create a new controller instance.
*
* @param EmailConfirmationService $emailConfirmationService
* @param UserRepo $userRepo
*/ */
public function __construct(EmailConfirmationService $emailConfirmationService, UserRepo $userRepo) public function __construct(EmailConfirmationService $emailConfirmationService, UserRepo $userRepo)
{ {
$this->emailConfirmationService = $emailConfirmationService; $this->emailConfirmationService = $emailConfirmationService;
$this->userRepo = $userRepo; $this->userRepo = $userRepo;
parent::__construct();
} }

View File

@ -2,6 +2,7 @@
namespace BookStack\Http\Controllers\Auth; namespace BookStack\Http\Controllers\Auth;
use BookStack\Actions\ActivityType;
use BookStack\Http\Controllers\Controller; use BookStack\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\SendsPasswordResetEmails; use Illuminate\Foundation\Auth\SendsPasswordResetEmails;
use Illuminate\Http\Request; use Illuminate\Http\Request;
@ -31,7 +32,6 @@ class ForgotPasswordController extends Controller
{ {
$this->middleware('guest'); $this->middleware('guest');
$this->middleware('guard:standard'); $this->middleware('guard:standard');
parent::__construct();
} }
@ -52,6 +52,10 @@ class ForgotPasswordController extends Controller
$request->only('email') $request->only('email')
); );
if ($response === Password::RESET_LINK_SENT) {
$this->logActivity(ActivityType::AUTH_PASSWORD_RESET, $request->get('email'));
}
if ($response === Password::RESET_LINK_SENT || $response === Password::INVALID_USER) { if ($response === Password::RESET_LINK_SENT || $response === Password::INVALID_USER) {
$message = trans('auth.reset_password_sent', ['email' => $request->get('email')]); $message = trans('auth.reset_password_sent', ['email' => $request->get('email')]);
$this->showSuccessNotification($message); $this->showSuccessNotification($message);

View File

@ -3,10 +3,10 @@
namespace BookStack\Http\Controllers\Auth; namespace BookStack\Http\Controllers\Auth;
use Activity; use Activity;
use BookStack\Actions\ActivityType;
use BookStack\Auth\Access\SocialAuthService; use BookStack\Auth\Access\SocialAuthService;
use BookStack\Exceptions\LoginAttemptEmailNeededException; use BookStack\Exceptions\LoginAttemptEmailNeededException;
use BookStack\Exceptions\LoginAttemptException; use BookStack\Exceptions\LoginAttemptException;
use BookStack\Exceptions\UserRegistrationException;
use BookStack\Http\Controllers\Controller; use BookStack\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers; use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request; use Illuminate\Http\Request;
@ -46,7 +46,6 @@ class LoginController extends Controller
$this->socialAuthService = $socialAuthService; $this->socialAuthService = $socialAuthService;
$this->redirectPath = url('/'); $this->redirectPath = url('/');
$this->redirectAfterLogout = url('/login'); $this->redirectAfterLogout = url('/login');
parent::__construct();
} }
public function username() public function username()
@ -151,6 +150,7 @@ class LoginController extends Controller
} }
} }
$this->logActivity(ActivityType::AUTH_LOGIN, $user);
return redirect()->intended($this->redirectPath()); return redirect()->intended($this->redirectPath());
} }

View File

@ -51,7 +51,6 @@ class RegisterController extends Controller
$this->redirectTo = url('/'); $this->redirectTo = url('/');
$this->redirectPath = url('/'); $this->redirectPath = url('/');
parent::__construct();
} }
/** /**

View File

@ -2,6 +2,7 @@
namespace BookStack\Http\Controllers\Auth; namespace BookStack\Http\Controllers\Auth;
use BookStack\Actions\ActivityType;
use BookStack\Http\Controllers\Controller; use BookStack\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ResetsPasswords; use Illuminate\Foundation\Auth\ResetsPasswords;
use Illuminate\Http\Request; use Illuminate\Http\Request;
@ -33,7 +34,6 @@ class ResetPasswordController extends Controller
{ {
$this->middleware('guest'); $this->middleware('guest');
$this->middleware('guard:standard'); $this->middleware('guard:standard');
parent::__construct();
} }
/** /**
@ -47,6 +47,7 @@ class ResetPasswordController extends Controller
{ {
$message = trans('auth.reset_password_success'); $message = trans('auth.reset_password_success');
$this->showSuccessNotification($message); $this->showSuccessNotification($message);
$this->logActivity(ActivityType::AUTH_PASSWORD_RESET_UPDATE, user());
return redirect($this->redirectPath()) return redirect($this->redirectPath())
->with('status', trans($response)); ->with('status', trans($response));
} }

View File

@ -15,7 +15,6 @@ class Saml2Controller extends Controller
*/ */
public function __construct(Saml2Service $samlService) public function __construct(Saml2Service $samlService)
{ {
parent::__construct();
$this->samlService = $samlService; $this->samlService = $samlService;
$this->middleware('guard:saml2'); $this->middleware('guard:saml2');
} }

View File

@ -27,8 +27,6 @@ class UserInviteController extends Controller
$this->inviteService = $inviteService; $this->inviteService = $inviteService;
$this->userRepo = $userRepo; $this->userRepo = $userRepo;
parent::__construct();
} }
/** /**

View File

@ -1,12 +1,13 @@
<?php namespace BookStack\Http\Controllers; <?php namespace BookStack\Http\Controllers;
use Activity; use Activity;
use BookStack\Entities\Managers\BookContents; use BookStack\Actions\ActivityType;
use BookStack\Entities\Bookshelf; use BookStack\Entities\Tools\BookContents;
use BookStack\Entities\Managers\EntityContext; use BookStack\Entities\Models\Bookshelf;
use BookStack\Entities\Tools\PermissionsUpdater;
use BookStack\Entities\Tools\ShelfContext;
use BookStack\Entities\Repos\BookRepo; use BookStack\Entities\Repos\BookRepo;
use BookStack\Exceptions\ImageUploadException; use BookStack\Exceptions\ImageUploadException;
use BookStack\Exceptions\NotifyException;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException; use Illuminate\Validation\ValidationException;
use Throwable; use Throwable;
@ -18,14 +19,10 @@ class BookController extends Controller
protected $bookRepo; protected $bookRepo;
protected $entityContextManager; protected $entityContextManager;
/** public function __construct(ShelfContext $entityContextManager, BookRepo $bookRepo)
* BookController constructor.
*/
public function __construct(EntityContext $entityContextManager, BookRepo $bookRepo)
{ {
$this->bookRepo = $bookRepo; $this->bookRepo = $bookRepo;
$this->entityContextManager = $entityContextManager; $this->entityContextManager = $entityContextManager;
parent::__construct();
} }
/** /**
@ -97,11 +94,10 @@ class BookController extends Controller
$book = $this->bookRepo->create($request->all()); $book = $this->bookRepo->create($request->all());
$this->bookRepo->updateCoverImage($book, $request->file('image', null)); $this->bookRepo->updateCoverImage($book, $request->file('image', null));
Activity::add($book, 'book_create', $book->id);
if ($bookshelf) { if ($bookshelf) {
$bookshelf->appendBook($book); $bookshelf->appendBook($book);
Activity::add($bookshelf, 'bookshelf_update'); Activity::addForEntity($bookshelf, ActivityType::BOOKSHELF_UPDATE);
} }
return redirect($book->getUrl()); return redirect($book->getUrl());
@ -162,8 +158,6 @@ class BookController extends Controller
$resetCover = $request->has('image_reset'); $resetCover = $request->has('image_reset');
$this->bookRepo->updateCoverImage($book, $request->file('image', null), $resetCover); $this->bookRepo->updateCoverImage($book, $request->file('image', null), $resetCover);
Activity::add($book, 'book_update', $book->id);
return redirect($book->getUrl()); return redirect($book->getUrl());
} }
@ -181,14 +175,12 @@ class BookController extends Controller
/** /**
* Remove the specified book from the system. * Remove the specified book from the system.
* @throws Throwable * @throws Throwable
* @throws NotifyException
*/ */
public function destroy(string $bookSlug) public function destroy(string $bookSlug)
{ {
$book = $this->bookRepo->getBySlug($bookSlug); $book = $this->bookRepo->getBySlug($bookSlug);
$this->checkOwnablePermission('book-delete', $book); $this->checkOwnablePermission('book-delete', $book);
Activity::addMessage('book_delete', $book->name);
$this->bookRepo->destroy($book); $this->bookRepo->destroy($book);
return redirect('/books'); return redirect('/books');
@ -211,14 +203,12 @@ class BookController extends Controller
* Set the restrictions for this book. * Set the restrictions for this book.
* @throws Throwable * @throws Throwable
*/ */
public function permissions(Request $request, string $bookSlug) public function permissions(Request $request, PermissionsUpdater $permissionsUpdater, string $bookSlug)
{ {
$book = $this->bookRepo->getBySlug($bookSlug); $book = $this->bookRepo->getBySlug($bookSlug);
$this->checkOwnablePermission('restrictions-manage', $book); $this->checkOwnablePermission('restrictions-manage', $book);
$restricted = $request->get('restricted') === 'true'; $permissionsUpdater->updateFromPermissionsForm($book, $request);
$permissions = $request->filled('restrictions') ? collect($request->get('restrictions')) : null;
$this->bookRepo->updatePermissions($book, $restricted, $permissions);
$this->showSuccessNotification(trans('entities.books_permissions_updated')); $this->showSuccessNotification(trans('entities.books_permissions_updated'));
return redirect($book->getUrl()); return redirect($book->getUrl());

View File

@ -2,7 +2,7 @@
namespace BookStack\Http\Controllers; namespace BookStack\Http\Controllers;
use BookStack\Entities\ExportService; use BookStack\Entities\Tools\ExportFormatter;
use BookStack\Entities\Repos\BookRepo; use BookStack\Entities\Repos\BookRepo;
use Throwable; use Throwable;
@ -10,16 +10,15 @@ class BookExportController extends Controller
{ {
protected $bookRepo; protected $bookRepo;
protected $exportService; protected $exportFormatter;
/** /**
* BookExportController constructor. * BookExportController constructor.
*/ */
public function __construct(BookRepo $bookRepo, ExportService $exportService) public function __construct(BookRepo $bookRepo, ExportFormatter $exportFormatter)
{ {
$this->bookRepo = $bookRepo; $this->bookRepo = $bookRepo;
$this->exportService = $exportService; $this->exportFormatter = $exportFormatter;
parent::__construct();
} }
/** /**
@ -29,7 +28,7 @@ class BookExportController extends Controller
public function pdf(string $bookSlug) public function pdf(string $bookSlug)
{ {
$book = $this->bookRepo->getBySlug($bookSlug); $book = $this->bookRepo->getBySlug($bookSlug);
$pdfContent = $this->exportService->bookToPdf($book); $pdfContent = $this->exportFormatter->bookToPdf($book);
return $this->downloadResponse($pdfContent, $bookSlug . '.pdf'); return $this->downloadResponse($pdfContent, $bookSlug . '.pdf');
} }
@ -40,7 +39,7 @@ class BookExportController extends Controller
public function html(string $bookSlug) public function html(string $bookSlug)
{ {
$book = $this->bookRepo->getBySlug($bookSlug); $book = $this->bookRepo->getBySlug($bookSlug);
$htmlContent = $this->exportService->bookToContainedHtml($book); $htmlContent = $this->exportFormatter->bookToContainedHtml($book);
return $this->downloadResponse($htmlContent, $bookSlug . '.html'); return $this->downloadResponse($htmlContent, $bookSlug . '.html');
} }
@ -50,7 +49,7 @@ class BookExportController extends Controller
public function plainText(string $bookSlug) public function plainText(string $bookSlug)
{ {
$book = $this->bookRepo->getBySlug($bookSlug); $book = $this->bookRepo->getBySlug($bookSlug);
$textContent = $this->exportService->bookToPlainText($book); $textContent = $this->exportFormatter->bookToPlainText($book);
return $this->downloadResponse($textContent, $bookSlug . '.txt'); return $this->downloadResponse($textContent, $bookSlug . '.txt');
} }
} }

View File

@ -2,8 +2,9 @@
namespace BookStack\Http\Controllers; namespace BookStack\Http\Controllers;
use BookStack\Entities\Book; use BookStack\Actions\ActivityType;
use BookStack\Entities\Managers\BookContents; use BookStack\Entities\Models\Book;
use BookStack\Entities\Tools\BookContents;
use BookStack\Entities\Repos\BookRepo; use BookStack\Entities\Repos\BookRepo;
use BookStack\Exceptions\SortOperationException; use BookStack\Exceptions\SortOperationException;
use BookStack\Facades\Activity; use BookStack\Facades\Activity;
@ -14,14 +15,9 @@ class BookSortController extends Controller
protected $bookRepo; protected $bookRepo;
/**
* BookSortController constructor.
* @param $bookRepo
*/
public function __construct(BookRepo $bookRepo) public function __construct(BookRepo $bookRepo)
{ {
$this->bookRepo = $bookRepo; $this->bookRepo = $bookRepo;
parent::__construct();
} }
/** /**
@ -74,7 +70,7 @@ class BookSortController extends Controller
// Rebuild permissions and add activity for involved books. // Rebuild permissions and add activity for involved books.
$booksInvolved->each(function (Book $book) { $booksInvolved->each(function (Book $book) {
Activity::add($book, 'book_sort', $book->id); Activity::addForEntity($book, ActivityType::BOOK_SORT);
}); });
return redirect($book->getUrl()); return redirect($book->getUrl());

View File

@ -1,8 +1,9 @@
<?php namespace BookStack\Http\Controllers; <?php namespace BookStack\Http\Controllers;
use Activity; use Activity;
use BookStack\Entities\Book; use BookStack\Entities\Models\Book;
use BookStack\Entities\Managers\EntityContext; use BookStack\Entities\Tools\PermissionsUpdater;
use BookStack\Entities\Tools\ShelfContext;
use BookStack\Entities\Repos\BookshelfRepo; use BookStack\Entities\Repos\BookshelfRepo;
use BookStack\Exceptions\ImageUploadException; use BookStack\Exceptions\ImageUploadException;
use BookStack\Exceptions\NotFoundException; use BookStack\Exceptions\NotFoundException;
@ -19,15 +20,11 @@ class BookshelfController extends Controller
protected $entityContextManager; protected $entityContextManager;
protected $imageRepo; protected $imageRepo;
/** public function __construct(BookshelfRepo $bookshelfRepo, ShelfContext $entityContextManager, ImageRepo $imageRepo)
* BookController constructor.
*/
public function __construct(BookshelfRepo $bookshelfRepo, EntityContext $entityContextManager, ImageRepo $imageRepo)
{ {
$this->bookshelfRepo = $bookshelfRepo; $this->bookshelfRepo = $bookshelfRepo;
$this->entityContextManager = $entityContextManager; $this->entityContextManager = $entityContextManager;
$this->imageRepo = $imageRepo; $this->imageRepo = $imageRepo;
parent::__construct();
} }
/** /**
@ -92,7 +89,6 @@ class BookshelfController extends Controller
$shelf = $this->bookshelfRepo->create($request->all(), $bookIds); $shelf = $this->bookshelfRepo->create($request->all(), $bookIds);
$this->bookshelfRepo->updateCoverImage($shelf, $request->file('image', null)); $this->bookshelfRepo->updateCoverImage($shelf, $request->file('image', null));
Activity::add($shelf, 'bookshelf_create');
return redirect($shelf->getUrl()); return redirect($shelf->getUrl());
} }
@ -156,7 +152,6 @@ class BookshelfController extends Controller
$shelf = $this->bookshelfRepo->update($shelf, $request->all(), $bookIds); $shelf = $this->bookshelfRepo->update($shelf, $request->all(), $bookIds);
$resetCover = $request->has('image_reset'); $resetCover = $request->has('image_reset');
$this->bookshelfRepo->updateCoverImage($shelf, $request->file('image', null), $resetCover); $this->bookshelfRepo->updateCoverImage($shelf, $request->file('image', null), $resetCover);
Activity::add($shelf, 'bookshelf_update');
return redirect($shelf->getUrl()); return redirect($shelf->getUrl());
} }
@ -182,7 +177,6 @@ class BookshelfController extends Controller
$shelf = $this->bookshelfRepo->getBySlug($slug); $shelf = $this->bookshelfRepo->getBySlug($slug);
$this->checkOwnablePermission('bookshelf-delete', $shelf); $this->checkOwnablePermission('bookshelf-delete', $shelf);
Activity::addMessage('bookshelf_delete', $shelf->name);
$this->bookshelfRepo->destroy($shelf); $this->bookshelfRepo->destroy($shelf);
return redirect('/shelves'); return redirect('/shelves');
@ -204,14 +198,12 @@ class BookshelfController extends Controller
/** /**
* Set the permissions for this bookshelf. * Set the permissions for this bookshelf.
*/ */
public function permissions(Request $request, string $slug) public function permissions(Request $request, PermissionsUpdater $permissionsUpdater, string $slug)
{ {
$shelf = $this->bookshelfRepo->getBySlug($slug); $shelf = $this->bookshelfRepo->getBySlug($slug);
$this->checkOwnablePermission('restrictions-manage', $shelf); $this->checkOwnablePermission('restrictions-manage', $shelf);
$restricted = $request->get('restricted') === 'true'; $permissionsUpdater->updateFromPermissionsForm($shelf, $request);
$permissions = $request->filled('restrictions') ? collect($request->get('restrictions')) : null;
$this->bookshelfRepo->updatePermissions($shelf, $restricted, $permissions);
$this->showSuccessNotification(trans('entities.shelves_permissions_updated')); $this->showSuccessNotification(trans('entities.shelves_permissions_updated'));
return redirect($shelf->getUrl()); return redirect($shelf->getUrl());

View File

@ -1,9 +1,9 @@
<?php namespace BookStack\Http\Controllers; <?php namespace BookStack\Http\Controllers;
use Activity; use BookStack\Entities\Models\Book;
use BookStack\Entities\Book; use BookStack\Entities\Tools\BookContents;
use BookStack\Entities\Managers\BookContents;
use BookStack\Entities\Repos\ChapterRepo; use BookStack\Entities\Repos\ChapterRepo;
use BookStack\Entities\Tools\PermissionsUpdater;
use BookStack\Exceptions\MoveOperationException; use BookStack\Exceptions\MoveOperationException;
use BookStack\Exceptions\NotFoundException; use BookStack\Exceptions\NotFoundException;
use Illuminate\Http\Request; use Illuminate\Http\Request;
@ -22,7 +22,6 @@ class ChapterController extends Controller
public function __construct(ChapterRepo $chapterRepo) public function __construct(ChapterRepo $chapterRepo)
{ {
$this->chapterRepo = $chapterRepo; $this->chapterRepo = $chapterRepo;
parent::__construct();
} }
/** /**
@ -51,7 +50,6 @@ class ChapterController extends Controller
$this->checkOwnablePermission('chapter-create', $book); $this->checkOwnablePermission('chapter-create', $book);
$chapter = $this->chapterRepo->create($request->all(), $book); $chapter = $this->chapterRepo->create($request->all(), $book);
Activity::add($chapter, 'chapter_create', $book->id);
return redirect($chapter->getUrl()); return redirect($chapter->getUrl());
} }
@ -100,7 +98,6 @@ class ChapterController extends Controller
$this->checkOwnablePermission('chapter-update', $chapter); $this->checkOwnablePermission('chapter-update', $chapter);
$this->chapterRepo->update($chapter, $request->all()); $this->chapterRepo->update($chapter, $request->all());
Activity::add($chapter, 'chapter_update', $chapter->book->id);
return redirect($chapter->getUrl()); return redirect($chapter->getUrl());
} }
@ -128,7 +125,6 @@ class ChapterController extends Controller
$chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug); $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
$this->checkOwnablePermission('chapter-delete', $chapter); $this->checkOwnablePermission('chapter-delete', $chapter);
Activity::addMessage('chapter_delete', $chapter->name, $chapter->book->id);
$this->chapterRepo->destroy($chapter); $this->chapterRepo->destroy($chapter);
return redirect($chapter->book->getUrl()); return redirect($chapter->book->getUrl());
@ -173,8 +169,6 @@ class ChapterController extends Controller
return redirect()->back(); return redirect()->back();
} }
Activity::add($chapter, 'chapter_move', $newBook->id);
$this->showSuccessNotification(trans('entities.chapter_move_success', ['bookName' => $newBook->name])); $this->showSuccessNotification(trans('entities.chapter_move_success', ['bookName' => $newBook->name]));
return redirect($chapter->getUrl()); return redirect($chapter->getUrl());
} }
@ -197,14 +191,12 @@ class ChapterController extends Controller
* Set the restrictions for this chapter. * Set the restrictions for this chapter.
* @throws NotFoundException * @throws NotFoundException
*/ */
public function permissions(Request $request, string $bookSlug, string $chapterSlug) public function permissions(Request $request, PermissionsUpdater $permissionsUpdater, string $bookSlug, string $chapterSlug)
{ {
$chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug); $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
$this->checkOwnablePermission('restrictions-manage', $chapter); $this->checkOwnablePermission('restrictions-manage', $chapter);
$restricted = $request->get('restricted') === 'true'; $permissionsUpdater->updateFromPermissionsForm($chapter, $request);
$permissions = $request->filled('restrictions') ? collect($request->get('restrictions')) : null;
$this->chapterRepo->updatePermissions($chapter, $restricted, $permissions);
$this->showSuccessNotification(trans('entities.chapters_permissions_success')); $this->showSuccessNotification(trans('entities.chapters_permissions_success'));
return redirect($chapter->getUrl()); return redirect($chapter->getUrl());

View File

@ -1,6 +1,6 @@
<?php namespace BookStack\Http\Controllers; <?php namespace BookStack\Http\Controllers;
use BookStack\Entities\ExportService; use BookStack\Entities\Tools\ExportFormatter;
use BookStack\Entities\Repos\ChapterRepo; use BookStack\Entities\Repos\ChapterRepo;
use BookStack\Exceptions\NotFoundException; use BookStack\Exceptions\NotFoundException;
use Throwable; use Throwable;
@ -9,16 +9,15 @@ class ChapterExportController extends Controller
{ {
protected $chapterRepo; protected $chapterRepo;
protected $exportService; protected $exportFormatter;
/** /**
* ChapterExportController constructor. * ChapterExportController constructor.
*/ */
public function __construct(ChapterRepo $chapterRepo, ExportService $exportService) public function __construct(ChapterRepo $chapterRepo, ExportFormatter $exportFormatter)
{ {
$this->chapterRepo = $chapterRepo; $this->chapterRepo = $chapterRepo;
$this->exportService = $exportService; $this->exportFormatter = $exportFormatter;
parent::__construct();
} }
/** /**
@ -29,7 +28,7 @@ class ChapterExportController extends Controller
public function pdf(string $bookSlug, string $chapterSlug) public function pdf(string $bookSlug, string $chapterSlug)
{ {
$chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug); $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
$pdfContent = $this->exportService->chapterToPdf($chapter); $pdfContent = $this->exportFormatter->chapterToPdf($chapter);
return $this->downloadResponse($pdfContent, $chapterSlug . '.pdf'); return $this->downloadResponse($pdfContent, $chapterSlug . '.pdf');
} }
@ -41,7 +40,7 @@ class ChapterExportController extends Controller
public function html(string $bookSlug, string $chapterSlug) public function html(string $bookSlug, string $chapterSlug)
{ {
$chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug); $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
$containedHtml = $this->exportService->chapterToContainedHtml($chapter); $containedHtml = $this->exportFormatter->chapterToContainedHtml($chapter);
return $this->downloadResponse($containedHtml, $chapterSlug . '.html'); return $this->downloadResponse($containedHtml, $chapterSlug . '.html');
} }
@ -52,7 +51,7 @@ class ChapterExportController extends Controller
public function plainText(string $bookSlug, string $chapterSlug) public function plainText(string $bookSlug, string $chapterSlug)
{ {
$chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug); $chapter = $this->chapterRepo->getBySlug($bookSlug, $chapterSlug);
$chapterText = $this->exportService->chapterToPlainText($chapter); $chapterText = $this->exportFormatter->chapterToPlainText($chapter);
return $this->downloadResponse($chapterText, $chapterSlug . '.txt'); return $this->downloadResponse($chapterText, $chapterSlug . '.txt');
} }
} }

View File

@ -1,8 +1,9 @@
<?php namespace BookStack\Http\Controllers; <?php namespace BookStack\Http\Controllers;
use Activity; use Activity;
use BookStack\Actions\ActivityType;
use BookStack\Actions\CommentRepo; use BookStack\Actions\CommentRepo;
use BookStack\Entities\Page; use BookStack\Entities\Models\Page;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException; use Illuminate\Validation\ValidationException;
@ -13,7 +14,6 @@ class CommentController extends Controller
public function __construct(CommentRepo $commentRepo) public function __construct(CommentRepo $commentRepo)
{ {
$this->commentRepo = $commentRepo; $this->commentRepo = $commentRepo;
parent::__construct();
} }
/** /**
@ -40,7 +40,6 @@ class CommentController extends Controller
// Create a new comment. // Create a new comment.
$this->checkPermission('comment-create-all'); $this->checkPermission('comment-create-all');
$comment = $this->commentRepo->create($page, $request->get('text'), $request->get('parent_id')); $comment = $this->commentRepo->create($page, $request->get('text'), $request->get('parent_id'));
Activity::add($page, 'commented_on', $page->book->id);
return view('comments.comment', ['comment' => $comment]); return view('comments.comment', ['comment' => $comment]);
} }

View File

@ -2,26 +2,21 @@
namespace BookStack\Http\Controllers; namespace BookStack\Http\Controllers;
use BookStack\Ownable; use BookStack\Facades\Activity;
use BookStack\Interfaces\Loggable;
use BookStack\HasCreatorAndUpdater;
use BookStack\Model;
use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests; use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\Exceptions\HttpResponseException; use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Http\Request; use Illuminate\Http\JsonResponse;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller as BaseController; use Illuminate\Routing\Controller as BaseController;
use Illuminate\Validation\ValidationException;
abstract class Controller extends BaseController abstract class Controller extends BaseController
{ {
use DispatchesJobs, ValidatesRequests; use DispatchesJobs, ValidatesRequests;
/**
* Controller constructor.
*/
public function __construct()
{
//
}
/** /**
* Check if the current user is signed in. * Check if the current user is signed in.
*/ */
@ -43,9 +38,8 @@ abstract class Controller extends BaseController
/** /**
* Adds the page title into the view. * Adds the page title into the view.
* @param $title
*/ */
public function setPageTitle($title) public function setPageTitle(string $title)
{ {
view()->share('pageTitle', $title); view()->share('pageTitle', $title);
} }
@ -67,79 +61,59 @@ abstract class Controller extends BaseController
} }
/** /**
* Checks for a permission. * Checks that the current user has the given permission otherwise throw an exception.
* @param string $permissionName
* @return bool|\Illuminate\Http\RedirectResponse
*/ */
protected function checkPermission($permissionName) protected function checkPermission(string $permission): void
{ {
if (!user() || !user()->can($permissionName)) { if (!user() || !user()->can($permission)) {
$this->showPermissionError(); $this->showPermissionError();
} }
return true;
} }
/** /**
* Check the current user's permissions against an ownable item. * Check the current user's permissions against an ownable item otherwise throw an exception.
* @param $permission
* @param Ownable $ownable
* @return bool
*/ */
protected function checkOwnablePermission($permission, Ownable $ownable) protected function checkOwnablePermission(string $permission, Model $ownable): void
{ {
if (userCan($permission, $ownable)) { if (!userCan($permission, $ownable)) {
return true; $this->showPermissionError();
} }
return $this->showPermissionError();
} }
/** /**
* Check if a user has a permission or bypass if the callback is true. * Check if a user has a permission or bypass the permission
* @param $permissionName * check if the given callback resolves true.
* @param $callback
* @return bool
*/ */
protected function checkPermissionOr($permissionName, $callback) protected function checkPermissionOr(string $permission, callable $callback): void
{ {
$callbackResult = $callback(); if ($callback() !== true) {
if ($callbackResult === false) { $this->checkPermission($permission);
$this->checkPermission($permissionName);
} }
return true;
} }
/** /**
* Check if the current user has a permission or bypass if the provided user * Check if the current user has a permission or bypass if the provided user
* id matches the current user. * id matches the current user.
* @param string $permissionName
* @param int $userId
* @return bool
*/ */
protected function checkPermissionOrCurrentUser(string $permissionName, int $userId) protected function checkPermissionOrCurrentUser(string $permission, int $userId): void
{ {
return $this->checkPermissionOr($permissionName, function () use ($userId) { $this->checkPermissionOr($permission, function () use ($userId) {
return $userId === user()->id; return $userId === user()->id;
}); });
} }
/** /**
* Send back a json error message. * Send back a json error message.
* @param string $messageText
* @param int $statusCode
* @return mixed
*/ */
protected function jsonError($messageText = "", $statusCode = 500) protected function jsonError(string $messageText = "", int $statusCode = 500): JsonResponse
{ {
return response()->json(['message' => $messageText, 'status' => 'error'], $statusCode); return response()->json(['message' => $messageText, 'status' => 'error'], $statusCode);
} }
/** /**
* Create a response that forces a download in the browser. * Create a response that forces a download in the browser.
* @param string $content
* @param string $fileName
* @return \Illuminate\Http\Response
*/ */
protected function downloadResponse(string $content, string $fileName) protected function downloadResponse(string $content, string $fileName): Response
{ {
return response()->make($content, 200, [ return response()->make($content, 200, [
'Content-Type' => 'application/octet-stream', 'Content-Type' => 'application/octet-stream',
@ -149,31 +123,37 @@ abstract class Controller extends BaseController
/** /**
* Show a positive, successful notification to the user on next view load. * Show a positive, successful notification to the user on next view load.
* @param string $message
*/ */
protected function showSuccessNotification(string $message) protected function showSuccessNotification(string $message): void
{ {
session()->flash('success', $message); session()->flash('success', $message);
} }
/** /**
* Show a warning notification to the user on next view load. * Show a warning notification to the user on next view load.
* @param string $message
*/ */
protected function showWarningNotification(string $message) protected function showWarningNotification(string $message): void
{ {
session()->flash('warning', $message); session()->flash('warning', $message);
} }
/** /**
* Show an error notification to the user on next view load. * Show an error notification to the user on next view load.
* @param string $message
*/ */
protected function showErrorNotification(string $message) protected function showErrorNotification(string $message): void
{ {
session()->flash('error', $message); session()->flash('error', $message);
} }
/**
* Log an activity in the system.
* @param string|Loggable
*/
protected function logActivity(string $type, $detail = ''): void
{
Activity::add($type, $detail);
}
/** /**
* Get the validation rules for image files. * Get the validation rules for image files.
*/ */

View File

@ -1,9 +1,9 @@
<?php namespace BookStack\Http\Controllers; <?php namespace BookStack\Http\Controllers;
use Activity; use Activity;
use BookStack\Entities\Book; use BookStack\Entities\Models\Book;
use BookStack\Entities\Managers\PageContent; use BookStack\Entities\Tools\PageContent;
use BookStack\Entities\Page; use BookStack\Entities\Models\Page;
use BookStack\Entities\Repos\BookRepo; use BookStack\Entities\Repos\BookRepo;
use BookStack\Entities\Repos\BookshelfRepo; use BookStack\Entities\Repos\BookshelfRepo;
use Illuminate\Http\Response; use Illuminate\Http\Response;
@ -14,7 +14,6 @@ class HomeController extends Controller
/** /**
* Display the homepage. * Display the homepage.
* @return Response
*/ */
public function index() public function index()
{ {
@ -22,17 +21,24 @@ class HomeController extends Controller
$draftPages = []; $draftPages = [];
if ($this->isSignedIn()) { if ($this->isSignedIn()) {
$draftPages = Page::visible()->where('draft', '=', true) $draftPages = Page::visible()
->where('draft', '=', true)
->where('created_by', '=', user()->id) ->where('created_by', '=', user()->id)
->orderBy('updated_at', 'desc')->take(6)->get(); ->orderBy('updated_at', 'desc')
->with('book')
->take(6)
->get();
} }
$recentFactor = count($draftPages) > 0 ? 0.5 : 1; $recentFactor = count($draftPages) > 0 ? 0.5 : 1;
$recents = $this->isSignedIn() ? $recents = $this->isSignedIn() ?
Views::getUserRecentlyViewed(12*$recentFactor, 0) Views::getUserRecentlyViewed(12*$recentFactor, 1)
: Book::visible()->orderBy('created_at', 'desc')->take(12 * $recentFactor)->get(); : Book::visible()->orderBy('created_at', 'desc')->take(12 * $recentFactor)->get();
$recentlyUpdatedPages = Page::visible()->where('draft', false) $recentlyUpdatedPages = Page::visible()->with('book')
->orderBy('updated_at', 'desc')->take(12)->get(); ->where('draft', false)
->orderBy('updated_at', 'desc')
->take(12)
->get();
$homepageOptions = ['default', 'books', 'bookshelves', 'page']; $homepageOptions = ['default', 'books', 'bookshelves', 'page'];
$homepageOption = setting('app-homepage-type', 'default'); $homepageOption = setting('app-homepage-type', 'default');

View File

@ -15,7 +15,6 @@ class DrawioImageController extends Controller
public function __construct(ImageRepo $imageRepo) public function __construct(ImageRepo $imageRepo)
{ {
$this->imageRepo = $imageRepo; $this->imageRepo = $imageRepo;
parent::__construct();
} }
/** /**

View File

@ -18,7 +18,6 @@ class GalleryImageController extends Controller
public function __construct(ImageRepo $imageRepo) public function __construct(ImageRepo $imageRepo)
{ {
$this->imageRepo = $imageRepo; $this->imageRepo = $imageRepo;
parent::__construct();
} }
/** /**

View File

@ -1,14 +1,11 @@
<?php namespace BookStack\Http\Controllers\Images; <?php namespace BookStack\Http\Controllers\Images;
use BookStack\Entities\Page;
use BookStack\Exceptions\ImageUploadException; use BookStack\Exceptions\ImageUploadException;
use BookStack\Http\Controllers\Controller; use BookStack\Http\Controllers\Controller;
use BookStack\Repos\PageRepo;
use BookStack\Uploads\Image; use BookStack\Uploads\Image;
use BookStack\Uploads\ImageRepo; use BookStack\Uploads\ImageRepo;
use Exception; use Exception;
use Illuminate\Filesystem\Filesystem as File; use Illuminate\Filesystem\Filesystem as File;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException; use Illuminate\Validation\ValidationException;
@ -26,7 +23,6 @@ class ImageController extends Controller
$this->image = $image; $this->image = $image;
$this->file = $file; $this->file = $file;
$this->imageRepo = $imageRepo; $this->imageRepo = $imageRepo;
parent::__construct();
} }
/** /**

View File

@ -2,6 +2,8 @@
namespace BookStack\Http\Controllers; namespace BookStack\Http\Controllers;
use BookStack\Actions\ActivityType;
use BookStack\Entities\Tools\TrashCan;
use BookStack\Notifications\TestEmail; use BookStack\Notifications\TestEmail;
use BookStack\Uploads\ImageService; use BookStack\Uploads\ImageService;
use Illuminate\Http\Request; use Illuminate\Http\Request;
@ -19,7 +21,13 @@ class MaintenanceController extends Controller
// Get application version // Get application version
$version = trim(file_get_contents(base_path('version'))); $version = trim(file_get_contents(base_path('version')));
return view('settings.maintenance', ['version' => $version]); // Recycle bin details
$recycleStats = (new TrashCan())->getTrashedCounts();
return view('settings.maintenance', [
'version' => $version,
'recycleStats' => $recycleStats,
]);
} }
/** /**
@ -28,6 +36,7 @@ class MaintenanceController extends Controller
public function cleanupImages(Request $request, ImageService $imageService) public function cleanupImages(Request $request, ImageService $imageService)
{ {
$this->checkPermission('settings-manage'); $this->checkPermission('settings-manage');
$this->logActivity(ActivityType::MAINTENANCE_ACTION_RUN, 'cleanup-images');
$checkRevisions = !($request->get('ignore_revisions', 'false') === 'true'); $checkRevisions = !($request->get('ignore_revisions', 'false') === 'true');
$dryRun = !($request->has('confirm')); $dryRun = !($request->has('confirm'));
@ -54,6 +63,7 @@ class MaintenanceController extends Controller
public function sendTestEmail() public function sendTestEmail()
{ {
$this->checkPermission('settings-manage'); $this->checkPermission('settings-manage');
$this->logActivity(ActivityType::MAINTENANCE_ACTION_RUN, 'send-test-email');
try { try {
user()->notify(new TestEmail()); user()->notify(new TestEmail());

View File

@ -1,11 +1,11 @@
<?php namespace BookStack\Http\Controllers; <?php namespace BookStack\Http\Controllers;
use Activity; use BookStack\Entities\Tools\BookContents;
use BookStack\Entities\Managers\BookContents; use BookStack\Entities\Tools\PageContent;
use BookStack\Entities\Managers\PageContent; use BookStack\Entities\Tools\PageEditActivity;
use BookStack\Entities\Managers\PageEditActivity; use BookStack\Entities\Models\Page;
use BookStack\Entities\Page;
use BookStack\Entities\Repos\PageRepo; use BookStack\Entities\Repos\PageRepo;
use BookStack\Entities\Tools\PermissionsUpdater;
use BookStack\Exceptions\NotFoundException; use BookStack\Exceptions\NotFoundException;
use BookStack\Exceptions\NotifyException; use BookStack\Exceptions\NotifyException;
use BookStack\Exceptions\PermissionsException; use BookStack\Exceptions\PermissionsException;
@ -26,7 +26,6 @@ class PageController extends Controller
public function __construct(PageRepo $pageRepo) public function __construct(PageRepo $pageRepo)
{ {
$this->pageRepo = $pageRepo; $this->pageRepo = $pageRepo;
parent::__construct();
} }
/** /**
@ -78,7 +77,7 @@ class PageController extends Controller
public function editDraft(string $bookSlug, int $pageId) public function editDraft(string $bookSlug, int $pageId)
{ {
$draft = $this->pageRepo->getById($pageId); $draft = $this->pageRepo->getById($pageId);
$this->checkOwnablePermission('page-create', $draft->parent()); $this->checkOwnablePermission('page-create', $draft->getParent());
$this->setPageTitle(trans('entities.pages_edit_draft')); $this->setPageTitle(trans('entities.pages_edit_draft'));
$draftsEnabled = $this->isSignedIn(); $draftsEnabled = $this->isSignedIn();
@ -104,10 +103,9 @@ class PageController extends Controller
'name' => 'required|string|max:255' 'name' => 'required|string|max:255'
]); ]);
$draftPage = $this->pageRepo->getById($pageId); $draftPage = $this->pageRepo->getById($pageId);
$this->checkOwnablePermission('page-create', $draftPage->parent()); $this->checkOwnablePermission('page-create', $draftPage->getParent());
$page = $this->pageRepo->publishDraft($draftPage, $request->all()); $page = $this->pageRepo->publishDraft($draftPage, $request->all());
Activity::add($page, 'page_create', $draftPage->book->id);
return redirect($page->getUrl()); return redirect($page->getUrl());
} }
@ -224,7 +222,6 @@ class PageController extends Controller
$this->checkOwnablePermission('page-update', $page); $this->checkOwnablePermission('page-update', $page);
$this->pageRepo->update($page, $request->all()); $this->pageRepo->update($page, $request->all());
Activity::add($page, 'page_update', $page->book->id);
return redirect($page->getUrl()); return redirect($page->getUrl());
} }
@ -304,13 +301,10 @@ class PageController extends Controller
{ {
$page = $this->pageRepo->getBySlug($bookSlug, $pageSlug); $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
$this->checkOwnablePermission('page-delete', $page); $this->checkOwnablePermission('page-delete', $page);
$parent = $page->getParent();
$book = $page->book;
$parent = $page->chapter ?? $book;
$this->pageRepo->destroy($page); $this->pageRepo->destroy($page);
Activity::addMessage('page_delete', $page->name, $book->id);
$this->showSuccessNotification(trans('entities.pages_delete_success'));
return redirect($parent->getUrl()); return redirect($parent->getUrl());
} }
@ -394,7 +388,6 @@ class PageController extends Controller
return redirect()->back(); return redirect()->back();
} }
Activity::add($page, 'page_move', $page->book->id);
$this->showSuccessNotification(trans('entities.pages_move_success', ['parentName' => $parent->name])); $this->showSuccessNotification(trans('entities.pages_move_success', ['parentName' => $parent->name]));
return redirect($page->getUrl()); return redirect($page->getUrl());
} }
@ -439,8 +432,6 @@ class PageController extends Controller
return redirect()->back(); return redirect()->back();
} }
Activity::add($pageCopy, 'page_create', $pageCopy->book->id);
$this->showSuccessNotification(trans('entities.pages_copy_success')); $this->showSuccessNotification(trans('entities.pages_copy_success'));
return redirect($pageCopy->getUrl()); return redirect($pageCopy->getUrl());
} }
@ -463,14 +454,12 @@ class PageController extends Controller
* @throws NotFoundException * @throws NotFoundException
* @throws Throwable * @throws Throwable
*/ */
public function permissions(Request $request, string $bookSlug, string $pageSlug) public function permissions(Request $request, PermissionsUpdater $permissionsUpdater, string $bookSlug, string $pageSlug)
{ {
$page = $this->pageRepo->getBySlug($bookSlug, $pageSlug); $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
$this->checkOwnablePermission('restrictions-manage', $page); $this->checkOwnablePermission('restrictions-manage', $page);
$restricted = $request->get('restricted') === 'true'; $permissionsUpdater->updateFromPermissionsForm($page, $request);
$permissions = $request->filled('restrictions') ? collect($request->get('restrictions')) : null;
$this->pageRepo->updatePermissions($page, $restricted, $permissions);
$this->showSuccessNotification(trans('entities.pages_permissions_success')); $this->showSuccessNotification(trans('entities.pages_permissions_success'));
return redirect($page->getUrl()); return redirect($page->getUrl());

View File

@ -2,8 +2,8 @@
namespace BookStack\Http\Controllers; namespace BookStack\Http\Controllers;
use BookStack\Entities\ExportService; use BookStack\Entities\Tools\ExportFormatter;
use BookStack\Entities\Managers\PageContent; use BookStack\Entities\Tools\PageContent;
use BookStack\Entities\Repos\PageRepo; use BookStack\Entities\Repos\PageRepo;
use BookStack\Exceptions\NotFoundException; use BookStack\Exceptions\NotFoundException;
use Throwable; use Throwable;
@ -12,18 +12,15 @@ class PageExportController extends Controller
{ {
protected $pageRepo; protected $pageRepo;
protected $exportService; protected $exportFormatter;
/** /**
* PageExportController constructor. * PageExportController constructor.
* @param PageRepo $pageRepo
* @param ExportService $exportService
*/ */
public function __construct(PageRepo $pageRepo, ExportService $exportService) public function __construct(PageRepo $pageRepo, ExportFormatter $exportFormatter)
{ {
$this->pageRepo = $pageRepo; $this->pageRepo = $pageRepo;
$this->exportService = $exportService; $this->exportFormatter = $exportFormatter;
parent::__construct();
} }
/** /**
@ -36,7 +33,7 @@ class PageExportController extends Controller
{ {
$page = $this->pageRepo->getBySlug($bookSlug, $pageSlug); $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
$page->html = (new PageContent($page))->render(); $page->html = (new PageContent($page))->render();
$pdfContent = $this->exportService->pageToPdf($page); $pdfContent = $this->exportFormatter->pageToPdf($page);
return $this->downloadResponse($pdfContent, $pageSlug . '.pdf'); return $this->downloadResponse($pdfContent, $pageSlug . '.pdf');
} }
@ -49,7 +46,7 @@ class PageExportController extends Controller
{ {
$page = $this->pageRepo->getBySlug($bookSlug, $pageSlug); $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
$page->html = (new PageContent($page))->render(); $page->html = (new PageContent($page))->render();
$containedHtml = $this->exportService->pageToContainedHtml($page); $containedHtml = $this->exportFormatter->pageToContainedHtml($page);
return $this->downloadResponse($containedHtml, $pageSlug . '.html'); return $this->downloadResponse($containedHtml, $pageSlug . '.html');
} }
@ -60,7 +57,7 @@ class PageExportController extends Controller
public function plainText(string $bookSlug, string $pageSlug) public function plainText(string $bookSlug, string $pageSlug)
{ {
$page = $this->pageRepo->getBySlug($bookSlug, $pageSlug); $page = $this->pageRepo->getBySlug($bookSlug, $pageSlug);
$pageText = $this->exportService->pageToPlainText($page); $pageText = $this->exportFormatter->pageToPlainText($page);
return $this->downloadResponse($pageText, $pageSlug . '.txt'); return $this->downloadResponse($pageText, $pageSlug . '.txt');
} }
} }

View File

@ -1,10 +1,9 @@
<?php namespace BookStack\Http\Controllers; <?php namespace BookStack\Http\Controllers;
use BookStack\Entities\Managers\PageContent; use BookStack\Entities\Tools\PageContent;
use BookStack\Entities\Repos\PageRepo; use BookStack\Entities\Repos\PageRepo;
use BookStack\Exceptions\NotFoundException; use BookStack\Exceptions\NotFoundException;
use BookStack\Facades\Activity; use Ssddanbrown\HtmlDiff\Diff;
use GatherContent\Htmldiff\Htmldiff;
class PageRevisionController extends Controller class PageRevisionController extends Controller
{ {
@ -17,7 +16,6 @@ class PageRevisionController extends Controller
public function __construct(PageRepo $pageRepo) public function __construct(PageRepo $pageRepo)
{ {
$this->pageRepo = $pageRepo; $this->pageRepo = $pageRepo;
parent::__construct();
} }
/** /**
@ -74,7 +72,7 @@ class PageRevisionController extends Controller
$prev = $revision->getPrevious(); $prev = $revision->getPrevious();
$prevContent = $prev->html ?? ''; $prevContent = $prev->html ?? '';
$diff = (new Htmldiff)->diff($prevContent, $revision->html); $diff = Diff::excecute($prevContent, $revision->html);
$page->fill($revision->toArray()); $page->fill($revision->toArray());
// TODO - Refactor PageContent so we don't need to juggle this // TODO - Refactor PageContent so we don't need to juggle this
@ -101,7 +99,6 @@ class PageRevisionController extends Controller
$page = $this->pageRepo->restoreRevision($page, $revisionId); $page = $this->pageRepo->restoreRevision($page, $revisionId);
Activity::add($page, 'page_restore', $page->book->id);
return redirect($page->getUrl()); return redirect($page->getUrl());
} }

View File

@ -16,7 +16,6 @@ class PageTemplateController extends Controller
public function __construct(PageRepo $pageRepo) public function __construct(PageRepo $pageRepo)
{ {
$this->pageRepo = $pageRepo; $this->pageRepo = $pageRepo;
parent::__construct();
} }
/** /**

Some files were not shown because too many files have changed in this diff Show More