iron_admin 0.7.0 → 0.8.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 (210) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +129 -1
  3. data/README.md +48 -1
  4. data/UPGRADING.md +495 -8
  5. data/app/assets/tailwind/iron_admin/engine.css +79 -4
  6. data/app/components/iron_admin/dashboards/activity_feed_component/item_component.html.haml +6 -9
  7. data/app/components/iron_admin/dashboards/activity_feed_component.html.haml +6 -5
  8. data/app/components/iron_admin/dashboards/activity_feed_component.rb +21 -24
  9. data/app/components/iron_admin/dashboards/chart_component.html.haml +3 -5
  10. data/app/components/iron_admin/dashboards/chart_component.rb +86 -24
  11. data/app/components/iron_admin/dashboards/concerns/themed_card.rb +39 -0
  12. data/app/components/iron_admin/dashboards/metric_card_component.html.haml +8 -7
  13. data/app/components/iron_admin/dashboards/metric_card_component.rb +2 -0
  14. data/app/components/iron_admin/dashboards/progress_component.html.haml +11 -7
  15. data/app/components/iron_admin/dashboards/progress_component.rb +11 -6
  16. data/app/components/iron_admin/dashboards/quick_links_component/link_component.html.haml +8 -9
  17. data/app/components/iron_admin/dashboards/quick_links_component.html.haml +6 -5
  18. data/app/components/iron_admin/dashboards/quick_links_component.rb +4 -12
  19. data/app/components/iron_admin/dashboards/recent_table_component.html.haml +15 -10
  20. data/app/components/iron_admin/dashboards/recent_table_component.rb +49 -0
  21. data/app/components/iron_admin/dashboards/stats_grid_component/stat_component.html.haml +13 -14
  22. data/app/components/iron_admin/dashboards/stats_grid_component.html.haml +1 -0
  23. data/app/components/iron_admin/dashboards/stats_grid_component.rb +9 -11
  24. data/app/components/iron_admin/filters/bar_component.html.haml +8 -6
  25. data/app/components/iron_admin/filters/bar_component.rb +56 -9
  26. data/app/components/iron_admin/filters/control_styles.rb +82 -0
  27. data/app/components/iron_admin/filters/date_range_component.html.haml +9 -4
  28. data/app/components/iron_admin/filters/date_range_component.rb +10 -6
  29. data/app/components/iron_admin/filters/search_component.html.haml +4 -4
  30. data/app/components/iron_admin/filters/search_component.rb +3 -5
  31. data/app/components/iron_admin/filters/select_filter_component.html.haml +1 -1
  32. data/app/components/iron_admin/filters/select_filter_component.rb +5 -16
  33. data/app/components/iron_admin/form/belongs_to_autocomplete_component.html.haml +9 -5
  34. data/app/components/iron_admin/form/belongs_to_autocomplete_component.rb +10 -8
  35. data/app/components/iron_admin/form/belongs_to_component.html.haml +2 -2
  36. data/app/components/iron_admin/form/belongs_to_component.rb +4 -18
  37. data/app/components/iron_admin/form/checkbox_component.rb +2 -3
  38. data/app/components/iron_admin/form/concerns/form_input_behavior.rb +1 -1
  39. data/app/components/iron_admin/form/control_classes.rb +108 -0
  40. data/app/components/iron_admin/form/date_picker_component.rb +3 -7
  41. data/app/components/iron_admin/form/field_wrapper_component.html.haml +3 -3
  42. data/app/components/iron_admin/form/field_wrapper_component.rb +3 -3
  43. data/app/components/iron_admin/form/nested_form_component.html.haml +4 -5
  44. data/app/components/iron_admin/form/nested_form_component.rb +12 -2
  45. data/app/components/iron_admin/form/select_component.html.haml +1 -1
  46. data/app/components/iron_admin/form/select_component.rb +2 -16
  47. data/app/components/iron_admin/form/text_input_component.rb +1 -5
  48. data/app/components/iron_admin/form/textarea_component.rb +1 -5
  49. data/app/components/iron_admin/layout/flash_component.html.haml +1 -1
  50. data/app/components/iron_admin/layout/flash_component.rb +4 -3
  51. data/app/components/iron_admin/layout/navbar_component.html.haml +15 -9
  52. data/app/components/iron_admin/layout/navbar_component.rb +29 -1
  53. data/app/components/iron_admin/layout/shell_component.html.haml +4 -4
  54. data/app/components/iron_admin/layout/shell_component.rb +16 -3
  55. data/app/components/iron_admin/layout/sidebar_component.html.haml +21 -25
  56. data/app/components/iron_admin/layout/sidebar_component.rb +76 -1
  57. data/app/components/iron_admin/layout/theme_switcher_component.html.haml +15 -0
  58. data/app/components/iron_admin/layout/theme_switcher_component.rb +74 -0
  59. data/app/components/iron_admin/resources/actions_component.html.haml +15 -8
  60. data/app/components/iron_admin/resources/actions_component.rb +34 -2
  61. data/app/components/iron_admin/resources/breadcrumb_component.html.haml +3 -3
  62. data/app/components/iron_admin/resources/breadcrumb_component.rb +8 -7
  63. data/app/components/iron_admin/resources/bulk_actions_component.html.haml +9 -8
  64. data/app/components/iron_admin/resources/bulk_actions_component.rb +47 -4
  65. data/app/components/iron_admin/resources/data_table_component.html.haml +15 -15
  66. data/app/components/iron_admin/resources/data_table_component.rb +34 -3
  67. data/app/components/iron_admin/resources/related_list_component.html.haml +7 -7
  68. data/app/components/iron_admin/resources/related_list_component.rb +26 -2
  69. data/app/components/iron_admin/resources/show_field_component.html.haml +3 -3
  70. data/app/components/iron_admin/resources/show_field_component.rb +10 -4
  71. data/app/components/iron_admin/resources/styles.rb +136 -0
  72. data/app/components/iron_admin/ui/alert_component.html.haml +2 -2
  73. data/app/components/iron_admin/ui/alert_component.rb +27 -31
  74. data/app/components/iron_admin/ui/badge_component.rb +40 -20
  75. data/app/components/iron_admin/ui/button_component.rb +37 -11
  76. data/app/components/iron_admin/ui/card_component.rb +3 -3
  77. data/app/components/iron_admin/ui/dropdown_component.html.haml +1 -1
  78. data/app/components/iron_admin/ui/dropdown_component.rb +34 -13
  79. data/app/components/iron_admin/ui/empty_state_component.html.haml +4 -5
  80. data/app/components/iron_admin/ui/empty_state_component.rb +2 -2
  81. data/app/components/iron_admin/ui/modal_component.html.haml +5 -5
  82. data/app/components/iron_admin/ui/modal_component.rb +6 -4
  83. data/app/components/iron_admin/ui/pagination_component.html.haml +14 -14
  84. data/app/components/iron_admin/ui/pagination_component.rb +26 -9
  85. data/app/components/iron_admin/ui/scopes_component.html.haml +2 -2
  86. data/app/components/iron_admin/ui/scopes_component.rb +44 -11
  87. data/app/components/iron_admin/ui/tooltip_component.rb +11 -4
  88. data/app/controllers/iron_admin/application_controller.rb +58 -4
  89. data/app/controllers/iron_admin/audit_controller.rb +30 -6
  90. data/app/controllers/iron_admin/concerns/action_executable.rb +37 -0
  91. data/app/controllers/iron_admin/concerns/assignable_references.rb +149 -0
  92. data/app/controllers/iron_admin/concerns/authorizable.rb +11 -0
  93. data/app/controllers/iron_admin/concerns/blank_password_filterable.rb +21 -0
  94. data/app/controllers/iron_admin/concerns/event_emittable.rb +47 -7
  95. data/app/controllers/iron_admin/concerns/filterable.rb +34 -9
  96. data/app/controllers/iron_admin/concerns/nested_permittable.rb +5 -3
  97. data/app/controllers/iron_admin/concerns/nested_tenant_check.rb +159 -0
  98. data/app/controllers/iron_admin/concerns/polymorphic_references.rb +131 -0
  99. data/app/controllers/iron_admin/concerns/scopeable.rb +54 -7
  100. data/app/controllers/iron_admin/concerns/searchable.rb +3 -3
  101. data/app/controllers/iron_admin/concerns/tenant_assignable.rb +118 -0
  102. data/app/controllers/iron_admin/concerns/tenant_containment.rb +146 -0
  103. data/app/controllers/iron_admin/exports_controller.rb +2 -2
  104. data/app/controllers/iron_admin/imports_controller.rb +1 -1
  105. data/app/controllers/iron_admin/resources_controller.rb +48 -41
  106. data/app/controllers/iron_admin/search_controller.rb +1 -2
  107. data/app/controllers/iron_admin/themes_controller.rb +50 -0
  108. data/app/controllers/iron_admin/tools_controller.rb +58 -5
  109. data/app/helpers/iron_admin/action_form_helper.rb +19 -0
  110. data/app/helpers/iron_admin/application_helper.rb +93 -16
  111. data/app/helpers/iron_admin/assignable_records_helper.rb +32 -0
  112. data/app/helpers/iron_admin/component_overrides_helper.rb +9 -4
  113. data/app/helpers/iron_admin/field_display_helper.rb +88 -43
  114. data/app/helpers/iron_admin/related_records_helper.rb +51 -0
  115. data/app/helpers/iron_admin/theme_helper.rb +56 -46
  116. data/app/javascript/iron_admin/controllers/cp_bulk_select_controller.js +12 -0
  117. data/app/javascript/iron_admin/controllers/cp_chart_controller.js +4 -0
  118. data/app/javascript/iron_admin/controllers/cp_disclosure_controller.js +19 -0
  119. data/app/javascript/iron_admin/controllers/cp_search_shortcut_controller.js +39 -0
  120. data/app/javascript/iron_admin/index.js +4 -0
  121. data/app/views/iron_admin/audit/index.html.haml +78 -81
  122. data/app/views/iron_admin/dashboard/index.html.haml +10 -9
  123. data/app/views/iron_admin/form/_action_field.html.haml +50 -0
  124. data/app/views/iron_admin/form/_nested_row.html.haml +18 -13
  125. data/app/views/iron_admin/imports/new.html.haml +14 -13
  126. data/app/views/iron_admin/imports/preview.html.haml +24 -21
  127. data/app/views/iron_admin/resources/_form.html.haml +70 -68
  128. data/app/views/iron_admin/resources/action_form.html.haml +16 -59
  129. data/app/views/iron_admin/resources/bulk_action_form.html.haml +20 -58
  130. data/app/views/iron_admin/resources/edit.html.haml +3 -3
  131. data/app/views/iron_admin/resources/index.html.haml +87 -74
  132. data/app/views/iron_admin/resources/new.html.haml +2 -2
  133. data/app/views/iron_admin/resources/show.html.haml +53 -41
  134. data/app/views/iron_admin/search/index.html.haml +17 -9
  135. data/app/views/iron_admin/shared/_pagination.html.haml +13 -10
  136. data/app/views/iron_admin/shared/error.html.haml +16 -12
  137. data/app/views/iron_admin/tools/action_form.html.haml +9 -55
  138. data/app/views/iron_admin/tools/show.html.haml +4 -5
  139. data/app/views/layouts/iron_admin/_document_head.html.haml +24 -0
  140. data/app/views/layouts/iron_admin/application.html.haml +4 -17
  141. data/app/views/layouts/iron_admin/minimal.html.haml +7 -0
  142. data/config/importmap.rb +2 -0
  143. data/config/locales/en.yml +23 -0
  144. data/config/routes.rb +1 -0
  145. data/docs/FAQ.md +87 -37
  146. data/docs/README.md +8 -2
  147. data/docs/components/README.md +6 -2
  148. data/docs/components/dashboard-components.md +24 -35
  149. data/docs/components/filter-components.md +44 -92
  150. data/docs/components/form-components.md +16 -32
  151. data/docs/components/resource-components.md +46 -27
  152. data/docs/components/ui-components.md +44 -26
  153. data/docs/getting-started/installation.md +5 -1
  154. data/docs/getting-started/quick-start.md +36 -23
  155. data/docs/guides/authentication.md +182 -42
  156. data/docs/guides/components.md +97 -15
  157. data/docs/guides/custom-adapters.md +24 -4
  158. data/docs/guides/dashboard.md +68 -44
  159. data/docs/guides/export.md +24 -8
  160. data/docs/guides/extending.md +206 -51
  161. data/docs/guides/fields.md +229 -70
  162. data/docs/guides/imports.md +33 -18
  163. data/docs/guides/resources.md +333 -111
  164. data/docs/guides/theming.md +930 -58
  165. data/docs/guides/troubleshooting.md +7 -3
  166. data/docs/plugin-system-design.md +8 -7
  167. data/docs/reference/configuration.md +51 -16
  168. data/docs/reference/routes.md +1 -0
  169. data/docs/reference/theme-properties.md +101 -38
  170. data/lib/generators/iron_admin/install/templates/initializer.rb.tt +7 -2
  171. data/lib/generators/iron_admin/install_audit/templates/create_iron_admin_audit_entries.rb.tt +2 -0
  172. data/lib/iron_admin/action_field.rb +11 -0
  173. data/lib/iron_admin/adapters/active_record.rb +95 -0
  174. data/lib/iron_admin/adapters/base.rb +107 -0
  175. data/lib/iron_admin/adapters/http/query.rb +21 -0
  176. data/lib/iron_admin/adapters/http.rb +84 -4
  177. data/lib/iron_admin/adapters/mongoid.rb +96 -2
  178. data/lib/iron_admin/audit_log.rb +64 -6
  179. data/lib/iron_admin/authentication_warning.rb +31 -0
  180. data/lib/iron_admin/concerns/filter_definable.rb +15 -1
  181. data/lib/iron_admin/concerns/nestable.rb +25 -0
  182. data/lib/iron_admin/concerns/scope_countable.rb +56 -0
  183. data/lib/iron_admin/concerns/soft_deletable.rb +28 -9
  184. data/lib/iron_admin/configuration/components.rb +14 -3
  185. data/lib/iron_admin/configuration/theme/neutral_defaults.rb +179 -0
  186. data/lib/iron_admin/configuration/theme.rb +292 -87
  187. data/lib/iron_admin/configuration.rb +340 -45
  188. data/lib/iron_admin/dashboard.rb +22 -38
  189. data/lib/iron_admin/engine.rb +16 -2
  190. data/lib/iron_admin/errors.rb +3 -0
  191. data/lib/iron_admin/field.rb +78 -17
  192. data/lib/iron_admin/field_inferrer.rb +61 -3
  193. data/lib/iron_admin/field_override_warning.rb +67 -0
  194. data/lib/iron_admin/import/import_preview.rb +56 -7
  195. data/lib/iron_admin/import/importer.rb +91 -14
  196. data/lib/iron_admin/import/tenant_precheck.rb +67 -0
  197. data/lib/iron_admin/plugin/registration.rb +64 -0
  198. data/lib/iron_admin/plugin_registry.rb +3 -2
  199. data/lib/iron_admin/policy.rb +22 -12
  200. data/lib/iron_admin/policy_coverage_warning.rb +46 -0
  201. data/lib/iron_admin/request_context.rb +18 -0
  202. data/lib/iron_admin/resource.rb +218 -77
  203. data/lib/iron_admin/resource_registry.rb +86 -13
  204. data/lib/iron_admin/scope_counts.rb +73 -0
  205. data/lib/iron_admin/tenant_scope.rb +30 -0
  206. data/lib/iron_admin/tool.rb +61 -12
  207. data/lib/iron_admin/tool_context.rb +10 -6
  208. data/lib/iron_admin/version.rb +1 -1
  209. data/lib/iron_admin.rb +29 -2
  210. metadata +37 -7
