moduler 0.8.0 → 0.9.0

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 (40) hide show
  1. checksums.yaml +4 -4
  2. data/app/assets/builds/moduler/tailwind.css +62 -0
  3. data/app/controllers/concerns/moduler/user_time_zone.rb +18 -0
  4. data/app/controllers/moduler/administration/audit_events_controller.rb +22 -0
  5. data/app/controllers/moduler/administration/memberships_controller.rb +3 -0
  6. data/app/controllers/moduler/administration/organizations_controller.rb +2 -0
  7. data/app/controllers/moduler/administration/roles_controller.rb +2 -0
  8. data/app/controllers/moduler/administration/sessions_controller.rb +1 -0
  9. data/app/controllers/moduler/administration/users_controller.rb +2 -0
  10. data/app/controllers/moduler/application_controller.rb +9 -0
  11. data/app/controllers/moduler/document_acceptances_controller.rb +1 -0
  12. data/app/controllers/moduler/organizations_controller.rb +11 -1
  13. data/app/controllers/moduler/profile/sessions/revocation_controller.rb +2 -1
  14. data/app/controllers/moduler/profile/sessions_controller.rb +1 -0
  15. data/app/controllers/moduler/profile_controller.rb +2 -1
  16. data/app/controllers/moduler/sessions_controller.rb +5 -2
  17. data/app/controllers/moduler/users_controller.rb +1 -0
  18. data/app/helpers/moduler/administration/audit_events_helper.rb +25 -0
  19. data/app/models/moduler/audit_event.rb +21 -0
  20. data/app/models/moduler/current.rb +2 -0
  21. data/app/models/moduler/membership.rb +13 -0
  22. data/app/models/moduler/user.rb +2 -0
  23. data/app/policies/moduler/administration/audit_event_policy.rb +13 -0
  24. data/app/services/moduler/audit.rb +29 -0
  25. data/app/views/moduler/administration/audit_events/_audit_event.html.erb +19 -0
  26. data/app/views/moduler/administration/audit_events/index.html.erb +10 -0
  27. data/app/views/moduler/administration/audit_events/show.html.erb +56 -0
  28. data/app/views/moduler/common/_sidebar.html.erb +1 -3
  29. data/app/views/moduler/common/administration/_sidebar.html.erb +1 -0
  30. data/app/views/moduler/profile/edit.html.erb +1 -0
  31. data/config/locales/moduler/administration/audit_events/en.yml +30 -0
  32. data/config/locales/moduler/common/en.yml +1 -3
  33. data/config/locales/moduler/organizations/en.yml +5 -0
  34. data/config/locales/moduler/profile/en.yml +3 -0
  35. data/config/routes.rb +1 -0
  36. data/db/migrate/20261112150000_add_time_zone_to_moduler_users.rb +5 -0
  37. data/db/migrate/20261115120000_create_moduler_audit_events.rb +23 -0
  38. data/lib/moduler/version.rb +1 -1
  39. metadata +31 -5
  40. data/app/views/moduler/organizations/show.html.erb +0 -7
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 38043a542a46b3fab8f574d2c177a80e2c79bbbd5a6303ace0e5e21d0d7e3ca5
4
- data.tar.gz: 4d8f5c2a53e892702bbc5f9ccce4cfdfa8bc78a7c18d8b667199afb0d97f5550
3
+ metadata.gz: 4c152abebb8cd8120bb1a16547cbd94bb5e87f5934d9fa832aa91595e7f33403
4
+ data.tar.gz: 1a22913be3aa832c4e5a6201ead50c2b38050b66dd40d05adf6262afb3eed70b
5
5
  SHA512:
6
- metadata.gz: 8bb6e309397bf8ada31a4cfd5925164b2ef3dcf4d234434ce9dd41205aa210d45117e04782e8b7a8d945d5b9b4c98c3643dc1aa65f7e69f8bb6eebffa6b783a7
7
- data.tar.gz: 90e433d24a4aa2f3a2b992a14e5909d77a065f160ab3f6c97b318849e10d9ba25e68660350d885a56ebc714496d7cc86353d72f9139022239dc016561f6c23e3
6
+ metadata.gz: 5d807a745e4518d324677499592232f27cb943b243a6174817906bb67367a06d094ba13081508869176ee5a420fa094b9875a8b74d93805632c0bda4f140f118
7
+ data.tar.gz: 97312eb149de029555f81e4faa44ac8c68937954537ecab53facc12166743b334271ff6cad92e7ce9d920e51c31f3196e02d0c285c9fe1358e1bb6cb842087d1
@@ -13,12 +13,16 @@
13
13
  --color-red-500: oklch(63.7% 0.237 25.331);
14
14
  --color-red-600: oklch(57.7% 0.245 27.325);
15
15
  --color-red-700: oklch(50.5% 0.213 27.518);
16
+ --color-red-900: oklch(39.6% 0.141 25.723);
16
17
  --color-amber-50: oklch(98.7% 0.022 95.277);
17
18
  --color-amber-200: oklch(92.4% 0.12 95.746);
18
19
  --color-amber-700: oklch(55.5% 0.163 48.998);
19
20
  --color-yellow-100: oklch(97.3% 0.071 103.193);
21
+ --color-green-50: oklch(98.2% 0.018 155.826);
20
22
  --color-green-100: oklch(96.2% 0.044 156.743);
21
23
  --color-green-600: oklch(62.7% 0.194 149.214);
24
+ --color-green-700: oklch(52.7% 0.154 150.069);
25
+ --color-green-900: oklch(39.3% 0.095 152.535);
22
26
  --color-emerald-50: oklch(97.9% 0.021 166.113);
23
27
  --color-emerald-200: oklch(90.5% 0.093 164.15);
24
28
  --color-emerald-600: oklch(59.6% 0.145 163.225);
@@ -218,6 +222,9 @@
218
222
  }
219
223
  }
