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,197 @@
|
|
|
1
|
+
module ReactiveAdmin
|
|
2
|
+
# Turns the links and button_to forms AA renders (action items, row items, content) into
|
|
3
|
+
# contract Actions, mapping classic URLs back to the API where the route is one of ours.
|
|
4
|
+
class ActionLinks
|
|
5
|
+
VERBS = %w[get post put patch delete].freeze
|
|
6
|
+
PERMISSIONS = {"edit" => AA::Auth::EDIT, "destroy" => AA::Auth::DESTROY}.freeze
|
|
7
|
+
UNSUPPORTED = "Not available here; open it in the classic admin".freeze
|
|
8
|
+
CLASSIC_ONLY = "Available in the classic admin".freeze
|
|
9
|
+
# What the browser's own POST would carry; the client supplies the session's CSRF token instead.
|
|
10
|
+
FRAMEWORK_FIELDS = %w[authenticity_token _method utf8].freeze
|
|
11
|
+
# Unusable path shapes: dot segments and encoded slashes, which a browser would resolve to somewhere else.
|
|
12
|
+
UNSAFE_PATH = %r{/\.\.?(/|\z)|%2[ef]|\\}i
|
|
13
|
+
|
|
14
|
+
# Elements that act rather than navigate: rails-ujs links and single-button forms.
|
|
15
|
+
def self.control?(el)
|
|
16
|
+
case el.name
|
|
17
|
+
when "a" then el["data-confirm"].present? || verb(el["data-method"]) != "get"
|
|
18
|
+
when "form" then single_button?(el)
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# Every control in rendered html. Dropdown toggles are `a href="#"`; their entries count instead.
|
|
23
|
+
def self.controls(html)
|
|
24
|
+
fragment = HtmlPolicy.parse(html) or return []
|
|
25
|
+
|
|
26
|
+
fragment.css("a, form").reject do |el|
|
|
27
|
+
el.ancestors("form").any? || el.classes.include?("dropdown_menu_button")
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def self.verb(value) = value.to_s.downcase.presence_in(VERBS) || "get"
|
|
32
|
+
|
|
33
|
+
def self.single_button?(form)
|
|
34
|
+
inputs = form.css("input, button, select, textarea")
|
|
35
|
+
buttons, others = inputs.partition { it["type"] == "submit" || (it.name == "button" && it["type"].blank?) }
|
|
36
|
+
buttons.one? && others.all? { it["type"] == "hidden" }
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# `base_url` makes the links to this host's own non-admin paths absolute: the SPA may be served from
|
|
40
|
+
# another origin than the API.
|
|
41
|
+
def initialize(host: nil, base_url: nil, authorizer: nil, subject: nil)
|
|
42
|
+
@host = host
|
|
43
|
+
@base_url = base_url
|
|
44
|
+
@authorizer = authorizer
|
|
45
|
+
@subject = subject
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def action(el, placement:, key: nil)
|
|
49
|
+
label, href, verb, confirm = parse(el)
|
|
50
|
+
base = {label:, placement:, method: verb, confirm:}
|
|
51
|
+
uri = parse_uri(href)
|
|
52
|
+
return unsupported(base, key, href) if uri.nil? || (uri.path.blank? && uri.host.blank?)
|
|
53
|
+
return navigation(base, key, uri, el) if verb == "post" && navigable?(uri)
|
|
54
|
+
return external(base, key, uri) if foreign?(uri) || (verb == "get" && outside_admin?(uri))
|
|
55
|
+
|
|
56
|
+
route = recognize(uri.path, verb)
|
|
57
|
+
config = Registry.for_controller_path(route[:controller]) if route
|
|
58
|
+
target = [uri.path, uri.query].compact.join("?")
|
|
59
|
+
return page(base, key, route, target) if route && !config
|
|
60
|
+
return unsupported(base, key, href) unless config
|
|
61
|
+
return classic_only(base, key, href) unless Exposure.allowed?(config)
|
|
62
|
+
|
|
63
|
+
# Route params come back as binary strings, which parameterize refuses.
|
|
64
|
+
name = route[:action].dup.force_encoding(Encoding::UTF_8)
|
|
65
|
+
ApiGuards.mark(internal(base, config, name, key, target), config, name)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# Links the SPA can't route: other hosts, mailto and tel, and this host's paths outside the admin namespace.
|
|
69
|
+
def external?(uri) = foreign?(uri) || outside_admin?(uri)
|
|
70
|
+
|
|
71
|
+
def absolute(uri) = (@base_url && uri.relative?) ? URI.join(@base_url, uri.to_s).to_s : uri.to_s
|
|
72
|
+
|
|
73
|
+
private
|
|
74
|
+
|
|
75
|
+
# A POST the browser itself must make, to a route of this host outside the admin and the API mount (an OmniAuth
|
|
76
|
+
# start): never through the API, so never for a token. Only this origin, over http(s), at a plain path.
|
|
77
|
+
def navigable?(uri)
|
|
78
|
+
web = uri.scheme.nil? || %w[http https].include?(uri.scheme.downcase)
|
|
79
|
+
path = uri.path.to_s
|
|
80
|
+
web && !foreign?(uri) && uri.userinfo.nil? && same_origin?(uri) && path.start_with?("/") && !path.match?(UNSAFE_PATH) && outside_admin?(uri) && !under_mount?(path)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def same_origin?(uri)
|
|
84
|
+
return uri.scheme.nil? if uri.host.nil?
|
|
85
|
+
|
|
86
|
+
origin = (URI.parse(@base_url) if @base_url)
|
|
87
|
+
origin.nil? ? uri.host.casecmp?(@host.to_s) : [uri.scheme.downcase, uri.host.downcase, uri.port] == [origin.scheme, origin.host.downcase, origin.port]
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def under_mount?(path)
|
|
91
|
+
mount = ReactiveAdmin.config.mount_path.to_s.chomp("/")
|
|
92
|
+
mount.present? && (path == mount || path.start_with?("#{mount}/"))
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# Nothing here for a token: it can't hold the cookie the target route expects, and it has no browser to navigate.
|
|
96
|
+
def navigation(base, key, uri, el)
|
|
97
|
+
return unless ApiGuards.channel == :session
|
|
98
|
+
|
|
99
|
+
fields = el.css("input[type=hidden]").reject { FRAMEWORK_FIELDS.include?(it["name"]) || it["name"].blank? }.to_h { [it["name"], it["value"].to_s] }
|
|
100
|
+
{key: slug(key || base[:label]), kind: "link", href: absolute(uri), navigate: true, params: fields.presence, enabled: true, **base}.compact
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def foreign?(uri) = uri.scheme.in?(%w[mailto tel]) || (uri.host.present? && !uri.host.casecmp?(@host.to_s))
|
|
104
|
+
|
|
105
|
+
def outside_admin?(uri) = uri.path.to_s.start_with?("/") && !Paths.admin?(uri.path)
|
|
106
|
+
|
|
107
|
+
def parse(el)
|
|
108
|
+
if el.name == "form"
|
|
109
|
+
button = el.at_css("button, input[type=submit]")
|
|
110
|
+
override = el.at_css("input[name=_method]")&.[]("value")
|
|
111
|
+
[(button.text.presence || button["value"]).to_s.strip, el["action"], self.class.verb(override || el["method"]),
|
|
112
|
+
(button["data-confirm"] || el["data-confirm"]).presence]
|
|
113
|
+
else
|
|
114
|
+
[el.text.strip, el["href"], self.class.verb(el["data-method"]), el["data-confirm"].presence]
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def parse_uri(href)
|
|
119
|
+
URI.parse(href.to_s)
|
|
120
|
+
rescue URI::InvalidURIError
|
|
121
|
+
nil
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def recognize(path, verb)
|
|
125
|
+
Rails.application.routes.recognize_path(path, method: verb.upcase)
|
|
126
|
+
rescue ActionController::RoutingError
|
|
127
|
+
nil
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def internal(base, config, route_action, key, href)
|
|
131
|
+
kind = kind(config, route_action)
|
|
132
|
+
member_or_collection = %w[member collection].include?(kind)
|
|
133
|
+
key = route_action if member_or_collection || PERMISSIONS.key?(route_action)
|
|
134
|
+
permission = member_or_collection ? route_action.to_sym : PERMISSIONS[route_action]
|
|
135
|
+
enabled = permission.nil? || permitted?(config, permission)
|
|
136
|
+
reason = "You don't have permission to #{base[:label].downcase}" unless enabled
|
|
137
|
+
view = Ladder.view(config, route_action, href:) if member_or_collection && base[:method] == "get"
|
|
138
|
+
{key: slug(key || base[:label]), kind:, href:, api_path: Paths.api(href), enabled:, disabled_reason: reason, view:, **base}.compact
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
# A link to a register_page or to one of its GET actions: shown by the ladder, or not at all.
|
|
142
|
+
# A declared page action with another verb is run through the API.
|
|
143
|
+
def page(base, key, route, href)
|
|
144
|
+
page = Registry.page_for_controller_path(route[:controller])
|
|
145
|
+
return classic_only(base, key, href) if page && !Exposure.allowed?(page)
|
|
146
|
+
|
|
147
|
+
name = route[:action].dup.force_encoding(Encoding::UTF_8)
|
|
148
|
+
if page && ApiGuards.denies?(page, name, verb: base[:method])
|
|
149
|
+
return ApiGuards.mark({key: slug(key || name), kind: "custom", href:, enabled: true, **base}, page, name)
|
|
150
|
+
end
|
|
151
|
+
return page_action(base, key, page, name, href) if page && base[:method] != "get"
|
|
152
|
+
|
|
153
|
+
view = (name == "index") ? Ladder.page_view(page, href:) : Ladder.view(page, name, href:) if page
|
|
154
|
+
return unsupported(base, key, href) unless view
|
|
155
|
+
|
|
156
|
+
{key: slug(key || base[:label]), kind: "link", href:, api_path: Paths.api(href), enabled: true, view:, **base}.compact
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
def page_action(base, key, page, name, href)
|
|
160
|
+
declared = page.page_actions.any? { it.name.to_s == name && Array.wrap(it.http_verb).include?(base[:method].to_sym) }
|
|
161
|
+
return unsupported(base, key, href) unless declared
|
|
162
|
+
|
|
163
|
+
{key: slug(key || name), kind: "custom", href:, api_path: Paths.api(href), enabled: true, **base}.compact
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
def external(base, key, uri)
|
|
167
|
+
{key: slug(key || base[:label]), kind: "link", href: absolute(uri), external: true, enabled: true, **base}.compact
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
def unsupported(base, key, href, reason = UNSUPPORTED)
|
|
171
|
+
{key: slug(key || base[:label]), kind: "link", href: href.presence, supported: false, enabled: false,
|
|
172
|
+
disabled_reason: reason, **base}.compact
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
def classic_only(base, key, href) = unsupported(base, key, href, CLASSIC_ONLY)
|
|
176
|
+
|
|
177
|
+
def kind(config, route_action)
|
|
178
|
+
name = route_action.to_sym
|
|
179
|
+
if config.member_actions.any? { it.name == name } then "member"
|
|
180
|
+
elsif config.collection_actions.any? { it.name == name } then "collection"
|
|
181
|
+
else
|
|
182
|
+
PERMISSIONS.key?(route_action) ? route_action : "link"
|
|
183
|
+
end
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
def slug(value) = value.to_s.parameterize(separator: "_")
|
|
187
|
+
|
|
188
|
+
# A member action is checked against the record it targets, which is the subject only when
|
|
189
|
+
# the link points at the same resource.
|
|
190
|
+
def permitted?(config, permission)
|
|
191
|
+
subject = @subject.is_a?(config.resource_class) ? @subject : config.resource_class
|
|
192
|
+
@authorizer.nil? || @authorizer.authorized?(permission, subject, config:)
|
|
193
|
+
rescue
|
|
194
|
+
false
|
|
195
|
+
end
|
|
196
|
+
end
|
|
197
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
module ReactiveAdmin
|
|
2
|
+
# GET and HEAD read, whatever the action does with the request. The one exception is a GET action the host
|
|
3
|
+
# declares with `reactive { unsafe_gets :name }`: it is held to what a write is (write scope, audit, `api :read_only`).
|
|
4
|
+
module ActionSafety
|
|
5
|
+
READ_VERBS = %w[GET HEAD].freeze
|
|
6
|
+
|
|
7
|
+
module_function
|
|
8
|
+
|
|
9
|
+
def read?(config, name, verb) = READ_VERBS.include?(verb.to_s.upcase) && !unsafe_get?(config, name)
|
|
10
|
+
|
|
11
|
+
def unsafe_get?(config, name) = !!config.try(:reactive_options)&.dig(:unsafe_gets)&.include?(name.to_sym)
|
|
12
|
+
|
|
13
|
+
# Shell endpoints borrow the root page as their config, so only their verb counts.
|
|
14
|
+
def request_read?(controller)
|
|
15
|
+
config = AA.config_of(controller) unless controller.is_a?(ApiController)
|
|
16
|
+
read?(config, controller.action_name, controller.request.request_method)
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
module ReactiveAdmin
|
|
2
|
+
# Buttons derived from a resource's declarations: edit and destroy on a record. Member and
|
|
3
|
+
# collection actions are never listed just for existing; classic only surfaces them through
|
|
4
|
+
# action items, links in the content, or the host's own code. Here they appear through an
|
|
5
|
+
# action item, an inline control, or `reactive { expose :name, if: ->(record) { ... } }`.
|
|
6
|
+
# `href` is the classic admin URL, `api_path` the API one.
|
|
7
|
+
class Actions
|
|
8
|
+
def initialize(config, authorizer, record = nil, view: nil)
|
|
9
|
+
@config = config
|
|
10
|
+
@authorizer = authorizer
|
|
11
|
+
@record = record
|
|
12
|
+
@view = view
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def as_json(*) = [edit, destroy].compact
|
|
16
|
+
|
|
17
|
+
# Declared member (or, without a record, GET collection) actions the resource exposes
|
|
18
|
+
# explicitly, except those an action item already produced. Collection actions that write are
|
|
19
|
+
# the target of a form, which can't be run without its params.
|
|
20
|
+
def exposed(linked = [])
|
|
21
|
+
declared = @record ? @config.member_actions : @config.collection_actions
|
|
22
|
+
declared.filter_map do |declaration|
|
|
23
|
+
name = declaration.name.to_s
|
|
24
|
+
condition = exposures[name.to_sym]
|
|
25
|
+
next if condition.nil? || linked.any? { it[:key] == name } || !visible?(condition, name)
|
|
26
|
+
next if !@record && Array.wrap(declaration.http_verb) != [:get]
|
|
27
|
+
|
|
28
|
+
@record ? member(declaration) : collection(declaration)
|
|
29
|
+
rescue NoMethodError, ActionController::UrlGenerationError => e
|
|
30
|
+
Degradation.record(kind: :action_dropped, where: "#{scope}/action[#{name}]", reason: "Action has no route (#{e.class}); left out")
|
|
31
|
+
nil # nested resources have no collection path without their parent; some actions lack a route
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
private
|
|
36
|
+
|
|
37
|
+
def edit
|
|
38
|
+
return unless defines?(:edit)
|
|
39
|
+
|
|
40
|
+
action("edit", "Edit", :edit, :get, @config.route_edit_instance_path(@record), authorized?(AA::Auth::EDIT))
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def destroy
|
|
44
|
+
return unless defines?(:destroy)
|
|
45
|
+
|
|
46
|
+
action("destroy", "Delete", :destroy, :delete, @config.route_instance_path(@record),
|
|
47
|
+
authorized?(AA::Auth::DESTROY), confirm: AA.t("delete_confirmation"))
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def member(declaration)
|
|
51
|
+
name = declaration.name.to_s
|
|
52
|
+
action(name, name.titleize, :member, Array.wrap(declaration.http_verb).first,
|
|
53
|
+
@config.route_member_action_path(declaration.name, @record), authorized?(declaration.name.to_sym))
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def collection(declaration)
|
|
57
|
+
name = declaration.name.to_s
|
|
58
|
+
action(name, name.titleize, :collection, :get, File.join(@config.route_collection_path, name),
|
|
59
|
+
authorized?(declaration.name.to_sym, @config.resource_class))
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def exposures = @config.reactive_options.fetch(:exposed, {})
|
|
63
|
+
|
|
64
|
+
# A guard that raises hides the action: showing a button the host meant to withhold is the worse error.
|
|
65
|
+
def visible?(condition, name)
|
|
66
|
+
return true if condition == true
|
|
67
|
+
|
|
68
|
+
args = condition.arity.zero? ? [] : [@record].compact
|
|
69
|
+
result = @view ? @view.instance_exec(*args, &condition) : condition.call(*args)
|
|
70
|
+
!!result
|
|
71
|
+
rescue => e
|
|
72
|
+
Degradation.record(kind: :action_guard_failed, where: "#{scope}/action[#{name}]", reason: "Exposure guard raised #{e.class}; action hidden")
|
|
73
|
+
false
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def scope = @record ? "show" : "index"
|
|
77
|
+
|
|
78
|
+
def action(key, label, kind, method, href, enabled, confirm: nil)
|
|
79
|
+
reason = "You don't have permission to #{label.downcase}#{" this record" if @record}" unless enabled
|
|
80
|
+
api_path = File.join(ReactiveAdmin.config.mount_path, href)
|
|
81
|
+
view = Ladder.view(@config, key, href:) if %w[member collection].include?(kind.to_s) && method.to_s == "get"
|
|
82
|
+
ApiGuards.mark({key:, label:, kind: kind.to_s, placement: "header", method: method.to_s, href:, api_path:, confirm:, enabled:,
|
|
83
|
+
disabled_reason: reason, view:}.compact, @config, key)
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def authorized?(action, subject = @record)
|
|
87
|
+
@authorizer.authorized?(action, subject, config: @config)
|
|
88
|
+
rescue => e
|
|
89
|
+
Degradation.record(kind: :authorization_check_failed, where: "#{scope}/action[#{action}]", reason: "Authorization check raised #{e.class}; treated as denied")
|
|
90
|
+
false
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def defines?(action) = Registry.actions(@config).include?(action)
|
|
94
|
+
end
|
|
95
|
+
end
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
module ReactiveAdmin
|
|
2
|
+
# JSON error responses for ReactiveAdmin requests; falls through to AA's behaviour otherwise.
|
|
3
|
+
# Implemented as method overrides rather than callbacks/rescue_from: those are copied per
|
|
4
|
+
# subclass, so registering them after AA built its controllers would miss them.
|
|
5
|
+
module ApiErrors
|
|
6
|
+
include WriteAudit
|
|
7
|
+
|
|
8
|
+
# Raise for bad client input that should answer 400 `invalid` with this message (kept free of sensitive data).
|
|
9
|
+
BadRequest = Class.new(StandardError)
|
|
10
|
+
|
|
11
|
+
def rescue_with_handler(exception, **)
|
|
12
|
+
return super unless reactiveadmin_request?
|
|
13
|
+
|
|
14
|
+
case exception
|
|
15
|
+
when ActiveRecord::RecordNotFound then render_api_error(:not_found, "Record not found", 404)
|
|
16
|
+
when ActionController::InvalidAuthenticityToken then render_api_error(:invalid_csrf, "Invalid CSRF token", 422)
|
|
17
|
+
when ActiveRecord::StaleObjectError then render_api_error(:stale, FormLock::STALE_MESSAGE, 409)
|
|
18
|
+
when ->(e) { retryable_error?(e) } then render_retry
|
|
19
|
+
when ActionController::ParameterMissing then render_api_error(:invalid, "Missing parameter: #{exception.param}", 400)
|
|
20
|
+
when BadRequest then render_api_error(:invalid, exception.message, 400)
|
|
21
|
+
when AA::AccessDenied, ->(e) { defined?(::Pundit::NotAuthorizedError) && e.is_a?(::Pundit::NotAuthorizedError) }
|
|
22
|
+
render_api_error(:forbidden, "Not authorized", 403)
|
|
23
|
+
else return super || render_unexpected_error(exception)
|
|
24
|
+
end
|
|
25
|
+
true
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# Public like Rails' own: responders call it on the controller. A redirect while authenticating
|
|
29
|
+
# is the host sending the user to sign in.
|
|
30
|
+
def redirect_to(options = {}, response_options = {})
|
|
31
|
+
return super unless reactiveadmin_request?
|
|
32
|
+
|
|
33
|
+
location = url_for(options)
|
|
34
|
+
if @reactiveadmin_authenticating
|
|
35
|
+
render_api_error(:unauthorized, "Authentication required", 401, location:)
|
|
36
|
+
else
|
|
37
|
+
render_redirect_result(location, response_options)
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
private
|
|
42
|
+
|
|
43
|
+
# 409 rather than 503: the server is healthy and the write lost a race with another operation, so
|
|
44
|
+
# the client must tell it apart from a stale lock (`stale`) and from an outage.
|
|
45
|
+
def render_retry
|
|
46
|
+
response.headers["Retry-After"] = "1"
|
|
47
|
+
render_api_error(:retry, "Couldn't save right now — another operation was in progress. Try again.", 409)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def retryable_error?(exception)
|
|
51
|
+
classes = ReactiveAdmin.config.retryable_errors.filter_map { it.is_a?(Module) ? it : it.to_s.safe_constantize }
|
|
52
|
+
[exception, exception.cause].compact.any? { |error| classes.any? { error.is_a?(it) } }
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# Memoized because writes switch the request format to html, so AA's responders behave
|
|
56
|
+
# as they do in the classic admin.
|
|
57
|
+
def reactiveadmin_request?
|
|
58
|
+
return @reactiveadmin_request if defined?(@reactiveadmin_request)
|
|
59
|
+
|
|
60
|
+
@reactiveadmin_request = request.format.symbol == :reactiveadmin || reactiveadmin_export?
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# The one API response that isn't JSON: AA's streamed CSV, served under the mount.
|
|
64
|
+
def reactiveadmin_export?
|
|
65
|
+
request.format.csv? && request.path.start_with?("#{ReactiveAdmin.config.mount_path}/")
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# Last resort so API clients never get an html error page. The exception message is left out
|
|
69
|
+
# of both the response and the log: it may carry sensitive data.
|
|
70
|
+
def render_unexpected_error(exception)
|
|
71
|
+
return false unless exception.is_a?(StandardError)
|
|
72
|
+
|
|
73
|
+
Rails.logger.error("[reactiveadmin] #{exception.class} at #{exception.backtrace&.first} in #{controller_path}##{action_name}")
|
|
74
|
+
ScrubbedError.report(exception, resource: resource_key_for_errors, action: action_name, request_id: request.request_id)
|
|
75
|
+
status = ActionDispatch::ExceptionWrapper.status_code_for_exception(exception.class.name)
|
|
76
|
+
render_api_error(:error, "#{exception.class} while rendering #{resource_key_for_errors} #{action_name}", status)
|
|
77
|
+
true
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def resource_key_for_errors = WriteAudit.resource_key(self)
|
|
81
|
+
|
|
82
|
+
def render_api_error(code, message, status, location: nil, **extra)
|
|
83
|
+
WriteAudit.rejected(self, code, guard: extra[:guard]) unless @reactiveadmin_write_rejected
|
|
84
|
+
@reactiveadmin_write_rejected = true
|
|
85
|
+
render json: {code:, message:, location:, **extra}.compact, status:, content_type: "application/json"
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def render_redirect_result(location, _response_options)
|
|
89
|
+
render_api_error(:error, "Unexpected redirect", 500, location:)
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# AA's hook for denied authorization: answer before the host's `on_unauthorized_access`,
|
|
93
|
+
# which redirects.
|
|
94
|
+
def dispatch_active_admin_access_denied(exception)
|
|
95
|
+
return super unless reactiveadmin_request?
|
|
96
|
+
|
|
97
|
+
render_api_error(:forbidden, "Not authorized", 403)
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# Left true when authentication fails by raising, so the host's handler (a redirect to sign in)
|
|
101
|
+
# is answered as 401.
|
|
102
|
+
def authenticate_active_admin_user
|
|
103
|
+
return super unless reactiveadmin_request?
|
|
104
|
+
|
|
105
|
+
@reactiveadmin_authenticating = true
|
|
106
|
+
super
|
|
107
|
+
@reactiveadmin_authenticating = false
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
# Exports keep AA's check that the format is one of the resource's download links.
|
|
111
|
+
AA.format_guard_names.each do |name|
|
|
112
|
+
define_method(name) { super() if !reactiveadmin_request? || reactiveadmin_export? }
|
|
113
|
+
private name
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
module ReactiveAdmin
|
|
2
|
+
# GET actions that render an HTML template (or only answer html) have nothing to say to the API.
|
|
3
|
+
# Without this they surface as 406 "unexpected error" reports; the contract says `unsupported`.
|
|
4
|
+
# Prepended after ApiErrors so it sees the exception first.
|
|
5
|
+
module ApiFallback
|
|
6
|
+
UNSUPPORTED = [ActionController::UnknownFormat, ActionView::MissingTemplate].freeze
|
|
7
|
+
|
|
8
|
+
def rescue_with_handler(exception, **)
|
|
9
|
+
return super unless reactiveadmin_request? && request.get? && UNSUPPORTED.any? { exception.is_a?(it) }
|
|
10
|
+
|
|
11
|
+
render_api_error(:unsupported, "Render in the classic admin", 501)
|
|
12
|
+
true
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|