reactiveadmin 0.2.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 (260) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +88 -0
  3. data/LICENSE +21 -0
  4. data/README.md +81 -0
  5. data/app/assets/reactive_admin/bridge.js +89 -0
  6. data/app/controllers/reactive_admin/api_controller.rb +30 -0
  7. data/app/controllers/reactive_admin/client_errors_controller.rb +38 -0
  8. data/app/controllers/reactive_admin/manifest_controller.rb +10 -0
  9. data/app/controllers/reactive_admin/mcp_controller.rb +48 -0
  10. data/app/controllers/reactive_admin/openapi_controller.rb +13 -0
  11. data/app/controllers/reactive_admin/session_controller.rb +11 -0
  12. data/app/views/layouts/reactive_admin/embed.html.erb +30 -0
  13. data/lib/generators/reactiveadmin/install/install_generator.rb +341 -0
  14. data/lib/generators/reactiveadmin/install/templates/menu.rb.tt +25 -0
  15. data/lib/generators/reactiveadmin/install/templates/reactive_admin.rb.tt +53 -0
  16. data/lib/generators/reactiveadmin/install/templates/reactive_admin_annotated.rb.tt +110 -0
  17. data/lib/reactive_admin/aa/v3/controller.rb +76 -0
  18. data/lib/reactive_admin/aa/v3/forms.rb +29 -0
  19. data/lib/reactive_admin/aa/v3/inputs/filters/typeahead_input.rb +51 -0
  20. data/lib/reactive_admin/aa/v3/inputs/typeahead_input.rb +8 -0
  21. data/lib/reactive_admin/aa/v3/install.rb +36 -0
  22. data/lib/reactive_admin/aa/v3/menu.rb +67 -0
  23. data/lib/reactive_admin/aa/v3/presenters.rb +102 -0
  24. data/lib/reactive_admin/aa/v3/registry.rb +78 -0
  25. data/lib/reactive_admin/aa/v3/views.rb +63 -0
  26. data/lib/reactive_admin/aa/v3.rb +23 -0
  27. data/lib/reactive_admin/aa/v4.rb +59 -0
  28. data/lib/reactive_admin/aa.rb +46 -0
  29. data/lib/reactive_admin/action_items.rb +39 -0
  30. data/lib/reactive_admin/action_links.rb +197 -0
  31. data/lib/reactive_admin/action_safety.rb +19 -0
  32. data/lib/reactive_admin/actions.rb +95 -0
  33. data/lib/reactive_admin/api_errors.rb +116 -0
  34. data/lib/reactive_admin/api_fallback.rb +15 -0
  35. data/lib/reactive_admin/api_guards.rb +445 -0
  36. data/lib/reactive_admin/audit.rb +72 -0
  37. data/lib/reactive_admin/auth/guard.rb +68 -0
  38. data/lib/reactive_admin/auth/token_requests.rb +15 -0
  39. data/lib/reactive_admin/auth.rb +135 -0
  40. data/lib/reactive_admin/authorizer.rb +47 -0
  41. data/lib/reactive_admin/background_requests.rb +33 -0
  42. data/lib/reactive_admin/batch_actions.rb +57 -0
  43. data/lib/reactive_admin/choices.rb +289 -0
  44. data/lib/reactive_admin/client_error.rb +70 -0
  45. data/lib/reactive_admin/composite_keys.rb +12 -0
  46. data/lib/reactive_admin/configuration.rb +221 -0
  47. data/lib/reactive_admin/controller_extension.rb +229 -0
  48. data/lib/reactive_admin/csv_injection.rb +26 -0
  49. data/lib/reactive_admin/date_bound.rb +31 -0
  50. data/lib/reactive_admin/degradation.rb +72 -0
  51. data/lib/reactive_admin/diagnostics.rb +16 -0
  52. data/lib/reactive_admin/doctor.rb +466 -0
  53. data/lib/reactive_admin/dry_run.rb +147 -0
  54. data/lib/reactive_admin/dry_run_flag.rb +18 -0
  55. data/lib/reactive_admin/embed.rb +73 -0
  56. data/lib/reactive_admin/engine.rb +47 -0
  57. data/lib/reactive_admin/error_keys.rb +23 -0
  58. data/lib/reactive_admin/exposure.rb +86 -0
  59. data/lib/reactive_admin/filters.rb +165 -0
  60. data/lib/reactive_admin/fixtures/scrubber.rb +1075 -0
  61. data/lib/reactive_admin/fixtures.rb +161 -0
  62. data/lib/reactive_admin/form_content.rb +78 -0
  63. data/lib/reactive_admin/form_field.rb +279 -0
  64. data/lib/reactive_admin/form_has_many.rb +116 -0
  65. data/lib/reactive_admin/form_lock.rb +71 -0
  66. data/lib/reactive_admin/form_payload.rb +57 -0
  67. data/lib/reactive_admin/form_recorder.rb +192 -0
  68. data/lib/reactive_admin/form_schema.rb +160 -0
  69. data/lib/reactive_admin/frame.rb +97 -0
  70. data/lib/reactive_admin/html_nodes.rb +390 -0
  71. data/lib/reactive_admin/html_policy.rb +338 -0
  72. data/lib/reactive_admin/html_rich.rb +186 -0
  73. data/lib/reactive_admin/implicit_defaults.rb +102 -0
  74. data/lib/reactive_admin/index_options.rb +41 -0
  75. data/lib/reactive_admin/index_payload.rb +259 -0
  76. data/lib/reactive_admin/index_recorder.rb +115 -0
  77. data/lib/reactive_admin/index_views.rb +85 -0
  78. data/lib/reactive_admin/inline_style.rb +125 -0
  79. data/lib/reactive_admin/labels_payload.rb +34 -0
  80. data/lib/reactive_admin/ladder.rb +268 -0
  81. data/lib/reactive_admin/layout_classes.rb +167 -0
  82. data/lib/reactive_admin/legacy_inventory.rb +547 -0
  83. data/lib/reactive_admin/location.rb +22 -0
  84. data/lib/reactive_admin/lookup_audit.rb +30 -0
  85. data/lib/reactive_admin/lookup_payload.rb +41 -0
  86. data/lib/reactive_admin/manifest.rb +36 -0
  87. data/lib/reactive_admin/masked_json.rb +62 -0
  88. data/lib/reactive_admin/masking.rb +100 -0
  89. data/lib/reactive_admin/mcp/access.rb +111 -0
  90. data/lib/reactive_admin/mcp/api.rb +123 -0
  91. data/lib/reactive_admin/mcp/count_tool.rb +22 -0
  92. data/lib/reactive_admin/mcp/layout.rb +175 -0
  93. data/lib/reactive_admin/mcp/output.rb +46 -0
  94. data/lib/reactive_admin/mcp/prompts.rb +111 -0
  95. data/lib/reactive_admin/mcp/server.rb +134 -0
  96. data/lib/reactive_admin/mcp/shapes.rb +164 -0
  97. data/lib/reactive_admin/mcp/tools.rb +386 -0
  98. data/lib/reactive_admin/mcp.rb +53 -0
  99. data/lib/reactive_admin/media_nodes.rb +34 -0
  100. data/lib/reactive_admin/menu.rb +90 -0
  101. data/lib/reactive_admin/menu_overlay.rb +291 -0
  102. data/lib/reactive_admin/node_registry.rb +47 -0
  103. data/lib/reactive_admin/open_api/builder.rb +156 -0
  104. data/lib/reactive_admin/open_api/context.rb +46 -0
  105. data/lib/reactive_admin/open_api/core.rb +51 -0
  106. data/lib/reactive_admin/open_api/operation_ids.rb +18 -0
  107. data/lib/reactive_admin/open_api/page_doc.rb +75 -0
  108. data/lib/reactive_admin/open_api/parts.rb +86 -0
  109. data/lib/reactive_admin/open_api/resource_doc.rb +423 -0
  110. data/lib/reactive_admin/open_api/security.rb +55 -0
  111. data/lib/reactive_admin/open_api/write_schema.rb +104 -0
  112. data/lib/reactive_admin/open_api.rb +41 -0
  113. data/lib/reactive_admin/option_source.rb +36 -0
  114. data/lib/reactive_admin/option_sources.rb +14 -0
  115. data/lib/reactive_admin/options_payload.rb +61 -0
  116. data/lib/reactive_admin/page_controller_extension.rb +112 -0
  117. data/lib/reactive_admin/page_forms.rb +181 -0
  118. data/lib/reactive_admin/page_payload.rb +120 -0
  119. data/lib/reactive_admin/param_normalizer.rb +89 -0
  120. data/lib/reactive_admin/paths.rb +63 -0
  121. data/lib/reactive_admin/permissions.rb +31 -0
  122. data/lib/reactive_admin/provenance.rb +57 -0
  123. data/lib/reactive_admin/query_guard.rb +108 -0
  124. data/lib/reactive_admin/ransack_guard.rb +107 -0
  125. data/lib/reactive_admin/rate_limit.rb +59 -0
  126. data/lib/reactive_admin/reactive_dsl.rb +99 -0
  127. data/lib/reactive_admin/readiness.rb +189 -0
  128. data/lib/reactive_admin/registry.rb +69 -0
  129. data/lib/reactive_admin/resource_meta.rb +120 -0
  130. data/lib/reactive_admin/resource_summary.rb +41 -0
  131. data/lib/reactive_admin/routes.rb +60 -0
  132. data/lib/reactive_admin/row_errors.rb +29 -0
  133. data/lib/reactive_admin/rspec.rb +116 -0
  134. data/lib/reactive_admin/scrubbed_error.rb +22 -0
  135. data/lib/reactive_admin/search.rb +79 -0
  136. data/lib/reactive_admin/server_timing.rb +31 -0
  137. data/lib/reactive_admin/show_payload.rb +88 -0
  138. data/lib/reactive_admin/sidebars.rb +34 -0
  139. data/lib/reactive_admin/site.rb +55 -0
  140. data/lib/reactive_admin/style_report.rb +129 -0
  141. data/lib/reactive_admin/style_sources.rb +83 -0
  142. data/lib/reactive_admin/template_actions.rb +126 -0
  143. data/lib/reactive_admin/template_content.rb +49 -0
  144. data/lib/reactive_admin/template_form.rb +442 -0
  145. data/lib/reactive_admin/test_support.rb +31 -0
  146. data/lib/reactive_admin/typeahead.rb +33 -0
  147. data/lib/reactive_admin/ui/assets.rb +91 -0
  148. data/lib/reactive_admin/ui/client_build.rb +47 -0
  149. data/lib/reactive_admin/ui/helper.rb +24 -0
  150. data/lib/reactive_admin/ui/packaged_client.rb +67 -0
  151. data/lib/reactive_admin/ui/shell.rb +81 -0
  152. data/lib/reactive_admin/ui/switch.rb +38 -0
  153. data/lib/reactive_admin/ui.rb +43 -0
  154. data/lib/reactive_admin/value_payload.rb +147 -0
  155. data/lib/reactive_admin/value_token.rb +36 -0
  156. data/lib/reactive_admin/values.rb +43 -0
  157. data/lib/reactive_admin/version.rb +3 -0
  158. data/lib/reactive_admin/write_audit.rb +60 -0
  159. data/lib/reactive_admin/write_guards.rb +142 -0
  160. data/lib/reactive_admin.rb +153 -0
  161. data/lib/reactiveadmin.rb +1 -0
  162. data/lib/tasks/reactiveadmin.rake +36 -0
  163. data/lib/tasks/reactiveadmin_client.rake +11 -0
  164. data/lib/tasks/reactiveadmin_legacy.rake +7 -0
  165. data/lib/tasks/reactiveadmin_styles.rake +6 -0
  166. data/openapi/core.yaml +1643 -0
  167. data/vendor/client/assets/ActionPage-XUYorI8E.js +1 -0
  168. data/vendor/client/assets/ActionView-BsZFcsgp.js +1 -0
  169. data/vendor/client/assets/CommandPalette-C59HT1ip.js +1 -0
  170. data/vendor/client/assets/DatePicker-DuaK0iMg.js +1 -0
  171. data/vendor/client/assets/DateRangeCalendar-B2BDsG-X.js +1 -0
  172. data/vendor/client/assets/DegradedNotice-DiH5n2DO.js +1 -0
  173. data/vendor/client/assets/FormDialog-pmYl8Eov.js +1 -0
  174. data/vendor/client/assets/FormSkeleton-DuSBZO3C.js +1 -0
  175. data/vendor/client/assets/FrameView-2X8cFvUt.css +1 -0
  176. data/vendor/client/assets/FrameView-zmUeDO8d.js +1 -0
  177. data/vendor/client/assets/GroupOverview-DI5T3k5K.js +1 -0
  178. data/vendor/client/assets/InlineForm-CMEIMgeh.js +1 -0
  179. data/vendor/client/assets/JsonDiff-o7JWr7CH.js +3 -0
  180. data/vendor/client/assets/JsonEditor-D4VXdlEx.js +1 -0
  181. data/vendor/client/assets/JsonView-BrJLDmSN.js +1 -0
  182. data/vendor/client/assets/MobileMenu-GAeFOeEb.js +1 -0
  183. data/vendor/client/assets/Node-SQl61V4K.js +2 -0
  184. data/vendor/client/assets/PageView-C-laNEPZ.js +1 -0
  185. data/vendor/client/assets/ResourceForm-BPjsoCY6.js +1 -0
  186. data/vendor/client/assets/ResourceIndex-COezAw5t.js +3 -0
  187. data/vendor/client/assets/ResourceOrPage-CRZGCY84.js +1 -0
  188. data/vendor/client/assets/ResourceShow-DweRXn18.js +1 -0
  189. data/vendor/client/assets/Segmented-BE9TCbph.js +1 -0
  190. data/vendor/client/assets/Sidebars-CsB9rFqX.js +1 -0
  191. data/vendor/client/assets/breadcrumb-DSA3Tlyd.js +1 -0
  192. data/vendor/client/assets/button-CvZ4_DFC.js +1 -0
  193. data/vendor/client/assets/calendar-BoyMpJIf.js +1 -0
  194. data/vendor/client/assets/check-C5A3J16N.js +1 -0
  195. data/vendor/client/assets/command-CMuk0oAt.js +1 -0
  196. data/vendor/client/assets/dist-CqEI82S_.js +1 -0
  197. data/vendor/client/assets/dist-IW4yX942.js +1 -0
  198. data/vendor/client/assets/editor-yqRLzgsJ.js +2 -0
  199. data/vendor/client/assets/external-link-C2aR4slL.js +1 -0
  200. data/vendor/client/assets/frames-C9gPCc3N.js +1 -0
  201. data/vendor/client/assets/humanize-DtU_Kpr4.js +1 -0
  202. data/vendor/client/assets/ibm-plex-mono-cyrillic-400-normal-BSMlKf0J.woff2 +0 -0
  203. data/vendor/client/assets/ibm-plex-mono-cyrillic-400-normal-CEL4l2ZJ.woff +0 -0
  204. data/vendor/client/assets/ibm-plex-mono-cyrillic-500-normal-Ael50iVv.woff +0 -0
  205. data/vendor/client/assets/ibm-plex-mono-cyrillic-500-normal-Bq9vWWag.woff2 +0 -0
  206. data/vendor/client/assets/ibm-plex-mono-cyrillic-ext-400-normal-DMdlQ8Kv.woff +0 -0
  207. data/vendor/client/assets/ibm-plex-mono-cyrillic-ext-400-normal-xuaO2J-f.woff2 +0 -0
  208. data/vendor/client/assets/ibm-plex-mono-cyrillic-ext-500-normal-BIfNGwUT.woff +0 -0
  209. data/vendor/client/assets/ibm-plex-mono-cyrillic-ext-500-normal-BqneJy0T.woff2 +0 -0
  210. data/vendor/client/assets/ibm-plex-mono-latin-400-normal-CvHOgSBP.woff +0 -0
  211. data/vendor/client/assets/ibm-plex-mono-latin-400-normal-DMJ8VG8y.woff2 +0 -0
  212. data/vendor/client/assets/ibm-plex-mono-latin-500-normal-CB9ihrfo.woff +0 -0
  213. data/vendor/client/assets/ibm-plex-mono-latin-500-normal-DSY6xOcd.woff2 +0 -0
  214. data/vendor/client/assets/ibm-plex-mono-latin-ext-400-normal-BmRBH3aV.woff2 +0 -0
  215. data/vendor/client/assets/ibm-plex-mono-latin-ext-400-normal-D3D2R8hC.woff +0 -0
  216. data/vendor/client/assets/ibm-plex-mono-latin-ext-500-normal-CAhNIIs5.woff2 +0 -0
  217. data/vendor/client/assets/ibm-plex-mono-latin-ext-500-normal-CZ70TYgx.woff +0 -0
  218. data/vendor/client/assets/ibm-plex-mono-vietnamese-400-normal-BulugwFq.woff2 +0 -0
  219. data/vendor/client/assets/ibm-plex-mono-vietnamese-400-normal-DDuiU_S-.woff +0 -0
  220. data/vendor/client/assets/ibm-plex-mono-vietnamese-500-normal-C8zxqsMH.woff +0 -0
  221. data/vendor/client/assets/ibm-plex-mono-vietnamese-500-normal-DZ4AoWbu.woff2 +0 -0
  222. data/vendor/client/assets/ibm-plex-sans-cyrillic-ext-wght-normal-d45eAU9y.woff2 +0 -0
  223. data/vendor/client/assets/ibm-plex-sans-cyrillic-wght-normal-BAAhND-U.woff2 +0 -0
  224. data/vendor/client/assets/ibm-plex-sans-greek-wght-normal-CmyJS8uq.woff2 +0 -0
  225. data/vendor/client/assets/ibm-plex-sans-latin-ext-wght-normal-CIII54If.woff2 +0 -0
  226. data/vendor/client/assets/ibm-plex-sans-latin-wght-normal-IvpUvPa2.woff2 +0 -0
  227. data/vendor/client/assets/ibm-plex-sans-vietnamese-wght-normal-Dg1JeJN0.woff2 +0 -0
  228. data/vendor/client/assets/index-B51aUaAY.js +43 -0
  229. data/vendor/client/assets/index-Cs9rCoWM.css +2 -0
  230. data/vendor/client/assets/input-DByFmQPa.js +1 -0
  231. data/vendor/client/assets/json-De3PN-R6.css +1 -0
  232. data/vendor/client/assets/json-oTp7Q-d-.js +5 -0
  233. data/vendor/client/assets/media-D4a_En8R.js +1 -0
  234. data/vendor/client/assets/plus-CfJ9vo8X.js +1 -0
  235. data/vendor/client/assets/popover-CDtI9R8Q.js +1 -0
  236. data/vendor/client/assets/purify-G7QKJyJq.js +1 -0
  237. data/vendor/client/assets/query-CCsZN2fa.js +1 -0
  238. data/vendor/client/assets/recent-BrzLwL0D.js +1 -0
  239. data/vendor/client/assets/rich-BQc_q5T7.js +1 -0
  240. data/vendor/client/assets/rolldown-runtime-XKyaHtl9.js +1 -0
  241. data/vendor/client/assets/select-C43qDA3k.js +1 -0
  242. data/vendor/client/assets/sheet-BPl2n-4n.js +1 -0
  243. data/vendor/client/assets/textarea-BnOphXry.js +1 -0
  244. data/vendor/client/assets/uiMode-z0ZbRV49.js +1 -0
  245. data/vendor/client/assets/uiToggle-DwnRQTH1.js +1 -0
  246. data/vendor/client/assets/useRecordForm-X_DExEP2.js +1 -0
  247. data/vendor/client/assets/useRecordVisit-BRtfMi-q.js +1 -0
  248. data/vendor/client/assets/utils-DwBGbDkG.js +1 -0
  249. data/vendor/client/assets/vendor-calendar-nTZiq9oY.js +1 -0
  250. data/vendor/client/assets/vendor-datefns-BoC6MCnl.js +1 -0
  251. data/vendor/client/assets/vendor-forms-D0h8hG_S.js +55 -0
  252. data/vendor/client/assets/vendor-purify-BXoPR7WT.js +3 -0
  253. data/vendor/client/assets/vendor-query-DZ6APRNI.js +1 -0
  254. data/vendor/client/assets/vendor-react-C1iE8mnY.js +9 -0
  255. data/vendor/client/assets/vendor-router-CI6Vh4k_.js +1 -0
  256. data/vendor/client/assets/vendor-table-DuQ835by.js +4 -0
  257. data/vendor/client/index.html +24 -0
  258. data/vendor/client/manifest.json +867 -0
  259. data/vendor/client/reactiveadmin-client.json +4 -0
  260. metadata +350 -0