220
224
  @layer utilities {
225
+ .visible {
226
+ visibility: visible;
227
+ }
221
228
  .absolute {
222
229
  position: absolute;
223
230
  }
@@ -925,6 +932,9 @@
925
932
  .flex {
926
933
  display: flex;
927
934
  }
935
+ .grid {
936
+ display: grid;
937
+ }
928
938
  .hidden {
929
939
  display: none;
930
940
  }
@@ -1083,6 +1093,12 @@
1083
1093
  margin-block-end: calc(calc(var(--spacing) * 12) * calc(1 - var(--tw-space-y-reverse)));
1084
1094
  }
1085
1095
  }
1096
+ .gap-x-6 {
1097
+ column-gap: calc(var(--spacing) * 6);
1098
+ }
1099
+ .gap-y-4 {
1100
+ row-gap: calc(var(--spacing) * 4);
1101
+ }
1086
1102
  .divide-y {
1087
1103
  :where(& > :not(:last-child)) {
1088
1104
  --tw-divide-y-reverse: 0;
@@ -1196,6 +1212,9 @@
1196
1212
  .bg-gray-200 {
1197
1213
  background-color: var(--color-gray-200);
1198
1214
  }
1215
+ .bg-green-50 {
1216
+ background-color: var(--color-green-50);
1217
+ }
1199
1218
  .bg-green-100 {
1200
1219
  background-color: var(--color-green-100);
1201
1220
  }
@@ -1238,6 +1257,9 @@
1238
1257
  .p-2 {
1239
1258
  padding: calc(var(--spacing) * 2);
1240
1259
  }
1260
+ .p-4 {
1261
+ padding: calc(var(--spacing) * 4);
1262
+ }
1241
1263
  .px-2 {
1242
1264
  padding-inline: calc(var(--spacing) * 2);
1243
1265
  }
@@ -1342,9 +1364,15 @@
1342
1364
  --tw-tracking: var(--tracking-wide);
1343
1365
  letter-spacing: var(--tracking-wide);
1344
1366
  }
1367
+ .break-words {
1368
+ overflow-wrap: break-word;
1369
+ }
1345
1370
  .whitespace-nowrap {
1346
1371
  white-space: nowrap;
1347
1372
  }
1373
+ .whitespace-pre-wrap {
1374
+ white-space: pre-wrap;
1375
+ }
1348
1376
  .text-amber-700 {
1349
1377
  color: var(--color-amber-700);
1350
1378
  }
@@ -1366,6 +1394,12 @@
1366
1394
  .text-green-600 {
1367
1395
  color: var(--color-green-600);
1368
1396
  }
1397
+ .text-green-700 {
1398
+ color: var(--color-green-700);
1399
+ }
1400
+ .text-green-900 {
1401
+ color: var(--color-green-900);
1402
+ }
1369
1403
  .text-red-500 {
1370
1404
  color: var(--color-red-500);
1371
1405
  }
@@ -1375,6 +1409,9 @@
1375
1409
  .text-red-700 {
1376
1410
  color: var(--color-red-700);
1377
1411
  }
1412
+ .text-red-900 {
1413
+ color: var(--color-red-900);
1414
+ }
1378
1415
  .text-slate-400 {
1379
1416
  color: var(--color-slate-400);
1380
1417
  }
@@ -1773,6 +1810,21 @@
1773
1810
  outline-color: var(--color-red-600);
1774
1811
  }
1775
1812
  }
1813
+ .sm\:col-span-4 {
1814
+ @media (width >= 40rem) {
1815
+ grid-column: span 4 / span 4;
1816
+ }
1817
+ }
1818
+ .sm\:grid-cols-2 {
1819
+ @media (width >= 40rem) {
1820
+ grid-template-columns: repeat(2, minmax(0, 1fr));
1821
+ }
1822
+ }
1823
+ .sm\:grid-cols-5 {
1824
+ @media (width >= 40rem) {
1825
+ grid-template-columns: repeat(5, minmax(0, 1fr));
1826
+ }
1827
+ }
1776
1828
  .sm\:flex-row {
1777
1829
  @media (width >= 40rem) {
1778
1830
  flex-direction: row;
@@ -1788,6 +1840,11 @@
1788
1840
  justify-content: space-between;
1789
1841
  }
1790
1842
  }
1843
+ .sm\:gap-4 {
1844
+ @media (width >= 40rem) {
1845
+ gap: calc(var(--spacing) * 4);
1846
+ }
1847
+ }
1791
1848
  .sm\:gap-6 {
1792
1849
  @media (width >= 40rem) {
1793
1850
  gap: calc(var(--spacing) * 6);
@@ -1798,6 +1855,11 @@
1798
1855
  gap: calc(var(--spacing) * 8);
1799
1856
  }
1800
1857
  }