@@ -11,6 +11,12 @@ module IronAdmin
11
11
  # @example Basic pagination
12
12
  # render IronAdmin::Ui::PaginationComponent.new(pagy: @pagy)
13
13
  class PaginationComponent < ViewComponent::Base
14
+ # Size, shape and type of every page and prev/next link; the theme adds
15
+ # their border width, radius and colors.
16
+ # @return [String]
17
+ ITEM_CLASSES = "inline-flex h-8 min-w-8 items-center justify-center px-2 text-sm font-medium tabular-nums " \
18
+ "transition-colors duration-150"
19
+
14
20
  # @return [Pagy] The Pagy pagination object
15
21
  attr_reader :pagy_obj
16
22
 
@@ -20,9 +26,9 @@ module IronAdmin
20
26
  end
21
27
 
22
28
  # @api private
23
- # @return [IronAdmin::Configuration::Theme] Theme configuration
29
+ # @return [IronAdmin::Configuration::Theme] Theme of the current request
24
30
  def theme
25
- IronAdmin.configuration.theme
31
+ IronAdmin.current_theme
26
32
  end
27
33
 
28
34
  # @api private
@@ -48,13 +54,13 @@ module IronAdmin
48
54
 
49
55
  # @api private
50
56
  # @param active [Boolean] Whether this is the current page
