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,67 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
require "pathname"
|
|
3
|
+
|
|
4
|
+
module ReactiveAdmin
|
|
5
|
+
module Ui
|
|
6
|
+
# The client build that ships inside the gem (`rake package`), so a host needs no Node toolchain. It is built
|
|
7
|
+
# for one gem version and one manifest `schema_version`, recorded in `INFO`; `Assets` serves it only while both
|
|
8
|
+
# still match. It refers to its own files relatively, so it works under any `ui.asset_prefix`.
|
|
9
|
+
module PackagedClient
|
|
10
|
+
Error = Class.new(StandardError)
|
|
11
|
+
|
|
12
|
+
DIR = Pathname(File.expand_path("../../../vendor/client", __dir__))
|
|
13
|
+
INFO = "reactiveadmin-client.json".freeze
|
|
14
|
+
SCHEMA_SOURCE = "src/lib/manifestCache.ts".freeze
|
|
15
|
+
|
|
16
|
+
@verdicts = {}
|
|
17
|
+
|
|
18
|
+
module_function
|
|
19
|
+
|
|
20
|
+
def dir = DIR
|
|
21
|
+
|
|
22
|
+
def info(dir = DIR)
|
|
23
|
+
JSON.parse(dir.join(INFO).read)
|
|
24
|
+
rescue SystemCallError, JSON::ParserError
|
|
25
|
+
nil
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def expected = {"gem_version" => VERSION, "schema_version" => Manifest::SCHEMA_VERSION}
|
|
29
|
+
|
|
30
|
+
# Memoized: the gem's files don't change while a process runs.
|
|
31
|
+
def usable?(dir = DIR)
|
|
32
|
+
@verdicts.fetch(dir) { @verdicts[dir] = verify(dir) }
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def reset! = @verdicts.clear
|
|
36
|
+
|
|
37
|
+
def verify(dir)
|
|
38
|
+
return false unless dir.join("manifest.json").exist?
|
|
39
|
+
|
|
40
|
+
found = info(dir)
|
|
41
|
+
return false unless found
|
|
42
|
+
return true if found == expected
|
|
43
|
+
|
|
44
|
+
warn_mismatch(found)
|
|
45
|
+
false
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# The release build: Vite writes into the gem, then the stamp names what it was built for. The schema version
|
|
49
|
+
# is read from the client's source, so a client that disagrees with the gem fails here, not in a browser.
|
|
50
|
+
def build!(dir: DIR, **options)
|
|
51
|
+
build = ClientBuild.new(out: dir, root: dir.parent.parent, **options)
|
|
52
|
+
build.call
|
|
53
|
+
schema = build.source.join(SCHEMA_SOURCE).read[/SCHEMA_VERSION\s*=\s*(\d+)/, 1]&.to_i
|
|
54
|
+
raise Error, "No SCHEMA_VERSION in #{SCHEMA_SOURCE}" unless schema
|
|
55
|
+
raise Error, "The client expects manifest schema_version #{schema}, the gem serves #{Manifest::SCHEMA_VERSION}" unless schema == Manifest::SCHEMA_VERSION
|
|
56
|
+
|
|
57
|
+
dir.join(INFO).write(JSON.pretty_generate(expected) + "\n")
|
|
58
|
+
dir
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def warn_mismatch(found)
|
|
62
|
+
message = "[reactiveadmin] ignoring the packaged client: built for #{found}, this gem needs #{expected}"
|
|
63
|
+
(defined?(Rails) && Rails.logger) ? Rails.logger.warn(message) : Kernel.warn(message)
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
module ReactiveAdmin
|
|
2
|
+
module Ui
|
|
3
|
+
# Answers a browser's navigation to the admin with the client's page when its `reactiveadmin_ui=new` cookie asks
|
|
4
|
+
# for it. It knows nothing of resources, users or authorization: the page is the same static document for
|
|
5
|
+
# everyone, and the client then asks the API (which authorizes as ever) and sends the browser back to classic
|
|
6
|
+
# when it can't show the URL. A cookie is the only way to choose: browsers can't add a header to a navigation.
|
|
7
|
+
#
|
|
8
|
+
# Everything else passes through: fetches and frames, writes, other formats, `?classic=1` and `?embed=1`,
|
|
9
|
+
# URLs outside the admin namespace, and every request while no client build is installed.
|
|
10
|
+
class Shell
|
|
11
|
+
include ERB::Util
|
|
12
|
+
|
|
13
|
+
def initialize(app)
|
|
14
|
+
@app = app
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def call(env)
|
|
18
|
+
request = ActionDispatch::Request.new(env)
|
|
19
|
+
shell?(request) ? respond(request) : @app.call(env)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
private
|
|
23
|
+
|
|
24
|
+
def shell?(request)
|
|
25
|
+
Ui.settings.enabled && Ui.offered? && (request.get? || request.head?) && in_namespace?(request.path_info) &&
|
|
26
|
+
request.cookies[COOKIE] == "new" && document?(request) && !escaped?(request) && Assets.ready?
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# A path ending in a known format is a download or another format (`posts.csv`), never a page. Other dots belong
|
|
30
|
+
# to the record id (`users/john.doe`).
|
|
31
|
+
def in_namespace?(path)
|
|
32
|
+
root = "/#{ReactiveAdmin.config.namespace}"
|
|
33
|
+
(path == root || path.start_with?("#{root}/")) && !Mime::Type.lookup_by_extension(File.extname(path).delete_prefix("."))
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Browsers say what a request is for; without `Sec-Fetch-Dest` (older ones) a page load asks for html.
|
|
37
|
+
def document?(request)
|
|
38
|
+
dest = request.get_header("HTTP_SEC_FETCH_DEST")
|
|
39
|
+
return dest == "document" if dest
|
|
40
|
+
|
|
41
|
+
!request.xhr? && request.get_header("HTTP_ACCEPT").to_s.include?("text/html")
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def escaped?(request)
|
|
45
|
+
request.GET.values_at("classic", "embed").include?("1")
|
|
46
|
+
rescue Rack::QueryParser::ParameterTypeError, Rack::QueryParser::InvalidParameterError
|
|
47
|
+
true
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def respond(request)
|
|
51
|
+
body = page(Assets.tags, request.content_security_policy_nonce)
|
|
52
|
+
headers = (ActionDispatch::Response.default_headers || {}).transform_keys(&:downcase).merge(
|
|
53
|
+
"content-type" => "text/html; charset=utf-8", "cache-control" => "no-store", "content-length" => body.bytesize.to_s
|
|
54
|
+
)
|
|
55
|
+
[200, headers, [request.head? ? "" : body]]
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def page(tags, nonce)
|
|
59
|
+
config = {admin_path: "/#{ReactiveAdmin.config.namespace}", mount_path: ReactiveAdmin.config.mount_path}.to_json
|
|
60
|
+
<<~HTML
|
|
61
|
+
<!DOCTYPE html>
|
|
62
|
+
<html lang="#{h(I18n.locale)}">
|
|
63
|
+
<head>
|
|
64
|
+
<meta charset="utf-8">
|
|
65
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
66
|
+
<title>Admin</title>
|
|
67
|
+
<meta name="reactiveadmin-config" content="#{h(config)}">
|
|
68
|
+
#{tags.styles.map { %(<link rel="stylesheet" href="#{h(it)}">) }.join("\n ")}
|
|
69
|
+
#{tags.preloads.map { %(<link rel="modulepreload" href="#{h(it)}">) }.join("\n ")}
|
|
70
|
+
<script type="module" src="#{h(tags.script)}"#{%( nonce="#{h(nonce)}") if nonce}></script>
|
|
71
|
+
</head>
|
|
72
|
+
<body>
|
|
73
|
+
<div id="root"></div>
|
|
74
|
+
<noscript><p>The admin needs JavaScript. <a href="?classic=1">Open the classic admin</a>.</p></noscript>
|
|
75
|
+
</body>
|
|
76
|
+
</html>
|
|
77
|
+
HTML
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
module ReactiveAdmin
|
|
2
|
+
module Ui
|
|
3
|
+
# Publishes `ui_switch` audit events. The switch itself is the client writing the `reactiveadmin_ui` cookie and
|
|
4
|
+
# reloading, so the server only sees it on the next request: a signed marker cookie remembers which UI this user
|
|
5
|
+
# last had in this browser, and a session request whose cookie disagrees with it is a switch. A browser with
|
|
6
|
+
# neither cookie never chose and gets no marker.
|
|
7
|
+
module Switch
|
|
8
|
+
MARKER = "reactiveadmin_ui_seen".freeze
|
|
9
|
+
|
|
10
|
+
private
|
|
11
|
+
|
|
12
|
+
def process_action(*)
|
|
13
|
+
super.tap { reactiveadmin_notice_ui_switch }
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def reactiveadmin_notice_ui_switch
|
|
17
|
+
return unless Ui.settings.enabled && !Auth.token_based?(request)
|
|
18
|
+
|
|
19
|
+
choice = cookies[COOKIE]
|
|
20
|
+
seen = cookies.signed[MARKER]
|
|
21
|
+
return if choice.nil? && seen.nil?
|
|
22
|
+
|
|
23
|
+
user_id = ReactiveAdmin.current_user(self)&.id&.to_s or return
|
|
24
|
+
# The shell renders the new admin for `new` only.
|
|
25
|
+
mode = (choice == "new") ? "new" : "classic"
|
|
26
|
+
seen_user, seen_mode = seen
|
|
27
|
+
return if seen_user == user_id && seen_mode == mode
|
|
28
|
+
|
|
29
|
+
# Another user's marker says nothing about this one; a missing one means the classic default.
|
|
30
|
+
from = (seen_user == user_id) ? seen_mode : ("classic" if seen.nil?)
|
|
31
|
+
Audit.publish(:ui_switch, self, resource: nil, action: nil, from:, to: mode) if from && from != mode
|
|
32
|
+
cookies.signed[MARKER] = {value: [user_id, mode], httponly: true, same_site: :lax, secure: request.ssl?, expires: 1.year}
|
|
33
|
+
rescue => e
|
|
34
|
+
Rails.logger.error("[reactiveadmin] ui switch audit failed: #{e.class}")
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
module ReactiveAdmin
|
|
2
|
+
# Serves the React client at the classic admin's own URLs. The browser's `reactiveadmin_ui=new` cookie picks it
|
|
3
|
+
# (`Ui::Shell` answers navigations with the client's page) and the client decides everything else: it sends the
|
|
4
|
+
# browser back to classic when the user can't use it or it has no screen for the URL. The server only says, in
|
|
5
|
+
# `_session`, whether the user may switch and which UI the host would default them to.
|
|
6
|
+
module Ui
|
|
7
|
+
autoload :Assets, "reactive_admin/ui/assets"
|
|
8
|
+
autoload :ClientBuild, "reactive_admin/ui/client_build"
|
|
9
|
+
autoload :Helper, "reactive_admin/ui/helper"
|
|
10
|
+
autoload :PackagedClient, "reactive_admin/ui/packaged_client"
|
|
11
|
+
autoload :Shell, "reactive_admin/ui/shell"
|
|
12
|
+
autoload :Switch, "reactive_admin/ui/switch"
|
|
13
|
+
|
|
14
|
+
MODES = %w[classic new].freeze
|
|
15
|
+
COOKIE = "reactiveadmin_ui".freeze
|
|
16
|
+
|
|
17
|
+
module_function
|
|
18
|
+
|
|
19
|
+
def settings = ReactiveAdmin.config.ui
|
|
20
|
+
|
|
21
|
+
# The host's callable runs on every `_session`, so a failure there means "not available".
|
|
22
|
+
def available?(user)
|
|
23
|
+
!user.nil? && !!settings.available.call(user)
|
|
24
|
+
rescue => e
|
|
25
|
+
Rails.logger.warn("[reactiveadmin] ui.available raised #{e.class}; treated as unavailable")
|
|
26
|
+
false
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def default_mode(user)
|
|
30
|
+
mode = settings.default
|
|
31
|
+
mode = mode.call(user) if mode.respond_to?(:call)
|
|
32
|
+
MODES.include?(mode.to_s) ? mode.to_s : "classic"
|
|
33
|
+
rescue => e
|
|
34
|
+
Rails.logger.warn("[reactiveadmin] ui.default raised #{e.class}; using classic")
|
|
35
|
+
"classic"
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Until the host names who may use the new admin, the shell stays out of the way, even though the gem ships a client.
|
|
39
|
+
def offered? = !settings.available.equal?(Configuration::Ui::NOBODY)
|
|
40
|
+
|
|
41
|
+
def payload(user) = {available: available?(user), default: default_mode(user), client_errors: !!settings.client_errors}
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
module ReactiveAdmin
|
|
2
|
+
# JSON values are often large and sensitive, so payloads carry a summary and an `expand_path` to
|
|
3
|
+
# the audited `_value` endpoint. Only a small value without masked keys is ever sent inline: on
|
|
4
|
+
# show, and in an edit form, where the same rule applies to the control.
|
|
5
|
+
module ValuePayload
|
|
6
|
+
MASK = "[masked]".freeze
|
|
7
|
+
MAX_KEYS = 20
|
|
8
|
+
MAX_PATH_DEPTH = 32
|
|
9
|
+
NotFound = ActiveRecord::RecordNotFound
|
|
10
|
+
Forbidden = Class.new(StandardError)
|
|
11
|
+
|
|
12
|
+
# Where a value lives: enough to point `expand_path` at it. `via` is the page it renders on
|
|
13
|
+
# (index, show, form); the token carries it so `_value` knows which permission to check.
|
|
14
|
+
Source = Struct.new(:config, :record, :field, :inline, :controller, :via)
|
|
15
|
+
|
|
16
|
+
module_function
|
|
17
|
+
|
|
18
|
+
def node(value, source)
|
|
19
|
+
return {type: "empty"} if value.blank?
|
|
20
|
+
|
|
21
|
+
return {type: "json", value:} if source&.inline && inline?(value, source.config)
|
|
22
|
+
|
|
23
|
+
{type: "json", summary: summary(value), expand_path: expand_path(source)}.compact
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def inline?(value, config) = value.to_json.bytesize <= ReactiveAdmin.config.inline_json_limit && !masked_key?(value, config)
|
|
27
|
+
|
|
28
|
+
def summary(value) = {kind: value.is_a?(Hash) ? "object" : "array", keys: keys(value), size: value.to_json.bytesize}
|
|
29
|
+
|
|
30
|
+
def expand_path(source)
|
|
31
|
+
return unless source&.record && source.field
|
|
32
|
+
|
|
33
|
+
query = {field: source.field}
|
|
34
|
+
query[:token] = ValueToken.mint(source.controller, source.config, source.record, source.field, source.via) if ReactiveAdmin.config.value_token
|
|
35
|
+
"#{Paths.api(File.join(Paths.member(source.config, source.record), "_value"))}?#{query.to_query}"
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Top-level key names only, to say what is inside without saying what it holds.
|
|
39
|
+
def keys(value) = value.is_a?(Hash) ? value.keys.first(MAX_KEYS).map(&:to_s) : []
|
|
40
|
+
|
|
41
|
+
def masked_key?(value, config)
|
|
42
|
+
case value
|
|
43
|
+
when Hash then value.any? { |key, nested| Masking.attribute?(key, config) || masked_key?(nested, config) }
|
|
44
|
+
when Array then value.any? { masked_key?(it, config) }
|
|
45
|
+
else false
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def mask(value, config)
|
|
50
|
+
case value
|
|
51
|
+
when Hash then value.to_h { |key, nested| [key, (Masking.attribute?(key, config) && nested.present?) ? MASK : mask(nested, config)] }
|
|
52
|
+
when Array then value.map { mask(it, config) }
|
|
53
|
+
else value
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def json?(value) = (value.is_a?(Hash) || value.is_a?(Array)) && Values.plain?(value)
|
|
58
|
+
|
|
59
|
+
# The `_value` answer for `field` of a record the caller may already read. The `expand_path` token
|
|
60
|
+
# says a page rendered the field for this user; without one (`config.value_token = false`) the pages
|
|
61
|
+
# are rendered again to find out. Either way an attribute no page shows is a 404, so the endpoint can
|
|
62
|
+
# never read more than the pages already show.
|
|
63
|
+
def fetch(controller, record, field, path = nil, token: nil)
|
|
64
|
+
config = AA.config_of(controller)
|
|
65
|
+
raise NotFound unless permitted?(controller, config, record, field, token)
|
|
66
|
+
|
|
67
|
+
value = AA.find_value(controller.view_context, record, field.to_sym)
|
|
68
|
+
return text(value, config, field, path) if value.is_a?(String)
|
|
69
|
+
raise NotFound unless json?(value)
|
|
70
|
+
|
|
71
|
+
dig(mask(value.as_json, config), path)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# Only a plain text column is served as a string: the full text a default view truncated.
|
|
75
|
+
def text(value, config, field, path)
|
|
76
|
+
raise NotFound unless path.blank? && ImplicitDefaults.text_column?(config, field)
|
|
77
|
+
|
|
78
|
+
value
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def permitted?(controller, config, record, field, token)
|
|
82
|
+
return rendered?(controller, config, record, field) unless ReactiveAdmin.config.value_token
|
|
83
|
+
|
|
84
|
+
page = ValueToken.verify(controller, config, record, field, token) or raise Forbidden
|
|
85
|
+
return false if field.empty? || Masking.attribute?(field, config)
|
|
86
|
+
|
|
87
|
+
(page == "form") ? editable?(controller, config, record) : Registry.actions(config).include?(page.to_sym)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def editable?(controller, config, record) = Registry.actions(config).include?(:edit) && Authorizer.for(controller).authorized?(AA::Auth::UPDATE, record)
|
|
91
|
+
|
|
92
|
+
def rendered?(controller, config, record, field)
|
|
93
|
+
return false if field.empty? || Masking.attribute?(field, config)
|
|
94
|
+
|
|
95
|
+
actions = Registry.actions(config)
|
|
96
|
+
named = ->(attributes) { attributes.any? { it.to_s == field } }
|
|
97
|
+
(actions.include?(:index) && named.call(IndexPayload.new(controller).symbol_columns)) ||
|
|
98
|
+
(actions.include?(:show) && named.call(ShowPayload.new(controller).rendered_attributes(record))) ||
|
|
99
|
+
form_field?(controller, config, record, field)
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
# A json control of the edit form, for whoever may update the record: the form summarizes it and
|
|
103
|
+
# this is how its editor loads the value.
|
|
104
|
+
def form_field?(controller, config, record, field)
|
|
105
|
+
return false unless editable?(controller, config, record)
|
|
106
|
+
|
|
107
|
+
sections = FormPayload.new(controller, only: field).schema.sections
|
|
108
|
+
sections.any? { |section| section[:fields]&.any? { it[:name] == field && it[:control] == "json" && !it[:masked] } }
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def dig(value, path)
|
|
112
|
+
keys = path.to_s.split(".", -1)
|
|
113
|
+
raise NotFound if keys.size > MAX_PATH_DEPTH
|
|
114
|
+
|
|
115
|
+
keys.reduce(value) do |node, key|
|
|
116
|
+
case node
|
|
117
|
+
when Hash then node.fetch(key) { raise NotFound }
|
|
118
|
+
when Array then key.match?(/\A\d+\z/) ? node.fetch(key.to_i) { raise NotFound } : raise(NotFound)
|
|
119
|
+
else raise NotFound
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# Extended onto the show view: json attribute rows become typed nodes, and the attributes
|
|
125
|
+
# rendered for the record are noted for the `_value` allowlist.
|
|
126
|
+
module Display
|
|
127
|
+
def reactiveadmin_attributes = (@reactiveadmin_attributes ||= Set.new)
|
|
128
|
+
|
|
129
|
+
def format_attribute(resource, attr)
|
|
130
|
+
config = @reactiveadmin_value_config
|
|
131
|
+
return super unless attr.is_a?(Symbol) || attr.is_a?(String)
|
|
132
|
+
return super unless resource.instance_of?(config.resource_class)
|
|
133
|
+
|
|
134
|
+
reactiveadmin_attributes << attr.to_s
|
|
135
|
+
value = AA.find_value(self, resource, attr)
|
|
136
|
+
return super unless ValuePayload.json?(value) && !Masking.attribute?(attr, config)
|
|
137
|
+
|
|
138
|
+
NodeRegistry.marker(ValuePayload.node(value, Source.new(config:, record: resource, field: attr.to_s, inline: true, controller:, via: "show")))
|
|
139
|
+
end
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def install(view, config)
|
|
143
|
+
view.instance_variable_set(:@reactiveadmin_value_config, config)
|
|
144
|
+
view.extend(Display)
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
module ReactiveAdmin
|
|
2
|
+
# The signed proof an `expand_path` carries: this server rendered `field` of this record for this
|
|
3
|
+
# user, on index, show or the edit form. `_value` checks the proof instead of re-rendering a page to
|
|
4
|
+
# find out what the field may expose; the caller's permission on the record is still checked per request.
|
|
5
|
+
module ValueToken
|
|
6
|
+
PURPOSE = "reactiveadmin_value".freeze
|
|
7
|
+
VIAS = %w[index show form].freeze
|
|
8
|
+
|
|
9
|
+
module_function
|
|
10
|
+
|
|
11
|
+
def mint(controller, config, record, field, via)
|
|
12
|
+
verifier.generate(
|
|
13
|
+
{"r" => Registry.route_key(config), "i" => record.to_param.to_s, "f" => field.to_s, "u" => user_id(controller), "v" => via},
|
|
14
|
+
purpose: PURPOSE, expires_in: ReactiveAdmin.config.value_token_ttl
|
|
15
|
+
)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# The page the token was minted for (index, show or form), or nil when it is missing, forged,
|
|
19
|
+
# expired or was issued for another user, record or field.
|
|
20
|
+
def verify(controller, config, record, field, token)
|
|
21
|
+
claims = verifier.verified(token.to_s, purpose: PURPOSE) if token.is_a?(String)
|
|
22
|
+
return unless claims.is_a?(Hash) && VIAS.include?(claims["v"])
|
|
23
|
+
|
|
24
|
+
expected = {"r" => Registry.route_key(config), "i" => record.to_param.to_s, "f" => field.to_s, "u" => user_id(controller)}
|
|
25
|
+
claims["v"] if expected.all? { |key, value| claims[key] == value }
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def verifier = Rails.application.message_verifier(PURPOSE)
|
|
29
|
+
|
|
30
|
+
# Hosts derive the user from queries, so it is asked once per request.
|
|
31
|
+
def user_id(controller)
|
|
32
|
+
controller.instance_variable_get(:@reactiveadmin_token_user) ||
|
|
33
|
+
controller.instance_variable_set(:@reactiveadmin_token_user, ReactiveAdmin.current_user(controller)&.id.to_s)
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
module ReactiveAdmin
|
|
2
|
+
# Typed nodes for plain attribute values (index cells backed by a symbol).
|
|
3
|
+
module Values
|
|
4
|
+
PLAIN = [String, Symbol, Numeric, TrueClass, FalseClass, NilClass, Date, Time, ActiveSupport::TimeWithZone].freeze
|
|
5
|
+
|
|
6
|
+
module_function
|
|
7
|
+
|
|
8
|
+
def for(value, view, source: nil)
|
|
9
|
+
case value
|
|
10
|
+
when nil then {type: "empty"}
|
|
11
|
+
when true, false then {type: "boolean", value:}
|
|
12
|
+
when Time, DateTime, ActiveSupport::TimeWithZone then {type: "datetime", value: value.iso8601}
|
|
13
|
+
when Date then {type: "date", value: value.iso8601}
|
|
14
|
+
when Numeric then {type: "number", value: value.is_a?(BigDecimal) ? value.to_f : value}
|
|
15
|
+
when String, Symbol then value.to_s.strip.empty? ? {type: "empty"} : {type: "text", text: value.to_s}
|
|
16
|
+
when ActiveRecord::Base then record(value, view)
|
|
17
|
+
when Hash, Array then plain?(value) ? ValuePayload.node(value, source) : formatted(value, view)
|
|
18
|
+
else formatted(value, view)
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def record(record, view)
|
|
23
|
+
label = display_label(record, view)
|
|
24
|
+
ref = Registry.ref(record)
|
|
25
|
+
ref ? {type: "ref", label:, ref:} : {type: "text", text: label}
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def display_label(record, view) = CGI.unescapeHTML(AA.display_name(view, record).to_s)
|
|
29
|
+
|
|
30
|
+
# AA's display_name, or nil where AA would only fall back to "Model #id".
|
|
31
|
+
def display_name(record, view) = AA.explicit_display_name(view, record)
|
|
32
|
+
|
|
33
|
+
def plain?(value)
|
|
34
|
+
case value
|
|
35
|
+
when Hash then value.all? { |k, v| plain?(k) && plain?(v) }
|
|
36
|
+
when Array then value.all? { plain?(it) }
|
|
37
|
+
else PLAIN.any? { value.is_a?(it) }
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def formatted(value, view) = HtmlNodes.convert(AA.pretty_format(view, value).to_s)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
module ReactiveAdmin
|
|
2
|
+
# A trail for API writes the model's own audit can't give: the ones that changed nothing, and which
|
|
3
|
+
# ones came through the API. Events carry who, what and which record, never params or values.
|
|
4
|
+
module WriteAudit
|
|
5
|
+
ID_FORMAT = /\A[\w-]{1,64}\z/
|
|
6
|
+
|
|
7
|
+
# A refused action is worth recording whatever its verb: GETs can act too.
|
|
8
|
+
def self.rejected(controller, code, guard: nil) = emit("write_rejected", controller, always: code.to_s == "api_denied", code: code.to_s, **{guard:}.compact)
|
|
9
|
+
|
|
10
|
+
# A dry run saves nothing, so it isn't a write.
|
|
11
|
+
def self.written(controller) = (emit("write", controller) unless dry_run?(controller))
|
|
12
|
+
|
|
13
|
+
# Never raises: the write has already been answered, and an audit hiccup must not turn it into a 500.
|
|
14
|
+
def self.emit(event, controller, always: false, **extra)
|
|
15
|
+
return if (!always && read?(controller)) || unaudited?(controller)
|
|
16
|
+
|
|
17
|
+
user_id = ReactiveAdmin.current_user(controller)&.id
|
|
18
|
+
return unless user_id
|
|
19
|
+
|
|
20
|
+
payload = {resource: resource_key(controller), action: controller.action_name, id: record_id(controller), user_id:, **batch_ids(controller), **extra}
|
|
21
|
+
payload[:dry_run] = true if dry_run?(controller)
|
|
22
|
+
# After the write's own transaction, so a rolled-back write isn't reported as done.
|
|
23
|
+
ActiveRecord.after_all_transactions_commit { Audit.instrument(event, controller, payload) }
|
|
24
|
+
rescue => e
|
|
25
|
+
Rails.logger.error("[reactiveadmin] write audit failed: #{e.class}")
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def self.read?(controller) = ActionSafety.request_read?(controller)
|
|
29
|
+
|
|
30
|
+
def self.dry_run?(controller) = controller.respond_to?(:reactiveadmin_dry_run?, true) && controller.send(:reactiveadmin_dry_run?)
|
|
31
|
+
|
|
32
|
+
def self.unaudited?(controller) = controller.respond_to?(:audit_writes?, true) && !controller.send(:audit_writes?)
|
|
33
|
+
|
|
34
|
+
def self.resource_key(controller) = AA.config_of(controller)&.resource_name&.route_key || controller.controller_path
|
|
35
|
+
|
|
36
|
+
# The path segment is client input, so anything that isn't plainly an id is left out.
|
|
37
|
+
def self.record_id(controller)
|
|
38
|
+
record = AA.resource_ivar(controller)
|
|
39
|
+
id = (record.try(:persisted?) || record.try(:destroyed?)) ? record.to_param : controller.params[:id]
|
|
40
|
+
plain_id(id)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def self.batch_ids(controller)
|
|
44
|
+
ids = controller.params[:collection_selection] if controller.action_name == "batch_action"
|
|
45
|
+
ids.is_a?(Array) ? {record_ids: ids.map(&:to_s).grep(ID_FORMAT)} : {}
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def self.plain_id(id) = (id.to_s if id.to_s.match?(ID_FORMAT))
|
|
49
|
+
|
|
50
|
+
private
|
|
51
|
+
|
|
52
|
+
# False for endpoints that aren't writes themselves: MCP's POSTs carry tool calls, and each tool's
|
|
53
|
+
# own request reports what it wrote.
|
|
54
|
+
def audit_writes? = true
|
|
55
|
+
|
|
56
|
+
def process_action(*)
|
|
57
|
+
super.tap { WriteAudit.written(self) if reactiveadmin_request? && response.successful? }
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
module ReactiveAdmin
|
|
2
|
+
# What create and update must pass before AA's own write runs: the form has to be one the API
|
|
3
|
+
# can submit completely, and the record must be unchanged since the client loaded it.
|
|
4
|
+
module WriteGuards
|
|
5
|
+
include DryRunFlag
|
|
6
|
+
|
|
7
|
+
GUARDED = %w[create update].freeze
|
|
8
|
+
|
|
9
|
+
private
|
|
10
|
+
|
|
11
|
+
# Runs after every before_action, immediately before the action itself.
|
|
12
|
+
def send_action(*)
|
|
13
|
+
refuse_unsupported_dry_run
|
|
14
|
+
return super unless reactiveadmin_request? && GUARDED.include?(action_name)
|
|
15
|
+
|
|
16
|
+
record, schema = guard_write
|
|
17
|
+
return if performed?
|
|
18
|
+
|
|
19
|
+
within_lock_transaction(record) do
|
|
20
|
+
guard_lock(record, schema) if action_name == "update"
|
|
21
|
+
next if performed?
|
|
22
|
+
|
|
23
|
+
reactiveadmin_dry_run? ? render_dry_run(record, schema) : super
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def dry_run_supported? = action_name == "update"
|
|
28
|
+
|
|
29
|
+
def render_dry_run(record, schema)
|
|
30
|
+
result = DryRun.new(self, record, schema.sections).call
|
|
31
|
+
return render_invalid unless result.valid
|
|
32
|
+
|
|
33
|
+
render json: {ok: true, dry_run: true, changes: result.changes, nested: result.nested.presence}.compact, content_type: "application/json"
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Timestamp-locked updates hold a row lock from the check until the write commits, so a
|
|
37
|
+
# concurrent update can't slip between them. Opened here rather than around the whole request so
|
|
38
|
+
# host before_actions and the schema build don't hold a connection and a row lock.
|
|
39
|
+
def within_lock_transaction(record, &)
|
|
40
|
+
return yield unless action_name == "update" && FormLock.field_for(record.class) == "updated_at"
|
|
41
|
+
|
|
42
|
+
record.class.transaction(&)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# Taken out of params so AA's permitted_params never sees it.
|
|
46
|
+
def take_lock_param = (@reactiveadmin_lock = params.delete(:lock) if GUARDED.include?(action_name))
|
|
47
|
+
|
|
48
|
+
def guard_write
|
|
49
|
+
prepared = AA.resource_ivar(self)
|
|
50
|
+
record = write_record
|
|
51
|
+
schema = FormSchema.new(self, record, controls_only: true)
|
|
52
|
+
return render_api_error(:unsupported, schema.unsupported_reason, 422) unless writable?(schema)
|
|
53
|
+
|
|
54
|
+
drop_disabled_params(schema)
|
|
55
|
+
keep_masked_values(record)
|
|
56
|
+
ParamNormalizer.call(params, schema.sections, permitted: -> { permitted_params })
|
|
57
|
+
MaskedJson.restore(params, schema.sections.flat_map { it[:fields] || [] }, (record if action_name == "update"), active_admin_config)
|
|
58
|
+
# The record was built from params that may have held withheld values; the action builds its own.
|
|
59
|
+
reset_prepared_record if action_name == "create" && prepared.nil?
|
|
60
|
+
[record, schema]
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# A create has no stored value for a blank to mean "keep".
|
|
64
|
+
def keep_masked_values(record)
|
|
65
|
+
attrs = params[active_admin_config.param_key]
|
|
66
|
+
Masking.keep_unchanged(attrs, active_admin_config, record) if action_name == "update" && attrs.respond_to?(:keys)
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def reset_prepared_record = AA.reset_resource_ivar(self)
|
|
70
|
+
|
|
71
|
+
# A partial renders only where its controller's `new` set up the ivars it needs, and a record built
|
|
72
|
+
# as the plain resource class lacks what a custom `new` gave it. Failures there say nothing about
|
|
73
|
+
# the form, so only raw inputs, which show regardless, refuse the write.
|
|
74
|
+
def writable?(schema)
|
|
75
|
+
return true if schema.submittable?
|
|
76
|
+
return false unless schema.partial? || @reactiveadmin_stand_in_record
|
|
77
|
+
|
|
78
|
+
(schema.unsupported_kinds - [:partial_failed, :block_failed, :section_failed, :input_degraded]).empty?
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# Browsers don't post disabled inputs, so a client that does is submitting what the form withheld.
|
|
82
|
+
def drop_disabled_params(schema)
|
|
83
|
+
schema.sections.each do |section|
|
|
84
|
+
if section[:kind] == "has_many" then drop_disabled_rows(section)
|
|
85
|
+
else (section[:fields] || []).each { drop_disabled_param(params, it) }
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# A row is held to the saved row its `id` names, or to a new row's fields, whatever index it was sent at.
|
|
91
|
+
def drop_disabled_rows(section)
|
|
92
|
+
rows = params.dig(*section[:param].scan(/[^\[\]]+/))
|
|
93
|
+
rows = rows.key?(:id) ? [rows] : rows.values if rows.is_a?(ActionController::Parameters)
|
|
94
|
+
Array(rows).grep(ActionController::Parameters).each do |row|
|
|
95
|
+
id = row[:id].to_s.presence
|
|
96
|
+
fields = id ? section[:records].to_a.find { it[:id] == id }&.dig(:fields) : section[:template_fields]
|
|
97
|
+
fields.to_a.each { drop_disabled_param(row, it, nested: true) }
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def drop_disabled_param(root, field, nested: false)
|
|
102
|
+
return unless field[:disabled] && field[:param]
|
|
103
|
+
|
|
104
|
+
*path, leaf = field[:param].delete_suffix("[]").scan(/[^\[\]]+/)
|
|
105
|
+
(nested ? root : path.inject(root) { |memo, key| memo.try(:[], key) }).try(:delete, leaf)
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# A custom create may build a different class than the resource's (AdhocPlan for AppointmentPlan),
|
|
109
|
+
# so assigning the submitted params to a plain one can fail.
|
|
110
|
+
def write_record
|
|
111
|
+
FormPayload.record_for(self)
|
|
112
|
+
rescue ActiveModel::UnknownAttributeError
|
|
113
|
+
@reactiveadmin_stand_in_record = true
|
|
114
|
+
active_admin_config.resource_class.new
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
# `lock!` reloads the row, so it runs before the action assigns the submitted attributes.
|
|
118
|
+
def guard_lock(record, schema)
|
|
119
|
+
lock_row(record)
|
|
120
|
+
return if performed?
|
|
121
|
+
|
|
122
|
+
lock = FormLock.for(record, nested: schema.nested_associations) or return
|
|
123
|
+
if @reactiveadmin_lock.nil?
|
|
124
|
+
render_api_error(:invalid, "Missing lock: reload the form and try again", 400) if ReactiveAdmin.config.require_lock
|
|
125
|
+
elsif !lock.matches?(@reactiveadmin_lock)
|
|
126
|
+
render_api_error(:stale, FormLock::STALE_MESSAGE, 409)
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
# A record deleted since it was loaded, or one a host before_action left with unsaved changes (which
|
|
131
|
+
# `lock!` refuses to discard), can't be compared against the client's token.
|
|
132
|
+
def lock_row(record)
|
|
133
|
+
record.lock! if FormLock.field_for(record.class) == "updated_at"
|
|
134
|
+
rescue ActiveRecord::RecordNotFound
|
|
135
|
+
render_api_error(:not_found, "Record not found", 404)
|
|
136
|
+
rescue RuntimeError
|
|
137
|
+
raise unless record.changed?
|
|
138
|
+
|
|
139
|
+
render_api_error(:stale, FormLock::STALE_MESSAGE, 409)
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
end
|