1858
+ .sm\:p-5 {
1859
+ @media (width >= 40rem) {
1860
+ padding: calc(var(--spacing) * 5);
1861
+ }
1862
+ }
1801
1863
  .sm\:px-0 {
1802
1864
  @media (width >= 40rem) {
1803
1865
  padding-inline: calc(var(--spacing) * 0);
@@ -0,0 +1,18 @@
1
+ module Moduler
2
+ # Runs each request in the time zone chosen by the signed-in user in their
3
+ # profile, or in the application's time zone (config.time_zone) for users
4
+ # without one and for visitors who are not signed in.
5
+ module UserTimeZone
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ around_action :use_user_time_zone
10
+ end
11
+
12
+ private
13
+
14
+ def use_user_time_zone(&)
15
+ Time.use_zone(Current.user&.time_zone.presence || Time.zone_default, &)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,22 @@
1
+ module Moduler
2
+ module Administration
3
+ class AuditEventsController < ApplicationController
4
+ before_action -> { authorize([ :moduler, :administration, :audit_event ]) }
5
+
6
+ def index
7
+ @filter, @audit_events = OrganizationQueryFilter.new(AuditEvent.includes(:user, :organization), permitted_filter_parameters).call
8
+ end
9
+
10
+ def show
11
+ @audit_event = AuditEvent.find(params[:id])
12
+ authorize(@audit_event, policy_class: AuditEventPolicy)
13
+ end
14
+
15
+ private
16
+
17
+ def permitted_filter_parameters
18
+ (params.permit(filter: %i[ query organization_id ])[:filter] || {}).merge(params.permit(:page))
19
+ end
20
+ end
21
+ end
22
+ end
@@ -16,6 +16,7 @@ module Moduler
16
16
  @membership = Membership.new(permitted_parameters)
17
17
 
18
18
  if @membership.save
19
+ Audit.record("membership.created", @membership, changes: @membership.saved_changes)
19
20
  redirect_to moduler.administration_memberships_path, notice: t(".notice")
20
21
  else
21
22
  render :new, status: :unprocessable_entity
@@ -26,6 +27,7 @@ module Moduler
26
27
 
27
28
  def update
28
29
  if @membership.update(permitted_update_parameters)
30
+ Audit.record("membership.updated", @membership, changes: @membership.saved_changes)
29
31
  redirect_to moduler.administration_memberships_path, notice: t(".notice")
30
32
  else
31
33
  render :edit, status: :unprocessable_entity
@@ -34,6 +36,7 @@ module Moduler
34
36
 
35
37
  def destroy
36
38
  @membership.destroy!
39
+ Audit.record("membership.destroyed", @membership, user_id: @membership.moduler_user_id, role_id: @membership.moduler_role_id)
37
40
 
38
41
  redirect_to moduler.administration_memberships_path, status: :see_other, notice: t(".notice")
39
42
  end
@@ -16,6 +16,7 @@ module Moduler
16
16
  @organization = Organization.new(permitted_parameters)
17
17
 
18
18
  if @organization.save
19
+ Audit.record("organization.created", @organization, changes: @organization.saved_changes)
19
20
  redirect_to moduler.administration_organizations_path, notice: t(".notice")
20
21
  else
21
22
  render :new, status: :unprocessable_entity
@@ -26,6 +27,7 @@ module Moduler
26
27
 
27
28
  def update
28
29
  if @organization.update(permitted_parameters)
30
+ Audit.record("organization.updated", @organization, changes: @organization.saved_changes)
29
31
  redirect_to moduler.administration_organizations_path, notice: t(".notice")
30
32
  else
31
33
  render :edit, status: :unprocessable_entity
@@ -16,6 +16,7 @@ module Moduler
16
16
  @role = Role.new(permitted_parameters)
17
17
 
18
18
  if @role.save
19
+ Audit.record("role.created", @role, changes: @role.saved_changes)
19
20
  redirect_to moduler.administration_roles_path, notice: t(".notice")
20
21
  else
21
22
  render :new, status: :unprocessable_entity
@@ -26,6 +27,7 @@ module Moduler
26
27
 
27
28
  def update
28
29
  if @role.update(permitted_update_parameters)
30
+ Audit.record("role.updated", @role, changes: @role.saved_changes)
29
31
  redirect_to moduler.administration_roles_path, notice: t(".notice")
30
32
  else
31
33
  render :edit, status: :unprocessable_entity
@@ -10,6 +10,7 @@ module Moduler
10
10
 
11
11
  def destroy
12
12
  @session.touch(:signed_out_at)
13
+ Audit.record("session.revoked", @session)
13
14
 
14
15
  if @session == Current.session
15
16
  cookies.delete(:session_id)
@@ -16,6 +16,7 @@ module Moduler
16
16
  @user = User.new(permitted_parameters)
17
17
 
18
18
  if @user.save
19
+ Audit.record("user.created", @user, changes: @user.saved_changes)
19
20
  redirect_to moduler.administration_users_path, notice: t(".notice")
20
21
  else
21
22
  render :new, status: :unprocessable_entity
@@ -27,6 +28,7 @@ module Moduler
27
28
 
28
29
  def update
29
30
  if @user.update(permitted_update_parameters)
31
+ Audit.record("user.updated", @user, changes: @user.saved_changes)
30
32
  redirect_to moduler.administration_users_path, notice: t(".notice")
31
33
  else
32
34
  render :edit, status: :unprocessable_entity
@@ -3,16 +3,25 @@ module Moduler
3
3
  include Authentication
4
4
  include RestrictAdministratorAccess
5
5
  include RequireDocumentAcceptance
6
+ include UserTimeZone
6
7
  include Pundit::Authorization
7
8
 
8
9
  default_form_builder FormBuilder
9
10
 
11
+ prepend_before_action :set_current_request
12
+
10
13
  rescue_from Pundit::NotAuthorizedError, with: :not_authorized
11
14
 
12
15
  helper_method :current_user, :current_organization, :current_membership, :can?
13
16
 
14
17
  private
15
18
 
19
+ # The request of audit events (see Moduler::Audit).
20
+ def set_current_request
21
+ Current.ip_address = request.remote_ip
22
+ Current.request_id = request.request_id
23
+ end
24
+
16
25
  def current_user
17
26
  Current.user
18
27
  end
@@ -8,6 +8,7 @@ module Moduler
8
8
 
9
9
  def create
10
10
  if @document_acceptance.call
11
+ Audit.record("documents.accepted", Current.user, document_ids: @document_acceptance.pending_documents.map(&:id))
11
12
  redirect_to moduler.root_path, notice: t(".notice")
12
13
  else
13
14
  flash.now[:alert] = @document_acceptance.errors.full_messages.to_sentence
@@ -1,8 +1,18 @@
1
1
  module Moduler
2
+ # Entry point of an organization, linked from the home page and the
3
+ # organization switcher. It has no page of its own: members are sent to the
4
+ # first sidebar resource from config/moduler.yml they may see, or back to
5
+ # the home page when their role grants none.
2
6
  class OrganizationsController < ApplicationController
3
7
  include OrganizationScoped
4
8
 
5
- def show; end
9
+ def show
10
+ if (resource = current_membership.home_resource)
11
+ redirect_to moduler.polymorphic_path([ current_organization, resource.name.to_sym ])
12
+ else
13
+ redirect_to moduler.root_path, alert: t(".no_resources")
14
+ end
15
+ end
6
16
 
7
17
  private
8
18
 
@@ -6,7 +6,8 @@ module Moduler
6
6
  allow_administrator_access
7
7
 
8
8
  def destroy
9
- Current.user.sessions.active.update_all(signed_out_at: Time.current)
9
+ count = Current.user.sessions.active.update_all(signed_out_at: Time.current)
10
+ Audit.record("user.sessions_revoked", Current.user, count:)
10
11
  cookies.delete(:session_id)
11
12
 
12
13
  redirect_to moduler.new_session_path, status: :see_other
@@ -11,6 +11,7 @@ module Moduler
11
11
  def destroy
12
12
  session = Current.user.sessions.active.find(params[:id])
13
13
  session.touch(:signed_out_at)
14
+ Audit.record("session.revoked", session)
14
15
 
15
16
  if session == Current.session
16
17
  cookies.delete(:session_id)
@@ -13,6 +13,7 @@ module Moduler
13
13
  @user.assign_attributes(permitted_parameters)
14
14
 
15
15
  if @user.save(context: :self_service_update)
16
+ Audit.record("user.updated", @user, changes: @user.saved_changes, password_changed: @user.saved_change_to_password_digest?)
16
17
  @user.sessions.active.where.not(id: Current.session.id).update_all(signed_out_at: Time.current) if @user.saved_change_to_password_digest?
17
18
 
18
19
  redirect_to moduler.edit_profile_path, notice: t(".notice")
@@ -25,7 +26,7 @@ module Moduler
25
26
 
26
27
  # The current password is required only to change the password.
27
28
  def permitted_parameters
28
- parameters = params.expect(user: %i[ name password_challenge password password_confirmation ])
29
+ parameters = params.expect(user: %i[ name time_zone password_challenge password password_confirmation ])
29
30
  parameters[:password].blank? ? parameters.except(:password_challenge, :password, :password_confirmation) : parameters
30
31
  end
31
32
  end
@@ -23,15 +23,18 @@ module Moduler
23
23
  user = User.authenticate_by(params.require(:user).permit(:email_address, :password))
24
24
 
25
25
  if user&.active?
26
- start_new_session_for user
26
+ Audit.record("session.signed_in", start_new_session_for(user))
27
27
 
28
28
  redirect_to after_authentication_url
29
29
  else
30
- render_sign_in_error(user ? "inactive_account" : "invalid_credentials")
30
+ reason = user ? "inactive_account" : "invalid_credentials"
31
+ Audit.record("session.sign_in_failed", User.find_by(email_address: params.dig(:user, :email_address).to_s), reason:)
32
+ render_sign_in_error(reason)
31
33
  end
32
34
  end
33
35
 
34
36
  def destroy
37
+ Audit.record("session.signed_out", Current.session)
35
38
  terminate_session
36
39
  redirect_to moduler.new_session_path, status: :see_other
37
40
  end
@@ -23,6 +23,7 @@ module Moduler
23
23
  @user = User.new(permitted_parameters)
24
24
 
25
25
  if @user.save
26
+ Audit.record("user.signed_up", @user, user: @user)
26
27
  Email.deliver_event("user.signup", to: @user)
27
28
  redirect_to moduler.new_session_path, notice: t("moduler.users.create.success")
28
29
  else
@@ -0,0 +1,25 @@
1
+ module Moduler
2
+ module Administration
3
+ module AuditEventsHelper
4
+ # What an audit event was done to: "Role 6c2a4345" (the whole id with +full: true+).
5
+ def audit_event_subject(event, full: false)
6
+ return unless event.subject_type
7
+
8
+ id = event.subject_id.to_s
9
+ "#{event.subject_type.demodulize} #{full ? id : id.first(8)}"
10
+ end
11
+
12
+ # A value of the details of an audit event as text: lists joined, hashes as JSON,
13
+ # "(empty)" for nothing.
14
+ def audit_event_value(value)
15
+ return t("moduler.administration.audit_events.show.empty") if value.nil? || value == "" || value == [] || value == {}
16
+
17
+ case value
18
+ when Hash then JSON.pretty_generate(value)
19
+ when Array then value.join(", ")
20
+ else value.to_s
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,21 @@
1
+ module Moduler
2
+ # Something done in the application, kept for security and compliance: who did what, to
3
+ # what, in which organization, from where and when. Events are recorded with
4
+ # Moduler::Audit.record and never changed or removed by the application.
5
+ class AuditEvent < ApplicationRecord
6
+ include PgSearch::Model
7
+
8
+ belongs_to :user, class_name: "Moduler::User", foreign_key: "moduler_user_id", optional: true
9
+ belongs_to :organization, class_name: "Moduler::Organization", foreign_key: "moduler_organization_id", optional: true
10
+ belongs_to :subject, polymorphic: true, optional: true
11
+
12
+ validates :action, presence: true
13
+
14
+ pg_search_scope :search_by_query, against: %i[ action ip_address ], associated_against: { user: %i[ email_address ] },
15
+ using: { trigram: { word_similarity: true } }
16
+
17
+ def readonly?
18
+ persisted?
19
+ end
20
+ end
21
+ end
@@ -1,6 +1,8 @@
1
1
  module Moduler
2
2
  class Current < ActiveSupport::CurrentAttributes
3
3
  attribute :session, :organization, :membership
4
+ # Of the request, for audit events (set by Moduler::ApplicationController).
5
+ attribute :ip_address, :request_id
4
6
  delegate :user, to: :session, allow_nil: true
5
7
  end
6
8
  end
@@ -14,6 +14,19 @@ module Moduler
14
14
 
15
15
  pg_search_scope :search_by_query, associated_against: { user: :email_address }, using: { trigram: { word_similarity: true } }
16
16
 
17
+ # Sidebar resources from config/moduler.yml the member may see: those
18
+ # without a permission and those whose permission the role grants, in
19
+ # the configured order.
20
+ def visible_resources(resources = Moduler.configuration.resources)
21
+ resources.select { |resource| resource.permission.nil? || permission?(resource.permission) }
22
+ end
23
+
24
+ # The page the member lands on when entering the organization: the first
25
+ # visible resource, or +nil+ when the member may see none.
26
+ def home_resource(resources = Moduler.configuration.resources)
27
+ visible_resources(resources).first
28
+ end
29
+
17
30
  private
18
31
 
19
32
  def user_must_be_member
@@ -15,10 +15,12 @@ module Moduler
15
15
 
16
16
  normalizes :email_address, with: ->(email_address) { email_address.strip.downcase }
17
17
  normalizes :name, with: ->(name) { name.strip.presence }
18
+ normalizes :time_zone, with: ->(time_zone) { time_zone.strip.presence }
18
19
 
19
20
  validates :email_address, presence: true, uniqueness: true, format: { with: URI::MailTo::EMAIL_REGEXP }
20
21
  validates :password, length: { minimum: 8 }, allow_nil: true
21
22
  validates :name, length: { maximum: 100 }
23
+ validates :time_zone, inclusion: { in: ->(_) { ActiveSupport::TimeZone.all.map(&:name) } }, allow_nil: true
22
24
 
23
25
  enum :role, { member: 0, administrator: 1 }
24
26
  enum :status, { waitlisted: 0, active: 1, inactive: 2 }, default: :active
@@ -0,0 +1,13 @@
1
+ module Moduler
2
+ module Administration
3
+ class AuditEventPolicy < BasePolicy
4
+ def index?
5
+ administrator?
6
+ end
7
+
8
+ def show?
9
+ administrator?
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,29 @@
1
+ module Moduler
2
+ # Records audit events (see Moduler::AuditEvent):
3
+ #
4
+ # Moduler::Audit.record("role.updated", role, changes: role.saved_changes)
5
+ # Moduler::Audit.record("report.exported", report, format: "csv")
6
+ #
7
+ # The action names what was done (+<subject>.<past tense>+), the subject is the record it
8
+ # was done to, and other keywords are kept as details. +changes+ (e.g. +saved_changes+)
9
+ # are kept without timestamps and password digests. The user, the IP address and the
10
+ # request come from Moduler::Current; the organization too, or else from the subject.
11
+ module Audit
12
+ IGNORED_CHANGES = %w[ created_at updated_at password_digest ].freeze
13
+
14
+ def self.record(action, subject = nil, user: Current.user, organization: nil, changes: nil, **details)
15
+ details[:changes] = changes.except(*IGNORED_CHANGES) if changes.present?
16
+
17
+ AuditEvent.create!(action:, subject:, user:, organization: organization || organization_of(subject), metadata: details,
18
+ ip_address: Current.ip_address, request_id: Current.request_id)
19
+ end
20
+
21
+ def self.organization_of(subject)
22
+ return Current.organization if Current.organization
23
+ return subject if subject.is_a?(Organization)
24
+
25
+ subject.organization if subject.respond_to?(:organization) && subject.organization.is_a?(Organization)
26
+ end
27
+ private_class_method :organization_of
28
+ end
29
+ end
@@ -0,0 +1,19 @@
1
+ <%= render(Moduler::ListItemComponent.new(audit_event.action, id: dom_id(audit_event))) do |item| %>
2
+ <% item.with_badge(audit_event.organization.name) if audit_event.organization %>
3
+
4
+ <% item.with_meta do %>
5
+ <%= audit_event.user&.email_address || t(".unknown_user") %>
6
+ <% if (subject = audit_event_subject(audit_event)) %>
7
+ <span aria-hidden="true">&middot;</span>
8
+ <%= subject %>
9
+ <% end %>
10
+ <span aria-hidden="true">&middot;</span>
11
+ <%= audit_event.ip_address.presence || t(".unknown_ip") %>
12
+ <span aria-hidden="true">&middot;</span>
13
+ <%= time_tag audit_event.created_at, t(".created", time: time_ago_in_words(audit_event.created_at)), title: l(audit_event.created_at, format: :long) %>
14
+ <% end %>
15
+
16
+ <% item.with_action do %>
17
+ <%= render(Moduler::ButtonLinkComponent.new(t(".action.show"), moduler.administration_audit_event_path(audit_event), size: :sm)) %>
18
+ <% end %>
19
+ <% end %>
@@ -0,0 +1,10 @@
1
+ <%= render(Moduler::Administration::PageComponent.new) do %>
2
+ <%= render(Moduler::PageHeaderComponent.new(t(".title"), description: t(".description"))) %>
3
+
4
+ <%= render(Moduler::FilterComponent.new(@filter, moduler.administration_audit_events_path,
5
+ selects: [ { name: :organization_id, choices: Moduler::Organization.order(:name).pluck(:name, :id), prompt: t("moduler.components.filter.placeholder.organization") } ])) %>
6
+
7
+ <%= render(Moduler::ListComponent.new(@audit_events, id: "audit_events", empty: t(".empty"))) do %>
8
+ <%= render partial: "moduler/administration/audit_events/audit_event", collection: @audit_events %>
9
+ <% end %>
10
+ <% end %>
@@ -0,0 +1,56 @@
1
+ <% changes = @audit_event.metadata.fetch("changes", {}) %>
2
+ <% details = @audit_event.metadata.except("changes") %>
3
+
4
+ <%= render(Moduler::Administration::PageComponent.new) do %>
5
+ <%= render(Moduler::PageHeaderComponent.new(@audit_event.action, description: l(@audit_event.created_at, format: :long))) do |header| %>
6
+ <% header.with_action do %>
7
+ <%= render(Moduler::ButtonLinkComponent.new(t(".action.back"), moduler.administration_audit_events_path)) %>
8
+ <% end %>
9
+ <% end %>
10
+
11
+ <dl id="event" class="grid gap-x-6 gap-y-4 rounded-lg border border-slate-200 bg-white p-4 text-sm sm:grid-cols-2 sm:p-5">
12
+ <% { user: @audit_event.user&.email_address || t(".unknown"),
13
+ organization: @audit_event.organization&.name || t(".none"),
14
+ subject: audit_event_subject(@audit_event, full: true) || t(".none"),
15
+ ip_address: @audit_event.ip_address.presence || t(".unknown"),
16
+ request_id: @audit_event.request_id.presence || t(".unknown") }.each do |label, value| %>
17
+ <div class="min-w-0">
18
+ <dt class="text-xs font-medium text-slate-500"><%= t(".label.#{label}") %></dt>
19
+ <dd class="mt-1 break-words text-gray-900"><%= value %></dd>
20
+ </div>
21
+ <% end %>
22
+ </dl>
23
+
24
+ <% if changes.any? %>
25
+ <h2 class="mt-8 text-sm font-semibold text-gray-900"><%= t(".changes") %></h2>
26
+ <dl id="changes" class="mt-3 divide-y divide-slate-100 rounded-lg border border-slate-200 bg-white text-sm">
27
+ <% changes.each do |attribute, (before, after)| %>
28
+ <div class="px-4 py-3">
29
+ <dt class="font-medium break-words text-gray-900"><%= attribute %></dt>
30
+ <dd class="mt-2 grid gap-2 sm:grid-cols-2">
31
+ <div class="min-w-0 rounded-md bg-red-50 px-3 py-2 text-red-900">
32
+ <p class="text-xs font-medium text-red-700"><%= t(".label.before") %></p>
33
+ <p class="mt-1 whitespace-pre-wrap break-words"><%= audit_event_value(before) %></p>
34
+ </div>
35
+ <div class="min-w-0 rounded-md bg-green-50 px-3 py-2 text-green-900">
36
+ <p class="text-xs font-medium text-green-700"><%= t(".label.after") %></p>
37
+ <p class="mt-1 whitespace-pre-wrap break-words"><%= audit_event_value(after) %></p>
38
+ </div>
39
+ </dd>
40
+ </div>
41
+ <% end %>
42
+ </dl>
43
+ <% end %>
44
+
45
+ <% if details.any? %>
46
+ <h2 class="mt-8 text-sm font-semibold text-gray-900"><%= t(".details") %></h2>
47
+ <dl id="details" class="mt-3 divide-y divide-slate-100 rounded-lg border border-slate-200 bg-white text-sm">
48
+ <% details.each do |key, value| %>
49
+ <div class="grid gap-1 px-4 py-2 sm:grid-cols-5 sm:gap-4">
50
+ <dt class="font-medium text-gray-900"><%= key %></dt>
51
+ <dd class="whitespace-pre-wrap break-words text-slate-700 sm:col-span-4"><%= audit_event_value(value) %></dd>
52
+ </div>
53
+ <% end %>
54
+ </dl>
55
+ <% end %>
56
+ <% end %>
@@ -2,9 +2,7 @@
2
2
  <%= render "moduler/common/organization_switcher" %>
3
3
 
4
4
  <nav class="flex gap-1 overflow-x-auto md:flex-col text-sm p-2">
5
- <%= render(Moduler::SidebarActionComponent.new(t(".action.dashboard"), moduler.organization_path(current_organization), "moduler/organizations")) %>
6
- <% Moduler.configuration.resources.each do |resource| %>
7
- <% next if resource.permission && !can?(resource.permission) %>
5
+ <% current_membership.visible_resources.each do |resource| %>
8
6
  <%= render(Moduler::SidebarActionComponent.new(t(".action.#{resource.name}"), moduler.url_for([current_organization, resource.name.to_sym]), "moduler/#{resource.name}")) %>
9
7
  <% end %>
10
8
  </nav>
@@ -5,6 +5,7 @@
5
5
  <%= render(Moduler::SidebarActionComponent.new(t(".action.organizations"), moduler.administration_organizations_path, "moduler/administration/organizations")) %>
6
6
  <%= render(Moduler::SidebarActionComponent.new(t(".action.roles"), moduler.administration_roles_path, "moduler/administration/roles")) %>
7
7
  <%= render(Moduler::SidebarActionComponent.new(t(".action.memberships"), moduler.administration_memberships_path, "moduler/administration/memberships")) %>
8
+ <%= render(Moduler::SidebarActionComponent.new(t(".action.audit_events"), moduler.administration_audit_events_path, "moduler/administration/audit_events")) %>
8
9
 
9
10
  <div role="separator" class="mx-1 w-px shrink-0 self-stretch bg-gray-200 md:mx-0 md:my-2 md:h-px md:w-auto"></div>
10
11
 
@@ -12,6 +12,7 @@
12
12
  <%= render(::Moduler::Form::SectionComponent.new(t(".section.data"))) do %>
13
13
  <%= render(::Moduler::Form::EmailComponent.new(form, :email_address, label: t(".label.email_address"), disabled: :disabled)) %>
14
14
  <%= render(::Moduler::Form::TextComponent.new(form, :name, label: t(".label.name"), description: t(".description.name"))) %>
15
+ <%= render(::Moduler::Form::SelectComponent.new(form, :time_zone, ActiveSupport::TimeZone.all.map { |zone| [ zone.to_s, zone.name ] }, label: t(".label.time_zone"), description: t(".description.time_zone"), include_blank: t(".time_zone_default", time_zone: Time.zone_default.to_s))) %>
15
16
  <% end %>
16
17
 
17
18
  <%= render(::Moduler::Form::SectionComponent.new(t(".section.password"), divider: true)) do %>
@@ -0,0 +1,30 @@
1
+ en:
2
+ moduler:
3
+ administration:
4
+ audit_events:
5
+ index:
6
+ title: Audit log
7
+ description: What was done in the application, by whom and when. Search by action, email address or IP address.
8
+ empty: No events found.
9
+ audit_event:
10
+ created: "%{time} ago"
11
+ unknown_user: Unknown user
12
+ unknown_ip: Unknown IP address
13
+ action:
14
+ show: Details
15
+ show:
16
+ action:
17
+ back: Back to audit log
18
+ label:
19
+ user: User
20
+ organization: Organization
21
+ subject: Subject
22
+ ip_address: IP address
23
+ request_id: Request ID
24
+ before: Before
25
+ after: After
26
+ changes: Changes
27
+ details: Details
28
+ empty: (empty)
29
+ unknown: Unknown
30
+ none: None
@@ -13,12 +13,10 @@ en:
13
13
  organizations: Organizations
14
14
  roles: Roles
15
15
  memberships: Members
16
+ audit_events: Audit log
16
17
  organization_switcher:
17
18
  label: Organization
18
19
  current: Current organization
19
- sidebar:
20
- action:
21
- dashboard: Dashboard
22
20
  profile:
23
21
  sidebar:
24
22
  action:
@@ -0,0 +1,5 @@
1
+ en:
2
+ moduler:
3
+ organizations:
4
+ show:
5
+ no_resources: You don't have access to any page in this organization.
@@ -6,13 +6,16 @@ en:
6
6
  label:
7
7
  email_address: Email address
8
8
  name: Name
9
+ time_zone: Time zone
9
10
  password: New password
10
11
  password_confirmation: Confirm new password
11
12
  password_challenge: Current password
12
13
  description:
13
14
  name: Optional. Shown instead of your email address.
15
+ time_zone: Dates and times in the application are shown in this time zone.
14
16
  password_confirmation: Leave blank to keep your current password. Changing it signs out your other active sessions.
15
17
  password_challenge: Required to save changes to password.
18
+ time_zone_default: "Application default %{time_zone}"
16
19
  action:
17
20
  save: Save
18
21
  section:
data/config/routes.rb CHANGED
@@ -32,6 +32,7 @@ Moduler::Engine.routes.draw do
32
32
  resources :organizations, only: %i[ index new create edit update ]
33
33
  resources :roles, only: %i[ index new create edit update ]
34
34
  resources :memberships, only: %i[ index new create edit update destroy ]
35
+ resources :audit_events, only: %i[ index show ]
35
36
 
36
37
  root "home#index"
37
38
  end
@@ -0,0 +1,5 @@
1
+ class AddTimeZoneToModulerUsers < ActiveRecord::Migration[8.1]
2
+ def change
3
+ add_column :moduler_users, :time_zone, :string
4
+ end
5
+ end
@@ -0,0 +1,23 @@
1
+ class CreateModulerAuditEvents < ActiveRecord::Migration[8.1]
2
+ def change
3
+ create_table :moduler_audit_events, id: :uuid do |t|
4
+ # Events outlive users and organizations: removing one only removes it from its events.
5
+ t.references :moduler_user, type: :uuid, foreign_key: { on_delete: :nullify }, index: false
6
+ t.references :moduler_organization, type: :uuid, foreign_key: { on_delete: :nullify }, index: false
7
+ t.string :action, null: false
8
+ # A string, so subjects can be records of the host application with integer ids too.
9
+ t.string :subject_type
10
+ t.string :subject_id
11
+ t.jsonb :metadata, null: false, default: {}
12
+ t.string :ip_address
13
+ t.string :request_id
14
+
15
+ t.datetime :created_at, null: false
16
+ end
17
+
18
+ add_index :moduler_audit_events, :created_at
19
+ add_index :moduler_audit_events, %i[ moduler_user_id created_at ]
20
+ add_index :moduler_audit_events, %i[ moduler_organization_id created_at ]
21
+ add_index :moduler_audit_events, %i[ subject_type subject_id ]
22
+ end
23
+ end
@@ -1,3 +1,3 @@
1
1
  module Moduler
2
- VERSION = "0.8.0"
2
+ VERSION = "0.9.0"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: moduler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomasz Kowalewski
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2026-11-12 00:00:00.000000000 Z
10
+ date: 2026-11-20 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: rails
@@ -18,7 +18,7 @@ dependencies:
18
18
  version: '8.1'
19
19
  - - ">="
20
20
  - !ruby/object:Gem::Version
21
- version: 8.1.2
21
+ version: 8.1.4.1
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
@@ -28,7 +28,7 @@ dependencies:
28
28
  version: '8.1'
29
29
  - - ">="
30
30
  - !ruby/object:Gem::Version
31
- version: 8.1.2
31
+ version: 8.1.4.1
32
32
  - !ruby/object:Gem::Dependency
33
33
  name: importmap-rails
34
34
  requirement: !ruby/object:Gem::Requirement
@@ -151,6 +151,20 @@ dependencies:
151
151
  - - "~>"
152
152
  - !ruby/object:Gem::Version
153
153
  version: '1.2'
154
+ - !ruby/object:Gem::Dependency
155
+ name: pg
156
+ requirement: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - "~>"
159
+ - !ruby/object:Gem::Version
160
+ version: '1.6'
161
+ type: :runtime
162
+ prerelease: false
163
+ version_requirements: !ruby/object:Gem::Requirement
164
+ requirements:
165
+ - - "~>"
166
+ - !ruby/object:Gem::Version
167
+ version: '1.6'
154
168
  - !ruby/object:Gem::Dependency
155
169
  name: pg_search
156
170
  requirement: !ruby/object:Gem::Requirement
@@ -274,8 +288,10 @@ files:
274
288
  - app/controllers/concerns/moduler/organization_scoped.rb
275
289
  - app/controllers/concerns/moduler/require_document_acceptance.rb
276
290
  - app/controllers/concerns/moduler/restrict_administrator_access.rb
291
+ - app/controllers/concerns/moduler/user_time_zone.rb
277
292
  - app/controllers/moduler/administration/application_controller.rb
278
293
  - app/controllers/moduler/administration/articles_controller.rb
294
+ - app/controllers/moduler/administration/audit_events_controller.rb
279
295
  - app/controllers/moduler/administration/documents_controller.rb
280
296
  - app/controllers/moduler/administration/emails/deliveries_controller.rb
281
297
  - app/controllers/moduler/administration/emails_controller.rb
@@ -301,6 +317,7 @@ files:
301
317
  - app/filters/email_delivery_filter.rb
302
318
  - app/filters/organization_query_filter.rb
303
319
  - app/filters/query_filter.rb
320
+ - app/helpers/moduler/administration/audit_events_helper.rb
304
321
  - app/helpers/moduler/application_helper.rb
305
322
  - app/jobs/moduler/application_job.rb
306
323
  - app/jobs/moduler/email_delivery_job.rb
@@ -309,6 +326,7 @@ files:
309
326
  - app/mailers/moduler/email_mailer.rb
310
327
  - app/models/moduler/application_record.rb
311
328
  - app/models/moduler/article.rb
329
+ - app/models/moduler/audit_event.rb
312
330
  - app/models/moduler/current.rb
313
331
  - app/models/moduler/document.rb
314
332
  - app/models/moduler/email.rb
@@ -322,6 +340,7 @@ files:
322
340
  - app/models/moduler/user_document.rb
323
341
  - app/models/moduler/user_email.rb
324
342
  - app/policies/moduler/administration/article_policy.rb
343
+ - app/policies/moduler/administration/audit_event_policy.rb
325
344
  - app/policies/moduler/administration/base_policy.rb
326
345
  - app/policies/moduler/administration/document_policy.rb
327
346
  - app/policies/moduler/administration/email_policy.rb
@@ -333,6 +352,7 @@ files:
333
352
  - app/policies/moduler/administration/session_policy.rb
334
353
  - app/policies/moduler/administration/user_policy.rb
335
354
  - app/policies/moduler/application_policy.rb
355
+ - app/services/moduler/audit.rb
336
356
  - app/services/moduler/document_acceptance.rb
337
357
  - app/views/kaminari/_gap.html.erb
338
358
  - app/views/kaminari/_next_page.html.erb
@@ -348,6 +368,9 @@ files:
348
368
  - app/views/moduler/administration/articles/edit.html.erb
349
369
  - app/views/moduler/administration/articles/index.html.erb
350
370
  - app/views/moduler/administration/articles/new.html.erb
371
+ - app/views/moduler/administration/audit_events/_audit_event.html.erb
372
+ - app/views/moduler/administration/audit_events/index.html.erb
373
+ - app/views/moduler/administration/audit_events/show.html.erb
351
374
  - app/views/moduler/administration/documents/_document.html.erb
352
375
  - app/views/moduler/administration/documents/_form.html.erb
353
376
  - app/views/moduler/administration/documents/edit.html.erb
@@ -399,7 +422,6 @@ files:
399
422
  - app/views/moduler/email_mailer/custom.html.erb
400
423
  - app/views/moduler/email_mailer/custom.text.erb
401
424
  - app/views/moduler/home/index.html.erb
402
- - app/views/moduler/organizations/show.html.erb
403
425
  - app/views/moduler/profile/edit.html.erb
404
426
  - app/views/moduler/profile/sent_emails/index.html.erb
405
427
  - app/views/moduler/profile/sessions/_session.html.erb
@@ -408,6 +430,7 @@ files:
408
430
  - app/views/moduler/users/new.html.erb
409
431
  - config/importmap.rb
410
432
  - config/locales/moduler/administration/articles/en.yml
433
+ - config/locales/moduler/administration/audit_events/en.yml
411
434
  - config/locales/moduler/administration/documents/en.yml
412
435
  - config/locales/moduler/administration/emails/en.yml
413
436
  - config/locales/moduler/administration/memberships/en.yml
@@ -423,6 +446,7 @@ files:
423
446
  - config/locales/moduler/en.yml
424
447
  - config/locales/moduler/home/en.yml
425
448
  - config/locales/moduler/models/en.yml
449
+ - config/locales/moduler/organizations/en.yml
426
450
  - config/locales/moduler/profile/en.yml
427
451
  - config/locales/moduler/profile/sent_emails/en.yml
428
452
  - config/locales/moduler/profile/sessions/en.yml
@@ -446,6 +470,8 @@ files:
446
470
  - db/migrate/20261111210000_create_moduler_emails.rb
447
471
  - db/migrate/20261111210001_create_moduler_user_emails.rb
448
472
  - db/migrate/20261111210002_create_moduler_sent_emails.rb
473
+ - db/migrate/20261112150000_add_time_zone_to_moduler_users.rb
474
+ - db/migrate/20261115120000_create_moduler_audit_events.rb
449
475
  - lib/moduler.rb
450
476
  - lib/moduler/configuration.rb
451
477
  - lib/moduler/engine.rb
@@ -1,7 +0,0 @@
1
- <main class="mx-auto flex w-full flex-1 max-w-[120rem] flex-col gap-4 px-4 md:flex-row md:gap-8 md:pl-0 md:pr-6">
2
- <%= render "moduler/common/sidebar" %>
3
-
4
- <div class="flex min-w-0 flex-1 flex-col pb-4 md:mt-8">
5
- <%= render(Moduler::PageHeaderComponent.new(current_organization.name)) %>
6
- </div>
7
- </main>