51
- # @return [String] CSS classes for page number links
57
+ # @return [String] CSS classes for page number links: the theme's
58
+ # +pagination_active+ for the current page, else a ghost link
52
59
  def page_link_classes(active: false)
53
- base = "px-3 py-2 text-sm font-medium border transition-colors duration-150"
54
60
  if active
55
- "#{base} bg-indigo-50 border-indigo-500 text-indigo-600 z-10"
61
+ "#{item_base} #{theme.pagination_active}"
56
62
  else
57
- "#{base} #{theme.card_bg} #{theme.card_border} #{theme.body_text} hover:bg-gray-50"
63
+ "#{item_base} border-transparent #{theme.body_text} #{theme.item_hover} #{theme.focus_ring}"
58
64
  end
59
65
  end
60
66
 
@@ -62,13 +68,24 @@ module IronAdmin
62
68
  # @param disabled [Boolean] Whether nav link is disabled
63
69
  # @return [String] CSS classes for prev/next navigation links
64
70
  def nav_link_classes(disabled: false)
65
- base = "relative inline-flex items-center px-3 py-2 text-sm font-medium border transition-colors duration-150"
66
71
  if disabled
67
- "#{base} #{theme.card_bg} #{theme.card_border} text-gray-300 cursor-not-allowed"
72
+ "#{item_base} border-transparent #{theme.disabled_text} cursor-not-allowed"
68
73
  else