@@ -0,0 +1,63 @@
1
+ module ReactiveAdmin
2
+ # Classic (`/admin/...`) and API paths of a resource's pages. A resource whose `belongs_to`
3
+ # isn't optional is only routed under its parent.
4
+ module Paths
5
+ PARENT_ID = "{parent_id}".freeze
6
+
7
+ module_function
8
+
9
+ def nested?(config) = config.belongs_to? && config.belongs_to_config.required?
10
+
11
+ def api(path) = File.join(ReactiveAdmin.config.mount_path, path)
12
+
13
+ # Whether the SPA routes this path itself: under the namespace, and not escaping it with dot segments.
14
+ def admin?(path)
15
+ prefix = "/#{ReactiveAdmin.config.namespace}"
16
+ (path == prefix || path.start_with?("#{prefix}/")) && !path.match?(%r{/\.\.?(/|\z)|%2[ef]}i)
17
+ end
18
+
19
+ def collection(config, parent_id: nil)
20
+ parent = config.belongs_to_config.target if parent_id
21
+ ["", ReactiveAdmin.config.namespace, (Registry.route_key(parent) if parent), parent_id, Registry.route_key(config)].compact.join("/")
22
+ end
23
+
24
+ def options(config, parent_id: nil) = File.join(collection(config, parent_id:), "_options")
25
+
26
+ def lookup(config, parent_id: nil) = File.join(options(config, parent_id:), "lookup")
27
+
28
+ def meta(config) = File.join(collection(config), "_meta")
29
+
30
+ def labels(config) = File.join(collection(config), "_labels")
31
+
32
+ def value(config, parent_id: nil) = File.join(collection(config, parent_id:), ":id", "_value")
33
+
34
+ def index_template(config) = (collection(config, parent_id: PARENT_ID) if config.belongs_to?)
35
+
36
+ def parent_id(config, params) = (params[config.belongs_to_config.to_param].presence if config.belongs_to?)
37
+
38
+ # The params that route `new` under the record's parent, for resources that can be (but needn't be) nested.
39
+ def parent_params(config, record)
40
+ return {} unless config.belongs_to? && record
41
+
42
+ fk = record[record.class.reflect_on_association(config.belongs_to_config.target_name).foreign_key]
43
+ fk.present? ? {config.belongs_to_config.to_param => fk.to_s} : {}
44
+ end
45
+
46
+ def record(config, record)
47
+ member = member(config, record)
48
+ show = member if Registry.actions(config).include?(:show)
49
+ edit = "#{member}/edit" if Registry.actions(config).include?(:edit)
50
+ {show:, edit:, api_show: (api(show) if show), api_edit: (api(edit) if edit)}.compact
51
+ end
52
+
53
+ # Composite keys join their parts, which may hold spaces or slashes.
54
+ def member(config, record) = File.join(collection(config, parent_id: parent_id_of(config, record)), ERB::Util.url_encode(record.to_param.to_s))
55
+
56
+ # Read from the foreign key: loading the parent would be an n+1 across index rows.
57
+ def parent_id_of(config, record)
58
+ return unless nested?(config)
59
+
60
+ record[record.class.reflect_on_association(config.belongs_to_config.target_name).foreign_key]&.to_s
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,31 @@
1
+ module ReactiveAdmin
2
+ # Maps AA authorization (action + subject) to the contract's Permissions object.
3
+ # Keys for actions the resource doesn't define are omitted rather than false. `create` and
4
+ # `update` answer the NEW and EDIT checks behind classic AA's buttons; writes still authorize
5
+ # CREATE and UPDATE.
6
+ class Permissions
7
+ CHECKS = {
8
+ index: [AA::Auth::READ, %i[index]],
9
+ show: [AA::Auth::READ, %i[show]],
10
+ create: [AA::Auth::NEW, %i[new create]],
11
+ update: [AA::Auth::EDIT, %i[edit update]],
12
+ destroy: [AA::Auth::DESTROY, %i[destroy]]
13
+ }.freeze
14
+
15
+ def initialize(config, authorizer)
16
+ @config = config
17
+ @authorizer = authorizer
18
+ end
19
+
20
+ def resource = build(@config.resource_class, CHECKS, Registry.top_level_actions(@config))
21
+
22
+ def record(record) = build(record, CHECKS.slice(:show, :update, :destroy))
23
+
24
+ private
25
+
26
+ def build(subject, checks, defined = Registry.actions(@config))
27
+ checks.select { |_, (_, actions)| defined.intersect?(actions) }
28
+ .transform_values { |(action, _)| @authorizer.authorized?(action, subject, config: @config) }
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,57 @@
1
+ module ReactiveAdmin
2
+ # Tells markup the host's DSL produced from markup a stored value carried in. Inside a scope, the view's
3
+ # `link_to`, `button_to` and form helpers stamp the anchor or form they return with a random per-request
4
+ # secret; the walker turns only stamped controls into actions and stamped forms into frames. Stored text
5
+ # (Markdown, `simple_format(notes)`) can't know the secret, so its `data-method` links and forms stay inert.
6
+ module Provenance
7
+ ATTRIBUTE = "data-reactiveadmin-origin".freeze
8
+ UNSTAMPED_TAG = /\A<(a|form)\b(?![^>]*#{ATTRIBUTE})/
9
+ KEY = :reactiveadmin_provenance
10
+ STATE = ActiveSupport::IsolatedExecutionState
11
+
12
+ module_function
13
+
14
+ def with_scope
15
+ previous = STATE[KEY]
16
+ STATE[KEY] = SecureRandom.hex(16)
17
+ yield
18
+ ensure
19
+ STATE[KEY] = previous
20
+ end
21
+
22
+ def trusted?(el)
23
+ secret = STATE[KEY]
24
+ !secret.nil? && ActiveSupport::SecurityUtils.secure_compare(el[ATTRIBUTE].to_s, secret)
25
+ end
26
+
27
+ def stamp(html)
28
+ secret = STATE[KEY]
29
+ return html unless secret && html.html_safe?
30
+
31
+ html.sub(UNSTAMPED_TAG) { %(#{$&} #{ATTRIBUTE}="#{secret}") }.html_safe
32
+ end
33
+
34
+ # Prepended to Arbre's tags: an `a` or `form` built in the DSL is the host's, unlike a string inserted as text.
35
+ module Tags
36
+ def build(*)
37
+ super
38
+ secret = STATE[KEY]
39
+ set_attribute(ATTRIBUTE, secret) if secret && tag_name.in?(%w[a form])
40
+ end
41
+ end
42
+
43
+ # Prepended to ActionView::Base, ahead of UrlHelper.
44
+ module Helpers
45
+ def link_to(...) = Provenance.stamp(super)
46
+
47
+ def button_to(...) = Provenance.stamp(super)
48
+
49
+ private
50
+
51
+ # form_for, form_with and form_tag all build their form through one of these.
52
+ def form_tag_html(...) = Provenance.stamp(super)
53
+
54
+ def form_tag_with_body(...) = Provenance.stamp(super)
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,108 @@
1
+ module ReactiveAdmin
2
+ # Bounds what one index or export request can cost and reveal. ActiveAdmin accepts any `per_page`
3
+ # and sorts by any column, so on the API `per_page` is capped, `order` only names columns the
4
+ # index shows, and exports have a row ceiling.
5
+ module QueryGuard
6
+ MAX_KEY_LENGTH = 100
7
+
8
+ module_function
9
+
10
+ # Page sizes the resource offers (`per_page: [25, 250]`) may be served even above the host cap.
11
+ # A resource that turned pagination off keeps its own `max_per_page`: that is the page it asked for.
12
+ def per_page_cap(config, offered = [])
13
+ return config.max_per_page unless config.paginate
14
+
15
+ host_cap = [ReactiveAdmin.config.max_per_page, *offered].compact.max
16
+ [config.max_per_page, host_cap || config.max_per_page].min
17
+ end
18
+
19
+ # Clamps rather than rejects: a client asking for more gets the most it may have.
20
+ def per_page(config, requested, fallback, offered = [])
21
+ size = requested.to_s.match?(/\A\d+\z/) ? requested.to_i : fallback.to_i
22
+ size = fallback.to_i unless size.positive?
23
+ [size, per_page_cap(config, offered)].min
24
+ end
25
+
26
+ # The sort key of `order` when the index doesn't offer it, or nil. `sort_keys` come from the
27
+ # displayed sortable columns; the resource's own default order is always allowed.
28
+ def order_violation(config, order, sort_keys)
29
+ return if order.blank? || order.to_s == config.sort_order.to_s
30
+
31
+ clause = AA.order_clause(config, order.to_s)
32
+ return if clause.valid? && sort_keys.include?(clause.field)
33
+
34
+ (clause.valid? ? clause.field : order.to_s).truncate(MAX_KEY_LENGTH)
35
+ end
36
+
37
+ # Rows up to one past `limit`: a cheap probe, since an export streams and can't be refused once
38
+ # it started.
39
+ def count_rows(collection, limit)
40
+ rows = collection.except(:order).limit(limit + 1).count(:all)
41
+ rows.is_a?(Hash) ? rows.size : rows
42
+ end
43
+
44
+ module Guard
45
+ # Exports are AA's own streaming, so the order and size checks run before it starts.
46
+ def index(*)
47
+ return super unless reactiveadmin_export?
48
+
49
+ super if export_allowed?
50
+ end
51
+
52
+ protected
53
+
54
+ def per_page
55
+ return super unless reactiveadmin_request?
56
+
57
+ QueryGuard.per_page(active_admin_config, super, configured_per_page, offered_per_page)
58
+ end
59
+
60
+ private
61
+
62
+ def offered_per_page
63
+ view = IndexViews.new(active_admin_config, as: params[:as]).per_page_options
64
+ [*view, *(active_admin_config.per_page if active_admin_config.per_page.is_a?(Array))].grep(Integer)
65
+ end
66
+
67
+ def export_allowed?
68
+ payload = IndexPayload.new(self)
69
+ collection = AA.collection(self)
70
+ sort = payload.unsupported_sort
71
+ raise ApiErrors::BadRequest, "Unsupported sort: #{sort}" if sort
72
+ return false unless export_columns(payload)
73
+
74
+ limit = ReactiveAdmin.config.max_export_rows
75
+ rows = QueryGuard.count_rows(collection, limit) if limit
76
+ if limit && rows > limit
77
+ render_api_error(:invalid, "Narrow your filters to export fewer than #{limit} rows", 422)
78
+ return false
79
+ end
80
+
81
+ Audit.instrument("export", self, {resource: Registry.route_key(active_admin_config),
82
+ user_id: ReactiveAdmin.current_user(self)&.id, filters: params[:q].try(:keys).to_a.map(&:to_s), scope: AA.current_scope(self)&.id, row_count: rows})
83
+ true
84
+ end
85
+
86
+ # Without a `csv` block AA exports every column, credentials included; the index's choice applies instead.
87
+ def export_columns(payload)
88
+ return true unless AA.default_csv?(active_admin_config)
89
+
90
+ columns = payload.export_columns
91
+ return @reactiveadmin_csv_builder = AA.csv_builder(active_admin_config, columns) if columns
92
+
93
+ render_api_error(:invalid, "This resource declares no csv block, and strict defaults refuse to export it", 422)
94
+ false
95
+ end
96
+
97
+ def reactiveadmin_csv_builder = @reactiveadmin_csv_builder
98
+ end
99
+
100
+ # Prepended to AA's CSV builder: an export the guard chose columns for is built by its own builder.
101
+ module ExportBuilder
102
+ def build(controller, csv)
103
+ builder = controller.send(:reactiveadmin_csv_builder) if controller.respond_to?(:reactiveadmin_csv_builder, true)
104
+ (builder && !builder.equal?(self)) ? builder.build(controller, csv) : super
105
+ end
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,107 @@
1
+ module ReactiveAdmin
2
+ # Ransack accepts any attribute, association and predicate a model exposes, which turns `q` into
3
+ # a way to probe columns the page never shows. On the API only the predicates of the resource's
4
+ # declared filters are honoured; anything else is rejected rather than silently dropped, so a
5
+ # client bug surfaces. AA sorts and scopes through `order` and `scope`, so `q` carries nothing else.
6
+ #
7
+ # Every action is guarded, because any of them may reach AA's `collection` with the raw `q`. The
8
+ # exceptions are actions whose `q` is their own search form: declared by the host
9
+ # (`reactive { own_query_params :search }` or `config.ransack_unguarded_actions`), or learned
10
+ # from a recorded template form that posts `q[...]` fields to them (see `learn`).
11
+ module RansackGuard
12
+ MAX_KEY_LENGTH = 100
13
+ LEARNED_KEY = "reactiveadmin:q_owner:%s#%s".freeze
14
+
15
+ def self.own_query?(config, controller_path, action)
16
+ action = action.to_s
17
+ declared = [*config.try(:reactive_options)&.dig(:own_query_params), *host_declared(config)]
18
+ declared.any? { it.to_s == action } || learned.include?([controller_path, action]) ||
19
+ Rails.cache.exist?(format(LEARNED_KEY, controller_path, action))
20
+ end
21
+
22
+ def self.host_declared(config)
23
+ Array((ReactiveAdmin.config.ransack_unguarded_actions || {}).with_indifferent_access[Registry.route_key(config)])
24
+ end
25
+
26
+ def self.learned = (@learned ||= Concurrent::Set.new)
27
+
28
+ # Actions that recorded template forms post `q[...]` fields to: their `q` is a search form's.
29
+ # Kept per process and on the shared cache, so another process serving the submit agrees.
30
+ def self.learn(forms)
31
+ forms.each do |form|
32
+ next unless Array(form[:items]).flat_map { fields_of(it) }.any? { it[:param].to_s.start_with?("q[") }
33
+
34
+ route = Rails.application.routes.recognize_path(form[:path].to_s, method: form[:method])
35
+ learned << [route[:controller], route[:action]]
36
+ Rails.cache.write(format(LEARNED_KEY, route[:controller], route[:action]), true)
37
+ rescue ActionController::RoutingError
38
+ next
39
+ end
40
+ end
41
+
42
+ def self.fields_of(item)
43
+ case item
44
+ in [:field, Hash => field] then [field]
45
+ in [:section, Hash => section] then section[:fields]
46
+ in [:group, Array => list] then list.flat_map { fields_of(it) }
47
+ else []
48
+ end
49
+ end
50
+
51
+ # Search names a declared filter can produce, without predicate: the attribute itself and the
52
+ # names AA's association inputs search on (`post_id`, or the foreign key of a has_many through).
53
+ # With a view, filters its `if:`/`unless:` hides from the user are left out.
54
+ def self.bases(config, view = nil)
55
+ klass = config.resource_class
56
+ AA.filters(config).reject { |_, opts| view && Filters.skip?(view, opts) }.keys.flat_map do |attr|
57
+ name = attr.to_s
58
+ reflection = klass.reflect_on_association(name)
59
+ next [name] unless reflection
60
+
61
+ [name, "#{name}_#{reflection.association_primary_key}", reflection.foreign_key.to_s,
62
+ ("#{reflection.through_reflection.name}_#{reflection.foreign_key}" if reflection.through_reflection)].compact
63
+ rescue
64
+ [name]
65
+ end.to_set
66
+ end
67
+
68
+ def self.allowed?(bases, key)
69
+ return true if bases.include?(key)
70
+
71
+ stripped = key.dup
72
+ Ransack::Predicate.detect_and_strip_from_string!(stripped) && bases.include?(stripped)
73
+ end
74
+
75
+ # The first key that no declared filter explains, or nil.
76
+ def self.violation(config, query, view = nil)
77
+ return "q" unless query.respond_to?(:keys)
78
+
79
+ bases = bases(config, view)
80
+ query.keys.map(&:to_s).find { !allowed?(bases, it) }
81
+ end
82
+
83
+ private
84
+
85
+ # Overridden as ControllerExtension does: callbacks registered on AA's base class are not
86
+ # inherited by controllers that were already built.
87
+ def authenticate_active_admin_user
88
+ super
89
+ reject_undeclared_filters unless performed?
90
+ end
91
+
92
+ # A filter's `if:` may read state the resource's own callbacks set up, so it can only be judged once
93
+ # they have run, just before the action. The check above still stops undeclared keys early.
94
+ def send_action(*)
95
+ reject_undeclared_filters(view_context)
96
+ super unless performed?
97
+ end
98
+
99
+ def reject_undeclared_filters(view = nil)
100
+ return unless reactiveadmin_request? && params.key?(:q)
101
+ return if RansackGuard.own_query?(active_admin_config, controller_path, action_name)
102
+
103
+ key = RansackGuard.violation(active_admin_config, params[:q], view)
104
+ render_api_error(:invalid, "Unsupported filter: #{key.truncate(MAX_KEY_LENGTH)}", 400) if key
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,59 @@
1
+ module ReactiveAdmin
2
+ # Per-user request budgets for the endpoints that are cheap to call and expensive to serve.
3
+ # Fixed windows on `Rails.cache` (atomic `increment`), keyed by user and bucket and never by IP,
4
+ # which is shared behind a VPN or proxy. A store that can't count (the null store) or fails
5
+ # leaves requests unlimited rather than locking operators out.
6
+ module RateLimit
7
+ BUCKETS = {"reactiveadmin_options" => :options, "reactiveadmin_lookup" => :lookup, "reactiveadmin_value" => :value}.freeze
8
+
9
+ module_function
10
+
11
+ # Seconds until the user may retry when the request is over budget, otherwise nil.
12
+ def retry_after(bucket, user_id, now: Time.now.to_i)
13
+ limit, period = limit_for(bucket)
14
+ return unless limit && user_id
15
+
16
+ window = period.to_i
17
+ count = Rails.cache.increment("reactiveadmin:rl:#{bucket}:#{user_id}:#{now / window}", 1, expires_in: window + 1)
18
+ window - (now % window) if count.to_i > limit
19
+ rescue => e
20
+ Rails.logger.warn("[reactiveadmin] rate limit unavailable: #{e.class}")
21
+ nil
22
+ end
23
+
24
+ def limit_for(bucket)
25
+ limits = ReactiveAdmin.config.rate_limits
26
+ return unless limits
27
+
28
+ limits.fetch(bucket) { limits[:default] }
29
+ end
30
+
31
+ def bucket(controller)
32
+ return :export if controller.send(:reactiveadmin_export?)
33
+ return :options if controller.params[:context] == "page_form"
34
+
35
+ BUCKETS.fetch(controller.action_name, :default)
36
+ end
37
+
38
+ # Prepended after authentication so the budget belongs to a known user, before any query runs.
39
+ module Guard
40
+ private
41
+
42
+ def authenticate_active_admin_user
43
+ super
44
+ throttle_reactiveadmin_request unless performed? || !reactiveadmin_request?
45
+ end
46
+
47
+ # The doctor's samples are set server-side, so they must not drain the operator's budget.
48
+ def throttle_reactiveadmin_request
49
+ return if request.env[BackgroundRequests::INTERNAL_ENV]
50
+
51
+ seconds = RateLimit.retry_after(RateLimit.bucket(self), ReactiveAdmin.current_user(self)&.id)
52
+ return unless seconds
53
+
54
+ response.headers["Retry-After"] = seconds.to_s
55
+ render_api_error(:rate_limited, "Too many requests; try again in #{seconds} seconds", 429)
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,99 @@
1
+ module ReactiveAdmin
2
+ # `reactive do ... end` inside ActiveAdmin.register: hints for the API that classic ignores.
3
+ module ResourceDSL
4
+ def reactive(&) = ReactiveDSL.new(config).instance_eval(&)
5
+
6
+ # What the API refuses, whoever asks, while the classic admin is unaffected; see ApiGuards.
7
+ #
8
+ # api false # everything
9
+ # api :deny, only: [:impersonate] # named actions (member, collection, page, standard, batch)
10
+ # api :deny, except: [:index, :show]
11
+ # api :deny, attributes: [:superadmin] # writes that include them, at any depth
12
+ # api :read_only # GET and HEAD only
13
+ # api :session_only, only: [:destroy] # not for API tokens
14
+ # api :superadmin_session, reason: "..." # registered with ReactiveAdmin.api_guard
15
+ #
16
+ # `:new` and `:create`, and `:edit` and `:update`, go together. `reason:` is what users read
17
+ # instead of the default message. Guards stack and any refusal wins.
18
+ def api(guard = nil, **) = ApiGuards.declare(config, guard, **)
19
+ end
20
+
21
+ module ResourceOptions
22
+ def reactive_options = @reactive_options ||= {}
23
+ end
24
+
25
+ class ReactiveDSL
26
+ def initialize(config)
27
+ @config = config
28
+ @options = config.reactive_options
29
+ end
30
+
31
+ # Columns typeahead searches when this resource is the target of an association input.
32
+ def search(*columns) = @options[:search] = columns
33
+
34
+ # Attributes whose values are masked in this resource's index, show and form (see Masking).
35
+ def mask(*attributes) = (@options[:mask] ||= []).concat(attributes.map(&:to_sym))
36
+
37
+ # For a resource whose `new` only builds under its parent (an optional `belongs_to`, and a build that
38
+ # needs the parent's id): the manifest stops offering `new` and `create` at the top level.
39
+ def new_needs_parent = @options[:new_needs_parent] = true
40
+
41
+ # How a has_many section's rows are laid out: `nested_layout :admin_policy_permissions, :cards`. Left
42
+ # alone, rows with a single control show as a compact list and the rest as cards.
43
+ def nested_layout(association, mode)
44
+ mode = mode.to_s
45
+ raise ArgumentError, "nested_layout #{mode.inspect}: expected one of #{FormHasMany::LAYOUTS.join(", ")}" unless FormHasMany::LAYOUTS.include?(mode)
46
+
47
+ (@options[:nested_layout] ||= {})[association.to_sym] = mode
48
+ end
49
+
50
+ # Marks this resource's display names as sensitive: lookups returning its records are audited (see
51
+ # LookupAudit). `sensitive false` overrides `config.sensitive_resources`.
52
+ def sensitive(flag = true) = @options[:sensitive] = flag
53
+
54
+ # Place this resource in the menu without touching its AA `menu` call: `path` is the group
55
+ # labels from the top (at most 2), an empty path meaning top level. Overridden by ReactiveAdmin.menu.
56
+ def menu(path: nil, priority: nil, label: nil, description: nil, icon: nil)
57
+ MenuOverlay.check_depth!(path.size + 1, "reactive menu path #{path.inspect}") if path
58
+ @options[:menu] = {path: (Array(path).map(&:to_s) if path), priority:, label:, description:, icon:}.compact
59
+ end
60
+
61
+ # Lists a member (or GET collection) action as a header button; an action that no action item
62
+ # or content link references is otherwise not listed. `if:` receives the record (nothing for
63
+ # collection actions) and runs in the view context.
64
+ #
65
+ # `expose true` / `expose false` instead serves or denies the whole resource (or page) under
66
+ # `config.expose = :listed`; see Exposure.
67
+ def expose(name, **options)
68
+ return @options[:expose] = name if [true, false].include?(name)
69
+
70
+ (@options[:exposed] ||= {})[name.to_sym] = options.fetch(:if, true)
71
+ end
72
+
73
+ # Actions whose `q` param is their own search form rather than AA's ransack filter, so the
74
+ # filter guard leaves them alone: `own_query_params :search_toc`. They must not hand `q` to AA's `collection`.
75
+ def own_query_params(*actions) = (@options[:own_query_params] ||= []).concat(actions.map(&:to_sym))
76
+
77
+ # GET actions that change data, which the API otherwise takes for reads: they need the write scope, are
78
+ # audited and confirmed in MCP, and `api :read_only` refuses them.
79
+ def unsafe_gets(*actions) = (@options[:unsafe_gets] ||= []).concat(actions.map(&:to_sym))
80
+
81
+ # What agents get through MCP: `mcp :full`, `mcp :none`, or `mcp :read_only, actions: [:approve]`
82
+ # (`mcp actions: [...]` for short). Defaults to `config.mcp.default`; see Mcp::Access.
83
+ def mcp(mode = nil, actions: nil) = Mcp::Access.declare(@config, mode, actions:)
84
+
85
+ # The same as `api` outside the block.
86
+ def api(guard = nil, **) = ApiGuards.declare(@config, guard, **)
87
+
88
+ # Forces how a custom view is shown (see Ladder): `render_as :frame, action: :care_team_history`.
89
+ # Without `action:` it applies to every custom action of the resource, or to a page's content.
90
+ def render_as(mode, action: nil, custom_key: nil)
91
+ mode = mode.to_s
92
+ raise ArgumentError, "render_as #{mode.inspect}: expected one of #{Ladder::MODES.join(", ")}" unless Ladder::MODES.include?(mode)
93
+ raise ArgumentError, "render_as :custom needs custom_key:" if mode == "custom" && custom_key.blank?
94
+
95
+ Ladder.reset!
96
+ (@options[:render_as] ||= {})[(action || "*").to_s] = {mode:, custom_key:}.compact
97
+ end
98
+ end
99
+ end