mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-05-07 17:40:57 +00:00
Updated activities table format
Renamed some columns to be more generic and applicable. Removed now redundant book_id column. Allowed nullable entity morph columns for non-entity activity. Ran tests and made required changes.
This commit is contained in:
parent
ee7e1122d3
commit
712ccd23c4
12 changed files with 106 additions and 50 deletions
app
Actions
Entities
Http/Controllers
database/migrations
resources/views/settings
tests
|
@ -5,16 +5,16 @@ namespace BookStack\Actions;
|
||||||
use BookStack\Auth\User;
|
use BookStack\Auth\User;
|
||||||
use BookStack\Entities\Entity;
|
use BookStack\Entities\Entity;
|
||||||
use BookStack\Model;
|
use BookStack\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @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 +32,18 @@ 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -53,6 +51,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];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,14 +12,12 @@ use Illuminate\Support\Facades\Log;
|
||||||
class ActivityService
|
class ActivityService
|
||||||
{
|
{
|
||||||
protected $activity;
|
protected $activity;
|
||||||
protected $user;
|
|
||||||
protected $permissionService;
|
protected $permissionService;
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -38,8 +36,8 @@ class ActivityService
|
||||||
protected function newActivityForUser(string $type): Activity
|
protected function newActivityForUser(string $type): Activity
|
||||||
{
|
{
|
||||||
return $this->activity->newInstance()->forceFill([
|
return $this->activity->newInstance()->forceFill([
|
||||||
'key' => strtolower($type),
|
'type' => strtolower($type),
|
||||||
'user_id' => $this->user->id,
|
'user_id' => user()->id,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -51,9 +49,9 @@ class ActivityService
|
||||||
public function removeEntity(Entity $entity)
|
public function removeEntity(Entity $entity)
|
||||||
{
|
{
|
||||||
$entity->activity()->update([
|
$entity->activity()->update([
|
||||||
'extra' => $entity->name,
|
'detail' => $entity->name,
|
||||||
'entity_id' => 0,
|
'entity_id' => null,
|
||||||
'entity_type' => '',
|
'entity_type' => null,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -150,9 +148,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);
|
||||||
|
|
|
@ -45,9 +45,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) {
|
||||||
|
|
|
@ -32,7 +32,7 @@ class AuditLogController extends Controller
|
||||||
->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']) {
|
||||||
|
@ -45,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,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,10 +15,6 @@ 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;
|
||||||
|
|
|
@ -0,0 +1,58 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
|
class SimplifyActivitiesTable extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function up()
|
||||||
|
{
|
||||||
|
Schema::table('activities', function (Blueprint $table) {
|
||||||
|
$table->renameColumn('key', 'type');
|
||||||
|
$table->renameColumn('extra', 'detail');
|
||||||
|
$table->dropColumn('book_id');
|
||||||
|
$table->integer('entity_id')->nullable()->change();
|
||||||
|
$table->string('entity_type', 191)->nullable()->change();
|
||||||
|
});
|
||||||
|
|
||||||
|
DB::table('activities')
|
||||||
|
->where('entity_id', '=', 0)
|
||||||
|
->update([
|
||||||
|
'entity_id' => null,
|
||||||
|
'entity_type' => null,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function down()
|
||||||
|
{
|
||||||
|
DB::table('activities')
|
||||||
|
->whereNull('entity_id')
|
||||||
|
->update([
|
||||||
|
'entity_id' => 0,
|
||||||
|
'entity_type' => '',
|
||||||
|
]);
|
||||||
|
|
||||||
|
Schema::table('activities', function (Blueprint $table) {
|
||||||
|
$table->renameColumn('type', 'key');
|
||||||
|
$table->renameColumn('detail', 'extra');
|
||||||
|
$table->integer('book_id');
|
||||||
|
|
||||||
|
$table->integer('entity_id')->change();
|
||||||
|
$table->string('entity_type', 191)->change();
|
||||||
|
|
||||||
|
$table->index('book_id');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
|
@ -19,8 +19,8 @@
|
||||||
<button refs="dropdown@toggle" aria-haspopup="true" aria-expanded="false" aria-label="{{ trans('common.sort_options') }}" class="input-base text-left">{{ $listDetails['event'] ?: trans('settings.audit_event_filter_no_filter') }}</button>
|
<button refs="dropdown@toggle" aria-haspopup="true" aria-expanded="false" aria-label="{{ trans('common.sort_options') }}" class="input-base text-left">{{ $listDetails['event'] ?: trans('settings.audit_event_filter_no_filter') }}</button>
|
||||||
<ul refs="dropdown@menu" class="dropdown-menu">
|
<ul refs="dropdown@menu" class="dropdown-menu">
|
||||||
<li @if($listDetails['event'] === '') class="active" @endif><a href="{{ sortUrl('/settings/audit', $listDetails, ['event' => '']) }}">{{ trans('settings.audit_event_filter_no_filter') }}</a></li>
|
<li @if($listDetails['event'] === '') class="active" @endif><a href="{{ sortUrl('/settings/audit', $listDetails, ['event' => '']) }}">{{ trans('settings.audit_event_filter_no_filter') }}</a></li>
|
||||||
@foreach($activityKeys as $key)
|
@foreach($activityTypes as $type)
|
||||||
<li @if($key === $listDetails['event']) class="active" @endif><a href="{{ sortUrl('/settings/audit', $listDetails, ['event' => $key]) }}">{{ $key }}</a></li>
|
<li @if($type === $listDetails['event']) class="active" @endif><a href="{{ sortUrl('/settings/audit', $listDetails, ['event' => $type]) }}">{{ $type }}</a></li>
|
||||||
@endforeach
|
@endforeach
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
|
@ -62,7 +62,7 @@
|
||||||
<td>
|
<td>
|
||||||
@include('partials.table-user', ['user' => $activity->user, 'user_id' => $activity->user_id])
|
@include('partials.table-user', ['user' => $activity->user, 'user_id' => $activity->user_id])
|
||||||
</td>
|
</td>
|
||||||
<td>{{ $activity->key }}</td>
|
<td>{{ $activity->type }}</td>
|
||||||
<td>
|
<td>
|
||||||
@if($activity->entity)
|
@if($activity->entity)
|
||||||
<a href="{{ $activity->entity->getUrl() }}" class="table-entity-item">
|
<a href="{{ $activity->entity->getUrl() }}" class="table-entity-item">
|
||||||
|
@ -71,10 +71,10 @@
|
||||||
{{ $activity->entity->name }}
|
{{ $activity->entity->name }}
|
||||||
</div>
|
</div>
|
||||||
</a>
|
</a>
|
||||||
@elseif($activity->extra)
|
@elseif($activity->detail)
|
||||||
<div class="px-m">
|
<div class="px-m">
|
||||||
{{ trans('settings.audit_deleted_item') }} <br>
|
{{ trans('settings.audit_deleted_item') }} <br>
|
||||||
{{ trans('settings.audit_deleted_item_name', ['name' => $activity->extra]) }}
|
{{ trans('settings.audit_deleted_item_name', ['name' => $activity->detail]) }}
|
||||||
</div>
|
</div>
|
||||||
@endif
|
@endif
|
||||||
</td>
|
</td>
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
use BookStack\Actions\Activity;
|
use BookStack\Actions\Activity;
|
||||||
use BookStack\Actions\ActivityService;
|
use BookStack\Actions\ActivityService;
|
||||||
|
use BookStack\Actions\ActivityType;
|
||||||
use BookStack\Auth\UserRepo;
|
use BookStack\Auth\UserRepo;
|
||||||
use BookStack\Entities\Managers\TrashCan;
|
use BookStack\Entities\Managers\TrashCan;
|
||||||
use BookStack\Entities\Page;
|
use BookStack\Entities\Page;
|
||||||
|
@ -10,6 +11,14 @@ use Carbon\Carbon;
|
||||||
|
|
||||||
class AuditLogTest extends TestCase
|
class AuditLogTest extends TestCase
|
||||||
{
|
{
|
||||||
|
/** @var ActivityService */
|
||||||
|
protected $activityService;
|
||||||
|
|
||||||
|
public function setUp(): void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
$this->activityService = app(ActivityService::class);
|
||||||
|
}
|
||||||
|
|
||||||
public function test_only_accessible_with_right_permissions()
|
public function test_only_accessible_with_right_permissions()
|
||||||
{
|
{
|
||||||
|
@ -34,7 +43,7 @@ class AuditLogTest extends TestCase
|
||||||
$admin = $this->getAdmin();
|
$admin = $this->getAdmin();
|
||||||
$this->actingAs($admin);
|
$this->actingAs($admin);
|
||||||
$page = Page::query()->first();
|
$page = Page::query()->first();
|
||||||
app(ActivityService::class)->add($page, 'page_create', $page->book->id);
|
$this->activityService->addForEntity($page, ActivityType::PAGE_CREATE);
|
||||||
$activity = Activity::query()->orderBy('id', 'desc')->first();
|
$activity = Activity::query()->orderBy('id', 'desc')->first();
|
||||||
|
|
||||||
$resp = $this->get('settings/audit');
|
$resp = $this->get('settings/audit');
|
||||||
|
@ -49,7 +58,7 @@ class AuditLogTest extends TestCase
|
||||||
$this->actingAs( $this->getAdmin());
|
$this->actingAs( $this->getAdmin());
|
||||||
$page = Page::query()->first();
|
$page = Page::query()->first();
|
||||||
$pageName = $page->name;
|
$pageName = $page->name;
|
||||||
app(ActivityService::class)->add($page, 'page_create', $page->book->id);
|
$this->activityService->addForEntity($page, ActivityType::PAGE_CREATE);
|
||||||
|
|
||||||
app(PageRepo::class)->destroy($page);
|
app(PageRepo::class)->destroy($page);
|
||||||
app(TrashCan::class)->empty();
|
app(TrashCan::class)->empty();
|
||||||
|
@ -64,7 +73,7 @@ class AuditLogTest extends TestCase
|
||||||
$viewer = $this->getViewer();
|
$viewer = $this->getViewer();
|
||||||
$this->actingAs($viewer);
|
$this->actingAs($viewer);
|
||||||
$page = Page::query()->first();
|
$page = Page::query()->first();
|
||||||
app(ActivityService::class)->add($page, 'page_create', $page->book->id);
|
$this->activityService->addForEntity($page, ActivityType::PAGE_CREATE);
|
||||||
|
|
||||||
$this->actingAs($this->getAdmin());
|
$this->actingAs($this->getAdmin());
|
||||||
app(UserRepo::class)->destroy($viewer);
|
app(UserRepo::class)->destroy($viewer);
|
||||||
|
@ -77,7 +86,7 @@ class AuditLogTest extends TestCase
|
||||||
{
|
{
|
||||||
$this->actingAs($this->getAdmin());
|
$this->actingAs($this->getAdmin());
|
||||||
$page = Page::query()->first();
|
$page = Page::query()->first();
|
||||||
app(ActivityService::class)->add($page, 'page_create', $page->book->id);
|
$this->activityService->addForEntity($page, ActivityType::PAGE_CREATE);
|
||||||
|
|
||||||
$resp = $this->get('settings/audit');
|
$resp = $this->get('settings/audit');
|
||||||
$resp->assertSeeText($page->name);
|
$resp->assertSeeText($page->name);
|
||||||
|
@ -90,7 +99,7 @@ class AuditLogTest extends TestCase
|
||||||
{
|
{
|
||||||
$this->actingAs($this->getAdmin());
|
$this->actingAs($this->getAdmin());
|
||||||
$page = Page::query()->first();
|
$page = Page::query()->first();
|
||||||
app(ActivityService::class)->add($page, 'page_create', $page->book->id);
|
$this->activityService->addForEntity($page, ActivityType::PAGE_CREATE);
|
||||||
|
|
||||||
$yesterday = (Carbon::now()->subDay()->format('Y-m-d'));
|
$yesterday = (Carbon::now()->subDay()->format('Y-m-d'));
|
||||||
$tomorrow = (Carbon::now()->addDay()->format('Y-m-d'));
|
$tomorrow = (Carbon::now()->addDay()->format('Y-m-d'));
|
||||||
|
|
|
@ -41,7 +41,7 @@ class CommandsTest extends TestCase
|
||||||
\Activity::addForEntity($page, ActivityType::PAGE_UPDATE);
|
\Activity::addForEntity($page, ActivityType::PAGE_UPDATE);
|
||||||
|
|
||||||
$this->assertDatabaseHas('activities', [
|
$this->assertDatabaseHas('activities', [
|
||||||
'key' => 'page_update',
|
'type' => 'page_update',
|
||||||
'entity_id' => $page->id,
|
'entity_id' => $page->id,
|
||||||
'user_id' => $this->getEditor()->id
|
'user_id' => $this->getEditor()->id
|
||||||
]);
|
]);
|
||||||
|
@ -51,7 +51,7 @@ class CommandsTest extends TestCase
|
||||||
|
|
||||||
|
|
||||||
$this->assertDatabaseMissing('activities', [
|
$this->assertDatabaseMissing('activities', [
|
||||||
'key' => 'page_update'
|
'type' => 'page_update'
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -79,7 +79,7 @@ class SortTest extends TestCase
|
||||||
$movePageResp = $this->actingAs($this->getEditor())->put($page->getUrl('/move'), [
|
$movePageResp = $this->actingAs($this->getEditor())->put($page->getUrl('/move'), [
|
||||||
'entity_selection' => 'book:' . $newBook->id
|
'entity_selection' => 'book:' . $newBook->id
|
||||||
]);
|
]);
|
||||||
$page = Page::find($page->id);
|
$page->refresh();
|
||||||
|
|
||||||
$movePageResp->assertRedirect($page->getUrl());
|
$movePageResp->assertRedirect($page->getUrl());
|
||||||
$this->assertTrue($page->book->id == $newBook->id, 'Page parent is now the new book');
|
$this->assertTrue($page->book->id == $newBook->id, 'Page parent is now the new book');
|
||||||
|
|
|
@ -136,7 +136,7 @@ class RecycleBinTest extends TestCase
|
||||||
$deletion = $page->deletions()->firstOrFail();
|
$deletion = $page->deletions()->firstOrFail();
|
||||||
|
|
||||||
$this->assertDatabaseHas('activities', [
|
$this->assertDatabaseHas('activities', [
|
||||||
'key' => 'page_delete',
|
'type' => 'page_delete',
|
||||||
'entity_id' => $page->id,
|
'entity_id' => $page->id,
|
||||||
'entity_type' => $page->getMorphClass(),
|
'entity_type' => $page->getMorphClass(),
|
||||||
]);
|
]);
|
||||||
|
@ -144,16 +144,16 @@ class RecycleBinTest extends TestCase
|
||||||
$this->asAdmin()->delete("/settings/recycle-bin/{$deletion->id}");
|
$this->asAdmin()->delete("/settings/recycle-bin/{$deletion->id}");
|
||||||
|
|
||||||
$this->assertDatabaseMissing('activities', [
|
$this->assertDatabaseMissing('activities', [
|
||||||
'key' => 'page_delete',
|
'type' => 'page_delete',
|
||||||
'entity_id' => $page->id,
|
'entity_id' => $page->id,
|
||||||
'entity_type' => $page->getMorphClass(),
|
'entity_type' => $page->getMorphClass(),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->assertDatabaseHas('activities', [
|
$this->assertDatabaseHas('activities', [
|
||||||
'key' => 'page_delete',
|
'type' => 'page_delete',
|
||||||
'entity_id' => 0,
|
'entity_id' => null,
|
||||||
'entity_type' => '',
|
'entity_type' => null,
|
||||||
'extra' => $page->name,
|
'detail' => $page->name,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -53,9 +53,9 @@ abstract class TestCase extends BaseTestCase
|
||||||
* Assert that an activity entry exists of the given key.
|
* Assert that an activity entry exists of the given key.
|
||||||
* Checks the activity belongs to the given entity if provided.
|
* Checks the activity belongs to the given entity if provided.
|
||||||
*/
|
*/
|
||||||
protected function assertActivityExists(string $key, Entity $entity = null)
|
protected function assertActivityExists(string $type, Entity $entity = null)
|
||||||
{
|
{
|
||||||
$detailsToCheck = ['key' => $key];
|
$detailsToCheck = ['type' => $type];
|
||||||
|
|
||||||
if ($entity) {
|
if ($entity) {
|
||||||
$detailsToCheck['entity_type'] = $entity->getMorphClass();
|
$detailsToCheck['entity_type'] = $entity->getMorphClass();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue