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
@@ -0,0 +1,146 @@
1
+ # frozen_string_literal: true
2
+
3
+ module IronAdmin
4
+ module Concerns
5
+ # Rolls a write back when the saved record would sit outside the tenant.
6
+ module TenantContainment
7
+ extend ActiveSupport::Concern
8
+
9
+ include NestedTenantCheck
10
+
11
+ private
12
+
13
+ # Runs a write and keeps it inside the tenant.
14
+ #
15
+ # With no tenant scope configured, returns the block's value. Adapters
16
+ # that support transactions run the block inside a transaction and roll
17
+ # it back when the saved record or a persisted nested child is outside
18
+ # the tenant. Adapters that cannot roll back verify an in-memory
19
+ # candidate, including each nested row, and yield only when every
20
+ # check is +:inside+.
21
+ #
22
+ # @param record [Object] record being created or updated
23
+ # @param attrs [Hash, nil] update attributes, or +nil+ on create
24
+ # @param submitted [Hash, nil] create params, read when +attrs+ is +nil+
25
+ # @yield the write, skipped when a non-transactional check is not +:inside+
26
+ # @return [Boolean, Object] +false+ when rejected, otherwise the block value
27
+ def write_within_tenant(record, attrs = nil, submitted = nil, &)
28
+ return yield unless IronAdmin::TenantScope.configured?
29
+ return verify_then_write(record, attrs, submitted, &) unless adapter.supports_transactions?
30
+
31
+ outcome = nil
32
+ adapter.transaction do
33
+ adapter.wrap_rollback do
34
+ outcome = yield
35
+ if outcome && !record_within_tenant?(record)
36
+ record.errors.add(:base, I18n.t("iron_admin.resources.errors.outside_tenant"))
37
+ outcome = false
38
+ raise IronAdmin::Rollback
39
+ end
40
+ end
41
+ end
42
+ outcome
43
+ end
44
+
45
+ # Yields only when the parent and every nested row are inside the tenant.
46
+ #
47
+ # +:outside+ is outside the tenant. +:unknown+ means the adapter cannot
48
+ # tell. Both add an error on +:base+ and skip the write. Nested rows come
49
+ # from +attrs+, or from +submitted+ when +attrs+ is +nil+ (create).
50
+ #
51
+ # @param record [Object]
52
+ # @param attrs [Hash, nil]
53
+ # @param submitted [Hash, nil]
54
+ # @yield the write
55
+ # @return [Boolean, Object]
56
+ def verify_then_write(record, attrs, submitted = nil)
57
+ verdict = prewrite_verdict(record, attrs, submitted)
58
+ return yield if verdict == :inside
59
+
60
+ record.errors.add(:base, containment_error(verdict))
61
+ false
62
+ end
63
+
64
+ # @param record [Object]
65
+ # @param attrs [Hash, nil]
66
+ # @param submitted [Hash, nil]
67
+ # @return [Symbol] +:inside+, +:outside+, or +:unknown+
68
+ def prewrite_verdict(record, attrs, submitted)
69
+ parent = tenant_verdict(record, attrs)
70
+ return parent unless parent == :inside
71
+
72
+ nested_rows_verdict(record, attrs.nil? ? submitted : attrs)
73
+ end
74
+
75
+ # @param record [Object]
76
+ # @param attrs [Hash, nil] +nil+ uses +record+ itself (create)
77
+ # @return [Symbol] +:inside+, +:outside+, or +:unknown+
78
+ def tenant_verdict(record, attrs)
79
+ candidate = attrs.nil? ? record : adapter.preview(record, attrs)
80
+ return :unknown if candidate.nil?
81
+
82
+ adapter.scope_match(base_scope, candidate)
83
+ end
84
+
85
+ # @param verdict [Symbol] +:outside+ or +:unknown+
86
+ # @return [String]
87
+ def containment_error(verdict)
88
+ key = verdict == :outside ? "outside_tenant" : "tenant_unverifiable"
89
+ I18n.t("iron_admin.resources.errors.#{key}")
90
+ end
91
+
92
+ # Whether +record+ and its persisted nested children are inside the tenant.
93
+ #
94
+ # +record+ must be found in {#base_scope} by its primary key. A composite
95
+ # key is the values joined with +"_"+, which is what {#find_record} accepts.
96
+ # Each persisted child of +@resource_class.nested_associations+ (a has_one
97
+ # child included) must be found in +TenantScope.apply(child.class.all)+ by
98
+ # that child's primary key.
99
+ #
100
+ # @param record [Object]
101
+ # @return [Boolean]
102
+ def record_within_tenant?(record)
103
+ return false unless found_in_scope?(base_scope, record)
104
+
105
+ nested_children_within_tenant?(record)
106
+ end
107
+
108
+ def found_in_scope?(scope, record)
109
+ find_record(scope, primary_key_value(record))
110
+ true
111
+ rescue IronAdmin::RecordNotFound
112
+ false
113
+ end
114
+
115
+ # @param record [Object]
116
+ # @return [String, Object] composite ids joined with +"_"+, or the raw key
117
+ def primary_key_value(record)
118
+ key = adapter.primary_key
119
+ return key.map { |column| record.public_send(column) }.join("_") if key.is_a?(Array)
120
+
121
+ record.public_send(key)
122
+ end
123
+
124
+ def nested_children_within_tenant?(record)
125
+ @resource_class.nested_associations.all? do |nested|
126
+ Array(record.public_send(nested.name)).all? { |child| child_inside_tenant?(child) }
127
+ end
128
+ end
129
+
130
+ def child_inside_tenant?(child)
131
+ return true if child.nil? || (child.respond_to?(:persisted?) && !child.persisted?)
132
+
133
+ scope = IronAdmin::TenantScope.apply(child.class.all)
134
+ child_scope_includes?(scope, child)
135
+ end
136
+
137
+ def child_scope_includes?(scope, child)
138
+ key = child.class.primary_key
139
+ located = Array(key).reduce(scope) do |current, column|
140
+ adapter.filter(current, column.to_sym, child.public_send(column))
141
+ end.first
142
+ !located.nil?
143
+ end
144
+ end
145
+ end
146
+ end
@@ -38,7 +38,7 @@ module IronAdmin
38
38
  end
39
39
 
40
40
  format.json do
41
- data = records.map { |r| fields.each_with_object({}) { |f, h| h[f.name] = safe_field_value(r, f, :json) } }
41
+ data = records.map { |r| fields.to_h { |f| [f.name, safe_field_value(r, f, :json)] } }
42
42
  render json: data
43
43
  end
44
44
  end
@@ -47,7 +47,7 @@ module IronAdmin
47
47
  private
48
48
 
49
49
  def set_resource_class
50
- @resource_class = ResourceRegistry.find(params[:resource_name])
50
+ @resource_class = ResourceRegistry.find(params.expect(:resource_name))
51
51
  render_not_found unless @resource_class
52
52
  end
53
53
 
@@ -63,7 +63,7 @@ module IronAdmin
63
63
  private
64
64
 
65
65
  def set_resource_class
66
- @resource_class = ResourceRegistry.find(params[:resource_name])
66
+ @resource_class = ResourceRegistry.find(params.expect(:resource_name))
67
67
  render_not_found unless @resource_class
68
68
  end
69
69
 
@@ -19,7 +19,9 @@ module IronAdmin
19
19
  # @see IronAdmin::Resource
20
20
  class ResourcesController < ApplicationController
21
21
  include Concerns::ActionExecutable
22
+ include Concerns::AssignableReferences
22
23
  include Concerns::Authorizable
24
+ include Concerns::BlankPasswordFilterable
23
25
  include Concerns::EventEmittable
24
26
  include Concerns::Filterable
25
27
  include Concerns::IndexReturnable
@@ -28,6 +30,7 @@ module IronAdmin
28
30
  include Concerns::NestedPermittable
29
31
  include Concerns::Scopeable
30
32
  include Concerns::Searchable
33
+ include Concerns::TenantAssignable
31
34
 
32
35
  before_action :set_resource_class
33
36
  before_action :check_action_allowed, only: %i[index show new create edit update destroy autocomplete]
