Commit cb4e02ae authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets Committed by Douglas Barbosa Alexandre

Add before/after filter to admin audit events UI

Signed-off-by: default avatarDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
parent 5ea88bf9
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
class Admin::AuditLogsController < Admin::ApplicationController class Admin::AuditLogsController < Admin::ApplicationController
before_action :check_license_admin_audit_log_available! before_action :check_license_admin_audit_log_available!
before_action :validate_date_params
PER_PAGE = 25 PER_PAGE = 25
def index def index
...@@ -18,11 +20,23 @@ class Admin::AuditLogsController < Admin::ApplicationController ...@@ -18,11 +20,23 @@ class Admin::AuditLogsController < Admin::ApplicationController
end end
end end
private
def audit_logs_params def audit_logs_params
params.permit(:entity_type, :entity_id) params.permit(:entity_type, :entity_id, :created_before, :created_after)
end end
def check_license_admin_audit_log_available! def check_license_admin_audit_log_available!
render_404 unless License.feature_available?(:admin_audit_log) render_404 unless License.feature_available?(:admin_audit_log)
end end
def validate_date_params
unless valid_utc_date?(params[:created_before]) && valid_utc_date?(params[:created_after])
flash[:alert] = _('Invalid date format. Please use UTC format as YYYY-MM-DD')
end
end
def valid_utc_date?(date)
date.blank? || date =~ Gitlab::Regex.utc_date_regex
end
end end
...@@ -41,6 +41,20 @@ ...@@ -41,6 +41,20 @@
placeholder: @entity&.full_path || _('Search groups'), idAttribute: 'id', placeholder: @entity&.full_path || _('Search groups'), idAttribute: 'id',
data: { order_by: 'last_activity_at', idattribute: 'id', all_available: true } }) data: { order_by: 'last_activity_at', idattribute: 'id', all_available: true } })
.prepend-top-10
.form-group
= label_tag :created_after, _('Created after'), class: 'label-bold'
= date_field_tag :created_after, params[:created_after],
class: 'form-control', placeholder: 'YYYY-MM-DD'
.form-group
= label_tag :created_before, _('Created before'), class: 'label-bold'
= date_field_tag :created_before, params[:created_before],
class: 'form-control', placeholder: 'YYYY-MM-DD'
= submit_tag _('Search'), class: 'btn'
- if @events.present? - if @events.present?
%table#events-table.table %table#events-table.table
%thead %thead
......
---
title: Add created_before/after filter to audit events
merge_request: 19035
author:
type: changed
...@@ -106,6 +106,48 @@ describe 'Admin::AuditLogs', :js do ...@@ -106,6 +106,48 @@ describe 'Admin::AuditLogs', :js do
expect(page).to have_content('Removed user access') expect(page).to have_content('Removed user access')
end end
end end
describe 'filter by date', js: false do
set(:audit_event_1) { create(:user_audit_event, created_at: 5.days.ago) }
set(:audit_event_2) { create(:user_audit_event, created_at: 3.days.ago) }
set(:audit_event_3) { create(:user_audit_event, created_at: 1.day.ago) }
before do
visit admin_audit_logs_path
end
it 'shows only 2 days old events' do
page.within '.content' do
fill_in 'Created after', with: 4.days.ago
fill_in 'Created before', with: 2.days.ago
click_button 'Search'
end
expect(page).to have_content(audit_event_2.author_name)
expect(page).not_to have_content(audit_event_1.author_name)
expect(page).not_to have_content(audit_event_3.author_name)
end
it 'shows only yesterday events' do
page.within '.content' do
fill_in 'Created after', with: 2.days.ago
click_button 'Search'
end
expect(page).to have_content(audit_event_3.author_name)
expect(page).not_to have_content(audit_event_1.author_name)
expect(page).not_to have_content(audit_event_2.author_name)
end
it 'shows a message if provided date is invalid' do
page.within '.content' do
fill_in 'Created after', with: '12-345-6789'
click_button 'Search'
end
expect(page).to have_content('Invalid date format. Please use UTC format as YYYY-MM-DD')
end
end
end end
def filter_by_type(type) def filter_by_type(type)
......
...@@ -128,6 +128,10 @@ module Gitlab ...@@ -128,6 +128,10 @@ module Gitlab
def aws_arn_regex_message def aws_arn_regex_message
"must be a valid Amazon Resource Name" "must be a valid Amazon Resource Name"
end end
def utc_date_regex
@utc_date_regex ||= /\A[0-9]{4}-[0-9]{2}-[0-9]{2}\z/.freeze
end
end end
end end
......
...@@ -4801,6 +4801,12 @@ msgstr "" ...@@ -4801,6 +4801,12 @@ msgstr ""
msgid "Created a branch and a merge request to resolve this issue." msgid "Created a branch and a merge request to resolve this issue."
msgstr "" msgstr ""
msgid "Created after"
msgstr ""
msgid "Created before"
msgstr ""
msgid "Created branch '%{branch_name}' and a merge request to resolve this issue." msgid "Created branch '%{branch_name}' and a merge request to resolve this issue."
msgstr "" msgstr ""
...@@ -9153,6 +9159,9 @@ msgstr "" ...@@ -9153,6 +9159,9 @@ msgstr ""
msgid "Invalid date" msgid "Invalid date"
msgstr "" msgstr ""
msgid "Invalid date format. Please use UTC format as YYYY-MM-DD"
msgstr ""
msgid "Invalid feature" msgid "Invalid feature"
msgstr "" msgstr ""
......
...@@ -75,4 +75,14 @@ describe Gitlab::Regex do ...@@ -75,4 +75,14 @@ describe Gitlab::Regex do
it { is_expected.not_to match('123456789012') } it { is_expected.not_to match('123456789012') }
it { is_expected.not_to match('role/role-name') } it { is_expected.not_to match('role/role-name') }
end end
describe '.utc_date_regex' do
subject { described_class.utc_date_regex }
it { is_expected.to match('2019-10-20') }
it { is_expected.to match('1990-01-01') }
it { is_expected.not_to match('11-1234-90') }
it { is_expected.not_to match('aa-1234-cc') }
it { is_expected.not_to match('9/9/2018') }
end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment