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,135 @@
|
|
|
1
|
+
module ReactiveAdmin
|
|
2
|
+
# How API requests prove who they are. The default is the session: ActiveAdmin's own
|
|
3
|
+
# `authentication_method` runs and writes need a CSRF token. A bearer adapter authenticates a token
|
|
4
|
+
# instead, for clients that have no cookie (the MCP endpoint, scripts):
|
|
5
|
+
#
|
|
6
|
+
# config.auth = ReactiveAdmin::Auth.any(
|
|
7
|
+
# ReactiveAdmin::Auth::Session.new,
|
|
8
|
+
# ReactiveAdmin::Auth::Bearer.new { |token, request| AdminToken.find_by_token(token)&.user }
|
|
9
|
+
# )
|
|
10
|
+
#
|
|
11
|
+
# A request under the API mount with an `Authorization: Bearer` header is answered by the bearer adapter
|
|
12
|
+
# alone and never falls back to cookies (they are dropped before the host sees them); every other request
|
|
13
|
+
# is a session request. Token requests skip CSRF, which only exists to protect cookies.
|
|
14
|
+
#
|
|
15
|
+
# Bearer tokens never authenticate outside the mount. The classic admin runs actions `api` guards keep
|
|
16
|
+
# off the API, in any format, so a token accepted there would bypass it.
|
|
17
|
+
#
|
|
18
|
+
# An adapter is any object with `claims?(request)`, `token_based?` and, when token based,
|
|
19
|
+
# `authenticate(request)` returning an Identity or nil.
|
|
20
|
+
module Auth
|
|
21
|
+
autoload :Authentication, "reactive_admin/auth/guard"
|
|
22
|
+
autoload :Request, "reactive_admin/auth/guard"
|
|
23
|
+
autoload :TokenRequests, "reactive_admin/auth/token_requests"
|
|
24
|
+
|
|
25
|
+
IDENTITY_ENV = "reactiveadmin.auth.identity".freeze
|
|
26
|
+
|
|
27
|
+
# `scopes` nil means the token carries no scope claim, so no scope is required of it.
|
|
28
|
+
Identity = Data.define(:user, :scopes, :client_id) do
|
|
29
|
+
def allows?(scope) = scope.blank? || scopes.nil? || scopes.include?(scope)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
class Session
|
|
33
|
+
def claims?(_request) = true
|
|
34
|
+
|
|
35
|
+
def token_based? = false
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# The block gets the token and the request and returns a user, `{user:, scopes:, client_id:}`, or nil
|
|
39
|
+
# for an invalid token. `scopes` is an array or a space-separated string.
|
|
40
|
+
class Bearer
|
|
41
|
+
SCHEME = /\ABearer(?:\s|\z)/i
|
|
42
|
+
TOKEN = /\ABearer\s+(\S+)\z/i
|
|
43
|
+
|
|
44
|
+
def initialize(&resolver)
|
|
45
|
+
raise ArgumentError, "Bearer needs a block" unless resolver
|
|
46
|
+
|
|
47
|
+
@resolver = resolver
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def claims?(request) = SCHEME.match?(request.authorization.to_s)
|
|
51
|
+
|
|
52
|
+
def token_based? = true
|
|
53
|
+
|
|
54
|
+
def authenticate(request)
|
|
55
|
+
token = TOKEN.match(request.authorization.to_s)&.[](1) or return
|
|
56
|
+
identity(@resolver.call(token, request))
|
|
57
|
+
rescue => e
|
|
58
|
+
Rails.logger.warn("[reactiveadmin] bearer authentication failed: #{e.class}")
|
|
59
|
+
nil
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
private
|
|
63
|
+
|
|
64
|
+
def identity(result)
|
|
65
|
+
result = {user: result} unless result.is_a?(Hash)
|
|
66
|
+
user = result[:user] or return
|
|
67
|
+
Identity.new(user:, scopes: scopes_of(result[:scopes]), client_id: result[:client_id]&.to_s)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def scopes_of(scopes)
|
|
71
|
+
return if scopes.nil?
|
|
72
|
+
|
|
73
|
+
Array(scopes.is_a?(String) ? scopes.split : scopes).map(&:to_s)
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# Token adapters claim their requests; everything else belongs to the first session-style adapter.
|
|
78
|
+
class Composite
|
|
79
|
+
def initialize(*adapters)
|
|
80
|
+
raise ArgumentError, "Auth.any needs at least one adapter" if adapters.empty?
|
|
81
|
+
|
|
82
|
+
@adapters = adapters
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
attr_reader :adapters
|
|
86
|
+
|
|
87
|
+
def adapter_for(request)
|
|
88
|
+
@adapters.find { it.token_based? && it.claims?(request) } || @adapters.find { !it.token_based? } || @adapters.first
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
SESSION = Session.new
|
|
93
|
+
|
|
94
|
+
module_function
|
|
95
|
+
|
|
96
|
+
def any(*adapters) = Composite.new(*adapters)
|
|
97
|
+
|
|
98
|
+
def session = SESSION
|
|
99
|
+
|
|
100
|
+
# Under the namespace of the mount, the only place a token can authenticate.
|
|
101
|
+
def api_path?(request)
|
|
102
|
+
config = ReactiveAdmin.config
|
|
103
|
+
request.path.start_with?("#{File.join(config.mount_path, config.namespace.to_s)}/")
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def adapter_for(request)
|
|
107
|
+
return SESSION unless api_path?(request)
|
|
108
|
+
|
|
109
|
+
auth = ReactiveAdmin.config.auth || SESSION
|
|
110
|
+
auth.respond_to?(:adapter_for) ? auth.adapter_for(request) : auth
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def adapters = Array(ReactiveAdmin.config.auth&.then { it.respond_to?(:adapters) ? it.adapters : it })
|
|
114
|
+
|
|
115
|
+
def bearer? = adapters.any?(&:token_based?)
|
|
116
|
+
|
|
117
|
+
def token_only? = adapters.any? && adapters.all?(&:token_based?)
|
|
118
|
+
|
|
119
|
+
def token_based?(request) = adapter_for(request).token_based?
|
|
120
|
+
|
|
121
|
+
# Memoized per request: the host's block may query.
|
|
122
|
+
def identity(request)
|
|
123
|
+
return request.env[IDENTITY_ENV] if request.env.key?(IDENTITY_ENV)
|
|
124
|
+
|
|
125
|
+
adapter = adapter_for(request)
|
|
126
|
+
request.env[IDENTITY_ENV] = (adapter.authenticate(request) if adapter.token_based?)
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def challenge(error: nil, scope: nil)
|
|
130
|
+
url = ReactiveAdmin.config.mcp.resource_metadata_url
|
|
131
|
+
params = {error:, scope:, resource_metadata: url}.compact.map { |key, value| %(#{key}="#{value}") }
|
|
132
|
+
["Bearer", params.join(", ")].compact_blank.join(" ")
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
module ReactiveAdmin
|
|
2
|
+
# Runs ActiveAdmin's configured authorization adapter for a user outside an AA controller.
|
|
3
|
+
class Authorizer
|
|
4
|
+
attr_reader :user
|
|
5
|
+
|
|
6
|
+
def self.for(controller)
|
|
7
|
+
method = AA.current_user_method(Registry.namespace)
|
|
8
|
+
new(method && controller.send(method))
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def initialize(user, namespace = Registry.namespace)
|
|
12
|
+
@user = user
|
|
13
|
+
@adapter = AA.authorization_adapter(namespace)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# The adapter's resource is only consulted when no subject is given.
|
|
17
|
+
def authorized?(action, subject = nil, config: nil)
|
|
18
|
+
@adapter.new(config, @user).authorized?(action, subject)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Limits a relation to what the user may read; models without an AA registration are left alone.
|
|
22
|
+
def scope(relation)
|
|
23
|
+
config = AA.resource_for(Registry.namespace, relation.klass)
|
|
24
|
+
return relation unless config
|
|
25
|
+
|
|
26
|
+
adapter = @adapter.new(config, @user)
|
|
27
|
+
return adapter.scope_collection(relation, AA::Auth::READ) if relation.limit_value
|
|
28
|
+
|
|
29
|
+
# Pundit inspects the relation when it has no policy of its own, which would load rows.
|
|
30
|
+
scoped = adapter.scope_collection(relation.limit(0), AA::Auth::READ)
|
|
31
|
+
return scoped unless scoped.limit_value&.zero?
|
|
32
|
+
|
|
33
|
+
# A zero that survives a second probe is the policy's own deny-all. That probe is `none` so
|
|
34
|
+
# a policy-less Pundit has no rows to load for its error message.
|
|
35
|
+
adapter.scope_collection(relation.limit(1).none, AA::Auth::READ).limit_value&.zero? ? scoped : scoped.unscope(:limit)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Stands in for a user with every permission, for documents that describe the whole surface.
|
|
40
|
+
class Authorizer::Permissive
|
|
41
|
+
def user = nil
|
|
42
|
+
|
|
43
|
+
def authorized?(*, **) = true
|
|
44
|
+
|
|
45
|
+
def scope(relation) = relation
|
|
46
|
+
end
|
|
47
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
module ReactiveAdmin
|
|
2
|
+
# Metadata the client revalidates on its own (_manifest, _meta, _session, _options, _labels) may
|
|
3
|
+
# carry X-Reactiveadmin-Background so a host can keep it out of its access audit and stop it
|
|
4
|
+
# extending an idle session.
|
|
5
|
+
#
|
|
6
|
+
# The header is the client's claim, so the route decides what may be background: record data
|
|
7
|
+
# (show, index, edit, new, _value, pages, actions) is always a view whatever the header says.
|
|
8
|
+
module BackgroundRequests
|
|
9
|
+
HEADER = "X-Reactiveadmin-Background".freeze
|
|
10
|
+
METADATA_ACTIONS = %w[reactiveadmin_meta reactiveadmin_options reactiveadmin_lookup reactiveadmin_labels].freeze
|
|
11
|
+
METADATA_CONTROLLERS = %w[reactive_admin/manifest reactive_admin/session].freeze
|
|
12
|
+
|
|
13
|
+
# Rack env key only server code can set (a client header arrives as HTTP_*): the doctor's samples.
|
|
14
|
+
INTERNAL_ENV = "reactiveadmin.internal".freeze
|
|
15
|
+
|
|
16
|
+
def self.background?(request)
|
|
17
|
+
request.env.key?(INTERNAL_ENV) ||
|
|
18
|
+
(request.get? && request.headers[HEADER].to_s.in?(%w[1 true]) && metadata?(request.path_parameters))
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def self.metadata?(params) = METADATA_ACTIONS.include?(params[:action]) || METADATA_CONTROLLERS.include?(params[:controller])
|
|
22
|
+
|
|
23
|
+
private
|
|
24
|
+
|
|
25
|
+
# Before callbacks, so the hook can mark the request before the host's own callbacks decide
|
|
26
|
+
# whether to refresh the session or track a view.
|
|
27
|
+
def process_action(*)
|
|
28
|
+
hook = ReactiveAdmin.config.on_background_request
|
|
29
|
+
hook.call(self) if hook && BackgroundRequests.background?(request)
|
|
30
|
+
super
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
module ReactiveAdmin
|
|
2
|
+
# Batch actions offered on a resource's index, with the inputs of their `form:` dialog.
|
|
3
|
+
class BatchActions
|
|
4
|
+
CONTROLS = {text: "text", textarea: "textarea", number: "number", datepicker: "date", checkbox: "boolean"}.freeze
|
|
5
|
+
|
|
6
|
+
def initialize(config, view)
|
|
7
|
+
@config = config
|
|
8
|
+
@view = view
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def as_json(*)
|
|
12
|
+
AA.batch_actions(@config).filter_map do |action|
|
|
13
|
+
next unless safely(false, action) { AA.batch_action_visible?(@view, action) }
|
|
14
|
+
|
|
15
|
+
key = action.sym.to_s
|
|
16
|
+
{key:, label: label(action), confirm: confirm(action), inputs: inputs(action), **api_marks(key)}.compact
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
private
|
|
21
|
+
|
|
22
|
+
def api_marks(key)
|
|
23
|
+
names = ["batch_action", key]
|
|
24
|
+
declaration = ApiGuards.denial(@config, names, verb: "post")
|
|
25
|
+
return ApiGuards.annotate({}, ApiGuards.custom_for(@config, names)) unless declaration
|
|
26
|
+
|
|
27
|
+
{api_denied: true, classic_url: Paths.collection(@config, parent_id: Paths.parent_id(@config, @view.params)), disabled_reason: ApiGuards.message(declaration)}
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def label(action)
|
|
31
|
+
title = AA.batch_action_title(@view, action)
|
|
32
|
+
AA.t("batch_actions.labels.#{action.sym}", default: title.to_s)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def confirm(action) = AA.batch_action_confirm(@view, action).presence
|
|
36
|
+
|
|
37
|
+
def inputs(action)
|
|
38
|
+
form = safely({}, action) { AA.batch_action_inputs(@view, action) }
|
|
39
|
+
form.to_h.map { |name, type| input(name.to_s, type) }
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def input(name, type)
|
|
43
|
+
field = {name:, param: name, label: name.humanize, value: nil, required: false}
|
|
44
|
+
return field.merge(control: CONTROLS.fetch(type.to_s.to_sym, "text")) unless type.is_a?(Array)
|
|
45
|
+
|
|
46
|
+
options = type.map { |item| item.is_a?(Array) ? {label: item.first.to_s, value: item.last.to_s} : {label: item.to_s, value: item.to_s} }
|
|
47
|
+
field.merge(control: "select", options:)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def safely(fallback, action)
|
|
51
|
+
yield
|
|
52
|
+
rescue => e
|
|
53
|
+
Degradation.record(kind: :batch_action_block_failed, where: "index/batch_action[#{action.sym}]", reason: "Block raised #{e.class}; used a default")
|
|
54
|
+
fallback
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
module ReactiveAdmin
|
|
2
|
+
# Option source behind a select-like control. Small sets are inlined into the payload;
|
|
3
|
+
# large ones are searched through the resource's _options endpoint instead.
|
|
4
|
+
module Choices
|
|
5
|
+
SCAN_CAP = 1000
|
|
6
|
+
|
|
7
|
+
module_function
|
|
8
|
+
|
|
9
|
+
# Relations are searched in SQL. Anything else is evaluated to entries ([label, value] or
|
|
10
|
+
# [label, value, hint], or the same as a hash), which `cache` (a key from `cache_key`) keeps for
|
|
11
|
+
# options_cache_ttl so repeated searches don't rebuild them.
|
|
12
|
+
# `policy` limits relations to what the current user may read.
|
|
13
|
+
def for(collection, view, label: nil, value: nil, cache: nil, policy: false)
|
|
14
|
+
cached(cache) do
|
|
15
|
+
collection = view.instance_exec(&collection) if collection.is_a?(Proc)
|
|
16
|
+
collection = collection.all if collection.is_a?(Class) && collection < ActiveRecord::Base
|
|
17
|
+
if collection.is_a?(ActiveRecord::Relation)
|
|
18
|
+
records(OptionSource.implicit(collection, label:, value:), view, collection, policy:)
|
|
19
|
+
else
|
|
20
|
+
pairs(collection, view, label, value)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# A registered source (by name or instance), refined by the input's `scope:`. Sources that
|
|
26
|
+
# aren't backed by a relation are treated like any other collection.
|
|
27
|
+
def source(source, view, scope: nil, object: nil, policy: false, cache: nil)
|
|
28
|
+
source = ReactiveAdmin.option_sources[source] unless source.is_a?(OptionSource)
|
|
29
|
+
cached(cache) do
|
|
30
|
+
scoped = source.scoped(view, scope, object)
|
|
31
|
+
scoped.is_a?(ActiveRecord::Relation) ? records(source, view, scoped, policy:) : pairs(scoped, view, source.label, source.value)
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Distinct values of a plain column, among the rows the user may read: the resource's own relation, under its
|
|
36
|
+
# policy scope, so the values of rows the index hides aren't listed. Read through a derived table, so the
|
|
37
|
+
# relation's includes and joins don't meet the distinct select. Its order is kept only where a
|
|
38
|
+
# limit or offset makes it pick the rows; a DISTINCT one keeps its own projection, which Postgres
|
|
39
|
+
# requires to hold its ordering columns. One that omits both the column and part of the key can't be tied
|
|
40
|
+
# back to its rows (an unresolved key column binds to the outer table and lists every row), so it offers nothing.
|
|
41
|
+
def column(relation, name, view)
|
|
42
|
+
readable = Authorizer.for(view.controller).scope(relation)
|
|
43
|
+
klass = relation.klass
|
|
44
|
+
attribute = klass.arel_table[name]
|
|
45
|
+
keys = Array(klass.primary_key)
|
|
46
|
+
rows =
|
|
47
|
+
if !(readable.limit_value || readable.offset_value) then readable.unscope(:order).reselect(attribute)
|
|
48
|
+
elsif !readable.distinct_value then bounded_rows(readable, attribute, name)
|
|
49
|
+
elsif projecting?(readable, name) then readable
|
|
50
|
+
elsif keys.any? && projecting?(readable, *keys) then rejoin(readable, keys)
|
|
51
|
+
else return Choices.for([], view)
|
|
52
|
+
end
|
|
53
|
+
scope = bare(klass).from(rows, klass.table_name).where.not(name => nil).distinct.reselect(name).reorder(name)
|
|
54
|
+
records(OptionSource.new(nil, scope, label: name, value: name, search: name), view, scope, policy: false, by_primary_key: false)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
COLUMN = /\A(?:["`]?\w+["`]?\.)?(?:["`]?\w+["`]?|\*)\z/
|
|
58
|
+
|
|
59
|
+
# Whether the relation is proven to project the named columns of its own table. Only bare column
|
|
60
|
+
# references count; any other expression is opaque, so a name inside it proves nothing, and one
|
|
61
|
+
# aliased to a name would shadow that column in the derived table.
|
|
62
|
+
def projecting?(relation, *names)
|
|
63
|
+
return true if relation.select_values.empty?
|
|
64
|
+
|
|
65
|
+
items = relation.select_values.flat_map do
|
|
66
|
+
if it.respond_to?(:relation) then ["#{it.relation.name}.#{it.name}"]
|
|
67
|
+
elsif it.is_a?(Arel::Nodes::As) then ["#{it.left.to_sql} AS #{it.right.to_sql}"]
|
|
68
|
+
else split_top_level(it.to_s)
|
|
69
|
+
end
|
|
70
|
+
end.map(&:strip)
|
|
71
|
+
bare, expressions = items.partition { COLUMN.match?(it) }
|
|
72
|
+
return false if expressions.any? { names.map(&:to_s).include?(it[/\s["`]?(\w+)["`]?\z/, 1]) }
|
|
73
|
+
|
|
74
|
+
projected = bare.map { it.delete('"`').delete_prefix("#{relation.klass.table_name}.") }
|
|
75
|
+
projected.include?("*") || names.all? { projected.include?(it.to_s) }
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# Commas inside parentheses or quotes belong to an expression. A doubled quote closes and reopens, so
|
|
79
|
+
# an escaped one stays inside its string.
|
|
80
|
+
def split_top_level(sql)
|
|
81
|
+
depth = 0
|
|
82
|
+
quote = nil
|
|
83
|
+
sql.each_char.with_object([+""]) do |char, parts|
|
|
84
|
+
if quote then quote = nil if char == quote
|
|
85
|
+
elsif "'\"`".include?(char) then quote = char
|
|
86
|
+
elsif char == "(" then depth += 1
|
|
87
|
+
elsif char == ")" then depth -= 1
|
|
88
|
+
end
|
|
89
|
+
(char == "," && depth.zero? && !quote) ? parts << +"" : parts.last << char
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# A bounded relation keeps any aliases its order refers to, so the column is added to its projection
|
|
94
|
+
# rather than replacing it. It goes in under its own name and is renamed outside, because the
|
|
95
|
+
# projection may already hold it (`posts.*, ...`) and Postgres rejects the duplicate.
|
|
96
|
+
def bounded_rows(relation, attribute, name)
|
|
97
|
+
return relation.reselect(attribute) if relation.select_values.empty?
|
|
98
|
+
return relation if projecting?(relation, name)
|
|
99
|
+
|
|
100
|
+
klass = relation.klass
|
|
101
|
+
bare(klass).from(relation.select(attribute.as("reactive_admin_option")), klass.table_name).select(klass.arel_table["reactive_admin_option"].as(name.to_s))
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
# The class's table with no default scope or inheritance predicate: a derived table may not carry the type column.
|
|
105
|
+
# The relations it wraps already apply them.
|
|
106
|
+
def bare(klass) = klass.unscoped.unscope(where: klass.inheritance_column)
|
|
107
|
+
|
|
108
|
+
# A bounded DISTINCT projection that omits the column: its chosen rows, read back from the table by key.
|
|
109
|
+
def rejoin(relation, keys)
|
|
110
|
+
klass = relation.klass
|
|
111
|
+
keys = keys.map { klass.arel_table[it] }
|
|
112
|
+
chosen = bare(klass).from(relation, klass.table_name).select(*keys).arel
|
|
113
|
+
klass.unscoped.where(Arel::Nodes::In.new(Arel::Nodes::Grouping.new(keys), chosen))
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def path(config, field, context, id: nil, parent_id: nil)
|
|
117
|
+
"#{Paths.api(Paths.options(config, parent_id:))}?#{{field:, context:, id:}.compact.to_query}"
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
# Varies by everything an options list can depend on, including who is asking and the page's
|
|
121
|
+
# other params (a collection proc may read the other filters' values).
|
|
122
|
+
def cache_key(config, context, field, view, id: nil, parent_id: nil)
|
|
123
|
+
controller = view.controller
|
|
124
|
+
who = [ReactiveAdmin.current_user(controller)&.id, ReactiveAdmin.user_fingerprint(controller)].compact.join(":")
|
|
125
|
+
query = OptionsPayload.page_query(view.request).to_query.presence&.then { Digest::SHA256.hexdigest(it) }
|
|
126
|
+
["reactiveadmin/options", Registry.route_key(config), context, field, parent_id, id, who.presence, query].compact.join("/")
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def option(label, value, hint: nil) = {label: CGI.unescapeHTML(label.to_s), value: value.to_s, hint: hint&.to_s}.compact
|
|
130
|
+
|
|
131
|
+
def pair(item, view, label, value)
|
|
132
|
+
case item
|
|
133
|
+
when Array then (item.size > 2) ? item.first(3) : [item.first, item.last]
|
|
134
|
+
when Hash then item.symbolize_keys.then { |h| [h[:label], h.fetch(:value, h[:label]), h[:hint]].compact }
|
|
135
|
+
when ActiveRecord::Base then [label ? call(label, item) : Values.display_label(item, view), call(value || (item.class.primary_key.is_a?(Array) ? :to_param : :id), item)]
|
|
136
|
+
else [item, item]
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
def call(method, record) = method.respond_to?(:call) ? method.call(record) : record.public_send(method)
|
|
141
|
+
|
|
142
|
+
def records(source, view, scope, policy:, **) = Records.new(scope, view, source:, policy: policy && source.policy?, **)
|
|
143
|
+
|
|
144
|
+
def pairs(collection, view, label, value)
|
|
145
|
+
Pairs.new(collection.is_a?(Hash) ? collection.to_a : Array(collection).map { pair(it, view, label, value) })
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
# Only pair lists are cached: relations are lazy and cheap to rebuild.
|
|
149
|
+
def cached(key)
|
|
150
|
+
ttl = ReactiveAdmin.config.options_cache_ttl.to_i
|
|
151
|
+
key = nil unless ttl.positive?
|
|
152
|
+
hit = Rails.cache.read(key) if key
|
|
153
|
+
return Pairs.new(hit) if hit
|
|
154
|
+
|
|
155
|
+
yield.tap { Rails.cache.write(key, it.entries, expires_in: ttl) if key && it.is_a?(Pairs) }
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
def encode_cursor(offset) = Base64.urlsafe_encode64(offset.to_s, padding: false)
|
|
159
|
+
|
|
160
|
+
def decode_cursor(cursor)
|
|
161
|
+
(Integer(Base64.urlsafe_decode64(cursor.to_s), exception: false) || 0).clamp(0..)
|
|
162
|
+
rescue ArgumentError
|
|
163
|
+
raise ApiErrors::BadRequest, "Invalid cursor"
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
# Shared contract: list(selected, limit), search(term, limit, cursor) and lookup(values).
|
|
167
|
+
# list and search return {options:, truncated:, next_cursor:}; lookup returns just the options.
|
|
168
|
+
class Pairs
|
|
169
|
+
attr_reader :entries
|
|
170
|
+
|
|
171
|
+
def initialize(entries)
|
|
172
|
+
@entries = entries
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
# [label, value] only: what the classic admin's selects take.
|
|
176
|
+
def pairs = @entries.map { it.first(2) }
|
|
177
|
+
|
|
178
|
+
def truncated?(limit) = @entries.size > limit
|
|
179
|
+
|
|
180
|
+
# The model whose records the options are, for auditing; literal lists have none.
|
|
181
|
+
def model = nil
|
|
182
|
+
|
|
183
|
+
def list(selected, limit)
|
|
184
|
+
return {options: options(@entries), truncated: false} if @entries.size <= limit
|
|
185
|
+
|
|
186
|
+
{options: lookup(selected), truncated: true}
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
def search(term, limit, cursor = nil)
|
|
190
|
+
tokens = term.downcase.split
|
|
191
|
+
hits = @entries.select { |entry| tokens.all? { |token| entry.any? { it.to_s.downcase.include?(token) } } }
|
|
192
|
+
page(hits, limit, Choices.decode_cursor(cursor))
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
def lookup(values) = options(@entries.select { |_, value| values.include?(value.to_s) })
|
|
196
|
+
|
|
197
|
+
private
|
|
198
|
+
|
|
199
|
+
def page(hits, limit, offset)
|
|
200
|
+
truncated = hits.size > offset + limit
|
|
201
|
+
{options: options(hits.drop(offset).first(limit)), truncated:, next_cursor: (Choices.encode_cursor(offset + limit) if truncated)}.compact
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
def options(entries) = entries.map { |label, value, hint| Choices.option(label, value, hint:) }
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
class Records
|
|
208
|
+
def initialize(scope, view, source:, policy: false, by_primary_key: true)
|
|
209
|
+
@relation = scope
|
|
210
|
+
@by_primary_key = by_primary_key
|
|
211
|
+
@view = view
|
|
212
|
+
@source = source
|
|
213
|
+
@policy = policy
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
def model = klass
|
|
217
|
+
|
|
218
|
+
# Whether `list` would fall back to a typeahead, without building labels or looking up the selection.
|
|
219
|
+
def truncated?(limit) = ordered.limit(limit + 1).to_a.size > limit
|
|
220
|
+
|
|
221
|
+
def list(selected, limit)
|
|
222
|
+
records = ordered.limit(limit + 1).to_a
|
|
223
|
+
return {options: options(records), truncated: false} if records.size <= limit
|
|
224
|
+
|
|
225
|
+
{options: lookup(selected), truncated: true}
|
|
226
|
+
end
|
|
227
|
+
|
|
228
|
+
def search(term, limit, cursor = nil)
|
|
229
|
+
offset = Choices.decode_cursor(cursor)
|
|
230
|
+
records, truncated = search_columns.any? ? by_columns(term, limit, offset) : scan(term, limit, offset)
|
|
231
|
+
{options: options(records), truncated:, next_cursor: (Choices.encode_cursor(offset + limit) if truncated)}.compact
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
def lookup(values)
|
|
235
|
+
return [] if values.empty?
|
|
236
|
+
|
|
237
|
+
records = value_column ? scope.where(value_column => values).to_a : scope.limit(SCAN_CAP).select { values.include?(value_of(it).to_s) }
|
|
238
|
+
options(records)
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
# Every option, for the classic admin which renders whole lists.
|
|
242
|
+
def pairs = scope.map { [label_of(it), value_of(it)] }
|
|
243
|
+
|
|
244
|
+
private
|
|
245
|
+
|
|
246
|
+
# Resolved on first use: building filters for a resource's metadata must not query.
|
|
247
|
+
def scope
|
|
248
|
+
@scope ||= (@policy && Authorizer.for(@view.controller)&.scope(@relation)) || @relation
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
def klass = @relation.klass
|
|
252
|
+
|
|
253
|
+
# Association targets arrive unordered: alphabetical is the least surprising order.
|
|
254
|
+
def ordered = (scope.order_values.empty? && label_column) ? scope.order(label_column) : scope
|
|
255
|
+
|
|
256
|
+
def options(records) = records.map { Choices.option(label_of(it), value_of(it), hint: hint_of(it)) }
|
|
257
|
+
|
|
258
|
+
def label_of(record) = @source.label ? Choices.call(@source.label, record) : Values.display_label(record, @view)
|
|
259
|
+
|
|
260
|
+
def value_of(record) = Choices.call(@source.value, record)
|
|
261
|
+
|
|
262
|
+
def hint_of(record) = (Choices.call(@source.hint, record) if @source.hint)
|
|
263
|
+
|
|
264
|
+
def label_column
|
|
265
|
+
@label_column ||= @source.label.to_s if @source.label.is_a?(Symbol) && Search.text_column?(klass, @source.label)
|
|
266
|
+
end
|
|
267
|
+
|
|
268
|
+
def value_column
|
|
269
|
+
@value_column ||= @source.value.to_s if !@source.value.respond_to?(:call) && klass.columns_hash.key?(@source.value.to_s)
|
|
270
|
+
end
|
|
271
|
+
|
|
272
|
+
def search_columns = @search_columns ||= Search.columns_for(klass, @source, label_column:)
|
|
273
|
+
|
|
274
|
+
def by_columns(term, limit, offset)
|
|
275
|
+
Search.new(ordered, columns: search_columns, value_column:, by_primary_key: @by_primary_key).call(term, limit:, offset:)
|
|
276
|
+
end
|
|
277
|
+
|
|
278
|
+
# No column to search: match computed labels in Ruby, over a bounded window.
|
|
279
|
+
def scan(term, limit, offset)
|
|
280
|
+
tokens = term.downcase.split
|
|
281
|
+
window = ordered.limit(SCAN_CAP + 1).to_a
|
|
282
|
+
capped = window.size > SCAN_CAP
|
|
283
|
+
ReactiveAdmin.diagnostics.record(:scan_capped, source: (@source.name || klass.name).to_s, cap: SCAN_CAP) if capped
|
|
284
|
+
hits = window.first(SCAN_CAP).select { |record| tokens.all? { label_of(record).to_s.downcase.include?(it) } }
|
|
285
|
+
[hits.drop(offset).first(limit), capped || hits.size > offset + limit]
|
|
286
|
+
end
|
|
287
|
+
end
|
|
288
|
+
end
|
|
289
|
+
end
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
module ReactiveAdmin
|
|
2
|
+
# What an error reporter sees of a crash in the React client. The client sends names only (the error's
|
|
3
|
+
# class, the components it unwound through, a route pattern, a resource key); each field is kept only
|
|
4
|
+
# if it is shaped like one of those, so error text, a URL's ids and record values can't ride along.
|
|
5
|
+
class ClientError < StandardError
|
|
6
|
+
EVENT = "client_error.reactiveadmin".freeze
|
|
7
|
+
NAME = /\A[A-Za-z_$][\w$]{0,63}\z/
|
|
8
|
+
BOUNDARY = /\A(?:app|route|frame|iframe|node:\w{1,40})\z/
|
|
9
|
+
VERSION = /\A[\w.+-]{1,32}\z/
|
|
10
|
+
REQUEST_ID = WriteAudit::ID_FORMAT
|
|
11
|
+
ROUTE_LITERALS = %w[groups new edit actions *].freeze
|
|
12
|
+
ROUTE_PARAM = /\A:\w{1,32}\z/
|
|
13
|
+
MAX_COMPONENTS = 20
|
|
14
|
+
MAX_ROUTE_SEGMENTS = 8
|
|
15
|
+
|
|
16
|
+
attr_reader :original_class, :boundary, :components, :route, :resource, :client_version, :client_request_id
|
|
17
|
+
|
|
18
|
+
# Nil without a usable error name: there is nothing to report. `authorizer` limits the resource to what its user may read.
|
|
19
|
+
def self.from_params(params, authorizer:)
|
|
20
|
+
name = text(params[:error_name], NAME) or return
|
|
21
|
+
|
|
22
|
+
new(name, boundary: text(params[:boundary], BOUNDARY), components: components_of(params[:components]), route: route_of(params[:route]),
|
|
23
|
+
resource: resource_of(params[:resource], authorizer), client_version: text(params[:client_version], VERSION), client_request_id: text(params[:request_id], REQUEST_ID))
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# Never raises: the client has already survived the crash, so a failing reporter mustn't turn the report into a 500.
|
|
27
|
+
# The event and the reporter are tried apart, so one failing (a subscriber's counter store, say) can't drop the other.
|
|
28
|
+
def self.report(error, request_id:, user_id:)
|
|
29
|
+
payload = error.to_h.merge(request_id:, user_id:)
|
|
30
|
+
deliver("event") { ActiveSupport::Notifications.instrument(EVENT, payload) }
|
|
31
|
+
deliver("error_reporter") { ReactiveAdmin.config.error_reporter.call(error, payload.except(:components, :user_id).merge(action: "client")) }
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def self.deliver(via)
|
|
35
|
+
yield
|
|
36
|
+
rescue => e
|
|
37
|
+
Rails.logger.error("[reactiveadmin] client error #{via} failed: #{e.class}")
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def self.text(value, format) = (value if value.is_a?(String) && value.match?(format))
|
|
41
|
+
|
|
42
|
+
def self.components_of(value) = Array.wrap(value).filter_map { text(it, NAME) }.first(MAX_COMPONENTS)
|
|
43
|
+
|
|
44
|
+
def self.route_of(value)
|
|
45
|
+
return unless value.is_a?(String)
|
|
46
|
+
|
|
47
|
+
segments = value.delete_prefix("/").split("/", -1)
|
|
48
|
+
value if segments.size <= MAX_ROUTE_SEGMENTS && segments.all? { it.empty? || it.in?(ROUTE_LITERALS) || it.match?(ROUTE_PARAM) }
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def self.resource_of(value, authorizer)
|
|
52
|
+
named = [*Registry.resources, *Registry.pages].select { (AA.page?(it) ? it.underscored_resource_name : Registry.route_key(it)) == value }
|
|
53
|
+
value if named.any? { Exposure.allowed?(it) && Exposure.permitted?(authorizer, it) }
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def initialize(name, boundary: nil, components: [], route: nil, resource: nil, client_version: nil, client_request_id: nil)
|
|
57
|
+
@original_class = name
|
|
58
|
+
@boundary = boundary
|
|
59
|
+
@components = components
|
|
60
|
+
@route = route
|
|
61
|
+
@resource = resource
|
|
62
|
+
@client_version = client_version
|
|
63
|
+
@client_request_id = client_request_id
|
|
64
|
+
super("#{name} in the client at #{route || "an unknown route"}")
|
|
65
|
+
set_backtrace(components.map { "in #{it}" })
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def to_h = {resource:, boundary:, route:, components:, client_version:, client_request_id:, error_class: original_class}.compact
|
|
69
|
+
end
|
|
70
|
+
end
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
module ReactiveAdmin
|
|
2
|
+
# AA's display-name fallback sends the primary key as a method name, which an array can't be.
|
|
3
|
+
module CompositeKeys
|
|
4
|
+
module DisplayName
|
|
5
|
+
def display_name(resource)
|
|
6
|
+
return super unless resource&.class.try(:primary_key).is_a?(Array) && AA.display_name_fallback?(self, resource)
|
|
7
|
+
|
|
8
|
+
ERB::Util.html_escape("#{resource.class.model_name.human} ##{resource.to_param}")
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
end
|