69
- "#{base} #{theme.card_bg} #{theme.card_border} #{theme.body_text} hover:bg-gray-50"
74
+ "#{item_base} border-transparent #{theme.body_text} #{theme.item_hover} #{theme.focus_ring}"
70
75
  end
71
76
  end
77
+
78
+ # @api private
79
+ # @return [String] CSS classes for the gap between page numbers
80
+ def gap_classes
81
+ "#{item_base} border-transparent #{theme.muted_text} cursor-default"
82
+ end
83
+
84
+ private
85
+
86
+ def item_base
87
+ "#{ITEM_CLASSES} #{theme.border_width} #{theme.control_radius}"
88
+ end
72
89
  end
73
90
  end
74
91
  end
@@ -1,3 +1,3 @@
1
- .flex.gap-1.border-b{ class: theme.card_border }
1
+ .flex.gap-1.overflow-x-auto.cp-scroll-fade.border-b{ class: theme.card_border }
2
2
  - scopes.each do |scope|
3
- = link_to scope[:name].to_s.humanize, scope_url(scope[:name]), class: scope_classes(scope)
3
+ = link_to scope_label(scope), scope_url(scope[:name]), class: scope_classes(scope), aria: { current: ("page" if active?(scope)) }
@@ -2,13 +2,22 @@
2
2
 
3
3
  module IronAdmin
4
4
  module Ui
5
- # Renders scope tabs for filtering resource lists.
5
+ # Renders scope tabs for filtering resource lists. Each tab links to
6
+ # +base_path+ with +params+ and its own scope, without the current scope
7
+ # and page, so switching tabs keeps the search, filters and sort and
8
+ # starts back at the first page.
6
9
  #
7
- # @example Scopes from resource
10
+ # Pass +counts+ (the hash the index assigns before rendering) to show
11
+ # how many records each tab would list. A missing key draws no number.
12
+ # Zero draws zero. Template overrides can read {#counts} and {#scope_label}.
13
+ #
14
+ # @example Scopes from resource, keeping the index state
8
15
  # render IronAdmin::Ui::ScopesComponent.new(
9
16
  # scopes: @resource_class.all_scopes,
10
17
  # current_scope: @current_scope,
11
- # base_path: resources_path(@resource_class.resource_name)
18
+ # base_path: resources_path(@resource_class.resource_name),
19
+ # params: iron_admin_index_state,
20
+ # counts: @scope_counts
12
21
  # )
13
22
  class ScopesComponent < ViewComponent::Base
14
23
  # @return [Array<Hash>] Scope definitions
@@ -20,31 +29,54 @@ module IronAdmin
20
29
  # @return [String] Base URL path
21
30
  attr_reader :base_path
22
31
 
23
- # @return [Hash] Additional query params
32
+ # @return [Hash] Query params the tabs keep
24
33
  attr_reader :params
