lesli_audit 1.1.2 → 1.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. checksums.yaml +4 -4
  2. data/app/assets/stylesheets/lesli_audit/application.css +183 -0
  3. data/app/controllers/lesli_audit/analytics_controller.rb +16 -0
  4. data/app/controllers/lesli_audit/logs_controller.rb +12 -2
  5. data/app/controllers/lesli_audit/traffic_controller.rb +25 -0
  6. data/app/controllers/lesli_audit/users_controller.rb +7 -4
  7. data/app/interfaces/lesli_audit/logger_interface.rb +5 -4
  8. data/app/jobs/lesli_audit/cloudflare_traffic_sync_job.rb +78 -0
  9. data/app/models/lesli_audit/account.rb +12 -0
  10. data/app/models/lesli_audit/cloudflare_traffic_sync.rb +99 -0
  11. data/app/models/lesli_audit/traffic_breakdown.rb +38 -0
  12. data/app/models/lesli_audit/traffic_rollup.rb +33 -0
  13. data/app/models/lesli_audit/user_journal.rb +0 -2
  14. data/app/services/lesli_audit/activity_audit.rb +8 -4
  15. data/app/services/lesli_audit/analytics_service.rb +237 -0
  16. data/app/services/lesli_audit/cloudflare/configuration.rb +92 -0
  17. data/app/services/lesli_audit/cloudflare/graphql_client.rb +309 -0
  18. data/app/services/lesli_audit/cloudflare/snapshot_converter.rb +101 -0
  19. data/app/services/lesli_audit/cloudflare/sync_service.rb +79 -0
  20. data/app/services/lesli_audit/cloudflare_traffic_snapshot.rb +182 -0
  21. data/app/services/lesli_audit/log_service.rb +99 -5
  22. data/app/services/lesli_audit/request_service.rb +73 -10
  23. data/app/services/lesli_audit/retention_cleanup.rb +16 -16
  24. data/app/services/lesli_audit/traffic_service.rb +216 -0
  25. data/app/services/lesli_audit/traffic_snapshot_importer.rb +120 -0
  26. data/app/services/lesli_audit/user_service.rb +119 -152
  27. data/app/views/lesli_audit/{visitors → analytics}/index.html.erb +40 -30
  28. data/app/views/lesli_audit/logs/index.html.erb +88 -19
  29. data/app/views/lesli_audit/logs/show.html.erb +36 -8
  30. data/app/views/lesli_audit/partials/_navigation.html.erb +5 -4
  31. data/app/views/lesli_audit/requests/index.html.erb +80 -19
  32. data/app/views/lesli_audit/traffic/_breakdown.html.erb +46 -0
  33. data/app/views/lesli_audit/traffic/index.html.erb +177 -0
  34. data/app/views/lesli_audit/users/index.html.erb +97 -40
  35. data/config/locales/translations.en.yml +137 -1
  36. data/config/locales/translations.es.yml +137 -1
  37. data/config/locales/translations.fr.yml +132 -1
  38. data/config/locales/translations.it.yml +132 -1
  39. data/config/locales/translations.pt.yml +132 -1
  40. data/config/routes.rb +6 -23
  41. data/db/migrate/v1.0/0501110110_create_lesli_audit_account_logs.rb +2 -1
  42. data/db/migrate/v1.0/0501120110_create_lesli_audit_user_logs.rb +2 -1
  43. data/db/migrate/v1.0/0501120210_create_lesli_audit_user_journals.rb +0 -3
  44. data/db/migrate/v1.0/0501130110_create_lesli_audit_traffic_rollups.rb +55 -0
  45. data/db/migrate/v1.0/0501130210_create_lesli_audit_traffic_breakdowns.rb +41 -0
  46. data/db/migrate/v1.0/0501130310_create_lesli_audit_cloudflare_traffic_syncs.rb +42 -0
  47. data/db/seed/devices.rb +2 -1
  48. data/db/seed/traffic.rb +128 -0
  49. data/db/seed/users.rb +68 -27
  50. data/db/seeds.rb +3 -2
  51. data/lib/lesli_audit/version.rb +2 -2
  52. data/lib/tasks/lesli_audit_cloudflare_traffic.rake +16 -0
  53. data/readme.md +10 -0
  54. metadata +24 -5
  55. data/app/controllers/lesli_audit/visitors_controller.rb +0 -14
  56. data/app/services/lesli_audit/visitor_service.rb +0 -212
