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

View File

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

View File

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

View File

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

View File

@ -28,7 +28,7 @@ $-xs: 6px;
$-xxs: 3px; $-xxs: 3px;
// List of our spacing sizes // 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 // Fonts
$text: -apple-system, BlinkMacSystemFont, $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) @foreach($users as $user)
<a href="#" class="flex-container-row items-center dropdown-search-item" data-id="{{ $user->id }}"> <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 }}"> <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 ?? '' }}"> <input refs="user-select@input" type="hidden" name="{{ $name }}" value="{{ $user->id ?? '' }}">
<div refs="dropdown@toggle" <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"> aria-haspopup="true" aria-expanded="false" tabindex="0">
<div refs="user-select@user-info" class="flex-container-row items-center px-s"> <div refs="user-select@user-info" class="flex-container-row items-center px-s">
@if($user) @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> <span>{{ $user->name }}</span>
@else @else
<span>{{ trans('settings.users_none_selected') }}</span> <span>{{ trans('settings.users_none_selected') }}</span>
@endif @endif
</div> </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') @icon('caret-down')
</span> </span>
</div> </div>

View File

@ -15,7 +15,7 @@
<div> <div>
<div class="form-group"> <div class="form-group">
<label for="owner">{{ trans('entities.permissions_owner') }}</label> <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> </div>
</div> </div>

View File

@ -25,21 +25,27 @@
</ul> </ul>
</div> </div>
@foreach(['date_from', 'date_to'] as $filterKey) <form action="{{ url('/settings/audit') }}" method="get" class="flex-container-row mr-m">
<form action="{{ url('/settings/audit') }}" method="get" class="block mr-m"> @if(!empty($listDetails['event']))
@foreach($listDetails as $param => $val) <input type="hidden" name="event" value="{{ $listDetails['event'] }}">
@if(!empty($val) && $param !== $filterKey) @endif
<input type="hidden" name="{{ $param }}" value="{{ $val }}">
@endif @foreach(['date_from', 'date_to'] as $filterKey)
@endforeach <div class="mr-m">
<label for="audit_filter_{{ $filterKey }}">{{ trans('settings.audit_' . $filterKey) }}</label> <label for="audit_filter_{{ $filterKey }}">{{ trans('settings.audit_' . $filterKey) }}</label>
<input id="audit_filter_{{ $filterKey }}" <input id="audit_filter_{{ $filterKey }}"
component="submit-on-change" component="submit-on-change"
type="date" type="date"
name="{{ $filterKey }}" name="{{ $filterKey }}"
value="{{ $listDetails[$filterKey] ?? '' }}"> value="{{ $listDetails[$filterKey] ?? '' }}">
</form> </div>
@endforeach @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> </div>
<hr class="mt-l mb-s"> <hr class="mt-l mb-s">

View File

@ -20,7 +20,7 @@
<p class="small">{{ trans('settings.users_migrate_ownership_desc') }}</p> <p class="small">{{ trans('settings.users_migrate_ownership_desc') }}</p>
</div> </div>
<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>
</div> </div>

View File

@ -4,6 +4,7 @@ use BookStack\Actions\Activity;
use BookStack\Actions\ActivityService; use BookStack\Actions\ActivityService;
use BookStack\Actions\ActivityType; use BookStack\Actions\ActivityType;
use BookStack\Auth\UserRepo; use BookStack\Auth\UserRepo;
use BookStack\Entities\Models\Chapter;
use BookStack\Entities\Tools\TrashCan; use BookStack\Entities\Tools\TrashCan;
use BookStack\Entities\Models\Page; use BookStack\Entities\Models\Page;
use BookStack\Entities\Repos\PageRepo; use BookStack\Entities\Repos\PageRepo;
@ -117,4 +118,26 @@ class AuditLogTest extends TestCase
$resp->assertDontSeeText($page->name); $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);
}
} }