25
34
 
35
+ # Counts keyed by scope name. A missing key draws no number; zero draws zero.
36
+ # Template overrides can read this.
37
+ #
38
+ # @return [Hash{String => Integer}]
39
+ attr_reader :counts
40
+
41
+ # Params a tab link never keeps: it sets its own scope and starts back at the first page.
42
+ DROPPED_PARAMS = %w[scope page].freeze
43
+
26
44
  # @param scopes [Array<Hash>] Scope definitions
27
45
  # @param current_scope [String, nil] Active scope name
28
46
  # @param base_path [String] Base URL
29
- # @param params [Hash] Additional query params
30
- def initialize(scopes:, current_scope:, base_path:, params: {})
47
+ # @param params [Hash] Query params the tabs keep, such as +iron_admin_index_state+
48
+ # (the search, filters and sort); their +scope+ and +page+ are dropped
49
+ # @param counts [Hash, nil] Record counts keyed by scope name (String or Symbol).
50
+ # The index computes this before rendering. The default, +{}+, draws names only.
51
+ def initialize(scopes:, current_scope:, base_path:, params: {}, counts: {})
31
52
  @scopes = scopes
32
53
  @current_scope = current_scope
33
54
  @base_path = base_path
34
55
  @params = params
56
+ @counts = (counts || {}).to_h.transform_keys(&:to_s)
57
+ end
58
+
59
+ # Label of a tab, including its count when one was computed.
60
+ # Template overrides can call this.
61
+ #
62
+ # @param scope [Hash] Scope definition
63
+ # @return [String]
64
+ def scope_label(scope)
65
+ ScopeCounts.label(scope, counts)
35
66
  end
36
67
 
37
68
  # @api private
38
- # @return [IronAdmin::Configuration::Theme] Theme configuration
69
+ # @return [IronAdmin::Configuration::Theme] Theme of the current request
39
70
  def theme
40
- IronAdmin.configuration.theme
71
+ IronAdmin.current_theme
41
72
  end
42
73
 
43
74
  # @api private
44
75
  # @param scope_name [Symbol, String] Scope name
45
- # @return [String] URL with scope parameter
76
+ # @return [String] URL with +params+ and the scope, without a page
46
77
  def scope_url(scope_name)
47
- "#{base_path}?#{params.merge(scope: scope_name).to_query}"
78
+ query = params.to_h.stringify_keys.except(*DROPPED_PARAMS).merge("scope" => scope_name.to_s)
79
+ "#{base_path}?#{query.to_query}"
48
80
  end
49
81
 
50
82
  # @api private
@@ -58,7 +90,8 @@ module IronAdmin
58
90
  # @param scope [Hash] Scope definition
59
91
  # @return [String] CSS classes for scope tab
60
92
  def scope_classes(scope)
61
- base = "px-4 py-2 text-sm font-medium border-b-2 -mb-px transition-colors duration-150"
93
+ base = "px-4 py-2 text-sm font-medium whitespace-nowrap border-b-2 -mb-px transition-colors duration-150 " \
94
+ "#{theme.focus_ring}"
62
95
  if active?(scope)
63
96
  "#{base} #{theme.scope_active}"
64
97
  else
@@ -38,11 +38,18 @@ module IronAdmin
38
38
  end
39
39
 
40
40
  # @api private
41
- # @return [String] CSS classes for tooltip container
41
+ # @return [IronAdmin::Configuration::Theme] Theme of the current request
42
+ def theme
43
+ IronAdmin.current_theme
44
+ end
45
+
46
+ # @api private
47
+ # @return [String] CSS classes for tooltip container: shown on hover and
48
+ # while the trigger has keyboard focus, colored by the theme's +tooltip+
42
49
  def tooltip_classes
43
- "absolute #{position_classes} px-2 py-1 text-xs font-medium text-white bg-gray-900 " \
44
- "rounded whitespace-nowrap opacity-0 invisible group-hover:opacity-100 group-hover:visible " \
45
- "transition-all duration-150 z-50"
50
+ "absolute #{position_classes} px-2 py-1 text-xs font-medium whitespace-nowrap pointer-events-none " \
51
+ "opacity-0 invisible group-hover:opacity-100 group-hover:visible group-focus-within:opacity-100 " \
52
+ "group-focus-within:visible transition-all duration-150 z-50 #{theme.tooltip} #{theme.control_radius}"
46
53
  end
47
54
  end
48
55
  end
@@ -3,28 +3,41 @@
3
3
  module IronAdmin
4
4
  # Base controller for all IronAdmin admin panel controllers.
5
5
  #
6
- # Provides authentication and current user handling for the admin panel.
7
- # All other IronAdmin controllers inherit from this class.
6
+ # Provides theme resolution, authentication and current user handling for the
7
+ # admin panel. All other IronAdmin controllers inherit from this class.
8
8
  #
9
9
  # @see IronAdmin::Configuration#authenticate
10
10
  # @see IronAdmin::Configuration#current_user
11
11
  class ApplicationController < ::ActionController::Base
12
12
  include Pagy::Method
13
13
 
14
+ # Before authentication, so pages and redirects it renders use the request's theme.
15
+ before_action :resolve_iron_admin_theme
14
16
  before_action :authenticate_iron_admin_user!
15
17
 
16
18
  rescue_from IronAdmin::RecordNotFound, with: :render_not_found
