Commit 40771e9c authored by Douglas Barbosa Alexandre's avatar Douglas Barbosa Alexandre

Merge branch 'dz-filter-audit-events' into 'master'

Add created_before/after filter to audit events

See merge request gitlab-org/gitlab!19035
parents 5ea88bf9 cb4e02ae
......@@ -2,6 +2,8 @@
class Admin::AuditLogsController < Admin::ApplicationController
before_action :check_license_admin_audit_log_available!
before_action :validate_date_params
PER_PAGE = 25
def index
......@@ -18,11 +20,23 @@ class Admin::AuditLogsController < Admin::ApplicationController
end
end
private
def audit_logs_params
params.permit(:entity_type, :entity_id)
params.permit(:entity_type, :entity_id, :created_before, :created_after)
end
def check_license_admin_audit_log_available!
render_404 unless License.feature_available?(:admin_audit_log)
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
......@@ -41,6 +41,20 @@
placeholder: @entity&.full_path || _('Search groups'), idAttribute: 'id',
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?
%table#events-table.table
%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
expect(page).to have_content('Removed user access')
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
def filter_by_type(type)
......
......@@ -128,6 +128,10 @@ module Gitlab
def aws_arn_regex_message
"must be a valid Amazon Resource Name"
end
def utc_date_regex
@utc_date_regex ||= /\A[0-9]{4}-[0-9]{2}-[0-9]{2}\z/.freeze
end
end
end
......
......@@ -4801,6 +4801,12 @@ msgstr ""
msgid "Created a branch and a merge request to resolve this issue."
msgstr ""
msgid "Created after"
msgstr ""
msgid "Created before"
msgstr ""
msgid "Created branch '%{branch_name}' and a merge request to resolve this issue."
msgstr ""
......@@ -9153,6 +9159,9 @@ msgstr ""
msgid "Invalid date"
msgstr ""
msgid "Invalid date format. Please use UTC format as YYYY-MM-DD"
msgstr ""
msgid "Invalid feature"
msgstr ""
......
......@@ -75,4 +75,14 @@ describe Gitlab::Regex do
it { is_expected.not_to match('123456789012') }
it { is_expected.not_to match('role/role-name') }
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
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