@@ -27,191 +27,158 @@ Building a better future, one line of code at a time.
27
27
  @license GPLv3 http://www.gnu.org/licenses/gpl-3.0.en.html
28
28
 
29
29
  // · ~·~ ~·~ ~·~ ~·~ ~·~ ~·~ ~·~ ~·~ ~·~
30
- // ·
30
+ // ·
31
31
  =end
32
32
 
33
33
  module LesliAudit
34
34
  class UserService < Lesli::ApplicationLesliService
35
+ REGISTRATION_MONTHS = 12
36
+ ACTIVITY_DAYS = 30
37
+ ACTIVITY_LIMIT = 20
35
38
 
36
- def users
37
- users = current_user.account.users
38
- users = users.with_deleted if users.respond_to?(:with_deleted)
39
-
40
- now = Time.current
41
- last_30_days = 30.days.ago
42
- quoted_false = ActiveRecord::Base.connection.quote(false)
43
-
44
- stats = users.pick(
45
- # total users
46
- Arel.sql("COUNT(*)"),
47
-
48
- # total deactivated users
49
- Arel.sql("SUM(CASE WHEN active = #{quoted_false} AND deleted_at IS NULL THEN 1 ELSE 0 END)"),
50
-
51
- # total locked users
52
- Arel.sql(
53
- "SUM(CASE WHEN (
54
- locked_at IS NOT NULL
55
- OR locked_until > #{ActiveRecord::Base.connection.quote(now)}
56
- ) AND deleted_at IS NULL THEN 1 ELSE 0 END)"
57
- ),
58
-
59
- # total not confirmed users
60
- Arel.sql("SUM(CASE WHEN confirmed_at IS NULL AND deleted_at IS NULL THEN 1 ELSE 0 END)"),
61
-
62
- # total deleted users
63
- Arel.sql("SUM(CASE WHEN deleted_at IS NOT NULL THEN 1 ELSE 0 END)"),
64
-
65
- # users that never signed in
66
- Arel.sql("SUM(CASE WHEN last_sign_in_at IS NULL AND deleted_at IS NULL THEN 1 ELSE 0 END)"),
67
-
68
- # total active users for the last 30 days
69
- Arel.sql("SUM(CASE WHEN last_sign_in_at >= #{ActiveRecord::Base.connection.quote(last_30_days)} AND deleted_at IS NULL THEN 1 ELSE 0 END)"),
70
-
71
- # users with failed logins
72
- Arel.sql("SUM(CASE WHEN failed_attempts > 0 AND deleted_at IS NULL THEN 1 ELSE 0 END)"),
73
- )
39
+ def dashboard
40
+ {
41
+ summary: summary,
42
+ attention: attention,
43
+ registrations: registrations,
44
+ working_hours: working_hours
45
+ }
46
+ end
74
47
 