17
19
  rescue_from ActionController::ParameterMissing, with: :render_bad_request
18
20
 
19
21
  helper_method :iron_admin_current_user, :iron_admin_filter_param, :iron_admin_active_filter?
22
+ helper_method :iron_admin_error_navigation?
20
23
 
21
24
  private
22
25
 
26
+ # @api private
27
+ def resolve_iron_admin_theme
28
+ IronAdmin::RequestContext.theme = IronAdmin.configuration.resolve_theme(request)
29
+ end
30
+
31
+ # Only an exact false from the block denies access, with 403. nil and any
32
+ # other value allow the request. A redirect or render from the block is kept.
23
33
  # @api private
24
34
  def authenticate_iron_admin_user!
25
35
  return unless IronAdmin.configuration.authenticate_block
26
36
 
27
- instance_exec(self, &IronAdmin.configuration.authenticate_block)
37
+ result = instance_exec(self, &IronAdmin.configuration.authenticate_block)
38
+ return if performed?
39
+
40
+ render_authentication_denied if result.equal?(false)
28
41
  end
29
42
 
30
43
  # @api private
@@ -34,6 +47,16 @@ module IronAdmin
34
47
  @iron_admin_current_user ||= instance_exec(self, &IronAdmin.configuration.current_user_block)
35
48
  end
36
49
 
50
+ # The current tenant's audit key from +config.audit_tenant+, as a String;
51
+ # nil without the block or when it returns nothing.
52
+ # @api private
53
+ def iron_admin_audit_tenant_key
54
+ block = IronAdmin.configuration.audit_tenant_block
55
+ return unless block
56
+
57
+ instance_exec(self, &block)&.to_s.presence
58
+ end
59
+
37
60
  def iron_admin_filter_param(*keys)
38
61
  iron_admin_param_dig(params[:filters], *keys)
39
62
  end
@@ -66,6 +89,34 @@ module IronAdmin
66
89
  render_error_page(:forbidden)
67
90
  end
68
91
 
92
+ # The admin shell asks every resource whether the visitor may read it.
93
+ # A condition such as `user.admin?` raises when that visitor is nil, so
94
+ # this denial keeps the error page and the theme, without the shell or
95
+ # a link back into the admin. Policy denials still use {#render_forbidden}.
96
+ # The template's only local is +error+. Navigation is a helper, so a host
97
+ # override that declares strict locals for +error+ still renders.
98
+ def render_authentication_denied
99
+ @iron_admin_error_navigation = false
100
+
101
+ respond_to do |format|
102
+ format.html do
103
+ render template: "iron_admin/shared/error",
104
+ layout: "iron_admin/minimal",
105
+ status: :forbidden,
106
+ locals: { error: :forbidden }
107
+ end
108
+ format.any { head(:forbidden) }
109
+ end
110
+ end
111
+
112
+ # True on policy denials and 404s, which render inside the admin layout.
113
+ # An authentication denial sets it false: the visitor is not signed in,
114
+ # so the error page must not link into the admin or run a policy.
115
+ # @return [Boolean]
116
+ def iron_admin_error_navigation?
117
+ @iron_admin_error_navigation != false
118
+ end
119
+
69
120
  # A bare 403/404 makes the browser show its own error page, so HTML requests
70
121
  # get a page inside the admin layout. Other formats keep the bare status.
71
122
  def render_error_page(status)
@@ -80,7 +131,10 @@ module IronAdmin
80
131
  end
81
132
 
82
133
  def log_iron_admin_error(error)
83
- Rails.logger.error("[IronAdmin] #{error.class}: #{error.message}")
134
+ message = "[IronAdmin] #{error.class}: #{error.message}"
135
+ backtrace = Rails.backtrace_cleaner.clean(Array(error.backtrace)).first(10)
136
+ message = "#{message}\n#{backtrace.join("\n")}" if backtrace.any?
137
+ Rails.logger.error(message)
84
138
  end
85
139
  end
86
140
  end
@@ -24,10 +24,23 @@ module IronAdmin
24
24
  @action_options = action_options
25
25
  @resource_labels = resource_labels
26
26
  @resource_options = resource_options
27
+ @user_labels = user_labels
27
28
  end
28
29
 
29
30
  private
30
31
 
32
+ # Labels for the User column, looked up once for the whole page. If the
33
+ # lookup fails, the error is logged and the column shows identifiers.
34
+ #
35
+ # @api private
36
+ # @return [Hash{String => String}]
37
+ def user_labels
38
+ AuditLog.user_labels(@entries)
39
+ rescue StandardError => e
40
+ log_iron_admin_error(e)
41
+ {}
42
+ end
43
+
31
44
  # Actions offered by the Action filter: every action logged so far,
32
45
  # custom resource actions included, plus the one currently selected.
33
46
  #
@@ -56,13 +69,23 @@ module IronAdmin
56
69
  @resource_labels.to_a + (others - @resource_labels.keys).map { |name| [name, name] }
57
70
  end
58
71
 
72
+ # Values of +attribute+ in the entries the page may list: with
73
+ # +config.audit_tenant+, only the current tenant's.
74
+ #
59
75
  # @api private
60
76
  def logged_values(attribute)
