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,445 @@
1
+ module ReactiveAdmin
2
+ # `api` in an ActiveAdmin registration: what the API refuses to serve or write, however the caller
3
+ # authenticates. The classic admin is unaffected. Enforced on the actual controller and action of
4
+ # every API request, so it holds for the React client and for anything else that dispatches through
5
+ # the API, never on what the manifest advertises.
6
+ #
7
+ # A guard is `:deny`, `:read_only`, `:session_only` or a name registered with `ReactiveAdmin.api_guard`.
8
+ # The built-ins are static: the metadata of a channel (a session, or a token) already leaves out or
9
+ # marks what they refuse. A registered guard runs per request, so the metadata only says it may refuse.
10
+ module ApiGuards
11
+ STATIC = %i[deny read_only session_only].freeze
12
+ ACTION_MESSAGE = "This isn't available through the API. Use the classic admin.".freeze
13
+ MESSAGES = {
14
+ deny: ACTION_MESSAGE,
15
+ read_only: "The API only reads this. Use the classic admin to change it.".freeze,
16
+ session_only: "This isn't available to API tokens. Use the classic admin.".freeze
17
+ }.freeze
18
+ API_SUFFIX = /\.(reactiveadmin|csv)\z/
19
+ # A form you can't open is one you may not submit, and the other way round.
20
+ PAIRS = {"new" => "create", "create" => "new", "edit" => "update", "update" => "edit"}.freeze
21
+ # The gem's own endpoints serve the data of the action they stand in for.
22
+ ENDPOINTS = {"reactiveadmin_meta" => "index", "reactiveadmin_page" => "index", "reactiveadmin_labels" => "show", "reactiveadmin_value" => "show"}.freeze
23
+ STANDARD = %w[index show new create edit update destroy batch_action].freeze
24
+ UNSAFE = %w[create update destroy batch_action].freeze
25
+
26
+ # `exempt` is for the MCP mode only: the custom actions and batch keys it leaves open, which unlike
27
+ # `except` never match the standard action of the same name.
28
+ Declaration = Data.define(:guard, :only, :except, :attributes, :reason, :exempt) do
29
+ def initialize(exempt: nil, **) = super
30
+
31
+ def name = guard.to_s
32
+
33
+ def static? = STATIC.include?(guard)
34
+
35
+ def blanket? = only.nil? && except.nil? && attributes.nil? && exempt.nil?
36
+ end
37
+ Refusal = Data.define(:guard, :message, :attributes)
38
+
39
+ # The channel of the request being served: :session for the React client, :token for bearer
40
+ # requests (scripts), :mcp for the calls MCP tools dispatch, which the resources' MCP modes also
41
+ # guard. Unset means :token, so metadata never offers more than it should.
42
+ class Current < ActiveSupport::CurrentAttributes
43
+ attribute :channel
44
+ end
45
+
46
+ # Prepended to the resource and page controllers. After authentication, so a signed-out client
47
+ # learns nothing, and next to Exposure, before any authorization or host callback that acts.
48
+ module Guard
49
+ private
50
+
51
+ def authenticate_active_admin_user
52
+ super
53
+ reject_api_denied unless performed?
54
+ end
55
+
56
+ def reject_api_denied
57
+ return if @reactiveadmin_api_clear || !ApiGuards.api_scope?(self)
58
+
59
+ refusal = ApiGuards.check(self)
60
+ return @reactiveadmin_api_clear = true unless refusal
61
+
62
+ render_api_denied(refusal)
63
+ end
64
+
65
+ def render_api_denied(refusal)
66
+ render_api_error(:api_denied, refusal.message, 403, guard: refusal.guard, classic_url: ApiGuards.classic_url(self), attributes: refusal.attributes)
67
+ end
68
+ end
69
+
70
+ # Outermost, so it also covers a controller that skips authentication, and runs before the page
71
+ # controller renders its content to shape a write.
72
+ module Backstop
73
+ private
74
+
75
+ def send_action(*)
76
+ reject_api_denied
77
+ super unless performed?
78
+ end
79
+ end
80
+
81
+ module_function
82
+
83
+ def channel = Current.channel || :token
84
+
85
+ def channel_of(request)
86
+ return :session unless Auth.token_based?(request)
87
+
88
+ request.env[Mcp::Api::ENV_KEY] ? :mcp : :token
89
+ end
90
+
91
+ def with_channel(channel)
92
+ previous = Current.channel
93
+ Current.channel = channel
94
+ yield
95
+ ensure
96
+ Current.channel = previous
97
+ end
98
+
99
+ def registry = ReactiveAdmin.config.api_guards
100
+
101
+ def registered(name) = registry.find { |key, _| key.to_s == name.to_s }&.last
102
+
103
+ def declare(config, guard, only: nil, except: nil, attributes: nil, reason: nil)
104
+ raise ArgumentError, "api needs a guard: false, :deny, :read_only, :session_only or a registered name" if guard.nil?
105
+ raise ArgumentError, "api: only: and except: don't combine" if only && except
106
+
107
+ guard = (guard == false) ? :deny : guard.to_sym
108
+ unknown_guard!(guard) unless STATIC.include?(guard) || registered(guard)
109
+ raise ArgumentError, "api :read_only has no attributes: it refuses every write already" if guard == :read_only && attributes
110
+
111
+ declaration = Declaration.new(guard:, only: normalize(only), except: normalize(except), attributes: normalize(attributes), reason: reason.presence)
112
+ ((config.reactive_options[:api_guards] ||= []) << declaration).last
113
+ end
114
+
115
+ def unknown_guard!(guard)
116
+ raise ArgumentError, "api #{guard.inspect}: no guard by that name. Built in: #{STATIC.map(&:inspect).join(", ")}; " \
117
+ "register your own with ReactiveAdmin.api_guard(#{guard.inspect}) { |user, request| ... } before the admin files load"
118
+ end
119
+
120
+ def normalize(list) = (Array(list).flatten.map(&:to_s) unless list.nil?)
121
+
122
+ def declarations(config) = config.try(:reactive_options)&.dig(:api_guards) || []
123
+
124
+ def declared?(config, channel = self.channel) = actions_of(config, channel).any? || attributes_of(config).any?
125
+
126
+ # On MCP requests the resource's MCP mode is one more guard, so it can only narrow what the others leave open.
127
+ def actions_of(config, channel = self.channel)
128
+ [*declarations(config).select { it.attributes.nil? }, *(Mcp::Access.declaration(config) if channel == :mcp)]
129
+ end
130
+
131
+ def attributes_of(config) = declarations(config).select(&:attributes)
132
+
133
+ def expand(names) = names.flat_map { [it.to_s, PAIRS[it.to_s]] }.compact.uniq
134
+
135
+ def matches?(list, names) = expand(names).intersect?(list)
136
+
137
+ def applies?(declaration, names)
138
+ return matches?(declaration.only, names) if declaration.only
139
+ return !matches?(declaration.except, names) if declaration.except
140
+ return !exempt?(declaration.exempt, names) if declaration.exempt
141
+
142
+ true
143
+ end
144
+
145
+ def exempt?(list, names)
146
+ case names
147
+ in ["batch_action", key] then list.include?(key)
148
+ in [key] then STANDARD.exclude?(key) && list.include?(key)
149
+ else false
150
+ end
151
+ end
152
+
153
+ # Whether a static guard refuses this request or action in `channel`. A read-only guard also
154
+ # closes the form of a write it refuses.
155
+ def refuses_statically?(declaration, config, names, verb:, channel:)
156
+ case declaration.guard
157
+ when :deny then true
158
+ when :session_only then channel != :session
159
+ when :read_only then unsafe?(config, names, verb)
160
+ else false
161
+ end
162
+ end
163
+
164
+ def unsafe?(config, names, verb)
165
+ return true if verb && %w[GET HEAD].exclude?(verb.to_s.upcase)
166
+
167
+ expand(names).any? { UNSAFE.include?(it) || custom_unsafe?(config, it, verb) }
168
+ end
169
+
170
+ def custom_unsafe?(config, name, verb)
171
+ declaration = AA.custom_actions(config).find { it.name.to_s == name }
172
+ !!declaration && (verb ? [verb] : Array.wrap(declaration.http_verb)).any? { !ActionSafety.read?(config, name, it) }
173
+ end
174
+
175
+ # The declaration that statically refuses these actions in `channel`, for the metadata.
176
+ def denial(config, names, verb: nil, channel: self.channel)
177
+ actions_of(config, channel).find do |declaration|
178
+ declaration.static? && applies?(declaration, names) && refuses_statically?(declaration, config, names, verb:, channel:)
179
+ end
180
+ end
181
+
182
+ def denies?(config, *names, verb: nil) = !denial(config, names.flatten.map(&:to_s), verb:).nil?
183
+
184
+ # Whether a session-only guard covers these actions, which a session still gets to use.
185
+ def session_only?(config, names)
186
+ channel == :session && actions_of(config).any? { it.guard == :session_only && applies?(it, names) }
187
+ end
188
+
189
+ # The registered guard that may refuse these actions, annotated since it can't be known ahead.
190
+ def custom_for(config, names) = actions_of(config).find { !it.static? && applies?(it, names) }
191
+
192
+ def message(declaration) = declaration.reason || MESSAGES.fetch(declaration.guard, ACTION_MESSAGE)
193
+
194
+ # A resource or page nothing of which the channel may use.
195
+ def whole?(config, channel: self.channel)
196
+ actions_of(config, channel).any? { it.blanket? && refuses_statically?(it, config, [], verb: nil, channel:) }
197
+ end
198
+
199
+ # Nothing of it is reachable through the API: denied whole, or every action it serves denied one by one.
200
+ def fully_denied?(config, channel: :session)
201
+ return true if whole?(config, channel:)
202
+
203
+ names = served_actions(config)
204
+ declared?(config, channel) && names.any? && names.all? { denial(config, [it], channel:) }
205
+ end
206
+
207
+ def served_actions(config)
208
+ return known_actions(config) if AA.page?(config)
209
+
210
+ [*Registry.actions(config).map(&:to_s), *known_actions(config) - STANDARD]
211
+ end
212
+
213
+ # The action a request exercises: refused when a guard refuses it, evaluated for real (registered
214
+ # guards run). `verb` is the request's own.
215
+ def refusal(controller, config, names, verb: controller.request.request_method)
216
+ channel = channel_of(controller.request)
217
+ actions_of(config, channel).each do |declaration|
218
+ next unless applies?(declaration, names)
219
+ next unless refused?(declaration, controller, config, names, verb:, channel:)
220
+
221
+ return Refusal.new(declaration.name, message(declaration), nil)
222
+ end
223
+ nil
224
+ end
225
+
226
+ def refused?(declaration, controller, config, names, verb:, channel:)
227
+ return refuses_statically?(declaration, config, names, verb:, channel:) if declaration.static?
228
+
229
+ !permits?(declaration, controller)
230
+ end
231
+
232
+ # A registered guard that is missing or raises refuses: nothing here fails open.
233
+ def permits?(declaration, controller)
234
+ guard = registered(declaration.guard) or return false
235
+ !!guard.call(ReactiveAdmin.current_user(controller), controller.request)
236
+ rescue => e
237
+ Rails.logger.warn("[reactiveadmin] api guard #{declaration.name} raised #{e.class}; refused")
238
+ false
239
+ end
240
+
241
+ # An API request by its format, or by living under the mount whatever format it claims.
242
+ def api_scope?(controller)
243
+ mount = ReactiveAdmin.config.mount_path.to_s.chomp("/")
244
+ controller.send(:reactiveadmin_request?) || (mount.present? && controller.request.path.start_with?("#{mount}/"))
245
+ end
246
+
247
+ # The action names this request exercises: its own, the one a gem endpoint stands in for, or the
248
+ # batch action it names.
249
+ def names_for(controller)
250
+ action = controller.action_name
251
+ params = controller.params
252
+ case action
253
+ when "batch_action" then [action, (params[:batch_action] if params[:batch_action].is_a?(String))].compact
254
+ when "reactiveadmin_options", "reactiveadmin_lookup" then [form_action(params)]
255
+ else [ENDPOINTS.fetch(action, action)]
256
+ end
257
+ end
258
+
259
+ def form_action(params)
260
+ return "index" unless params[:context] == "form"
261
+
262
+ params[:id].present? ? "edit" : "new"
263
+ end
264
+
265
+ def check(controller)
266
+ config = AA.config_of(controller)
267
+ return unless declared?(config, channel_of(controller.request))
268
+
269
+ refusal(controller, config, names_for(controller)) || attribute_refusal(controller, config)
270
+ end
271
+
272
+ # Guarded attributes the request writes, at any depth: key names are compared, wherever the schema
273
+ # maps them. Only unsafe verbs write; reads of these attributes are unchanged.
274
+ def attribute_refusal(controller, config)
275
+ request = controller.request
276
+ return if request.get? || request.head?
277
+
278
+ names = names_for(controller)
279
+ keys = keys_in(request.parameters)
280
+ channel = channel_of(request)
281
+ refused = attributes_of(config).filter_map do |declaration|
282
+ next unless applies?(declaration, names)
283
+
284
+ written = watched(config, declaration).filter_map { |key, declared| declared if keys.include?(key) }.uniq
285
+ [declaration, written] if written.any? && refused?(declaration, controller, config, names, verb: request.request_method, channel:)
286
+ end
287
+ return if refused.empty?
288
+
289
+ attributes = refused.flat_map(&:last).uniq.sort
290
+ declaration = refused.first.first
291
+ Refusal.new(declaration.name, declaration.reason || attribute_message(declaration, attributes), attributes)
292
+ end
293
+
294
+ def attribute_message(declaration, attributes)
295
+ who = (declaration.guard == :session_only) ? "A signed-in admin session" : "Only the classic admin"
296
+ "#{who} can change: #{attributes.join(", ")}"
297
+ end
298
+
299
+ # An association's foreign key and ids are the attribute by another name.
300
+ def watched(config, declaration)
301
+ declaration.attributes.each_with_object({}) do |name, map|
302
+ [name, "#{name}_attributes", *association_keys(config, name)].each { map[it] = name }
303
+ end
304
+ end
305
+
306
+ def association_keys(config, name)
307
+ association = config.try(:resource_class)&.reflect_on_association(name)
308
+ return [] unless association
309
+
310
+ association.collection? ? ["#{name.to_s.singularize}_ids"] : Array(association.foreign_key).map(&:to_s)
311
+ end
312
+
313
+ def keys_in(value, keys = Set.new)
314
+ case value
315
+ when Hash
316
+ value.each do |key, nested|
317
+ keys << key.to_s
318
+ keys_in((key.to_s == "batch_action_inputs") ? parsed(nested) : nested, keys)
319
+ end
320
+ when Array then value.each { keys_in(it, keys) }
321
+ end
322
+ keys
323
+ end
324
+
325
+ def parsed(value)
326
+ value.is_a?(String) ? JSON.parse(value) : value
327
+ rescue JSON::ParserError
328
+ nil
329
+ end
330
+
331
+ # Attribute declarations of the named actions that a static guard refuses in `channel`.
332
+ def denied_attributes(config, names, channel: self.channel)
333
+ attributes_of(config).select do |declaration|
334
+ declaration.static? && applies?(declaration, names) && refuses_statically?(declaration, config, names, verb: nil, channel:)
335
+ end
336
+ end
337
+
338
+ # The keys a write of these actions may not carry in `channel`, for the write schema and form fields.
339
+ def reserved_keys(config, names, channel: self.channel)
340
+ denied_attributes(config, names, channel:).flat_map { watched(config, it).keys }
341
+ end
342
+
343
+ # The registered guard that may refuse a write of one of these keys.
344
+ def attribute_guard(config, names, keys)
345
+ attributes_of(config).find { !it.static? && applies?(it, names) && watched(config, it).keys.intersect?(keys) }
346
+ end
347
+
348
+ # The classic page of the request: the same page when it can be fetched, else the record's or
349
+ # the resource's.
350
+ def classic_url(controller)
351
+ config = AA.config_of(controller)
352
+ request = controller.request
353
+ action = controller.action_name
354
+ return same_page(request) if request.get? && (action == "reactiveadmin_page" || !action.start_with?("reactiveadmin_"))
355
+
356
+ AA.page?(config) ? page_url(config, controller.params) : record_url(config, controller.params)
357
+ end
358
+
359
+ def same_page(request)
360
+ path = request.path.delete_prefix(ReactiveAdmin.config.mount_path).sub(API_SUFFIX, "").sub("/_pages/", "/")
361
+ [path, request.query_string.presence].compact.join("?")
362
+ end
363
+
364
+ def record_url(config, params)
365
+ collection = Paths.collection(config, parent_id: Paths.parent_id(config, params))
366
+ return collection if params[:id].blank? || !Registry.actions(config).include?(:show)
367
+
368
+ File.join(collection, ERB::Util.url_encode(params[:id].to_s))
369
+ end
370
+
371
+ def page_url(config, params)
372
+ parent = config.belongs_to_config if Paths.nested?(config)
373
+ ["", ReactiveAdmin.config.namespace, *(parent && [Registry.route_key(parent.target), params[parent.to_param]]), config.underscored_resource_name].compact.join("/")
374
+ end
375
+
376
+ # An action of a payload turned into a link to the classic admin when a static guard refuses it
377
+ # (nothing in it can run through the API), or annotated when a registered guard may.
378
+ def mark(action, config, *names)
379
+ names = names.map(&:to_s)
380
+ declaration = denial(config, names, verb: action[:method])
381
+ return annotate(action, custom_for(config, names)) unless declaration
382
+
383
+ action.except(:api_path, :form_path, :view, :confirm).merge(
384
+ api_denied: true, classic_url: action_url(action), supported: false, disabled_reason: message(declaration)
385
+ )
386
+ end
387
+
388
+ def annotate(payload, declaration) = declaration ? payload.merge(api_guard: declaration.name) : payload
389
+
390
+ # A GET is its own classic page; anything else lands on the page its target belongs to.
391
+ def action_url(action)
392
+ href = action[:href].to_s
393
+ return href if action[:method].to_s == "get"
394
+
395
+ path = href.split("?").first.to_s
396
+ %w[member collection custom].include?(action[:kind].to_s) ? File.dirname(path) : path
397
+ end
398
+
399
+ # What the manifest says of a resource that is only partly denied, in the current channel.
400
+ def summary(config)
401
+ return unless declared?(config) && !whole?(config)
402
+
403
+ known = known_actions(config).to_h { [it, action_names(config, it)] }
404
+ denied = known.select { |_, names| denies?(config, *names) }.keys
405
+ guarded = (known.keys - denied).to_h { [it, custom_for(config, known[it])&.name] }.compact
406
+ attributes = known.values.flat_map { denied_attributes(config, it) }.uniq.flat_map(&:attributes).uniq
407
+ summary = {actions: denied.presence, attributes: attributes.presence, guards: guarded.presence}.compact
408
+ summary.merge(classic_url: (Paths.collection(config) unless Paths.nested?(config))).compact unless summary.empty?
409
+ end
410
+
411
+ # A batch action is named with the batch endpoint; the rest by themselves.
412
+ def action_names(config, name)
413
+ standalone = AA.page?(config) || STANDARD.include?(name) || AA.custom_actions(config).any? { it.name.to_s == name }
414
+ standalone ? [name] : ["batch_action", name]
415
+ end
416
+
417
+ # Accepted decisions, for the doctor.
418
+ def accepted
419
+ [*Registry.resources, *Registry.pages].flat_map do |config|
420
+ declarations(config).map do |declaration|
421
+ {key: Exposure.key(config), kind: AA.page?(config) ? "page" : "resource", guard: declaration.name, whole: declaration.blanket?,
422
+ only: declaration.only, except: declaration.except, attributes: declaration.attributes, reason: declaration.reason}.compact
423
+ end
424
+ end
425
+ end
426
+
427
+ # A name that matches no action guards nothing, which a typo must not do quietly.
428
+ def audit
429
+ [*Registry.resources, *Registry.pages].flat_map do |config|
430
+ declarations(config).flat_map do |declaration|
431
+ (Array(declaration.only) + Array(declaration.except) - known_actions(config)).map do |name|
432
+ {kind: :api_guard_unknown_action, resource: Exposure.key(config), action: name,
433
+ reason: "api #{declaration.guard.inspect} names #{name}, which matches no action of this #{AA.page?(config) ? "page" : "resource"}"}
434
+ end
435
+ end
436
+ end
437
+ end
438
+
439
+ def known_actions(config)
440
+ return ["index", *AA.custom_actions(config).map { it.name.to_s }] if AA.page?(config)
441
+
442
+ [*STANDARD, *AA.custom_actions(config).map { it.name.to_s }, *AA.batch_actions(config).map { it.sym.to_s }]
443
+ end
444
+ end
445
+ end
@@ -0,0 +1,72 @@
1
+ module ReactiveAdmin
2
+ # One `audit.reactiveadmin` event for everything the per-kind events report, in one shape, plus record views
3
+ # (`config.audit_views`) and UI switches. Every field is an id or a name, never a value.
4
+ module Audit
5
+ EVENT = "audit.reactiveadmin".freeze
6
+ KINDS = %w[write write_rejected export value_viewed lookup mcp_call degraded view ui_switch].freeze
7
+ # Set by Mcp::Api on the requests a tool call makes (a client header arrives as HTTP_*), so its writes join the call.
8
+ CORRELATION_ENV = "reactiveadmin.correlation_id".freeze
9
+ ENVELOPE_KEYS = %i[user_id resource action id record_ids client_id status].freeze
10
+ OUTCOMES = {"write_rejected" => "rejected", "degraded" => "degraded"}.freeze
11
+
12
+ Event = Data.define(:kind, :user_id, :resource, :action, :record_ids, :channel, :client_id, :request_id, :correlation_id, :outcome, :at, :details)
13
+
14
+ module_function
15
+
16
+ # Publishes the per-kind event exactly as before, then the envelope, even if a legacy subscriber raises.
17
+ # `envelope` is what the envelope reads, when it differs from the legacy payload.
18
+ def instrument(kind, controller, payload, envelope = payload)
19
+ ActiveSupport::Notifications.instrument("#{kind}.reactiveadmin", payload)
20
+ ensure
21
+ publish(kind, controller, **envelope)
22
+ end
23
+
24
+ # Never raises: whatever is being audited has already happened.
25
+ def publish(kind, controller = nil, **fields)
26
+ ActiveSupport::Notifications.instrument(EVENT, snapshot(build(kind.to_s, controller, fields).to_h))
27
+ rescue => e
28
+ Rails.logger.error("[reactiveadmin] audit envelope failed: #{e.class}")
29
+ end
30
+
31
+ def build(kind, controller, fields)
32
+ request = controller&.request
33
+ Event.new(
34
+ kind:,
35
+ user_id: fields.fetch(:user_id) { ReactiveAdmin.current_user(controller)&.id if controller },
36
+ resource: fields[:resource]&.to_s,
37
+ action: fields.fetch(:action) { controller&.action_name }&.to_s,
38
+ record_ids: Array(fields[:record_ids] || fields[:id]).compact.map(&:to_s),
39
+ channel: request && channel(request),
40
+ client_id: request&.env&.dig(Auth::IDENTITY_ENV)&.client_id,
41
+ request_id: request&.request_id,
42
+ correlation_id: request && (request.env[CORRELATION_ENV] || request.request_id),
43
+ outcome: OUTCOMES.fetch(kind) { fields[:status] || "ok" },
44
+ at: Time.current,
45
+ # Both lookup and degraded payloads name their own sub-kind `kind`.
46
+ details: fields.except(*ENVELOPE_KEYS).compact.transform_keys(kind: :type)
47
+ )
48
+ end
49
+
50
+ # A deep frozen copy, so subscribers can't alter each other's events or the producer's objects.
51
+ def snapshot(value)
52
+ case value
53
+ when Hash then value.to_h { |k, v| [k, snapshot(v)] }.freeze
54
+ when Array then value.map { snapshot(it) }.freeze
55
+ else value.frozen? ? value : value.dup.freeze
56
+ end
57
+ end
58
+
59
+ def channel(request)
60
+ return "mcp" if request.env.key?(CORRELATION_ENV) || request.path_parameters[:controller] == "reactive_admin/mcp"
61
+
62
+ request.env[Auth::IDENTITY_ENV] ? "token" : "session"
63
+ end
64
+
65
+ # Subscribes `block` to the envelope; returns the subscriber, for ActiveSupport::Notifications.unsubscribe.
66
+ def subscribe(&block)
67
+ raise ArgumentError, "on_audit needs a block" unless block
68
+
69
+ ActiveSupport::Notifications.subscribe(EVENT) { |*, payload| block.call(Event.new(**payload)) }
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,68 @@
1
+ module ReactiveAdmin
2
+ module Auth
3
+ # Innermost on every API controller, so the guards stacked above it (rate limits, exposure, declared
4
+ # action authorization) still run after a token authenticates. For a token request it replaces what
5
+ # the session does: no `authentication_method`, the token's user as the current user, no CSRF.
6
+ module Authentication
7
+ private
8
+
9
+ def authenticate_active_admin_user
10
+ super unless token_request?
11
+ end
12
+
13
+ # For hosts whose `current_user_method` isn't defined on the controller.
14
+ def current_active_admin_user = token_request? ? Auth.identity(request)&.user : super
15
+
16
+ # A token isn't ambient the way a cookie is, so a forged cross-site request can't carry one.
17
+ def verified_request? = token_request? || super
18
+
19
+ def token_request? = reactiveadmin_request? && Auth.token_based?(request)
20
+ end
21
+
22
+ # Outermost: the token is checked, and the user installed, before any callback (the host's included)
23
+ # can ask who is signed in.
24
+ module Request
25
+ private
26
+
27
+ def process_action(*)
28
+ return super unless reactiveadmin_request?
29
+
30
+ ApiGuards::Current.channel = ApiGuards.channel_of(request)
31
+ unless token_request?
32
+ return super unless token_only?
33
+
34
+ return reject_credentials(:unauthorized, "Authentication required", Auth.challenge)
35
+ end
36
+
37
+ identity = Auth.identity(request)
38
+ return reject_credentials(:unauthorized, "Invalid or expired token", Auth.challenge(error: "invalid_token")) unless identity
39
+
40
+ scope = required_scope
41
+ return reject_credentials(:insufficient_scope, "Token lacks the #{scope} scope", Auth.challenge(error: "insufficient_scope", scope:), 403) unless identity.allows?(scope)
42
+
43
+ install(identity)
44
+ super
45
+ end
46
+
47
+ # Only the MCP endpoint refuses sessions: its callers are agents, never a browser.
48
+ def token_only? = false
49
+
50
+ def required_scope
51
+ mcp = ReactiveAdmin.config.mcp
52
+ ActionSafety.request_read?(self) ? mcp.read_scope : mcp.write_scope
53
+ end
54
+
55
+ def install(identity)
56
+ request.session_options[:skip] = true
57
+ user = identity.user
58
+ method = AA.current_user_method(Registry.namespace)
59
+ define_singleton_method(method) { user } if method
60
+ end
61
+
62
+ def reject_credentials(code, message, challenge, status = 401)
63
+ response.headers["WWW-Authenticate"] = challenge
64
+ render_api_error(code, message, status)
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,15 @@
1
+ module ReactiveAdmin
2
+ module Auth
3
+ # A bearer request to the API carries no session: its cookies are dropped before the host's session
4
+ # and authentication middleware or callbacks can read them, so the token is the only credential.
5
+ class TokenRequests
6
+ def initialize(app) = @app = app
7
+
8
+ def call(env)
9
+ request = ActionDispatch::Request.new(env)
10
+ env.delete("HTTP_COOKIE") if Auth.token_based?(request)
11
+ @app.call(env)
12
+ end
13
+ end
14
+ end
15
+ end