75
- summary = {
76
- total_users: stats[0].to_i,
77
- total_users_deactivated: stats[1].to_i,
78
- total_users_locked: stats[2].to_i,
79
- total_users_unconfirmed: stats[3].to_i,
80
- total_users_deleted: stats[4].to_i,
48
+ private
81
49
 
82
- total_users_never_signed_in: stats[5].to_i,
83
- total_users_active_last_30_days: stats[6].to_i,
84
- total_users_with_failed_attempts: stats[7].to_i
50
+ def summary
51
+ {
52
+ registered: all_users.count,
53
+ enabled: current_users.where(active: true).count,
54
+ active_last_30_days: current_users.where(last_sign_in_at: ACTIVITY_DAYS.days.ago..).count,
55
+ deactivated: current_users.where(active: false).count,
56
+ deleted: all_users.where.not(deleted_at: nil).count
85
57
  }
58
+ end
86
59
 
60
+ def attention
87
61
  {
88
- overview: [{
89
- label: "Total users",
90
- value: summary[:total_users],
91
- percentage: 100,
92
- color: "info"
93
- }, {
94
- label: "Deactivated users",
95
- value: summary[:total_users_deactivated],
96
- percentage: percentage(summary[:total_users_deactivated], summary[:total_users]),
97
- color: "warning"
98
- }, {
99
- label: "Locked users",
100
- value: summary[:total_users_locked],
101
- percentage: percentage(summary[:total_users_locked], summary[:total_users]),
102
- color: "danger"
103
- }, {
104
- label: "Unconfirmed users",
105
- value: summary[:total_users_unconfirmed],
106
- percentage: percentage(summary[:total_users_unconfirmed], summary[:total_users]),
107
- color: "warning",
108
- }, {
109
- label: "Deleted users",
110
- value: summary[:total_users_deleted],
111
- percentage: percentage(summary[:total_users_deleted], summary[:total_users]),
112
- color: "danger"
113
- }],
114
- attention_needed: [{
115
- label: "Unconfirmed users",
116
- value: summary[:total_users_unconfirmed],
117
- color: "info",
118
- icon: "user-add-line"
119
- }, {
120
- label: "Never signed in",
121
- value: summary[:total_users_never_signed_in],
122
- color: "warning",
123
- icon: "time-line"
124
- }, {
125
- label: "Failed login attempts",
126
- value: summary[:total_users_with_failed_attempts],
127
- color: "danger",
128
- icon: "shield-keyhole-line"
129
- }, {
130
- label: "Locked users",
131
- value: summary[:total_users_locked],
132
- color: "danger",
133
- icon: "lock-line"
134
- }]
62
+ unconfirmed: current_users.where(confirmed_at: nil).count,
63
+ never_signed_in: current_users.where(last_sign_in_at: nil).count,
64
+ failed_sign_ins: current_users.where(failed_attempts: 1..).count,
65
+ locked: locked_users.count
135
66
  }
136
67
  end
137
68
 
138
- def working_hours
69
+ def locked_users
70
+ locked_at = current_users.where.not(locked_at: nil)
71
+ locked_until = current_users.where(locked_until: Time.current..)
139
72
 
140
- group = 'day'
73
+ locked_at.or(locked_until)
74
+ end
141
75
 
142
- group_by = "DATE_TRUNC('month', lesli_audit_user_journals.date)" if group == 'month'
143
- group_by = "DATE_TRUNC('week', lesli_audit_user_journals.date)" if group == 'week'
144
- group_by = "DATE_TRUNC('day', lesli_audit_user_journals.date)" if group == 'day'
76
+ def registrations
77
+ counts = all_users
78
+ .where(created_at: registration_start.beginning_of_day..)
79
+ .group(Arel.sql(registration_month_group))
80
+ .count
81
+ .to_h do |month, count|
82
+ [ month.to_date.beginning_of_month, count ]
83
+ end
145
84
 
146
- # compatibility for SQLite
147
- if ActiveRecord::Base.connection.adapter_name == "SQLite"
148
- group_by = "strftime('%Y-%m-%d', lesli_audit_user_journals.date)"
85
+ registration_months.map do |month|
86
+ {
87
+ "xaxiskey" => month.iso8601,
88
+ "yaxiskey" => counts.fetch(month, 0)
89
+ }
149
90
  end
91
+ end
150
92
 
151
- user_journals = current_user.account.users
152
- .joins(:journals)
153
- .merge(LesliAudit::UserJournal.retained.processed)
154
-
155
- user_journals = user_journals.select(
156
- "lesli_users.id as user_id",
157
- "lesli_users.email as user_email",
158
- "#{group_by} date",
159
- "min(lesli_audit_user_journals.created_at) as first_activity",
160
- "max(lesli_audit_user_journals.created_at) as last_activity",
161
- "count(lesli_audit_user_journals.id) requests"
162
- )
163
-
164
- user_journals = user_journals.group("lesli_users.id", group_by)
165
-
166
- user_journals.map do |request|
167
- request[:first_activity] = Date2.new(request[:first_activity]).time
168
- request[:last_activity] = Date2.new(request[:last_activity]).time
169
- request
93
+ def registration_months
94
+ REGISTRATION_MONTHS.times.map do |offset|
95
+ registration_start >> offset
170
96
  end
171
-
172
- user_journals.reverse
173
97
  end
174
98
 
175
- def registrations
99
+ def registration_start
100
+ (Date.current << (REGISTRATION_MONTHS - 1)).beginning_of_month
101
+ end
176
102
 
177
- #Get filters from the request
178
- group = query[:group]
179
- group = "month"
103
+ # PostgreSQL and SQLite expose different date-grouping functions. Both
104
+ # expressions return a month that is normalized to a Date immediately.
105
+ def registration_month_group
106
+ return "strftime('%Y-%m-01', lesli_users.created_at)" if sqlite?
180
107
 
181
- #Get period only if the request have filters
182
- period = group unless group.blank?
108
+ "DATE_TRUNC('month', lesli_users.created_at)"
109
+ end
183
110
 
184
- group_by = "DATE_TRUNC('month', created_at)" if group == 'month'
185
- group_by = "DATE_TRUNC('week', created_at)" if group == 'week'
186
- group_by = "DATE_TRUNC('day', created_at)" if group == 'day'
111
+ def working_hours
112
+ # Journals are reduced to a daily boundary instead of exposed as a
113
+ # request list. Requests keeps ownership of endpoint-level detail.
114
+ journals = audit_account.user_journals
115
+ .retained
116
+ .where(date: activity_start..Date.current)
117
+
118
+ account_users
119
+ .joins(:journals)
120
+ .merge(journals)
121
+ .select(
122
+ "lesli_users.id AS user_id",
123
+ "lesli_users.email AS user_email",
124
+ "#{activity_day_group} AS activity_date",
125
+ "MIN(lesli_audit_user_journals.created_at) AS first_activity",
126
+ "MAX(lesli_audit_user_journals.created_at) AS last_activity"
127
+ )
128
+ .group("lesli_users.id", "lesli_users.email", Arel.sql(activity_day_group))
129
+ .order(Arel.sql("#{activity_day_group} DESC"), "lesli_users.email ASC")
130
+ .limit(ACTIVITY_LIMIT)
131
+ .map { |activity| normalize_activity(activity) }
132
+ end
187
133
 
188
- # compatibility for SQLite
189
- if ActiveRecord::Base.connection.adapter_name == "SQLite"
190
- group_by = "strftime('%Y-%m', created_at)"
191
- end
134
+ def activity_start
135
+ (ACTIVITY_DAYS - 1).days.ago.to_date
136
+ end
192
137
 
193
- registrations = []
138
+ def activity_day_group
139
+ return "strftime('%Y-%m-%d', lesli_audit_user_journals.date)" if sqlite?
194
140
 
195
- if ["day", "week", "month", "year"].include?(period)
141
+ "DATE_TRUNC('day', lesli_audit_user_journals.date)"
142
+ end
196
143
 
197
- registrations = current_user.account.users
198
- .group(group_by)
199
- .count.map do |request|
200
- {
201
- :xaxiskey => request[0],
202
- :yaxiskey => request[1]
203
- }
204
- end
144
+ def normalize_activity(activity)
145
+ {
146
+ user_id: activity[:user_id],
147
+ user_email: activity[:user_email],
148
+ date: activity[:activity_date].to_date,
149
+ first_activity: normalize_time(activity[:first_activity]),
150
+ last_activity: normalize_time(activity[:last_activity])
151
+ }
152
+ end
153
+
154
+ def normalize_time(value)
155
+ time = if value.respond_to?(:in_time_zone)
156
+ value.in_time_zone
157
+ else
158
+ Time.zone.parse(value.to_s)
205
159
  end
206
160
 
207
- registrations
161
+ time.strftime("%H:%M")
208
162
  end
209
163
 
210
- private
164
+ def account_users
165
+ current_user.account.users
166
+ end
167
+
168
+ def all_users
169
+ @all_users ||= account_users.with_deleted
170
+ end
171
+
172
+ def current_users
173
+ @current_users ||= all_users.where(deleted_at: nil)
174
+ end
175
+
176
+ def audit_account
177
+ @audit_account ||= LesliAudit::Account.find_by!(account_id: current_user.account_id)
178
+ end
211
179
 
212
- def percentage(value, total)
213
- return 0 if total.to_i.zero?
214
- ((value.to_f / total) * 100).round
180
+ def sqlite?
181
+ ActiveRecord::Base.connection.adapter_name == "SQLite"
215
182
  end
216
183
  end
217
184
  end
@@ -1,26 +1,32 @@
1
1
 
2
2
  <%
3
- traffic = @traffic.reverse
4
- top_days = traffic.last(3)
3
+ request_trend = @request_trend.reverse
4
+ top_days = request_trend.last(3)
5
5
  %>
6
6
 
7
7
  <%= render(LesliView::Layout::Container.new("audit-analytics")) do %>
8
- <%= render(LesliView::Components::Header.new("Analytics")) %>
8
+ <%= render(LesliView::Components::Header.new(
9
+ t("lesli_audit.analytics.title", default: "Analytics")
10
+ )) %>
9
11
 
10
12
  <section
11
13
  class="grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-5"
12
- aria-label="Traffic overview">
14
+ aria-label="<%= t("lesli_audit.analytics.overview", default: "Authenticated usage overview") %>">
13
15
  <article class="rounded-2xl border border-slate-200 bg-white p-5 shadow-sm">
14
- <h2 class="text-sm font-medium text-slate-500">Controllers</h2>
16
+ <h2 class="text-sm font-medium text-slate-500">
17
+ <%= t("lesli_audit.analytics.controllers", default: "Controllers") %>
18
+ </h2>
15
19
  <p class="mt-3 text-3xl font-semibold tracking-tight text-slate-900 tabular-nums">
16
- <%= @visits[:controllers] %>
20
+ <%= @summary[:controllers] %>
17
21
  </p>
18
22
  </article>
19
23
 
20
24
  <article class="rounded-2xl border border-slate-200 bg-white p-5 shadow-sm">
21
- <h2 class="text-sm font-medium text-slate-500">Requests</h2>
25
+ <h2 class="text-sm font-medium text-slate-500">
26
+ <%= t("lesli_audit.analytics.requests", default: "Requests") %>
27
+ </h2>
22
28
  <p class="mt-3 text-3xl font-semibold tracking-tight text-slate-900 tabular-nums">
23
- <%= @visits[:requests] %>
29
+ <%= @summary[:requests] %>
24
30
  </p>
25
31
  </article>
26
32
 
@@ -36,21 +42,23 @@ top_days = traffic.last(3)
36
42
 
37
43
  <section class="mt-6 rounded-2xl border border-slate-200 bg-white p-5 shadow-sm">
38
44
  <%= render(LesliView::Charts::Line.new(
39
- database_to_dataset: traffic,
40
- title: "Requests"
45
+ database_to_dataset: request_trend,
46
+ title: t("lesli_audit.analytics.requests", default: "Requests")
41
47
  )) %>
42
48
  </section>
43
49
 
44
- <section class="mt-6 grid grid-cols-1 gap-6 lg:grid-cols-3" aria-label="Request traffic breakdown">
50
+ <section
51
+ class="mt-6 grid grid-cols-1 gap-6 lg:grid-cols-3"
52
+ aria-label="<%= t("lesli_audit.analytics.login_breakdown", default: "Successful login breakdown") %>">
45
53
  <article class="min-w-0">
46
54
  <%= render(LesliView::Elements::Table.new(
47
55
  columns: [
48
- { label: "Browsers", align: :left },
49
- { label: "Requests", align: :center }
56
+ { label: t("lesli_audit.analytics.browsers", default: "Browsers"), align: :left },
57
+ { label: t("lesli_audit.analytics.logins", default: "Logins"), align: :center }
50
58
  ],
51
59
  class_name: "!min-w-0",
52
60
  compact: true,
53
- caption: "Requests by browser"
61
+ caption: t("lesli_audit.analytics.logins_by_browser", default: "Successful logins by browser")
54
62
  )) do |table| %>
55
63
  <% @browsers.each do |browser| %>
56
64
  <% percentage = browser[:percentage].to_i.clamp(0, 100) %>
@@ -80,15 +88,15 @@ top_days = traffic.last(3)
80
88
  <article class="min-w-0">
81
89
  <%= render(LesliView::Elements::Table.new(
82
90
  columns: [
83
- { label: "OS", align: :left },
84
- { label: "Requests", align: :center }
91
+ { label: t("lesli_audit.analytics.operating_systems", default: "OS"), align: :left },
92
+ { label: t("lesli_audit.analytics.logins", default: "Logins"), align: :center }
85
93
  ],
86
94
  class_name: "!min-w-0",
87
95
  compact: true,
88
- caption: "Requests by operating system"
96
+ caption: t("lesli_audit.analytics.logins_by_operating_system", default: "Successful logins by operating system")
89
97
  )) do |table| %>
90
- <% @os.each do |device| %>
91
- <% percentage = device[:percentage].to_i.clamp(0, 100) %>
98
+ <% @operating_systems.each do |operating_system| %>
99
+ <% percentage = operating_system[:percentage].to_i.clamp(0, 100) %>
92
100
  <% table.with_row do |row| %>
93
101
  <% row.with_cell(class_name: "!p-0") do %>
94
102
  <div class="relative isolate overflow-hidden px-3 py-2.5">
@@ -98,14 +106,14 @@ top_days = traffic.last(3)
98
106
  aria-hidden="true"></span>
99
107
  <span class="flex items-center gap-2 text-slate-700">
100
108
  <span class="inline-flex h-7 w-7 shrink-0 items-center justify-center text-slate-500">
101
- <span class="ri-<%= device[:icon].downcase %> text-lg leading-none" aria-hidden="true"></span>
109
+ <span class="ri-<%= operating_system[:icon].downcase %> text-lg leading-none" aria-hidden="true"></span>
102
110
  </span>
103
- <span class="truncate"><%= device[:device] %></span>
111
+ <span class="truncate"><%= operating_system[:operating_system] %></span>
104
112
  </span>
105
113
  </div>
106
114
  <% end %>
107
115
  <% row.with_cell(class_name: "font-semibold tabular-nums text-slate-900", align: :center) do %>
108
- <%= device[:count] %>
116
+ <%= operating_system[:count] %>
109
117
  <% end %>
110
118
  <% end %>
111
119
  <% end %>
@@ -115,12 +123,12 @@ top_days = traffic.last(3)
115
123
  <article class="min-w-0">
116
124
  <%= render(LesliView::Elements::Table.new(
117
125
  columns: [
118
- { label: "Device", align: :left },
119
- { label: "Requests", align: :center }
126
+ { label: t("lesli_audit.analytics.devices", default: "Devices"), align: :left },
127
+ { label: t("lesli_audit.analytics.logins", default: "Logins"), align: :center }
120
128
  ],
121
129
  class_name: "!min-w-0",
122
130
  compact: true,
123
- caption: "Requests by device"
131
+ caption: t("lesli_audit.analytics.logins_by_device", default: "Successful logins by device")
124
132
  )) do |table| %>
125
133
  <% @devices.each do |device| %>
126
134
  <% percentage = device[:percentage].to_i.clamp(0, 100) %>
@@ -148,17 +156,19 @@ top_days = traffic.last(3)
148
156
  </article>
149
157
  </section>
150
158
 
151
- <section class="mt-6 pb-8" aria-label="Top controllers">
159
+ <section
160
+ class="mt-6 pb-8"
161
+ aria-label="<%= t("lesli_audit.analytics.top_controllers", default: "Top controllers") %>">
152
162
  <%= render(LesliView::Elements::Table.new(
153
163
  columns: [{
154
- label: "Top controllers",
164
+ label: t("lesli_audit.analytics.top_controllers", default: "Top controllers"),
155
165
  field: "request_controller"
156
166
  }, {
157
- label: "Requests",
167
+ label: t("lesli_audit.analytics.requests", default: "Requests"),
158
168
  field: "requests"
159
169
  }],
160
- records: @requests,
161
- caption: "Requests by controller"
170
+ records: @top_controllers,
171
+ caption: t("lesli_audit.analytics.requests_by_controller", default: "Requests by controller")
162
172
  )) %>
163
173
  </section>
164
174
  <% end %>
@@ -1,23 +1,92 @@
1
1
  <%= render(LesliView::Layout::Container.new("audit-logs")) do %>
2
- <%= render(LesliView::Components::Header.new("User logs")) %>
3
- <%= render LesliView::Components::Toolbar.new() %>
2
+ <%= render(LesliView::Components::Header.new(
3
+ t("lesli_audit.logs.title", default: "Logs"),
4
+ t("lesli_audit.logs.subtitle", default: "Read-only business and security events")
5
+ )) %>
6
+
7
+ <%= render(LesliView::Components::Toolbar.new(
8
+ t("lesli_audit.logs.search_placeholder", default: "Search by operation, actor, source, or subject"),
9
+ url: lesli_audit.logs_path
10
+ )) do |toolbar| %>
11
+ <% toolbar.with_filters do %>
12
+ <%= label_tag :kind, t("lesli_audit.logs.event_type", default: "Event type"), class: "sr-only" %>
13
+ <%= select_tag :kind, options_for_select(
14
+ @available_log_kinds.map do |kind|
15
+ [t("lesli_audit.logs.kinds_#{kind}", default: kind.humanize), kind]
16
+ end,
17
+ @log_kind
18
+ ), class: "rounded-md border border-slate-300 bg-white px-3 py-2 text-sm text-slate-700" %>
19
+
20
+ <%= label_tag :orderBy, t("lesli_audit.logs.sort_by", default: "Sort by"), class: "sr-only" %>
21
+ <%= select_tag :orderBy, options_for_select([
22
+ [t("lesli_audit.logs.fields_created_at", default: "Date"), "created_at"],
23
+ [t("lesli_audit.logs.fields_operation", default: "Operation"), "operation"],
24
+ [t("lesli_audit.logs.fields_source", default: "Source"), "source"],
25
+ [t("lesli_audit.logs.fields_actor_uid", default: "Actor"), "actor_uid"]
26
+ ], params[:orderBy].presence || "created_at"),
27
+ class: "rounded-md border border-slate-300 bg-white px-3 py-2 text-sm text-slate-700" %>
28
+
29
+ <%= label_tag :order, t("lesli_audit.logs.direction", default: "Direction"), class: "sr-only" %>
30
+ <%= select_tag :order, options_for_select([
31
+ [t("lesli_audit.logs.descending", default: "Descending"), "desc"],
32
+ [t("lesli_audit.logs.ascending", default: "Ascending"), "asc"]
33
+ ], params[:order].presence || "desc"),
34
+ class: "rounded-md border border-slate-300 bg-white px-3 py-2 text-sm text-slate-700" %>
35
+
36
+ <%= label_tag :perPage, t("lesli_audit.logs.per_page", default: "Results per page"), class: "sr-only" %>
37
+ <%= select_tag :perPage, options_for_select([12, 25, 50, 100], params[:perPage].presence || 12),
38
+ class: "rounded-md border border-slate-300 bg-white px-3 py-2 text-sm text-slate-700" %>
39
+ <% end %>
40
+ <% end %>
41
+
4
42
  <%= render(LesliView::Elements::Table.new(
5
- columns: [{
6
- label: "Module",
7
- field: "engine"
8
- },{
9
- label:"Action",
10
- field:"action"
11
- },{
12
- label:"Operation",
13
- field:"operation"
14
- },{
15
- label:"Description",
16
- field:"description"
17
- },{
18
- label:"Date",
19
- field:"created_at"
20
- }],
21
- records: @logs[:records]
43
+ columns: [
44
+ {
45
+ label: t("lesli_audit.logs.fields_operation", default: "Operation"),
46
+ field: "operation"
47
+ }, {
48
+ label: t("lesli_audit.logs.fields_actor_uid", default: "Actor"),
49
+ field: "actor_uid"
50
+ }, {
51
+ label: t("lesli_audit.logs.fields_source", default: "Source"),
52
+ field: "source"
53
+ }, {
54
+ label: t("lesli_audit.logs.fields_description", default: "Description"),
55
+ field: "description"
56
+ }, {
57
+ label: t("lesli_audit.logs.fields_created_at", default: "Date"),
58
+ field: "created_at"
59
+ }
60
+ ],
61
+ records: @logs[:records],
62
+ link: ->(log) { lesli_audit.log_path(log, kind: @log_kind) },
63
+ caption: t("lesli_audit.logs.captions_#{@log_kind}", default: "Audit events"),
64
+ empty: t("lesli_audit.logs.empty", default: "No audit events found")
22
65
  )) %>
66
+
67
+ <% pagination = @logs.fetch(:pagination) %>
68
+ <% if pagination[:total].positive? %>
69
+ <nav class="mt-4 flex items-center justify-between gap-4" aria-label="<%= t("lesli_audit.logs.pagination", default: "Logs pagination") %>">
70
+ <div>
71
+ <% if pagination[:page] > 1 %>
72
+ <%= link_to t("lesli_audit.logs.previous", default: "Previous"),
73
+ lesli_audit.logs_path(request.query_parameters.merge(page: pagination[:page] - 1)),
74
+ class: "text-sm font-medium text-sky-700 hover:text-sky-900" %>
75
+ <% end %>
76
+ </div>
77
+
78
+ <p class="text-sm text-slate-600">
79
+ <%= t("lesli_audit.logs.page", default: "Page %{page} of %{pages}",
80
+ page: pagination[:page], pages: pagination[:pages]) %>
81
+ </p>
82
+
83
+ <div>
84
+ <% if pagination[:page] < pagination[:pages] %>
85
+ <%= link_to t("lesli_audit.logs.next", default: "Next"),
86
+ lesli_audit.logs_path(request.query_parameters.merge(page: pagination[:page] + 1)),
87
+ class: "text-sm font-medium text-sky-700 hover:text-sky-900" %>
88
+ <% end %>
89
+ </div>
90
+ </nav>
91
+ <% end %>
23
92
  <% end %>
@@ -1,16 +1,44 @@
1
1
  <%= render(LesliView::Layout::Container.new("audit-log")) do %>
2
- <%= render(LesliView::Components::Header.new("Audit event")) %>
2
+ <%= render(LesliView::Components::Header.new(
3
+ t("lesli_audit.logs.show_title", default: "Audit event"),
4
+ t("lesli_audit.logs.kinds_#{@log_kind}", default: @log_kind.humanize),
5
+ back: lesli_audit.logs_path(kind: @log_kind)
6
+ )) %>
7
+
8
+ <% fields = %i[
9
+ engine
10
+ source
11
+ action
12
+ operation
13
+ description
14
+ actor_uid
15
+ session_id
16
+ subject_type
17
+ subject_id
18
+ created_at
19
+ ] %>
3
20
 
4
21
  <dl class="grid grid-cols-1 gap-4 rounded-2xl border border-slate-200 bg-white p-5 shadow-sm sm:grid-cols-2">
5
- <% %i[engine source action operation description actor_uid session_id created_at].each do |field| %>
22
+ <% fields.each do |field| %>
6
23
  <div>
7
- <dt class="text-sm font-medium text-slate-500"><%= field.to_s.humanize %></dt>
8
- <dd class="mt-1 break-words text-sm text-slate-900"><%= @log.public_send(field) %></dd>
24
+ <dt class="text-sm font-medium text-slate-500">
25
+ <%= t("lesli_audit.logs.fields_#{field}", default: field.to_s.humanize) %>
26
+ </dt>
27
+ <dd class="mt-1 break-words text-sm text-slate-900">
28
+ <%= @log.public_send(field).presence || t("lesli_audit.logs.not_available", default: "Not available") %>
29
+ </dd>
9
30
  </div>
10
31
  <% end %>
11
- </dl>
12
32
 
13
- <div class="mt-6">
14
- <%= link_to "Back to logs", logs_path, class: "text-sm font-medium text-sky-700 hover:text-sky-900" %>
15
- </div>
33
+ <% if @log.details.present? %>
34
+ <div class="sm:col-span-2">
35
+ <dt class="text-sm font-medium text-slate-500">
36
+ <%= t("lesli_audit.logs.fields_details") %>
37
+ </dt>
38
+ <dd class="mt-1 text-sm text-slate-900">
39
+ <pre class="whitespace-pre-wrap break-words"><%= JSON.pretty_generate(@log.details) %></pre>
40
+ </dd>
41
+ </div>
42
+ <% end %>
43
+ </dl>
16
44
  <% end %>
@@ -32,7 +32,8 @@ Building a better future, one line of code at a time.
32
32
  =end
33
33
  %>
34
34
 
35
- <%= navigation_item(lesli_audit.users_path, 'Users', 'ri-team-line'); %>
36
- <%= navigation_item(lesli_audit.analytics_path, 'Traffic', 'ri-line-chart-fill'); %>
37
- <%= navigation_item(lesli_audit.requests_path, 'Requests', 'ri-arrow-left-right-fill'); %>
38
- <%= navigation_item(lesli_audit.logs_path, 'Logs', 'ri-history-line'); %>
35
+ <%= navigation_item(lesli_audit.users_path, I18n.t("lesli_audit.users.navigation", default: "Users"), 'ri-team-line'); %>
36
+ <%= navigation_item(lesli_audit.analytics_path, I18n.t("lesli_audit.analytics.navigation", default: "Analytics"), 'ri-line-chart-fill'); %>
37
+ <%= navigation_item(lesli_audit.traffic_index_path, I18n.t("lesli_audit.traffic.navigation", default: "Traffic"), 'ri-global-line'); %>
38
+ <%= navigation_item(lesli_audit.requests_path, I18n.t("lesli_audit.requests.navigation", default: "Requests"), 'ri-arrow-left-right-fill'); %>
39
+ <%= navigation_item(lesli_audit.logs_path, I18n.t("lesli_audit.logs.navigation", default: "Logs"), 'ri-history-line'); %>