61
- if AuditLog.database_storage_available?
62
- AuditEntry.distinct.pluck(attribute)
63
- else
64
- AuditLog.entries.map { |entry| entry.public_send(attribute).to_s }
65
- end
77
+ entries = AuditLog.query(tenant_filter)
78
+ return entries.map { |entry| entry.public_send(attribute).to_s } if entries.is_a?(Array)
79
+
80
+ entries.reorder(nil).distinct.pluck(attribute)
81
+ end
82
+
83
+ # @api private
84
+ # @return [Hash] the +:tenant_key+ filter when +config.audit_tenant+ is set
85
+ def tenant_filter
86
+ return {} unless IronAdmin.configuration.audit_tenant_block
87
+
88
+ { tenant_key: iron_admin_audit_tenant_key }
66
89
  end
67
90
 
68
91
  # @api private
@@ -73,7 +96,8 @@ module IronAdmin
73
96
  filters[:from] = parse_time(params[:from])
74
97
  filters[:to] = parse_time(params[:to])&.then { |time| date_only?(params[:to]) ? time.end_of_day : time }
75
98
 
76
- AuditLog.query(filters.compact)
99
+ # The tenant filter is added after compact: a nil key must match nothing.
100
+ AuditLog.query(filters.compact.merge(tenant_filter))
77
101
  end
78
102
 
79
103
  # @api private
@@ -71,6 +71,43 @@ module IronAdmin
71
71
  permitted_keys = fields.map(&:name)
72
72
  params.fetch(:action_form, {}).permit(*permitted_keys).to_h.symbolize_keys
73
73
  end
74
+
75
+ # Sets what an action form view renders: the action, its fields, the URL
76
+ # the form posts to, the value of each field and its errors.
77
+ #
78
+ # @param action [Hash] The action definition
79
+ # @param url [String] Path of the action the form posts to
80
+ # @param values [Hash, nil] Submitted values, or nil for the fields' defaults
81
+ # @param errors [Hash{Symbol => Array<String>}] Error messages by field name
82
+ # @return [void]
83
+ def assign_action_form(action, url, values: nil, errors: {})
84
+ @action = action
85
+ @form_fields = action[:form_fields]
86
+ @form_url = url
87
+ @form_values = values || @form_fields.to_h { |field| [field.name, field.default] }
88
+ @form_errors = errors
89
+ end
90
+
91
+ # Renders the action's form again, as unprocessable, when a required
92
+ # field is blank: the action does not run, and the form keeps the
93
+ # submitted values with an error under each blank field. A direct POST
94
+ # that leaves the field out gets the same form.
95
+ #
96
+ # @param action [Hash] The action definition
97
+ # @param collected [Hash] The permitted form params
98
+ # @param template [Symbol] :action_form or :bulk_action_form
99
+ # @param url [String] Path of the action the form posts to
100
+ # @return [Boolean] Whether it rendered the form
101
+ def render_invalid_action_form?(action, collected, template, url)
102
+ errors = Array(action[:form_fields]).each_with_object({}) do |field, found|
103
+ found[field.name] = [I18n.t("errors.messages.blank")] if field.missing?(collected[field.name])
104
+ end
105
+ return false if errors.empty?
106
+
107
+ assign_action_form(action, url, values: collected, errors: errors)
108
+ render template, status: :unprocessable_content
109
+ true
110
+ end
74
111
  end
75
112
  end
76
113
  end
