});
$activity = $query->orderBy('created_at', 'desc')
- ->with(['entity' => function (Relation $query) {
+ ->with(['loggable' => function (Relation $query) {
$query->withTrashed();
}, 'user.avatar'])
->skip($count * ($page - 1))
/**
* @property string $type
* @property User $user
- * @property Entity $entity
+ * @property Entity $loggable
* @property string $detail
* @property string $loggable_type
* @property int $loggable_id
{{ $activity->getText() }}
- @if($activity->entity && is_null($activity->entity->deleted_at))
- <a href="{{ $activity->entity->getUrl() }}">{{ $activity->entity->name }}</a>
+ @if($activity->loggable && is_null($activity->loggable->deleted_at))
+ <a href="{{ $activity->loggable->getUrl() }}">{{ $activity->loggable->name }}</a>
@endif
- @if($activity->entity && !is_null($activity->entity->deleted_at))
- "{{ $activity->entity->name }}"
+ @if($activity->loggable && !is_null($activity->loggable->deleted_at))
+ "{{ $activity->loggable->name }}"
@endif
<br>
--- /dev/null
+<?php
+
+namespace Activity;
+
+use BookStack\Activity\ActivityType;
+use BookStack\Facades\Activity;
+use Tests\Api\TestsApi;
+use Tests\TestCase;
+
+class AuditLogApiTest extends TestCase
+{
+ use TestsApi;
+
+ public function test_user_and_settings_manage_permissions_needed()
+ {
+ $editor = $this->users->editor();
+
+ $assertPermissionErrorOnCall = function () use ($editor) {
+ $resp = $this->actingAsForApi($editor)->getJson('/api/audit-log');
+ $resp->assertStatus(403);
+ $resp->assertJson($this->permissionErrorResponse());
+ };
+
+ $assertPermissionErrorOnCall();
+ $this->permissions->grantUserRolePermissions($editor, ['users-manage']);
+ $assertPermissionErrorOnCall();
+ $this->permissions->removeUserRolePermissions($editor, ['users-manage']);
+ $this->permissions->grantUserRolePermissions($editor, ['settings-manage']);
+ $assertPermissionErrorOnCall();
+
+ $this->permissions->grantUserRolePermissions($editor, ['settings-manage', 'users-manage']);
+ $resp = $this->actingAsForApi($editor)->getJson('/api/audit-log');
+ $resp->assertOk();
+ }
+
+ public function test_index_endpoint_returns_expected_data()
+ {
+ $page = $this->entities->page();
+ $admin = $this->users->admin();
+ $this->actingAsForApi($admin);
+ Activity::add(ActivityType::PAGE_UPDATE, $page);
+
+ $resp = $this->get("/api/audit-log?filter[loggable_id]={$page->id}");
+ $resp->assertJson(['data' => [
+ [
+ 'type' => 'page_update',
+ 'detail' => "({$page->id}) {$page->name}",
+ 'user_id' => $admin->id,
+ 'loggable_id' => $page->id,
+ 'loggable_type' => 'page',
+ 'ip' => '127.0.0.1',
+ 'user' => [
+ 'id' => $admin->id,
+ 'name' => $admin->name,
+ 'slug' => $admin->slug,
+ ],
+ ]
+ ]]);
+ }
+}
<?php
-namespace Tests\Actions;
+namespace Activity;
use BookStack\Activity\ActivityType;
use BookStack\Activity\Models\Activity;
'type' => ActivityType::PAGE_UPDATE,
'ip' => '192.123.45.1',
'user_id' => $editor->id,
- 'entity_id' => $page->id,
+ 'loggable_id' => $page->id,
]);
$resp = $this->asAdmin()->get('/settings/audit');
'type' => ActivityType::PAGE_UPDATE,
'ip' => '127.0.0.1',
'user_id' => $editor->id,
- 'entity_id' => $page->id,
+ 'loggable_id' => $page->id,
]);
}
'type' => ActivityType::PAGE_UPDATE,
'ip' => '192.123.x.x',
'user_id' => $editor->id,
- 'entity_id' => $page->id,
+ 'loggable_id' => $page->id,
]);
}
}
<?php
-namespace Tests\Actions;
+namespace Activity;
use BookStack\Activity\ActivityType;
use BookStack\Activity\DispatchWebhookJob;
<?php
-namespace Tests\Actions;
+namespace Activity;
use BookStack\Activity\ActivityType;
use BookStack\Activity\Models\Webhook;
<?php
-namespace Tests\Actions;
+namespace Activity;
use BookStack\Activity\ActivityType;
use BookStack\Activity\Models\Webhook;
$this->assertDatabaseHas('activities', [
'type' => 'page_update',
- 'entity_id' => $page->id,
+ 'loggable_id' => $page->id,
'user_id' => $this->users->editor()->id,
]);
$this->assertDatabaseHas('activities', [
'type' => 'page_delete',
- 'entity_id' => $page->id,
- 'entity_type' => $page->getMorphClass(),
+ 'loggable_id' => $page->id,
+ 'loggable_type' => $page->getMorphClass(),
]);
$this->asAdmin()->delete("/settings/recycle-bin/{$deletion->id}");
$this->assertDatabaseMissing('activities', [
'type' => 'page_delete',
- 'entity_id' => $page->id,
- 'entity_type' => $page->getMorphClass(),
+ 'loggable_id' => $page->id,
+ 'loggable_type' => $page->getMorphClass(),
]);
$this->assertDatabaseHas('activities', [
'type' => 'page_delete',
- 'entity_id' => null,
- 'entity_type' => null,
+ 'loggable_id' => null,
+ 'loggable_type' => null,
'detail' => $page->name,
]);
}
$detailsToCheck = ['type' => $type];
if ($entity) {
- $detailsToCheck['entity_type'] = $entity->getMorphClass();
- $detailsToCheck['entity_id'] = $entity->id;
+ $detailsToCheck['loggable_type'] = $entity->getMorphClass();
+ $detailsToCheck['loggable_id'] = $entity->id;
}
if ($detail) {
namespace Tests\User;
-use Activity;
use BookStack\Activity\ActivityType;
+use BookStack\Facades\Activity;
use BookStack\Users\Models\User;
use Tests\TestCase;