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.
- checksums.yaml +7 -0
- data/CHANGELOG.md +88 -0
- data/LICENSE +21 -0
- data/README.md +81 -0
- data/app/assets/reactive_admin/bridge.js +89 -0
- data/app/controllers/reactive_admin/api_controller.rb +30 -0
- data/app/controllers/reactive_admin/client_errors_controller.rb +38 -0
- data/app/controllers/reactive_admin/manifest_controller.rb +10 -0
- data/app/controllers/reactive_admin/mcp_controller.rb +48 -0
- data/app/controllers/reactive_admin/openapi_controller.rb +13 -0
- data/app/controllers/reactive_admin/session_controller.rb +11 -0
- data/app/views/layouts/reactive_admin/embed.html.erb +30 -0
- data/lib/generators/reactiveadmin/install/install_generator.rb +341 -0
- data/lib/generators/reactiveadmin/install/templates/menu.rb.tt +25 -0
- data/lib/generators/reactiveadmin/install/templates/reactive_admin.rb.tt +53 -0
- data/lib/generators/reactiveadmin/install/templates/reactive_admin_annotated.rb.tt +110 -0
- data/lib/reactive_admin/aa/v3/controller.rb +76 -0
- data/lib/reactive_admin/aa/v3/forms.rb +29 -0
- data/lib/reactive_admin/aa/v3/inputs/filters/typeahead_input.rb +51 -0
- data/lib/reactive_admin/aa/v3/inputs/typeahead_input.rb +8 -0
- data/lib/reactive_admin/aa/v3/install.rb +36 -0
- data/lib/reactive_admin/aa/v3/menu.rb +67 -0
- data/lib/reactive_admin/aa/v3/presenters.rb +102 -0
- data/lib/reactive_admin/aa/v3/registry.rb +78 -0
- data/lib/reactive_admin/aa/v3/views.rb +63 -0
- data/lib/reactive_admin/aa/v3.rb +23 -0
- data/lib/reactive_admin/aa/v4.rb +59 -0
- data/lib/reactive_admin/aa.rb +46 -0
- data/lib/reactive_admin/action_items.rb +39 -0
- data/lib/reactive_admin/action_links.rb +197 -0
- data/lib/reactive_admin/action_safety.rb +19 -0
- data/lib/reactive_admin/actions.rb +95 -0
- data/lib/reactive_admin/api_errors.rb +116 -0
- data/lib/reactive_admin/api_fallback.rb +15 -0
- data/lib/reactive_admin/api_guards.rb +445 -0
- data/lib/reactive_admin/audit.rb +72 -0
- data/lib/reactive_admin/auth/guard.rb +68 -0
- data/lib/reactive_admin/auth/token_requests.rb +15 -0
- data/lib/reactive_admin/auth.rb +135 -0
- data/lib/reactive_admin/authorizer.rb +47 -0
- data/lib/reactive_admin/background_requests.rb +33 -0
- data/lib/reactive_admin/batch_actions.rb +57 -0
- data/lib/reactive_admin/choices.rb +289 -0
- data/lib/reactive_admin/client_error.rb +70 -0
- data/lib/reactive_admin/composite_keys.rb +12 -0
- data/lib/reactive_admin/configuration.rb +221 -0
- data/lib/reactive_admin/controller_extension.rb +229 -0
- data/lib/reactive_admin/csv_injection.rb +26 -0
- data/lib/reactive_admin/date_bound.rb +31 -0
- data/lib/reactive_admin/degradation.rb +72 -0
- data/lib/reactive_admin/diagnostics.rb +16 -0
- data/lib/reactive_admin/doctor.rb +466 -0
- data/lib/reactive_admin/dry_run.rb +147 -0
- data/lib/reactive_admin/dry_run_flag.rb +18 -0
- data/lib/reactive_admin/embed.rb +73 -0
- data/lib/reactive_admin/engine.rb +47 -0
- data/lib/reactive_admin/error_keys.rb +23 -0
- data/lib/reactive_admin/exposure.rb +86 -0
- data/lib/reactive_admin/filters.rb +165 -0
- data/lib/reactive_admin/fixtures/scrubber.rb +1075 -0
- data/lib/reactive_admin/fixtures.rb +161 -0
- data/lib/reactive_admin/form_content.rb +78 -0
- data/lib/reactive_admin/form_field.rb +279 -0
- data/lib/reactive_admin/form_has_many.rb +116 -0
- data/lib/reactive_admin/form_lock.rb +71 -0
- data/lib/reactive_admin/form_payload.rb +57 -0
- data/lib/reactive_admin/form_recorder.rb +192 -0
- data/lib/reactive_admin/form_schema.rb +160 -0
- data/lib/reactive_admin/frame.rb +97 -0
- data/lib/reactive_admin/html_nodes.rb +390 -0
- data/lib/reactive_admin/html_policy.rb +338 -0
- data/lib/reactive_admin/html_rich.rb +186 -0
- data/lib/reactive_admin/implicit_defaults.rb +102 -0
- data/lib/reactive_admin/index_options.rb +41 -0
- data/lib/reactive_admin/index_payload.rb +259 -0
- data/lib/reactive_admin/index_recorder.rb +115 -0
- data/lib/reactive_admin/index_views.rb +85 -0
- data/lib/reactive_admin/inline_style.rb +125 -0
- data/lib/reactive_admin/labels_payload.rb +34 -0
- data/lib/reactive_admin/ladder.rb +268 -0
- data/lib/reactive_admin/layout_classes.rb +167 -0
- data/lib/reactive_admin/legacy_inventory.rb +547 -0
- data/lib/reactive_admin/location.rb +22 -0
- data/lib/reactive_admin/lookup_audit.rb +30 -0
- data/lib/reactive_admin/lookup_payload.rb +41 -0
- data/lib/reactive_admin/manifest.rb +36 -0
- data/lib/reactive_admin/masked_json.rb +62 -0
- data/lib/reactive_admin/masking.rb +100 -0
- data/lib/reactive_admin/mcp/access.rb +111 -0
- data/lib/reactive_admin/mcp/api.rb +123 -0
- data/lib/reactive_admin/mcp/count_tool.rb +22 -0
- data/lib/reactive_admin/mcp/layout.rb +175 -0
- data/lib/reactive_admin/mcp/output.rb +46 -0
- data/lib/reactive_admin/mcp/prompts.rb +111 -0
- data/lib/reactive_admin/mcp/server.rb +134 -0
- data/lib/reactive_admin/mcp/shapes.rb +164 -0
- data/lib/reactive_admin/mcp/tools.rb +386 -0
- data/lib/reactive_admin/mcp.rb +53 -0
- data/lib/reactive_admin/media_nodes.rb +34 -0
- data/lib/reactive_admin/menu.rb +90 -0
- data/lib/reactive_admin/menu_overlay.rb +291 -0
- data/lib/reactive_admin/node_registry.rb +47 -0
- data/lib/reactive_admin/open_api/builder.rb +156 -0
- data/lib/reactive_admin/open_api/context.rb +46 -0
- data/lib/reactive_admin/open_api/core.rb +51 -0
- data/lib/reactive_admin/open_api/operation_ids.rb +18 -0
- data/lib/reactive_admin/open_api/page_doc.rb +75 -0
- data/lib/reactive_admin/open_api/parts.rb +86 -0
- data/lib/reactive_admin/open_api/resource_doc.rb +423 -0
- data/lib/reactive_admin/open_api/security.rb +55 -0
- data/lib/reactive_admin/open_api/write_schema.rb +104 -0
- data/lib/reactive_admin/open_api.rb +41 -0
- data/lib/reactive_admin/option_source.rb +36 -0
- data/lib/reactive_admin/option_sources.rb +14 -0
- data/lib/reactive_admin/options_payload.rb +61 -0
- data/lib/reactive_admin/page_controller_extension.rb +112 -0
- data/lib/reactive_admin/page_forms.rb +181 -0
- data/lib/reactive_admin/page_payload.rb +120 -0
- data/lib/reactive_admin/param_normalizer.rb +89 -0
- data/lib/reactive_admin/paths.rb +63 -0
- data/lib/reactive_admin/permissions.rb +31 -0
- data/lib/reactive_admin/provenance.rb +57 -0
- data/lib/reactive_admin/query_guard.rb +108 -0
- data/lib/reactive_admin/ransack_guard.rb +107 -0
- data/lib/reactive_admin/rate_limit.rb +59 -0
- data/lib/reactive_admin/reactive_dsl.rb +99 -0
- data/lib/reactive_admin/readiness.rb +189 -0
- data/lib/reactive_admin/registry.rb +69 -0
- data/lib/reactive_admin/resource_meta.rb +120 -0
- data/lib/reactive_admin/resource_summary.rb +41 -0
- data/lib/reactive_admin/routes.rb +60 -0
- data/lib/reactive_admin/row_errors.rb +29 -0
- data/lib/reactive_admin/rspec.rb +116 -0
- data/lib/reactive_admin/scrubbed_error.rb +22 -0
- data/lib/reactive_admin/search.rb +79 -0
- data/lib/reactive_admin/server_timing.rb +31 -0
- data/lib/reactive_admin/show_payload.rb +88 -0
- data/lib/reactive_admin/sidebars.rb +34 -0
- data/lib/reactive_admin/site.rb +55 -0
- data/lib/reactive_admin/style_report.rb +129 -0
- data/lib/reactive_admin/style_sources.rb +83 -0
- data/lib/reactive_admin/template_actions.rb +126 -0
- data/lib/reactive_admin/template_content.rb +49 -0
- data/lib/reactive_admin/template_form.rb +442 -0
- data/lib/reactive_admin/test_support.rb +31 -0
- data/lib/reactive_admin/typeahead.rb +33 -0
- data/lib/reactive_admin/ui/assets.rb +91 -0
- data/lib/reactive_admin/ui/client_build.rb +47 -0
- data/lib/reactive_admin/ui/helper.rb +24 -0
- data/lib/reactive_admin/ui/packaged_client.rb +67 -0
- data/lib/reactive_admin/ui/shell.rb +81 -0
- data/lib/reactive_admin/ui/switch.rb +38 -0
- data/lib/reactive_admin/ui.rb +43 -0
- data/lib/reactive_admin/value_payload.rb +147 -0
- data/lib/reactive_admin/value_token.rb +36 -0
- data/lib/reactive_admin/values.rb +43 -0
- data/lib/reactive_admin/version.rb +3 -0
- data/lib/reactive_admin/write_audit.rb +60 -0
- data/lib/reactive_admin/write_guards.rb +142 -0
- data/lib/reactive_admin.rb +153 -0
- data/lib/reactiveadmin.rb +1 -0
- data/lib/tasks/reactiveadmin.rake +36 -0
- data/lib/tasks/reactiveadmin_client.rake +11 -0
- data/lib/tasks/reactiveadmin_legacy.rake +7 -0
- data/lib/tasks/reactiveadmin_styles.rake +6 -0
- data/openapi/core.yaml +1643 -0
- data/vendor/client/assets/ActionPage-XUYorI8E.js +1 -0
- data/vendor/client/assets/ActionView-BsZFcsgp.js +1 -0
- data/vendor/client/assets/CommandPalette-C59HT1ip.js +1 -0
- data/vendor/client/assets/DatePicker-DuaK0iMg.js +1 -0
- data/vendor/client/assets/DateRangeCalendar-B2BDsG-X.js +1 -0
- data/vendor/client/assets/DegradedNotice-DiH5n2DO.js +1 -0
- data/vendor/client/assets/FormDialog-pmYl8Eov.js +1 -0
- data/vendor/client/assets/FormSkeleton-DuSBZO3C.js +1 -0
- data/vendor/client/assets/FrameView-2X8cFvUt.css +1 -0
- data/vendor/client/assets/FrameView-zmUeDO8d.js +1 -0
- data/vendor/client/assets/GroupOverview-DI5T3k5K.js +1 -0
- data/vendor/client/assets/InlineForm-CMEIMgeh.js +1 -0
- data/vendor/client/assets/JsonDiff-o7JWr7CH.js +3 -0
- data/vendor/client/assets/JsonEditor-D4VXdlEx.js +1 -0
- data/vendor/client/assets/JsonView-BrJLDmSN.js +1 -0
- data/vendor/client/assets/MobileMenu-GAeFOeEb.js +1 -0
- data/vendor/client/assets/Node-SQl61V4K.js +2 -0
- data/vendor/client/assets/PageView-C-laNEPZ.js +1 -0
- data/vendor/client/assets/ResourceForm-BPjsoCY6.js +1 -0
- data/vendor/client/assets/ResourceIndex-COezAw5t.js +3 -0
- data/vendor/client/assets/ResourceOrPage-CRZGCY84.js +1 -0
- data/vendor/client/assets/ResourceShow-DweRXn18.js +1 -0
- data/vendor/client/assets/Segmented-BE9TCbph.js +1 -0
- data/vendor/client/assets/Sidebars-CsB9rFqX.js +1 -0
- data/vendor/client/assets/breadcrumb-DSA3Tlyd.js +1 -0
- data/vendor/client/assets/button-CvZ4_DFC.js +1 -0
- data/vendor/client/assets/calendar-BoyMpJIf.js +1 -0
- data/vendor/client/assets/check-C5A3J16N.js +1 -0
- data/vendor/client/assets/command-CMuk0oAt.js +1 -0
- data/vendor/client/assets/dist-CqEI82S_.js +1 -0
- data/vendor/client/assets/dist-IW4yX942.js +1 -0
- data/vendor/client/assets/editor-yqRLzgsJ.js +2 -0
- data/vendor/client/assets/external-link-C2aR4slL.js +1 -0
- data/vendor/client/assets/frames-C9gPCc3N.js +1 -0
- data/vendor/client/assets/humanize-DtU_Kpr4.js +1 -0
- data/vendor/client/assets/ibm-plex-mono-cyrillic-400-normal-BSMlKf0J.woff2 +0 -0
- data/vendor/client/assets/ibm-plex-mono-cyrillic-400-normal-CEL4l2ZJ.woff +0 -0
- data/vendor/client/assets/ibm-plex-mono-cyrillic-500-normal-Ael50iVv.woff +0 -0
- data/vendor/client/assets/ibm-plex-mono-cyrillic-500-normal-Bq9vWWag.woff2 +0 -0
- data/vendor/client/assets/ibm-plex-mono-cyrillic-ext-400-normal-DMdlQ8Kv.woff +0 -0
- data/vendor/client/assets/ibm-plex-mono-cyrillic-ext-400-normal-xuaO2J-f.woff2 +0 -0
- data/vendor/client/assets/ibm-plex-mono-cyrillic-ext-500-normal-BIfNGwUT.woff +0 -0
- data/vendor/client/assets/ibm-plex-mono-cyrillic-ext-500-normal-BqneJy0T.woff2 +0 -0
- data/vendor/client/assets/ibm-plex-mono-latin-400-normal-CvHOgSBP.woff +0 -0
- data/vendor/client/assets/ibm-plex-mono-latin-400-normal-DMJ8VG8y.woff2 +0 -0
- data/vendor/client/assets/ibm-plex-mono-latin-500-normal-CB9ihrfo.woff +0 -0
- data/vendor/client/assets/ibm-plex-mono-latin-500-normal-DSY6xOcd.woff2 +0 -0
- data/vendor/client/assets/ibm-plex-mono-latin-ext-400-normal-BmRBH3aV.woff2 +0 -0
- data/vendor/client/assets/ibm-plex-mono-latin-ext-400-normal-D3D2R8hC.woff +0 -0
- data/vendor/client/assets/ibm-plex-mono-latin-ext-500-normal-CAhNIIs5.woff2 +0 -0
- data/vendor/client/assets/ibm-plex-mono-latin-ext-500-normal-CZ70TYgx.woff +0 -0
- data/vendor/client/assets/ibm-plex-mono-vietnamese-400-normal-BulugwFq.woff2 +0 -0
- data/vendor/client/assets/ibm-plex-mono-vietnamese-400-normal-DDuiU_S-.woff +0 -0
- data/vendor/client/assets/ibm-plex-mono-vietnamese-500-normal-C8zxqsMH.woff +0 -0
- data/vendor/client/assets/ibm-plex-mono-vietnamese-500-normal-DZ4AoWbu.woff2 +0 -0
- data/vendor/client/assets/ibm-plex-sans-cyrillic-ext-wght-normal-d45eAU9y.woff2 +0 -0
- data/vendor/client/assets/ibm-plex-sans-cyrillic-wght-normal-BAAhND-U.woff2 +0 -0
- data/vendor/client/assets/ibm-plex-sans-greek-wght-normal-CmyJS8uq.woff2 +0 -0
- data/vendor/client/assets/ibm-plex-sans-latin-ext-wght-normal-CIII54If.woff2 +0 -0
- data/vendor/client/assets/ibm-plex-sans-latin-wght-normal-IvpUvPa2.woff2 +0 -0
- data/vendor/client/assets/ibm-plex-sans-vietnamese-wght-normal-Dg1JeJN0.woff2 +0 -0
- data/vendor/client/assets/index-B51aUaAY.js +43 -0
- data/vendor/client/assets/index-Cs9rCoWM.css +2 -0
- data/vendor/client/assets/input-DByFmQPa.js +1 -0
- data/vendor/client/assets/json-De3PN-R6.css +1 -0
- data/vendor/client/assets/json-oTp7Q-d-.js +5 -0
- data/vendor/client/assets/media-D4a_En8R.js +1 -0
- data/vendor/client/assets/plus-CfJ9vo8X.js +1 -0
- data/vendor/client/assets/popover-CDtI9R8Q.js +1 -0
- data/vendor/client/assets/purify-G7QKJyJq.js +1 -0
- data/vendor/client/assets/query-CCsZN2fa.js +1 -0
- data/vendor/client/assets/recent-BrzLwL0D.js +1 -0
- data/vendor/client/assets/rich-BQc_q5T7.js +1 -0
- data/vendor/client/assets/rolldown-runtime-XKyaHtl9.js +1 -0
- data/vendor/client/assets/select-C43qDA3k.js +1 -0
- data/vendor/client/assets/sheet-BPl2n-4n.js +1 -0
- data/vendor/client/assets/textarea-BnOphXry.js +1 -0
- data/vendor/client/assets/uiMode-z0ZbRV49.js +1 -0
- data/vendor/client/assets/uiToggle-DwnRQTH1.js +1 -0
- data/vendor/client/assets/useRecordForm-X_DExEP2.js +1 -0
- data/vendor/client/assets/useRecordVisit-BRtfMi-q.js +1 -0
- data/vendor/client/assets/utils-DwBGbDkG.js +1 -0
- data/vendor/client/assets/vendor-calendar-nTZiq9oY.js +1 -0
- data/vendor/client/assets/vendor-datefns-BoC6MCnl.js +1 -0
- data/vendor/client/assets/vendor-forms-D0h8hG_S.js +55 -0
- data/vendor/client/assets/vendor-purify-BXoPR7WT.js +3 -0
- data/vendor/client/assets/vendor-query-DZ6APRNI.js +1 -0
- data/vendor/client/assets/vendor-react-C1iE8mnY.js +9 -0
- data/vendor/client/assets/vendor-router-CI6Vh4k_.js +1 -0
- data/vendor/client/assets/vendor-table-DuQ835by.js +4 -0
- data/vendor/client/index.html +24 -0
- data/vendor/client/manifest.json +867 -0
- data/vendor/client/reactiveadmin-client.json +4 -0
- metadata +350 -0
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
module ReactiveAdmin
|
|
2
|
+
class Configuration
|
|
3
|
+
# `info`: OpenAPI info overrides (default title "Admin API", the gem's version). `output`: where
|
|
4
|
+
# `rake reactiveadmin:openapi` writes. `security_schemes`: name => OpenAPI scheme, replacing the
|
|
5
|
+
# session cookie and CSRF token defaults (e.g. a bearer scheme).
|
|
6
|
+
OpenApiSettings = Struct.new(:info, :output, :security_schemes)
|
|
7
|
+
# The MCP endpoint (`{mount}/_mcp`) and the token scopes the API enforces; see ReactiveAdmin::Mcp.
|
|
8
|
+
class Mcp
|
|
9
|
+
# false, true, or ->(user) { boolean } so a host can gate the endpoint per user (feature flags).
|
|
10
|
+
# Disabled answers 404.
|
|
11
|
+
attr_accessor :enabled
|
|
12
|
+
# Scopes a token needs for reads (GET) and writes. They apply only to tokens whose bearer block
|
|
13
|
+
# returned `scopes:`; set one to nil to stop requiring it.
|
|
14
|
+
attr_accessor :read_scope, :write_scope
|
|
15
|
+
# Protected resource metadata URL (RFC 9728) sent in `WWW-Authenticate` on 401, so MCP clients can
|
|
16
|
+
# find the authorization server.
|
|
17
|
+
attr_accessor :resource_metadata_url
|
|
18
|
+
# Browser origins besides this site's own allowed to call `_mcp`; requests without Origin are
|
|
19
|
+
# not browsers and pass.
|
|
20
|
+
attr_accessor :allowed_origins
|
|
21
|
+
# Cap on one tool result, in bytes of JSON, and on `per_page` for list_records.
|
|
22
|
+
attr_accessor :max_response_bytes, :max_per_page
|
|
23
|
+
# Shown to the agent by `initialize`.
|
|
24
|
+
attr_accessor :instructions
|
|
25
|
+
# What agents get of a resource or page without `reactive { mcp ... }`: :read_only, :none or :full.
|
|
26
|
+
attr_reader :default
|
|
27
|
+
|
|
28
|
+
def initialize
|
|
29
|
+
@enabled = false
|
|
30
|
+
@read_scope = "admin:read"
|
|
31
|
+
@write_scope = "admin:write"
|
|
32
|
+
@allowed_origins = []
|
|
33
|
+
@max_response_bytes = 20_000
|
|
34
|
+
@max_per_page = 25
|
|
35
|
+
@default = :read_only
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def default=(mode)
|
|
39
|
+
raise ArgumentError, "mcp.default must be one of #{ReactiveAdmin::Mcp::Access::MODES.map(&:inspect).join(", ")}" unless ReactiveAdmin::Mcp::Access::MODES.include?(mode)
|
|
40
|
+
|
|
41
|
+
@default = mode
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def enabled_for?(user) = enabled.respond_to?(:call) ? !!enabled.call(user) : !!enabled
|
|
45
|
+
|
|
46
|
+
def possibly_enabled? = enabled.respond_to?(:call) || !!enabled
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# Serves the React client at the classic admin's URLs; see ReactiveAdmin::Ui.
|
|
50
|
+
class Ui
|
|
51
|
+
NOBODY = ->(_user) { false }
|
|
52
|
+
|
|
53
|
+
# Whether the middleware answers browsers that chose the new admin (cookie) with the client's page.
|
|
54
|
+
attr_accessor :enabled
|
|
55
|
+
# ->(user) { boolean }: who may use the new admin and switch between the two. The client reads it from
|
|
56
|
+
# `_session`; it is a UI gate, and the API authorizes the same as ever. Nobody by default.
|
|
57
|
+
attr_accessor :available
|
|
58
|
+
# :classic or :new, or ->(user) { mode }: the UI to start an available user in, until they choose.
|
|
59
|
+
attr_accessor :default
|
|
60
|
+
# Directory of the built client (`rake reactiveadmin:client:build`); a Pathname, String or callable.
|
|
61
|
+
# Default: public/reactiveadmin in the host.
|
|
62
|
+
attr_accessor :client_assets
|
|
63
|
+
# URL prefix the built client is served under. A path (the gem serves it when nothing else does) or
|
|
64
|
+
# an absolute URL (a CDN). Builds refer to their own files relatively, so no rebuild is needed for either.
|
|
65
|
+
attr_accessor :asset_prefix
|
|
66
|
+
# Whether the client reports crashes to `_client_errors`, which hands them to `config.error_reporter`.
|
|
67
|
+
# Off, the endpoint answers 404 and the client sends nothing.
|
|
68
|
+
attr_accessor :client_errors
|
|
69
|
+
|
|
70
|
+
def initialize
|
|
71
|
+
@enabled = true
|
|
72
|
+
@available = NOBODY
|
|
73
|
+
@default = :classic
|
|
74
|
+
@client_assets = nil
|
|
75
|
+
@asset_prefix = "/reactiveadmin"
|
|
76
|
+
@client_errors = true
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
attr_accessor :namespace, :inline_options_limit, :current_user,
|
|
81
|
+
:meta_cache_key, :meta_cache_version, :meta_cache_ttl, :options_cache_ttl, :scope_options_by_policy,
|
|
82
|
+
:after_authorize, :on_background_request, :doctor_user, :doctor_page_params, :masked_attributes, :mask_filter_parameters, :mask_csv, :html_trusted_hosts,
|
|
83
|
+
:require_lock, :retryable_errors, :error_reporter,
|
|
84
|
+
:inline_json_limit, :inline_text_limit, :value_token, :value_token_ttl, :strict_defaults, :exposed_resources, :max_per_page, :max_export_rows, :rate_limits, :sensitive_resources,
|
|
85
|
+
:ransack_unguarded_actions, :auth, :audit_views, :event_counts
|
|
86
|
+
attr_reader :mount_path, :authorize_custom_actions, :implicit_defaults, :expose, :openapi, :mcp, :api_guards, :ui
|
|
87
|
+
|
|
88
|
+
# Callers join and strip this as a prefix, so "/api/" or "api" must mean "/api", and a root mount "".
|
|
89
|
+
def mount_path=(path)
|
|
90
|
+
bare = path.to_s.delete_prefix("/").chomp("/")
|
|
91
|
+
@mount_path = bare.empty? ? "" : "/#{bare}"
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def initialize
|
|
95
|
+
@namespace = :admin
|
|
96
|
+
self.mount_path = "/api"
|
|
97
|
+
@inline_options_limit = 100
|
|
98
|
+
# ->(controller) { user }; falls back to the namespace's current_user_method.
|
|
99
|
+
@current_user = nil
|
|
100
|
+
# ->(user) { JSON-able }: what in the user changes the _meta payload (permissions, roles).
|
|
101
|
+
# Users with an equal key share a cached payload.
|
|
102
|
+
@meta_cache_key = ->(user) { user.try(:id) }
|
|
103
|
+
# -> { value }: must change whenever admin code does, so a deploy never serves stale metadata.
|
|
104
|
+
@meta_cache_version = nil
|
|
105
|
+
@meta_cache_ttl = 5.minutes
|
|
106
|
+
# Seconds a non-relation options list (literal arrays, computed collections) is kept; 0 disables.
|
|
107
|
+
@options_cache_ttl = 60
|
|
108
|
+
# Limit typeahead options to what the user may read (the target model's AA authorization scope).
|
|
109
|
+
@scope_options_by_policy = true
|
|
110
|
+
# true: every custom member/collection action (and batch_action) is authorized up front
|
|
111
|
+
# through the adapter. :match_active_admin: only what AA itself authorizes, i.e. when the
|
|
112
|
+
# action body loads `resource`/`collection`.
|
|
113
|
+
@authorize_custom_actions = true
|
|
114
|
+
# ->(controller) { ... } run after the manifest is authorized; a hook for hosts whose base
|
|
115
|
+
# controller has authorization-verifying callbacks (e.g. `skip_authorization` under Pundit).
|
|
116
|
+
@after_authorize = nil
|
|
117
|
+
# ->(controller) { ... } run for requests the client marks as background (prefetch, _meta,
|
|
118
|
+
# _options, _session): the host's place to skip view audits or session extension.
|
|
119
|
+
@on_background_request = nil
|
|
120
|
+
# -> { current user } what the namespace's current_user_method returns, for `rake reactiveadmin:doctor` to
|
|
121
|
+
# sample pages and records as; without one it only reads code.
|
|
122
|
+
@doctor_user = nil
|
|
123
|
+
# ->(page_key) { {param: value} or nil }: query params the doctor renders a page with, for pages
|
|
124
|
+
# that read required params (`Model.find(params[:id])`). nil renders without any.
|
|
125
|
+
@doctor_page_params = nil
|
|
126
|
+
# Attribute names (symbols or regexps) whose values are masked everywhere in the API, on top of
|
|
127
|
+
# a resource's `reactive { mask ... }`; see Masking.
|
|
128
|
+
@masked_attributes = []
|
|
129
|
+
# Also mask attributes matching Rails' `filter_parameters`. Turn off where that list holds
|
|
130
|
+
# fields operators legitimately see (names, emails) and name the secrets in masked_attributes.
|
|
131
|
+
@mask_filter_parameters = true
|
|
132
|
+
# Mask the same attributes in CSV exports, classic and API alike.
|
|
133
|
+
@mask_csv = true
|
|
134
|
+
# Updates must carry the `lock` token the edit form handed out; false accepts those without one
|
|
135
|
+
# (clients that predate locking), still rejecting a stale token.
|
|
136
|
+
@require_lock = true
|
|
137
|
+
# Error classes (or their names, for constants that reload) answered 409 `retry` (with Retry-After)
|
|
138
|
+
# instead of 500: concurrency guards the host raises when a write loses a race.
|
|
139
|
+
@retryable_errors = []
|
|
140
|
+
# ->(error, context) { ... } where unexpected errors are reported. `error` is a ScrubbedError (class,
|
|
141
|
+
# backtrace and request only, never the original message), so it is safe to hand to any service.
|
|
142
|
+
@error_reporter = ->(error, context) { Rails.error.report(error, context:, handled: true, source: "reactiveadmin") }
|
|
143
|
+
# Hosts (an array, or a callable returning one) whose images and embedded documents html nodes may
|
|
144
|
+
# load besides this site, e.g. a document CDN. Everything else external becomes a link.
|
|
145
|
+
@html_trusted_hosts = []
|
|
146
|
+
# Largest json value (bytes of JSON) a show page sends inline; anything bigger, and every index
|
|
147
|
+
# cell, is summarized and fetched on demand from the audited `_value` endpoint.
|
|
148
|
+
@inline_json_limit = 2048
|
|
149
|
+
# The same for text columns a resource shows by default (see `implicit_defaults`): up to this many bytes
|
|
150
|
+
# inline on show, anything longer, and every index cell, is truncated with an audited `expand_path`.
|
|
151
|
+
@inline_text_limit = 2048
|
|
152
|
+
# `_value` answers only for an `expand_path` this server signed for the same user, record and
|
|
153
|
+
# field, so it never re-renders a page to learn what the field may expose. false: the legacy allowlist.
|
|
154
|
+
@value_token = true
|
|
155
|
+
@value_token_ttl = 15.minutes
|
|
156
|
+
# Columns a resource without an `index`/`show` block shows. :secure shows json and text columns
|
|
157
|
+
# summarized or truncated (the full value is an audited `_value` call) and leaves out binary
|
|
158
|
+
# columns, encrypted, filtered and masked attributes; :all is AA's behavior.
|
|
159
|
+
@implicit_defaults = :secure
|
|
160
|
+
# true: a resource without an `index`/`show` block gets no columns or layout at all.
|
|
161
|
+
@strict_defaults = false
|
|
162
|
+
# :all serves every resource and page the policy permits. :listed serves only what the host
|
|
163
|
+
# names (menu overlay, exposed_resources, `reactive { expose true }`); see Exposure.
|
|
164
|
+
@expose = :all
|
|
165
|
+
# Route keys (resources) and page keys served under :listed, for nested-only and hidden ones.
|
|
166
|
+
@exposed_resources = []
|
|
167
|
+
# Largest page the API serves, whatever `per_page` asks for. A resource's own `max_per_page`
|
|
168
|
+
# still applies, and the page sizes it declares (`config.per_page = [25, 250]`) raise this for it.
|
|
169
|
+
@max_per_page = 100
|
|
170
|
+
# An export of more rows is refused (422); nil disables the limit.
|
|
171
|
+
@max_export_rows = 10_000
|
|
172
|
+
# Requests per user in a window, by endpoint: [count, period]. A bucket set to nil is unlimited;
|
|
173
|
+
# `default` covers every other API request. false turns limiting off. Needs a shared
|
|
174
|
+
# Rails.cache; a null store leaves requests unlimited.
|
|
175
|
+
# Route keys of resources whose display names are sensitive (authors). Typeahead searches, option lookups
|
|
176
|
+
# and `_labels` that return their records emit `lookup.reactiveadmin` (never the term or the names),
|
|
177
|
+
# though they stay background requests. `reactive { sensitive true }` marks a resource from its own file.
|
|
178
|
+
@sensitive_resources = []
|
|
179
|
+
# Actions by resource route key whose `q` param is their own search form, not a ransack filter:
|
|
180
|
+
# `{categories: [:search_by_tag]}`. The same as `reactive { own_query_params }`.
|
|
181
|
+
@ransack_unguarded_actions = {}
|
|
182
|
+
# Publish an `audit.reactiveadmin` event of kind `view` for every API show and edit read.
|
|
183
|
+
@audit_views = false
|
|
184
|
+
# ->(event, resource) { Integer or nil }: how many `degraded.reactiveadmin` or `client_error.reactiveadmin`
|
|
185
|
+
# events the host recorded recently for `resource` as the event publishes it; `rake reactiveadmin:readiness`
|
|
186
|
+
# reads it. The gem stores nothing, so nil (the default) means "not recorded".
|
|
187
|
+
@event_counts = nil
|
|
188
|
+
@openapi = OpenApiSettings.new({}, "openapi/admin.yaml", nil)
|
|
189
|
+
@rate_limits = {options: [30, 10.seconds], lookup: [120, 1.minute], export: [5, 1.minute], value: [60, 1.minute], mcp: [120, 1.minute], client_error: [10, 1.minute], default: nil}
|
|
190
|
+
# How API requests authenticate: nil is the session (AA's `authentication_method`, CSRF on writes).
|
|
191
|
+
# `ReactiveAdmin::Auth::Bearer.new { |token, request| user }` is for tokens, and
|
|
192
|
+
# `ReactiveAdmin::Auth.any(session, bearer)` accepts both; see Auth.
|
|
193
|
+
@auth = nil
|
|
194
|
+
# Name => ->(user, request) { boolean }: the guards `api :name` in a registration runs on every
|
|
195
|
+
# API request it covers; false (or an error) refuses. See ApiGuards.
|
|
196
|
+
@api_guards = {}
|
|
197
|
+
@mcp = Mcp.new
|
|
198
|
+
@ui = Ui.new
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
def expose=(mode)
|
|
202
|
+
raise ArgumentError, "expose must be :all or :listed" unless %i[all listed].include?(mode)
|
|
203
|
+
|
|
204
|
+
@expose = mode
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
def implicit_defaults=(mode)
|
|
208
|
+
raise ArgumentError, "implicit_defaults must be :secure or :all" unless %i[secure all].include?(mode)
|
|
209
|
+
|
|
210
|
+
@implicit_defaults = mode
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
def authorize_custom_actions=(mode)
|
|
214
|
+
raise ArgumentError, "authorize_custom_actions must be true or :match_active_admin" unless [true, :match_active_admin].include?(mode)
|
|
215
|
+
|
|
216
|
+
@authorize_custom_actions = mode
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
def authorize_custom_actions? = authorize_custom_actions == true
|
|
220
|
+
end
|
|
221
|
+
end
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
module ReactiveAdmin
|
|
2
|
+
# Prepended to ActiveAdmin::ResourceController: AA keeps doing authentication, authorization,
|
|
3
|
+
# filtering, scoping, sorting, pagination and writes; we only change how requests are
|
|
4
|
+
# parsed and how results are rendered.
|
|
5
|
+
module ControllerExtension
|
|
6
|
+
PAYLOADS = {"index" => IndexPayload, "show" => ShowPayload, "new" => FormPayload, "edit" => FormPayload}.freeze
|
|
7
|
+
|
|
8
|
+
include Frame
|
|
9
|
+
include Embed
|
|
10
|
+
include TemplateActions
|
|
11
|
+
include WriteGuards
|
|
12
|
+
|
|
13
|
+
# CSV exports are AA's own streaming, so `csv do` blocks, filters, scopes and order all apply.
|
|
14
|
+
def index(*)
|
|
15
|
+
return super unless reactiveadmin_request? && !reactiveadmin_export?
|
|
16
|
+
|
|
17
|
+
render_payload IndexPayload
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def show(*)
|
|
21
|
+
return super unless reactiveadmin_request?
|
|
22
|
+
|
|
23
|
+
render_payload ShowPayload
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def new(*, &block)
|
|
27
|
+
return super unless reactiveadmin_request?
|
|
28
|
+
|
|
29
|
+
AA.build_resource(self)
|
|
30
|
+
prepare_record(&block)
|
|
31
|
+
render_payload FormPayload
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def edit(*, &block)
|
|
35
|
+
return super unless reactiveadmin_request?
|
|
36
|
+
|
|
37
|
+
AA.resource(self)
|
|
38
|
+
prepare_record(&block)
|
|
39
|
+
render_payload FormPayload
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# Custom actions that only set up instance variables and leave the rendering implicit.
|
|
43
|
+
def default_render(*)
|
|
44
|
+
payload = PAYLOADS[action_name] if reactiveadmin_request?
|
|
45
|
+
return render_payload(payload) if payload
|
|
46
|
+
return render_native_template(action_name) if reactiveadmin_request? && request.get? && native_template?(action_name)
|
|
47
|
+
|
|
48
|
+
super
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
attr_reader :reactiveadmin_prepare_failure
|
|
52
|
+
|
|
53
|
+
def reactiveadmin_options
|
|
54
|
+
authorize_options
|
|
55
|
+
render json: OptionsPayload.new(self).as_json, content_type: "application/json"
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def reactiveadmin_lookup
|
|
59
|
+
authorize_options
|
|
60
|
+
render json: LookupPayload.new(self).as_json, content_type: "application/json"
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def reactiveadmin_labels
|
|
64
|
+
authorize! AA::Auth::READ, active_admin_config.resource_class
|
|
65
|
+
lookup_context.formats = [:html]
|
|
66
|
+
response.cache_control[:no_store] = true
|
|
67
|
+
render json: {labels: LabelsPayload.new(self).as_json}, content_type: "application/json"
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def reactiveadmin_value
|
|
71
|
+
record = AA.resource(self)
|
|
72
|
+
field = params[:field].to_s
|
|
73
|
+
value = ValuePayload.fetch(self, record, field, params[:path], token: params[:token])
|
|
74
|
+
Audit.instrument("value_viewed", self,
|
|
75
|
+
{resource: Registry.route_key(active_admin_config), id: WriteAudit.record_id(self), field:, user_id: ReactiveAdmin.current_user(self)&.id})
|
|
76
|
+
response.cache_control[:no_store] = true
|
|
77
|
+
render json: {value:}, content_type: "application/json"
|
|
78
|
+
rescue ValuePayload::Forbidden
|
|
79
|
+
render_api_error(:forbidden, "Not authorized", 403)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def reactiveadmin_meta
|
|
83
|
+
authorize! AA::Auth::READ, active_admin_config.resource_class
|
|
84
|
+
raise ActiveRecord::RecordNotFound unless Registry.actions(active_admin_config).include?(:index)
|
|
85
|
+
|
|
86
|
+
lookup_context.formats = [:html]
|
|
87
|
+
meta = Degradation.with_collection(resource: active_admin_config.resource_name, action: "meta", controller: self) { ResourceMeta.fetch(self) }
|
|
88
|
+
render json: meta, content_type: "application/json"
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def render(*args, &)
|
|
92
|
+
return super unless reactiveadmin_request? && template_render?(args)
|
|
93
|
+
|
|
94
|
+
template = rendered_template(args)
|
|
95
|
+
if %w[new edit].include?(template)
|
|
96
|
+
render_invalid
|
|
97
|
+
elsif template.blank? && %w[create update destroy].include?(action_name)
|
|
98
|
+
super # implicit render: responders rescue the missing template and redirect instead
|
|
99
|
+
elsif native_template?(rendered_template_path(args))
|
|
100
|
+
render_native_page_or_rejection(rendered_template_path(args))
|
|
101
|
+
else
|
|
102
|
+
render_api_error(:unsupported, "This action renders an HTML page; use the classic admin", 501)
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
private
|
|
107
|
+
|
|
108
|
+
def audit_view
|
|
109
|
+
return unless ReactiveAdmin.config.audit_views && %w[show edit].include?(action_name) && response.successful?
|
|
110
|
+
|
|
111
|
+
Audit.publish(:view, self, resource: Registry.route_key(active_admin_config), id: WriteAudit.record_id(self))
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
# Choices for a form's controls expose what the form's own action would: someone who can't open
|
|
115
|
+
# `new` or `edit` doesn't get them. Filter choices only need READ.
|
|
116
|
+
def authorize_options
|
|
117
|
+
resource_class = active_admin_config.resource_class
|
|
118
|
+
authorize! AA::Auth::READ, resource_class
|
|
119
|
+
return unless params[:context] == "form"
|
|
120
|
+
|
|
121
|
+
params[:id].present? ? authorize!(AA::Auth::EDIT, AA.resource(self)) : authorize!(AA::Auth::NEW, resource_class)
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# AA blocks render partials and templates by name; those only exist as html.
|
|
125
|
+
def render_payload(payload_class)
|
|
126
|
+
lookup_context.formats = [:html]
|
|
127
|
+
render_collected((payload_class == FormPayload) ? "form" : action_name) { payload_class.new(self).as_json }
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
# Writes run as html so AA's responders, flashes and `controller do` overrides behave as in
|
|
131
|
+
# the classic admin; the outcome is translated back to JSON by redirect_to and render.
|
|
132
|
+
def process_action(*)
|
|
133
|
+
take_dry_run_param if reactiveadmin_request?
|
|
134
|
+
if reactiveadmin_request? && !request.get?
|
|
135
|
+
request.format = :html
|
|
136
|
+
inputs = params[:batch_action_inputs]
|
|
137
|
+
params[:batch_action_inputs] = inputs.to_unsafe_h.to_json if inputs.respond_to?(:to_unsafe_h)
|
|
138
|
+
record_params = params[active_admin_config.param_key]
|
|
139
|
+
Masking.keep_unchanged(record_params, active_admin_config) if record_params.respond_to?(:keys) && WriteGuards::GUARDED.exclude?(action_name)
|
|
140
|
+
take_lock_param
|
|
141
|
+
end
|
|
142
|
+
# Here rather than in render_payload so a host override that renders JSON itself is audited too.
|
|
143
|
+
super.tap { audit_view if reactiveadmin_request? }
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
# AA authorizes custom actions only through the host's own code; the API enforces the same
|
|
147
|
+
# permission the show page's action buttons are gated on.
|
|
148
|
+
def authenticate_active_admin_user
|
|
149
|
+
super
|
|
150
|
+
return if performed?
|
|
151
|
+
|
|
152
|
+
authorize_declared_action if reactiveadmin_request? && ReactiveAdmin.config.authorize_custom_actions?
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
# AA hands the joined param to `find`, which wants one value per key part. The last part takes
|
|
156
|
+
# any remaining delimiters, since it is the one most likely to hold them (a timestamp, a slug).
|
|
157
|
+
def find_resource = AA.find_composite_resource(self) || super
|
|
158
|
+
|
|
159
|
+
def authorize_declared_action
|
|
160
|
+
if active_admin_config.member_actions.any? { it.name.to_s == action_name }
|
|
161
|
+
AA.resource(self)
|
|
162
|
+
elsif active_admin_config.collection_actions.any? { it.name.to_s == action_name }
|
|
163
|
+
authorize! action_to_permission(action_name), active_admin_config.resource_class
|
|
164
|
+
elsif action_name == "batch_action"
|
|
165
|
+
authorize! AA::Auth::READ, active_admin_config.resource_class
|
|
166
|
+
end
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
def action_to_permission(action)
|
|
170
|
+
%w[reactiveadmin_options reactiveadmin_lookup reactiveadmin_labels reactiveadmin_value].include?(action.to_s) ? AA::Auth::READ : super
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
def render_redirect_result(location, response_options)
|
|
174
|
+
notes = flash.to_hash.merge(response_options.fetch(:flash, {}).to_h.stringify_keys, response_options.slice(:notice, :alert, :error).stringify_keys)
|
|
175
|
+
failure = notes.values_at("alert", "error").compact_blank.first
|
|
176
|
+
flash.clear
|
|
177
|
+
return render_invalid(failure, base: [failure], location: Location.for(location, host: request.host)) if failure
|
|
178
|
+
|
|
179
|
+
render json: written(notes["notice"].presence, Location.for(location, host: request.host)),
|
|
180
|
+
status: (action_name == "create") ? :created : :ok, content_type: "application/json"
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
def written(message, location)
|
|
184
|
+
record = AA.resource_ivar(self) if %w[create update destroy].include?(action_name)
|
|
185
|
+
{ok: true, id: record&.to_param&.to_s, message:, location:}.compact
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
def render_invalid(message = "Validation failed", base: [], location: nil, form: nil)
|
|
189
|
+
record = rejected_record
|
|
190
|
+
base = [*base, *flash.to_hash.values_at("alert", "error")].compact_blank
|
|
191
|
+
flash.clear
|
|
192
|
+
errors = record.respond_to?(:errors) ? record.errors : ActiveModel::Errors.new(nil)
|
|
193
|
+
messages, row_errors = field_errors(record, errors.to_hash.except(:base))
|
|
194
|
+
render_api_error(:invalid, message, 422, errors: messages, row_errors:, base: [*errors[:base], *base].uniq, location:, form:)
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
# A custom create that saves its own record and renders `new` hands it to the view as @resource,
|
|
198
|
+
# which is where classic's form reads the errors from.
|
|
199
|
+
def rejected_record
|
|
200
|
+
candidates = [AA.resource_ivar(self), view_assigns["resource"]]
|
|
201
|
+
candidates.find { it.respond_to?(:errors) && it.errors.any? } || candidates.first
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
# Errors keyed by the field a client shows (the raw attribute keys if the form can't be described),
|
|
205
|
+
# and which of a has_many's rows they belong to.
|
|
206
|
+
def field_errors(record, errors)
|
|
207
|
+
return [errors, nil] if errors.empty?
|
|
208
|
+
|
|
209
|
+
schema = FormSchema.new(self, record, controls_only: true)
|
|
210
|
+
[ErrorKeys.call(errors, schema.sections), RowErrors.call(record, schema.nested_associations)]
|
|
211
|
+
rescue
|
|
212
|
+
[errors, nil]
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
# Host overrides such as `def new; super { resource.config = default }; end` prepare the record
|
|
216
|
+
# classic's form shows; the responder runs that block, so the API must too. A block that takes
|
|
217
|
+
# the responder's format can't run without one. One that raises (typically dereferencing a
|
|
218
|
+
# parent the unnested route lacks, which classic's `new` can't survive either) leaves the
|
|
219
|
+
# record as built and the form unsubmittable.
|
|
220
|
+
def prepare_record(&block)
|
|
221
|
+
block.call if block && block.arity <= 0
|
|
222
|
+
rescue AA::AccessDenied, ActiveRecord::RecordNotFound
|
|
223
|
+
raise
|
|
224
|
+
rescue => e
|
|
225
|
+
Rails.logger.warn("[reactiveadmin] #{controller_path}##{action_name} prepare block failed: #{e.class} at #{e.backtrace&.first}")
|
|
226
|
+
@reactiveadmin_prepare_failure = e.class
|
|
227
|
+
end
|
|
228
|
+
end
|
|
229
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
module ReactiveAdmin
|
|
2
|
+
# OWASP CSV injection: a cell that starts with one of these runs as a formula in a spreadsheet.
|
|
3
|
+
# AA escapes only String cells, classic and API alike; API exports escape every cell, so a symbol
|
|
4
|
+
# or any other value whose text starts this way can't slip through.
|
|
5
|
+
module CsvInjection
|
|
6
|
+
FORMULA_START = ["=", "+", "-", "@", "\t", "\r"].freeze
|
|
7
|
+
KEY = :reactiveadmin_csv_api
|
|
8
|
+
STATE = ActiveSupport::IsolatedExecutionState
|
|
9
|
+
|
|
10
|
+
# Per request, not on the builder: one builder serves every concurrent export of its resource.
|
|
11
|
+
def build(controller, csv)
|
|
12
|
+
previous = STATE[KEY]
|
|
13
|
+
STATE[KEY] = controller.send(:reactiveadmin_export?)
|
|
14
|
+
super
|
|
15
|
+
ensure
|
|
16
|
+
STATE[KEY] = previous
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def sanitize(content)
|
|
20
|
+
return super unless STATE[KEY]
|
|
21
|
+
|
|
22
|
+
text = content.to_s
|
|
23
|
+
text.start_with?(*FORMULA_START) ? "'#{text}" : content
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
module ReactiveAdmin
|
|
2
|
+
# A date input's `min` or `max` as the client takes it: an ISO day for a date control, an ISO time for a
|
|
3
|
+
# datetime one. Accepts what a host gives `input_html:` or jQuery UI's `datepicker_options`: a Date or
|
|
4
|
+
# Time, an ISO string, a number of days from today, or a relative string such as "+1m +7d".
|
|
5
|
+
module DateBound
|
|
6
|
+
RELATIVE = /\A\s*(?:[+-]?\d+\s*[dwmy]\s*)+\z/i
|
|
7
|
+
UNITS = {"d" => :days, "w" => :weeks, "m" => :months, "y" => :years}.freeze
|
|
8
|
+
|
|
9
|
+
module_function
|
|
10
|
+
|
|
11
|
+
def call(value, control)
|
|
12
|
+
time = resolve(value) or return
|
|
13
|
+
(control == "date") ? time.to_date.iso8601 : time.iso8601
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def resolve(value)
|
|
17
|
+
case value
|
|
18
|
+
when Date, Time then value.in_time_zone
|
|
19
|
+
when Integer then Date.current.advance(days: value).in_time_zone
|
|
20
|
+
when String then value.match?(RELATIVE) ? relative(value).in_time_zone : Time.zone.parse(value)
|
|
21
|
+
end
|
|
22
|
+
rescue ArgumentError, RangeError
|
|
23
|
+
nil
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def relative(text)
|
|
27
|
+
steps = text.scan(/([+-]?\d+)\s*([dwmy])/i).each_with_object(Hash.new(0)) { |(n, unit), sum| sum[UNITS.fetch(unit.downcase)] += n.to_i }
|
|
28
|
+
Date.current.advance(steps)
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
module ReactiveAdmin
|
|
2
|
+
# What a request fell back to instead of failing: a block that raised, markup shown as html,
|
|
3
|
+
# a sidebar dropped. Collected per request, attached to the response as `degraded`, and
|
|
4
|
+
# published as `degraded.reactiveadmin` notifications for metrics. Entries carry structural
|
|
5
|
+
# paths and exception classes only, never record data or exception messages.
|
|
6
|
+
module Degradation
|
|
7
|
+
EVENT = "degraded.reactiveadmin".freeze
|
|
8
|
+
HEADER = "X-Reactiveadmin-Degraded".freeze
|
|
9
|
+
KEY = :reactiveadmin_degradation
|
|
10
|
+
SYNTHETIC_KEY = :reactiveadmin_synthetic
|
|
11
|
+
STATE = ActiveSupport::IsolatedExecutionState
|
|
12
|
+
|
|
13
|
+
Collector = Struct.new(:resource, :route_key, :action, :entries, :tallies, :controller)
|
|
14
|
+
|
|
15
|
+
module_function
|
|
16
|
+
|
|
17
|
+
# Outside a collection (the manifest, a console) the notification still fires.
|
|
18
|
+
def record(kind:, reason:, where: nil)
|
|
19
|
+
collector = STATE[KEY]
|
|
20
|
+
entry = {kind: kind.to_s, where:, reason:}.compact
|
|
21
|
+
instrument(entry, collector) if collector.nil? || collector.entries.add?(entry)
|
|
22
|
+
nil
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# Repeats within a response collapse into one entry; `reason` takes the count as `%d`.
|
|
26
|
+
def tally(kind:, reason:, where: nil)
|
|
27
|
+
collector = STATE[KEY] or return
|
|
28
|
+
(collector.tallies[kind.to_s] ||= {where:, reason:, count: 0})[:count] += 1
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def entries
|
|
32
|
+
collector = STATE[KEY] or return []
|
|
33
|
+
collector.entries.to_a + collector.tallies.map { |kind, t| {kind:, where: t[:where], reason: format(t[:reason], t[:count])}.compact }
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Structural location under the current action, e.g. `show/div[0]/ul[1]`.
|
|
37
|
+
def where(*steps) = [STATE[KEY]&.action, *steps].compact.join("/")
|
|
38
|
+
|
|
39
|
+
# `controller` is the request's own, so the audit envelope can be joined to it (channel, client id, request and correlation ids).
|
|
40
|
+
def with_collection(resource: nil, action: nil, controller: nil)
|
|
41
|
+
previous = STATE[KEY]
|
|
42
|
+
route_key = resource.respond_to?(:route_key) ? resource.route_key : resource&.to_s
|
|
43
|
+
collector = STATE[KEY] = Collector.new(resource&.to_s, route_key, action&.to_s, Set.new, {}, controller)
|
|
44
|
+
yield.tap { collector.tallies.each { |kind, t| instrument({kind:, where: t[:where]}, collector) } }
|
|
45
|
+
ensure
|
|
46
|
+
STATE[KEY] = previous
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# The doctor's own sample requests aren't traffic; events they publish carry `synthetic: true`.
|
|
50
|
+
def synthetic
|
|
51
|
+
previous = STATE[SYNTHETIC_KEY]
|
|
52
|
+
STATE[SYNTHETIC_KEY] = true
|
|
53
|
+
yield
|
|
54
|
+
ensure
|
|
55
|
+
STATE[SYNTHETIC_KEY] = previous
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# Not a user's screen: the doctor's samples, or an OpenAPI build. Readiness counts should skip these.
|
|
59
|
+
def synthetic?(payload) = payload[:synthetic] == true || payload[:action] == "openapi"
|
|
60
|
+
|
|
61
|
+
def attach(payload)
|
|
62
|
+
list = entries
|
|
63
|
+
list.empty? ? payload : payload.merge(degraded: list)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def instrument(entry, collector)
|
|
67
|
+
synthetic = STATE[SYNTHETIC_KEY] ? {synthetic: true} : {}
|
|
68
|
+
payload = {resource: collector&.resource, action: collector&.action, **entry.slice(:kind, :where), **synthetic}
|
|
69
|
+
Audit.instrument("degraded", collector&.controller, payload, payload.merge(resource: collector&.route_key, user_id: nil))
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
module ReactiveAdmin
|
|
2
|
+
# Degradations worth fixing in the host's admin code, collected for a future `doctor` task.
|
|
3
|
+
# Entries name resources, sources and limits only, never record data.
|
|
4
|
+
class Diagnostics
|
|
5
|
+
def initialize
|
|
6
|
+
@entries = Set.new
|
|
7
|
+
@lock = Mutex.new
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def record(kind, **details) = @lock.synchronize { @entries << {kind:, **details} }
|
|
11
|
+
|
|
12
|
+
def entries = @lock.synchronize { @entries.to_a }
|
|
13
|
+
|
|
14
|
+
def clear = @lock.synchronize { @entries.clear }
|
|
15
|
+
end
|
|
16
|
+
end
|