@@ -0,0 +1,149 @@
1
+ # frozen_string_literal: true
2
+
3
+ module IronAdmin
4
+ module Concerns
5
+ # Rejects foreign keys outside the tenant and polymorphic types outside the allowlist.
6
+ module AssignableReferences
7
+ extend ActiveSupport::Concern
8
+ include PolymorphicReferences
9
+
10
+ private
11
+
12
+ # Whether every posted reference points at an assignable record.
13
+ #
14
+ # The polymorphic type allowlist always applies. Tenant checks run only
15
+ # when a tenant scope is configured. Only keys present in +attrs+ are
16
+ # checked. Each rejected reference adds an error on +record+.
17
+ #
18
+ # @param record [ActiveRecord::Base] record about to be saved
19
+ # @param attrs [ActionController::Parameters, Hash] permitted attributes
20
+ # @return [Boolean] true when this check added no error
21
+ def references_assignable?(record, attrs)
22
+ attrs = deep_stringify(unwrap_params(attrs))
23
+ types_allowed = polymorphic_types_allowed?(record, attrs)
24
+ IronAdmin::TenantScope.configured? ? reference_checks_pass?(record, attrs) && types_allowed : types_allowed
25
+ end
26
+
27
+ def reference_checks_pass?(record, attrs)
28
+ [
29
+ belongs_to_references_assignable?(record, attrs),
30
+ polymorphic_references_assignable?(record, attrs),
31
+ habtm_references_assignable?(record, attrs),
32
+ nested_references_assignable?(record, attrs),
33
+ ].all?
34
+ end
35
+
36
+ def belongs_to_references_assignable?(record, attrs)
37
+ belongs_to_fields.map { |field| belongs_to_field_assignable?(record, attrs, field) }.all?
38
+ end
39
+
40
+ def belongs_to_fields
41
+ writable_form_fields.select { |field| field.type == :belongs_to }
42
+ end
43
+
44
+ def belongs_to_field_assignable?(record, attrs, field)
45
+ return true if foreign_key_assignable?(attrs, field, @resource_class.model)
46
+
47
+ record.errors.add(field.name, message: not_assignable_message)
48
+ false
49
+ end
50
+
51
+ def habtm_references_assignable?(record, attrs)
52
+ @resource_class.habtm_associations.map { |assoc| habtm_association_assignable?(record, attrs, assoc) }.all?
53
+ end
54
+
55
+ def habtm_association_assignable?(record, attrs, assoc)
56
+ column = "#{assoc[:name].to_s.singularize}_ids"
57
+ return true unless attrs.key?(column)
58
+ return true if habtm_ids_assignable?(assoc, attrs[column])
59
+
60
+ record.errors.add(assoc[:name], message: not_assignable_message)
61
+ false
62
+ end
63
+
64
+ def habtm_ids_assignable?(assoc, raw_ids)
65
+ ids = Array(raw_ids).compact_blank.uniq
66
+ return true if ids.empty?
67
+
68
+ klass = assoc[:reflection].klass
69
+ helpers.assignable_records(klass).where(klass.primary_key => ids).count == ids.size
70
+ end
71
+
72
+ def nested_references_assignable?(record, attrs)
73
+ @resource_class.nested_associations.map { |nested| nested_association_assignable?(record, attrs, nested) }.all?
74
+ end
75
+
76
+ def nested_association_assignable?(record, attrs, nested)
77
+ column = "#{nested.name}_attributes"
78
+ return true unless attrs.key?(column)
79
+
80
+ nested_child_rows(nested, attrs[column]).map { |child| nested_child_assignable?(record, nested, child) }.all?
81
+ end
82
+
83
+ def nested_child_rows(nested, raw)
84
+ rows = nested.kind == :has_one ? [raw] : indexed_or_array_rows(raw)
85
+ rows.grep(Hash)
86
+ end
87
+
88
+ def indexed_or_array_rows(raw)
89
+ raw.is_a?(Hash) ? raw.values : Array(raw)
90
+ end
91
+
92
+ def nested_child_assignable?(record, nested, child)
93
+ nested_belongs_to_fields(nested).map { |field| nested_field_assignable?(record, nested, child, field) }.all?
94
+ end
95
+
96
+ def nested_belongs_to_fields(nested)
97
+ Array(nested.fields).select { |field| field.type == :belongs_to }
98
+ end
99
+
100
+ def nested_field_assignable?(record, nested, child, field)
101
+ return true if foreign_key_assignable?(child, field, nested.reflection.klass)
102
+
103
+ record.errors.add(:base, message: nested_not_assignable_message(nested))
104
+ false
105
+ end
106
+
107
+ def foreign_key_assignable?(attrs, field, owner_class)
108
+ column = field.options[:foreign_key].to_s
109
+ return true unless present_attr?(attrs, column)
110
+
111
+ value_key = helpers.association_value_key(owner_class, field.options[:association_name])
112
+ helpers.assignable_records(field.options[:association_class]).exists?(value_key => attrs[column])
113
+ end
114
+
115
+ def present_attr?(attrs, column)
116
+ key = column.to_s
117
+ attrs.is_a?(Hash) && attrs.key?(key) && attrs[key].present?
118
+ end
119
+
120
+ def not_assignable_message
121
+ I18n.t("iron_admin.resources.errors.not_assignable")
122
+ end
123
+
124
+ def nested_not_assignable_message(nested)
125
+ I18n.t(
126
+ "iron_admin.resources.errors.nested_not_assignable",
127
+ association: nested.name.to_s.humanize
128
+ )
129
+ end
130
+
131
+ def unwrap_params(value)
132
+ return value.to_unsafe_h if value.respond_to?(:to_unsafe_h)
133
+
134
+ value.is_a?(ActionController::Parameters) ? value.to_h : value
135
+ end
136
+
137
+ def deep_stringify(value)
138
+ case value
139
+ when Hash
140
+ value.to_h { |key, nested| [key.to_s, deep_stringify(unwrap_params(nested))] }
141
+ when Array
142
+ value.map { |nested| deep_stringify(unwrap_params(nested)) }
143
+ else
144
+ value
145
+ end
146
+ end
147
+ end
148
+ end
149
+ end
@@ -14,6 +14,17 @@ module IronAdmin
14
14
  render_forbidden unless @resource_class.crud_allowed?(crud_action_name, iron_admin_current_user)
15
15
  end
16
16
 
17
+ # The index lists a bulk action only when its policy condition allows it.
18
+ # A condition that raises (for example `user.admin?` when the current
19
+ # user is nil) hides that action and is logged. Posting the action does
20
+ # not use this method, so it still runs the condition.
21
+ def listed_bulk_action?(action)
22
+ action_authorized?(action[:name])
23
+ rescue StandardError => e
24
+ log_iron_admin_error(e)
25
+ false
26
+ end
27
+
17
28
  def crud_action_name
18
29
  case action_name.to_sym
19
30
  when :index, :show, :autocomplete then :read
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module IronAdmin
4
+ module Concerns
5
+ # Omits blank password params so an empty input does not clear the stored value.
6
+ module BlankPasswordFilterable
7
+ extend ActiveSupport::Concern
8
+
9
+ private
10
+
11
+ # Password inputs never render the stored value, so a blank one means "unchanged".
12
+ #
13
+ # @param attrs [Hash, ActionController::Parameters] permitted attributes
14
+ # @return [Hash, ActionController::Parameters]
15
+ def without_blank_passwords(attrs)
16
+ names = password_field_names
17
+ attrs.reject { |name, value| name.in?(names) && value.blank? }
18
+ end
19
+ end
20
+ end
21
+ end