Added user filter to audit log

Included testing to cover.
Closes #2472
This commit is contained in:
Dan Brown 2021-03-21 15:04:32 +00:00
parent c548c06086
commit 06706a2d9c
11 changed files with 70 additions and 24 deletions

View File

@ -20,6 +20,7 @@ class AuditLogController extends Controller
'sort' => $request->get('sort', 'created_at'),
'date_from' => $request->get('date_from', ''),
'date_to' => $request->get('date_to', ''),
'user' => $request->get('user', ''),
];
$query = Activity::query()
@ -34,6 +35,9 @@ class AuditLogController extends Controller
if ($listDetails['event']) {
$query->where('type', '=', $listDetails['event']);
}
if ($listDetails['user']) {
$query->where('user_id', '=', $listDetails['user']);
}
if ($listDetails['date_from']) {
$query->where('created_at', '>=', $listDetails['date_from']);

View File

@ -13,9 +13,11 @@ class UserSelect {
}
selectUser(event, userEl) {
event.preventDefault();
const id = userEl.getAttribute('data-id');
this.input.value = id;
this.userInfoContainer.innerHTML = userEl.innerHTML;
this.input.dispatchEvent(new Event('change', {bubbles: true}));
this.hide();
}

View File

@ -783,6 +783,6 @@ body.flexbox-support #entity-selector-wrap .popup-body .form-group {
.custom-select-input {
max-width: 280px;
border: 1px solid #DDD;
border-radius: 4px;
border: 1px solid #D4D4D4;
border-radius: 3px;
}

View File

@ -269,7 +269,7 @@ header .search-box {
.dropdown-search {
position: relative;
.dropdown-search-toggle {
padding: 6px;
padding: $-xs;
border: 1px solid transparent;
border-radius: 4px;
&:hover {
@ -281,6 +281,14 @@ header .search-box {
}
}
.dropdown-search-toggle.compact {
padding: $-xxs $-xs;
.avatar {
height: 22px;
width: 22px;
}
}
.faded {
a, button, span, span > div {
color: #666;

View File

@ -28,7 +28,7 @@ $-xs: 6px;
$-xxs: 3px;
// List of our spacing sizes
$spacing: (('none', 0), ('xxs', $-xxs), ('xs', $-xs), ('s', $-s), ('m', $-m), ('l', $-l), ('xl', $-xl), ('xxl', $-xxl));
$spacing: (('none', 0), ('xxs', $-xxs), ('xs', $-xs), ('s', $-s), ('m', $-m), ('l', $-l), ('xl', $-xl), ('xxl', $-xxl), ('auto', auto));
// Fonts
$text: -apple-system, BlinkMacSystemFont,

View File

@ -1,3 +1,6 @@
<a href="#" class="flex-container-row items-center dropdown-search-item" data-id="">
<span>{{ trans('settings.users_none_selected') }}</span>
</a>
@foreach($users as $user)
<a href="#" class="flex-container-row items-center dropdown-search-item" data-id="{{ $user->id }}">
<img class="avatar mr-m" src="{{ $user->getAvatar(30) }}" alt="{{ $user->name }}">

View File

@ -3,17 +3,17 @@
>
<input refs="user-select@input" type="hidden" name="{{ $name }}" value="{{ $user->id ?? '' }}">
<div refs="dropdown@toggle"
class="dropdown-search-toggle flex-container-row items-center"
class="dropdown-search-toggle {{ $compact ? 'compact' : '' }} flex-container-row items-center"
aria-haspopup="true" aria-expanded="false" tabindex="0">
<div refs="user-select@user-info" class="flex-container-row items-center px-s">
@if($user)
<img class="avatar mr-m" src="{{ $user->getAvatar(30) }}" alt="{{ $user->name }}">
<img class="avatar small mr-m" src="{{ $user->getAvatar($compact ? 22 : 30) }}" alt="{{ $user->name }}">
<span>{{ $user->name }}</span>
@else
<span>{{ trans('settings.users_none_selected') }}</span>
@endif
</div>
<span style="font-size: 1.5rem; margin-left: auto;">
<span style="font-size: {{ $compact ? '1.15rem' : '1.5rem' }}; margin-left: auto;">
@icon('caret-down')
</span>
</div>

View File

@ -15,7 +15,7 @@
<div>
<div class="form-group">
<label for="owner">{{ trans('entities.permissions_owner') }}</label>
@include('components.user-select', ['user' => $model->ownedBy, 'name' => 'owned_by'])
@include('components.user-select', ['user' => $model->ownedBy, 'name' => 'owned_by', 'compact' => false])
</div>
</div>
</div>

View File

@ -25,21 +25,27 @@
</ul>
</div>
@foreach(['date_from', 'date_to'] as $filterKey)
<form action="{{ url('/settings/audit') }}" method="get" class="block mr-m">
@foreach($listDetails as $param => $val)
@if(!empty($val) && $param !== $filterKey)
<input type="hidden" name="{{ $param }}" value="{{ $val }}">
@endif
@endforeach
<label for="audit_filter_{{ $filterKey }}">{{ trans('settings.audit_' . $filterKey) }}</label>
<input id="audit_filter_{{ $filterKey }}"
component="submit-on-change"
type="date"
name="{{ $filterKey }}"
value="{{ $listDetails[$filterKey] ?? '' }}">
</form>
@endforeach
<form action="{{ url('/settings/audit') }}" method="get" class="flex-container-row mr-m">
@if(!empty($listDetails['event']))
<input type="hidden" name="event" value="{{ $listDetails['event'] }}">
@endif
@foreach(['date_from', 'date_to'] as $filterKey)
<div class="mr-m">
<label for="audit_filter_{{ $filterKey }}">{{ trans('settings.audit_' . $filterKey) }}</label>
<input id="audit_filter_{{ $filterKey }}"
component="submit-on-change"
type="date"
name="{{ $filterKey }}"
value="{{ $listDetails[$filterKey] ?? '' }}">
</div>
@endforeach
<div class="form-group ml-auto" component="submit-on-change">
<label for="owner">{{ trans('settings.audit_table_user') }}</label>
@include('components.user-select', ['user' => $listDetails['user'] ? \BookStack\Auth\User::query()->find($listDetails['user']) : null, 'name' => 'user', 'compact' => true])
</div>
</form>
</div>
<hr class="mt-l mb-s">

View File

@ -20,7 +20,7 @@
<p class="small">{{ trans('settings.users_migrate_ownership_desc') }}</p>
</div>
<div>
@include('components.user-select', ['name' => 'new_owner_id', 'user' => null])
@include('components.user-select', ['name' => 'new_owner_id', 'user' => null, 'compact' => false])
</div>
</div>

View File

@ -4,6 +4,7 @@ use BookStack\Actions\Activity;
use BookStack\Actions\ActivityService;
use BookStack\Actions\ActivityType;
use BookStack\Auth\UserRepo;
use BookStack\Entities\Models\Chapter;
use BookStack\Entities\Tools\TrashCan;
use BookStack\Entities\Models\Page;
use BookStack\Entities\Repos\PageRepo;
@ -117,4 +118,26 @@ class AuditLogTest extends TestCase
$resp->assertDontSeeText($page->name);
}
public function test_user_filter()
{
$admin = $this->getAdmin();
$editor = $this->getEditor();
$this->actingAs($admin);
$page = Page::query()->first();
$this->activityService->addForEntity($page, ActivityType::PAGE_CREATE);
$this->actingAs($editor);
$chapter = Chapter::query()->first();
$this->activityService->addForEntity($chapter, ActivityType::CHAPTER_UPDATE);
$resp = $this->actingAs($admin)->get('settings/audit?user=' . $admin->id);
$resp->assertSeeText($page->name);
$resp->assertDontSeeText($chapter->name);
$resp = $this->actingAs($admin)->get('settings/audit?user=' . $editor->id);
$resp->assertSeeText($chapter->name);
$resp->assertDontSeeText($page->name);
}
}