Skip to content

Commit

Permalink
Added revision activity types to system and audit log
Browse files Browse the repository at this point in the history
Closes #3628
  • Loading branch information
ssddanbrown committed Aug 9, 2022
1 parent 7fa934e commit 6e0a734
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 2 deletions.
3 changes: 3 additions & 0 deletions app/Actions/ActivityType.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ class ActivityType
const COMMENTED_ON = 'commented_on';
const PERMISSIONS_UPDATE = 'permissions_update';

const REVISION_RESTORE = 'revision_restore';
const REVISION_DELETE = 'revision_delete';

const SETTINGS_UPDATE = 'settings_update';
const MAINTENANCE_ACTION_RUN = 'maintenance_action_run';

Expand Down
8 changes: 7 additions & 1 deletion app/Entities/Models/PageRevision.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace BookStack\Entities\Models;

use BookStack\Auth\User;
use BookStack\Interfaces\Loggable;
use BookStack\Model;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
Expand All @@ -27,7 +28,7 @@
* @property Page $page
* @property-read ?User $createdBy
*/
class PageRevision extends Model
class PageRevision extends Model implements Loggable
{
protected $fillable = ['name', 'text', 'summary'];
protected $hidden = ['html', 'markdown', 'restricted', 'text'];
Expand Down Expand Up @@ -83,4 +84,9 @@ public static function isA(string $type): bool
{
return $type === 'revision';
}

public function logDescriptor(): string
{
return "Revision #{$this->revision_number} (ID: {$this->id}) for page ID {$this->page_id}";
}
}
1 change: 1 addition & 0 deletions app/Entities/Repos/PageRepo.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ public function restoreRevision(Page $page, int $revisionId): Page
$this->savePageRevision($page, $summary);

Activity::add(ActivityType::PAGE_RESTORE, $page);
Activity::add(ActivityType::REVISION_RESTORE, $revision);

return $page;
}
Expand Down
2 changes: 1 addition & 1 deletion app/Facades/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Illuminate\Support\Facades\Facade;

/**
* @see \BookStack\Actions\ActivityLogger
* @mixin \BookStack\Actions\ActivityLogger
*/
class Activity extends Facade
{
Expand Down
3 changes: 3 additions & 0 deletions app/Http/Controllers/PageRevisionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace BookStack\Http\Controllers;

use BookStack\Actions\ActivityType;
use BookStack\Entities\Repos\PageRepo;
use BookStack\Entities\Tools\PageContent;
use BookStack\Exceptions\NotFoundException;
use BookStack\Facades\Activity;
use Ssddanbrown\HtmlDiff\Diff;

class PageRevisionController extends Controller
Expand Down Expand Up @@ -132,6 +134,7 @@ public function destroy(string $bookSlug, string $pageSlug, int $revId)
}

$revision->delete();
Activity::add(ActivityType::REVISION_DELETE, $revision);
$this->showSuccessNotification(trans('entities.revision_delete_success'));

return redirect($page->getUrl('/revisions'));
Expand Down
7 changes: 7 additions & 0 deletions tests/Entity/PageRevisionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Tests\Entity;

use BookStack\Actions\ActivityType;
use BookStack\Entities\Models\Page;
use BookStack\Entities\Repos\PageRepo;
use Tests\TestCase;
Expand Down Expand Up @@ -117,6 +118,9 @@ public function test_page_revision_restore_sets_new_revision_with_summary()
'type' => 'version',
'summary' => "Restored from #{$revToRestore->id}; My first update",
]);

$detail = "Revision #{$revToRestore->revision_number} (ID: {$revToRestore->id}) for page ID {$revToRestore->page_id}";
$this->assertActivityExists(ActivityType::REVISION_RESTORE, null, $detail);
}

public function test_page_revision_count_increments_on_update()
Expand Down Expand Up @@ -164,6 +168,9 @@ public function test_revision_deletion()

$this->assertTrue($beforeRevisionCount === ($afterRevisionCount + 1));

$detail = "Revision #{$revision->revision_number} (ID: {$revision->id}) for page ID {$revision->page_id}";
$this->assertActivityExists(ActivityType::REVISION_DELETE, null, $detail);

// Try to delete the latest revision
$beforeRevisionCount = $page->revisions->count();
$resp = $this->asEditor()->delete($page->currentRevision->getUrl('/delete/'));
Expand Down

0 comments on commit 6e0a734

Please sign in to comment.