@@ -40,7 +43,8 @@ module IronAdmin
40
43
  return redirect_to_last_page if @pagy.page > @pagy.last
41
44
 
42
45
  @fields = index_fields
43
- @current_scope = current_scope_name
46
+ assign_index_scope
47
+ @permitted_bulk_actions = @resource_class.defined_bulk_actions.select { |action| listed_bulk_action?(action) }
44
48
  end
45
49
 
46
50
  # Shows a single record.
@@ -58,7 +62,7 @@ module IronAdmin
58
62
  #
59
63
  # @return [void]
60
64
  def new
61
- @record = adapter.build
65
+ @record = adapter.build_in(base_scope)
62
66
  @fields = form_fields
63
67
  end
64
68
 
@@ -75,10 +79,11 @@ module IronAdmin
75
79
  #
76
80
  # @return [void]
77
81
  def create
78
- @record = adapter.build(resource_params)
82
+ submitted = with_nested_tenant_attributes(resource_params)
83
+ @record = adapter.build_in(base_scope, submitted)
79
84
  return render_invalid_json(:new) if invalid_json_submission?
80
85
 
81
- if adapter.save(@record)
86
+ if references_assignable?(@record, submitted) && write_within_tenant(@record, nil, submitted) { adapter.save(@record) }
82
87
  emit_event(:create, @record)
83
88
  redirect_to resource_path(@resource_class.resource_name, @record),
84
89
  notice: I18n.t("iron_admin.resources.create.success", model: adapter.human_name)
@@ -94,12 +99,12 @@ module IronAdmin
94
99
  # @raise [IronAdmin::RecordNotFound] if record doesn't exist
95
100
  def update
96
101
  @record = find_record(record_scope, params[:id])
97
- attrs = without_blank_passwords(resource_params)
102
+ attrs = with_nested_tenant_attributes(without_tenant_attributes(without_blank_passwords(resource_params)))
98
103
  return render_invalid_json(:edit) if invalid_json_submission?
99
104
 
100
105
  purge_attachments(@record)
101
106
 
102
- if adapter.update(@record, attrs)
107
+ if references_assignable?(@record, attrs) && write_within_tenant(@record, prepared = preserve_hidden_habtm_ids(attrs)) { adapter.update(@record, prepared) } # rubocop:disable Layout/LineLength
103
108
  emit_event(:update, @record)
104
109
  redirect_to resource_path(@resource_class.resource_name, @record),
105
110
  notice: I18n.t("iron_admin.resources.update.success", model: adapter.human_name)
@@ -130,21 +135,17 @@ module IronAdmin
130
135
 
131
136
  return unless prepare_action_record?(@action)
132
137
 
133
- @form_fields = @action[:form_fields]
134
- @form_url = resource_action_path(@resource_class.resource_name, @record, @action[:name])
138
+ assign_action_form(@action, resource_action_path(@resource_class.resource_name, @record, @action[:name]))
135
139
  end
136
140
 
137
- # Renders the action form for a bulk action with form_fields.
141
+ # Renders the action form for a bulk action with form_fields, for the
142
+ # records selected on the index.
138
143
  #
139
144
  # @return [void]
140
145
  def bulk_action_form
141
- action = find_bulk_action
142
- return render_not_found unless action
143
- return render_forbidden unless action_authorized?(action[:name])
146
+ return unless prepare_bulk_action?
144
147
 
145
- @action = action
146
- @form_fields = action[:form_fields]
147
- @form_url = resource_bulk_action_path(@resource_class.resource_name, action[:name])
148
+ assign_action_form(@bulk_action, resource_bulk_action_path(@resource_class.resource_name, @bulk_action[:name]))
148
149
  end
149
150
 
150
151
  # Executes a custom action on a single record.
@@ -157,6 +158,8 @@ module IronAdmin
157
158
  return unless prepare_action_record?(action)
158
159
 
159
160
  collected = action_form_params(action)
161
+ return if render_invalid_action_form?(action, collected, :action_form,
162
+ resource_action_path(@resource_class.resource_name, @record, action[:name]))
160
163
 
161
164
  completed = action_transaction_completed? do
162
165
  result = call_action_block(action[:block], @record, collected)
@@ -182,18 +185,13 @@ module IronAdmin
182
185
  #
183
186
  # @return [void]
184
187
  def execute_bulk_action
185
- ids = bulk_action_ids
186
- return redirect_bulk(:alert, I18n.t("iron_admin.resources.bulk_action.no_records")) if ids.empty?
187
-
188
- requested, records = find_bulk_records(ids)
189
- action = find_bulk_action
190
-
191
- return render_not_found unless action
192
- return render_forbidden unless action_authorized?(action[:name])
193
- return redirect_bulk(:alert, inaccessible_bulk_message) unless records.count == requested.size
188
+ return unless prepare_bulk_action?
194
189
 
190
+ collected = action_form_params(@bulk_action)
191
+ return if render_invalid_action_form?(@bulk_action, collected, :bulk_action_form,
192
+ resource_bulk_action_path(@resource_class.resource_name, @bulk_action[:name]))
195
193
  return redirect_bulk(:alert, I18n.t("iron_admin.resources.bulk_action.not_completed")) unless
196
- run_bulk_action_in_transaction(action, records)
194
+ run_bulk_action_in_transaction(@bulk_action, @bulk_records, collected)
197
195
 
198
196
  redirect_bulk(:notice, I18n.t("iron_admin.resources.bulk_action.success"), state: bulk_return_state)
199
197
  rescue StandardError => e
@@ -226,18 +224,12 @@ module IronAdmin
226
224
  end
227
225
 
228
226
  def set_resource_class
229
- @resource_class = ResourceRegistry.find(params[:resource_name])
227
+ @resource_class = ResourceRegistry.find(params.expect(:resource_name))
230
228
  render_not_found unless @resource_class
231
229
  end
232
230
 
233
- def resource_policy
234
- @resource_class.resource_policy
235
- end
236
-
237
231
  def action_authorized?(action_name)
238
- return true unless resource_policy
239
-
240
- resource_policy.action_allowed?(action_name, iron_admin_current_user)
232
+ @resource_class.custom_action_allowed?(action_name, iron_admin_current_user)
241
233
  end
242
234
 
243
235
  def bulk_action_ids
@@ -262,6 +254,28 @@ module IronAdmin
262
254
  @resource_class.defined_bulk_actions.find { |a| a[:name].to_s == params[:action_name] }
263
255
  end
264
256
 
257
+ # Loads the bulk action (@bulk_action) and the selected records
258
+ # (@bulk_records, their ids in @bulk_ids), as the form and the action
259
+ # both do. Responds instead and returns false: back to the index when
260
+ # nothing is selected or a selected record is out of reach (outside the
261
+ # tenant scope, or gone), 404 for an unknown action, 403 for a denied one.
262
+ def prepare_bulk_action?
263
+ load_bulk_action
264
+ !performed?
265
+ end
266
+
267
+ def load_bulk_action
268
+ ids = bulk_action_ids
269
+ return redirect_bulk(:alert, I18n.t("iron_admin.resources.bulk_action.no_records")) if ids.empty?
270
+
271
+ @bulk_ids, @bulk_records = find_bulk_records(ids)
272
+ @bulk_action = find_bulk_action
273
+ return render_not_found unless @bulk_action
274
+ return render_forbidden unless action_authorized?(@bulk_action[:name])
275
+
276
+ redirect_bulk(:alert, inaccessible_bulk_message) unless @bulk_records.count == @bulk_ids.size
277
+ end
278
+
265
279
  def inaccessible_bulk_message
266
280
  I18n.t("iron_admin.resources.bulk_action.inaccessible")
267
281
  end
@@ -270,8 +284,7 @@ module IronAdmin
270
284
  # the block still gets an unloaded relation, destroyed records keep their
271
285
  # entry, and objects the block loaded replace the copies. Returns false
272
286
  # when the block rolled back.
273
- def run_bulk_action_in_transaction(action, records)
274
- collected = action_form_params(action)
287
+ def run_bulk_action_in_transaction(action, records, collected)
275
288
  action_transaction_completed? do
276
289
  selected = records.dup.to_a
277
290
  result = call_action_block(action[:block], records, collected)
@@ -371,11 +384,5 @@ module IronAdmin
371
384
  coerce_json_field_params!(parsed)
372
385
  parsed
373
386
  end
374
-
375
- # Password inputs never render the stored value, so a blank one means "unchanged".
376
- def without_blank_passwords(attrs)
377
- names = password_field_names
378
- attrs.reject { |name, value| name.in?(names) && value.blank? }
379
- end
380
387
  end
381
388
  end
@@ -24,8 +24,7 @@ module IronAdmin
24
24
  next if columns.empty?
25
25
 
26
26
  resource_adapter = resource_class.adapter
27
- scope = resource_adapter.all
28
- scope = IronAdmin.configuration.tenant_scope_block.call(scope) if IronAdmin.configuration.tenant_scope_block
27
+ scope = IronAdmin::TenantScope.apply(resource_adapter.all)
29
28
  records = resource_adapter.search_columns(scope, columns, @query)
30
29
  records = resource_adapter.limit(records, 5)
31
30
 
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module IronAdmin
4
+ # Stores the theme picked in the theme switcher in the theme cookie, which the
5
+ # default theme resolver reads. Only registered themes are stored.
6
+ #
7
+ # @see IronAdmin::Configuration#register_theme
8
+ # @see IronAdmin::Configuration#theme_resolver
9
+ class ThemesController < ApplicationController
10
+ protect_from_forgery with: :exception
11
+
12
+ # Characters that make a path unsafe to redirect to: browsers read a
13
+ # backslash as a slash and drop tabs and newlines, so "/\evil.example"
14
+ # becomes the protocol-relative "//evil.example".
15
+ UNSAFE_PATH_CHARACTERS = /[\\[:cntrl:]]|[[:space:]]/
16
+
17
+ # POST /theme
18
+ #
19
+ # @return [void]
20
+ def update
21
+ theme = IronAdmin.configuration.find_theme(theme_param)
22
+ if theme
23
+ cookies[Configuration::THEME_COOKIE] = {
24
+ value: theme.name.to_s, expires: 1.year, httponly: true, same_site: :lax, secure: request.ssl?,
25
+ }
26
+ else
27
+ flash[:alert] = I18n.t("iron_admin.theme_switcher.unknown")
28
+ end
29
+ redirect_to internal_return_path || root_path, status: :see_other
30
+ end
31
+
32
+ private
33
+
34
+ def theme_param
35
+ params[:theme] if params[:theme].is_a?(String)
36
+ end
37
+
38
+ # @return [String, nil] +return_to+ when it is a path on this host
39
+ def internal_return_path
40
+ path = params[:return_to]
41
+ return unless path.is_a?(String) && path.start_with?("/") && !path.start_with?("//")
42
+ return if path.match?(UNSAFE_PATH_CHARACTERS)
43
+
44
+ uri = URI.parse(path)
45
+ path if uri.scheme.nil? && uri.host.nil?
46
+ rescue URI::InvalidURIError
47
+ nil
48
+ end
49
+ end
50
+ end
@@ -3,10 +3,36 @@
3
3
  module IronAdmin
4
4
  # Controller for rendering and executing admin tools.
5
5
  #
6
- # Supports both legacy bare-method tools and enhanced tools with
7
- # tool_action declarations, context injection, and authorization.
6
+ # A tool with one or more actions declared with +tool_action+ dispatches only
7
+ # those. A tool that declares none, and inherits none, still dispatches its own public methods.
8
+ # That fallback is deprecated and removed in 1.0. +config.strict_tool_actions+
9
+ # turns it off. Declared actions support context injection and authorization.
8
10
  class ToolsController < ApplicationController
11
+ @legacy_dispatch_mutex = Mutex.new
12
+ @legacy_dispatch_warnings = {}
13
+
14
+ class << self
15
+ # @api private
16
+ def with_legacy_dispatch_warning(tool_class, method_name)
17
+ key = [tool_class, method_name.to_sym]
18
+ @legacy_dispatch_mutex.synchronize do
19
+ unless @legacy_dispatch_warnings[key]
20
+ yield
21
+ @legacy_dispatch_warnings[key] = true
22
+ end
23
+ end
24
+ end
25
+
26
+ # Forgets warned legacy dispatches so a later one warns again.
27
+ # Pairs are stored only after +warn+ returns, under a mutex.
28
+ # @api private
29
+ def reset_legacy_dispatch_warnings!
30
+ @legacy_dispatch_mutex.synchronize { @legacy_dispatch_warnings = {} }
31
+ end
32
+ end
33
+
9
34
  before_action :set_tool
35
+ before_action :authorize_tool!
10
36
 
11
37
  def show
12
38
  tool_template = "iron_admin/tools/#{@tool_class.tool_name}/show"
@@ -20,7 +46,7 @@ module IronAdmin
20
46
  if declared
21
47
  return render_forbidden unless declared.allowed?(iron_admin_current_user)
22
48
  return unless run_tool_action(declared)
23
- elsif own_tool_method?(action_name)
49
+ elsif legacy_fallback?(action_name)
24
50
  run_legacy_action(action_name)
25
51
  else
26
52
  return render_not_found
@@ -42,12 +68,24 @@ module IronAdmin
42
68
  private
43
69
 
44
70
  def set_tool
45
- @tool_class = ToolRegistry.find(params[:tool_name])
71
+ @tool_class = ToolRegistry.find(params.expect(:tool_name))
46
72
  render_not_found unless @tool_class
47
73
  end
48
74
 
75
+ def authorize_tool!
76
+ render_forbidden unless @tool_class.accessible_by?(iron_admin_current_user)
77
+ end
78
+
49
79
  def own_tool_method?(action_name)
50
- @tool_class.method_defined?(action_name.to_sym, false)
80
+ @tool_class.public_method_defined?(action_name.to_sym, false)
81
+ end
82
+
83
+ # Legacy tools declare no actions. Mixed tools and +strict_tool_actions+ do not use this.
84
+ def legacy_fallback?(action_name)
85
+ return false if IronAdmin.configuration.strict_tool_actions
86
+ return false unless @tool_class.defined_tool_actions.empty?
87
+
88
+ own_tool_method?(action_name)
51
89
  end
52
90
 
53
91
  def run_tool_action(declared)
@@ -62,10 +100,25 @@ module IronAdmin
62
100
  end
63
101
 
64
102
  def run_legacy_action(action_name)
103
+ warn_legacy_dispatch(action_name)
65
104
  tool_instance = build_tool_instance
66
105
  dispatch_with_arity(tool_instance, action_name)
67
106
  end
68
107
 
108
+ def warn_legacy_dispatch(action_name)
109
+ method_name = action_name.to_sym
110
+ callstack = caller_locations(1)
111
+ ToolsController.with_legacy_dispatch_warning(@tool_class, method_name) do
112
+ IronAdmin.deprecator.warn(
113
+ "#{@tool_class.name}##{method_name} was dispatched without a tool_action declaration. " \
114
+ "Declare it, and every other method this tool dispatches, with tool_action: " \
115
+ "a tool that declares one action dispatches only declared actions. " \
116
+ "Dispatching undeclared methods is removed in 1.0.",
117
+ callstack
118
+ )
119
+ end
120
+ end
121
+
69
122
  def build_tool_instance
70
123
  tool = @tool_class.new
71
124
  tool.context = build_context
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module IronAdmin
4
+ # Text of the action form pages.
5
+ module ActionFormHelper
6
+ # Names a member action's record by model, id and the label display_record_label picks.
7
+ #
8
+ # @param resource_class [Class] The resource class
9
+ # @param record [Object] The record the action runs on
10
+ # @return [String] Such as "Applies to Article #16 (Slow news)."
11
+ def action_form_record_text(resource_class, record)
12
+ names = { model: resource_class.model.model_name.human, id: record.id }
13
+ label = ApplicationHelper::DISPLAY_METHODS.lazy.filter_map { |method| record.try(method).presence }.first
14
+ return I18n.t("iron_admin.resources.action_form.record", **names) if label.nil?
15
+
16
+ I18n.t("iron_admin.resources.action_form.record_with_label", label: label, **names)
17
+ end
18
+ end
19
+ end