decidim-restfull-core 0.3.11
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/app/commands/decidim/rest_full/core/create_api_client.rb +40 -0
- data/app/commands/decidim/rest_full/core/impersonate_resource_owner_from_credentials.rb +151 -0
- data/app/commands/decidim/rest_full/core/syncronize_unconfirmed_host.rb +45 -0
- data/app/controllers/concerns/decidim/api/rest_full/async_api_job_enqueuing.rb +69 -0
- data/app/controllers/concerns/decidim/api/rest_full/collection_pagination.rb +78 -0
- data/app/controllers/concerns/decidim/api/rest_full/collection_pagination_meta.rb +25 -0
- data/app/controllers/concerns/decidim/api/rest_full/conditional_get_rendering.rb +49 -0
- data/app/controllers/concerns/decidim/api/rest_full/extended_data_endpoints.rb +96 -0
- data/app/controllers/concerns/decidim/api/rest_full/job_response_linking.rb +31 -0
- data/app/controllers/decidim/api/rest_full/application_controller.rb +245 -0
- data/app/controllers/decidim/api/rest_full/attachments/attachments_controller.rb +81 -0
- data/app/controllers/decidim/api/rest_full/components/component_extended_data_controller.rb +31 -0
- data/app/controllers/decidim/api/rest_full/components/components_controller.rb +68 -0
- data/app/controllers/decidim/api/rest_full/core/resources_controller.rb +142 -0
- data/app/controllers/decidim/api/rest_full/core.rb +11 -0
- data/app/controllers/decidim/api/rest_full/jobs/jobs_controller.rb +109 -0
- data/app/controllers/decidim/api/rest_full/organizations/organization_extended_data_controller.rb +31 -0
- data/app/controllers/decidim/api/rest_full/organizations/organizations_controller.rb +234 -0
- data/app/controllers/decidim/api/rest_full/roles/roles_controller.rb +125 -0
- data/app/controllers/decidim/api/rest_full/spaces/space_extended_data_controller.rb +29 -0
- data/app/controllers/decidim/api/rest_full/spaces/spaces_controller.rb +225 -0
- data/app/controllers/decidim/api/rest_full/users/magic_links_controller.rb +81 -0
- data/app/controllers/decidim/api/rest_full/users/user_extended_data_controller.rb +92 -0
- data/app/controllers/decidim/api/rest_full/users/users_controller.rb +40 -0
- data/app/controllers/decidim/api/rest_full/webhooks/webhook_events_controller.rb +33 -0
- data/app/controllers/decidim/api/rest_full/webhooks/webhook_registrations_controller.rb +106 -0
- data/app/controllers/decidim/rest_full/pages_controller.rb +9 -0
- data/app/controllers/decidim/rest_full/system/api_clients_controller.rb +69 -0
- data/app/controllers/decidim/rest_full/system/permissions_controller.rb +30 -0
- data/app/controllers/decidim/rest_full/system/webhook_registrations_controller.rb +49 -0
- data/app/forms/decidim/rest_full/core/admin/config_form.rb +49 -0
- data/app/forms/decidim/rest_full/core/api_client_form.rb +20 -0
- data/app/forms/decidim/rest_full/core/api_permissions.rb +18 -0
- data/app/forms/decidim/rest_full/core/magic_link_redirect_url_form.rb +114 -0
- data/app/forms/decidim/rest_full/core/webhook_event_form.rb +35 -0
- data/app/forms/decidim/rest_full/core/webhook_registration_form.rb +49 -0
- data/app/jobs/decidim/rest_full/application_job.rb +10 -0
- data/app/jobs/decidim/rest_full/core/space_webhook_job.rb +60 -0
- data/app/jobs/decidim/rest_full/core/syncronize_unconfirmed_host_job.rb +18 -0
- data/app/jobs/decidim/rest_full/core/webhook_job.rb +13 -0
- data/app/jobs/decidim/rest_full/execute_api_job_job.rb +55 -0
- data/app/models/decidim/rest_full/api_job.rb +74 -0
- data/app/models/decidim/rest_full/core/ability.rb +195 -0
- data/app/models/decidim/rest_full/core/api_client.rb +56 -0
- data/app/models/decidim/rest_full/core/magic_token.rb +45 -0
- data/app/models/decidim/rest_full/core/permission.rb +15 -0
- data/app/models/decidim/rest_full/core/resource_extended_data.rb +16 -0
- data/app/models/decidim/rest_full/core/webhook_registration.rb +61 -0
- data/app/overrides/system_host_input.rb +40 -0
- data/app/serializers/decidim/api/rest_full/core/application_serializer.rb +19 -0
- data/app/serializers/decidim/api/rest_full/core/assembly_serializer.rb +13 -0
- data/app/serializers/decidim/api/rest_full/core/attachment_serializer.rb +76 -0
- data/app/serializers/decidim/api/rest_full/core/awesome_iframe_component_serializer.rb +12 -0
- data/app/serializers/decidim/api/rest_full/core/awesome_map_component_serializer.rb +12 -0
- data/app/serializers/decidim/api/rest_full/core/component_serializer.rb +97 -0
- data/app/serializers/decidim/api/rest_full/core/helpers/resource_links_helper.rb +48 -0
- data/app/serializers/decidim/api/rest_full/core/magic_link_redirect_serializer.rb +43 -0
- data/app/serializers/decidim/api/rest_full/core/magic_link_serializer.rb +44 -0
- data/app/serializers/decidim/api/rest_full/core/organization_serializer.rb +60 -0
- data/app/serializers/decidim/api/rest_full/core/organization_webhook_serializer.rb +21 -0
- data/app/serializers/decidim/api/rest_full/core/page_component_serializer.rb +20 -0
- data/app/serializers/decidim/api/rest_full/core/page_serializer.rb +12 -0
- data/app/serializers/decidim/api/rest_full/core/participatory_process_serializer.rb +13 -0
- data/app/serializers/decidim/api/rest_full/core/resource_serializer.rb +84 -0
- data/app/serializers/decidim/api/rest_full/core/role_serializer.rb +21 -0
- data/app/serializers/decidim/api/rest_full/core/serializer_lookup.rb +55 -0
- data/app/serializers/decidim/api/rest_full/core/space_serializer.rb +117 -0
- data/app/serializers/decidim/api/rest_full/core/space_webhook_serializer.rb +64 -0
- data/app/serializers/decidim/api/rest_full/core/user_serializer.rb +43 -0
- data/app/serializers/decidim/api/rest_full/core/user_webhook_serializer.rb +21 -0
- data/app/serializers/decidim/api/rest_full/core/webhook_registration_serializer.rb +25 -0
- data/app/serializers/decidim/api/rest_full/core/webhook_serializer.rb +68 -0
- data/app/serializers/decidim/api/rest_full/core.rb +10 -0
- data/app/services/decidim/rest_full/core/api_system_operations.rb +505 -0
- data/app/services/decidim/rest_full/core/fire_webhook.rb +114 -0
- data/app/services/decidim/rest_full/core/participatory_process_step_activated_webhook_handler.rb +39 -0
- data/app/services/decidim/rest_full/participatory_space_visibility.rb +64 -0
- data/app/views/decidim/rest_full/core/admin/_organization_settings_tab.html.erb +35 -0
- data/app/views/decidim/rest_full/pages/show.html.erb +60 -0
- data/app/views/decidim/rest_full/system/api_clients/_edit_info_tab.erb +261 -0
- data/app/views/decidim/rest_full/system/api_clients/_edit_webhook_tab.erb +74 -0
- data/app/views/decidim/rest_full/system/api_clients/_form.html.erb +23 -0
- data/app/views/decidim/rest_full/system/api_clients/_permissions_scope.html.erb +22 -0
- data/app/views/decidim/rest_full/system/api_clients/edit.html.erb +53 -0
- data/app/views/decidim/rest_full/system/api_clients/index.html.erb +37 -0
- data/app/views/decidim/rest_full/system/api_clients/new.html.erb +13 -0
- data/app/views/decidim/rest_full/system/api_clients/show.html.erb +10 -0
- data/config/i18n-tasks.yml +381 -0
- data/config/locales/decidim_rest_full.en.yml +184 -0
- data/config/routes.rb +116 -0
- data/db/migrate/20241125061513_create_api_client_permissions.rb +12 -0
- data/db/migrate/20250112155305_create_proposal_application_id.rb +11 -0
- data/db/migrate/20250131155931_add_users_magic_token.rb +14 -0
- data/db/migrate/20250609083335_create_webhooks_table.rb +16 -0
- data/db/migrate/20250609091039_add_is_event_to_api_client_permissions.rb +7 -0
- data/db/migrate/20250624080106_create_organization_extended_data.rb +11 -0
- data/db/migrate/20260321120000_add_redirect_url_to_decidim_rest_full_user_magic_tokens.rb +7 -0
- data/db/migrate/20260511120000_create_decidim_rest_full_api_jobs.rb +23 -0
- data/db/migrate/20260515120000_add_poll_and_oauth_context_to_rest_full_api_jobs.rb +38 -0
- data/db/migrate/20260516153000_remove_poll_secret_from_decidim_rest_full_api_jobs.rb +7 -0
- data/db/migrate/20260516160000_add_org_status_index_to_rest_full_api_jobs.rb +9 -0
- data/db/migrate/20260805070000_create_resource_extended_data.rb +44 -0
- data/lib/decidim/rest_full/api_execution_context.rb +58 -0
- data/lib/decidim/rest_full/api_job_command_runner.rb +48 -0
- data/lib/decidim/rest_full/api_job_payload.rb +19 -0
- data/lib/decidim/rest_full/cli/base.rb +84 -0
- data/lib/decidim/rest_full/cli/create.rb +122 -0
- data/lib/decidim/rest_full/cli/delete.rb +48 -0
- data/lib/decidim/rest_full/cli/get.rb +53 -0
- data/lib/decidim/rest_full/cli/grant.rb +58 -0
- data/lib/decidim/rest_full/cli/revoke.rb +53 -0
- data/lib/decidim/rest_full/cli.rb +70 -0
- data/lib/decidim/rest_full/core/apartment_tenant_switch.rb +23 -0
- data/lib/decidim/rest_full/core/api_exception.rb +179 -0
- data/lib/decidim/rest_full/core/attachments_operations.rb +232 -0
- data/lib/decidim/rest_full/core/configuration.rb +116 -0
- data/lib/decidim/rest_full/core/definition_registry.rb +509 -0
- data/lib/decidim/rest_full/core/doorkeeper_config.rb +130 -0
- data/lib/decidim/rest_full/core/engine.rb +167 -0
- data/lib/decidim/rest_full/core/extended_data_at_path.rb +22 -0
- data/lib/decidim/rest_full/core/gem_spec_paths.rb +56 -0
- data/lib/decidim/rest_full/core/has_extended_data.rb +70 -0
- data/lib/decidim/rest_full/core/http_cache/collection_fingerprint.rb +97 -0
- data/lib/decidim/rest_full/core/http_cache/fingerprint_contributor_registry.rb +53 -0
- data/lib/decidim/rest_full/core/http_cache/request_oauth_context.rb +34 -0
- data/lib/decidim/rest_full/core/http_cache/resource_show_fingerprint.rb +74 -0
- data/lib/decidim/rest_full/core/menu.rb +18 -0
- data/lib/decidim/rest_full/core/module_availability.rb +181 -0
- data/lib/decidim/rest_full/core/open_api_definition_paths.rb +59 -0
- data/lib/decidim/rest_full/core/overrides/application_mailer_override.rb +50 -0
- data/lib/decidim/rest_full/core/overrides/organization_client_ids_override.rb +17 -0
- data/lib/decidim/rest_full/core/overrides/update_organization_command_override.rb +27 -0
- data/lib/decidim/rest_full/core/overrides/update_organization_form_override.rb +46 -0
- data/lib/decidim/rest_full/core/overrides/user_extended_data_ransack.rb +15 -0
- data/lib/decidim/rest_full/core/overrides/user_magic_token_override.rb +27 -0
- data/lib/decidim/rest_full/core/permission_registry.rb +65 -0
- data/lib/decidim/rest_full/core/ransackers.rb +99 -0
- data/lib/decidim/rest_full/core/rest_enhancement_builder.rb +115 -0
- data/lib/decidim/rest_full/core/rest_enhancement_registration.rb +21 -0
- data/lib/decidim/rest_full/core/roles/role_id_codec.rb +50 -0
- data/lib/decidim/rest_full/core/roles/roles_aggregator.rb +203 -0
- data/lib/decidim/rest_full/core/roles/roles_writer.rb +343 -0
- data/lib/decidim/rest_full/core/route_registry.rb +123 -0
- data/lib/decidim/rest_full/core/serializer_additions_registry.rb +100 -0
- data/lib/decidim/rest_full/core/settings_tab.rb +22 -0
- data/lib/decidim/rest_full/core/swagger_spec_paths.rb +61 -0
- data/lib/decidim/rest_full/core/tokens_availability.rb +23 -0
- data/lib/decidim/rest_full/core/webhook_dispatcher.rb +55 -0
- data/lib/decidim/rest_full/core/webhook_event_catalog.rb +136 -0
- data/lib/decidim/rest_full/extension.rb +276 -0
- data/lib/decidim/rest_full/rest_full.rb +12 -0
- data/lib/decidim/rest_full/routes.rb +58 -0
- data/lib/decidim/rest_full/routing.rb +53 -0
- data/lib/decidim/rest_full/sync_runner.rb +14 -0
- data/lib/decidim/rest_full/test/apartment.rb +52 -0
- data/lib/decidim/rest_full/test/definitions/README.md +96 -0
- data/lib/decidim/rest_full/test/definitions/attachment.rb +64 -0
- data/lib/decidim/rest_full/test/definitions/collection_meta.rb +20 -0
- data/lib/decidim/rest_full/test/definitions/component.rb +99 -0
- data/lib/decidim/rest_full/test/definitions/component_type.rb +8 -0
- data/lib/decidim/rest_full/test/definitions/core.rb +16 -0
- data/lib/decidim/rest_full/test/definitions/creation_date.rb +11 -0
- data/lib/decidim/rest_full/test/definitions/edition_date.rb +11 -0
- data/lib/decidim/rest_full/test/definitions/error.rb +40 -0
- data/lib/decidim/rest_full/test/definitions/filter_param.rb +105 -0
- data/lib/decidim/rest_full/test/definitions/grant_param.rb +82 -0
- data/lib/decidim/rest_full/test/definitions/health.rb +12 -0
- data/lib/decidim/rest_full/test/definitions/introspect_data.rb +67 -0
- data/lib/decidim/rest_full/test/definitions/job.rb +130 -0
- data/lib/decidim/rest_full/test/definitions/locales_param.rb +19 -0
- data/lib/decidim/rest_full/test/definitions/magic_link.rb +37 -0
- data/lib/decidim/rest_full/test/definitions/magic_link_redirect.rb +32 -0
- data/lib/decidim/rest_full/test/definitions/manifests.rb +28 -0
- data/lib/decidim/rest_full/test/definitions/oauth_token_response.rb +18 -0
- data/lib/decidim/rest_full/test/definitions/organization.rb +75 -0
- data/lib/decidim/rest_full/test/definitions/organization_extended_data.rb +19 -0
- data/lib/decidim/rest_full/test/definitions/publication_date.rb +11 -0
- data/lib/decidim/rest_full/test/definitions/relationship_identifier.rb +29 -0
- data/lib/decidim/rest_full/test/definitions/resource_extended_data.rb +20 -0
- data/lib/decidim/rest_full/test/definitions/role.rb +61 -0
- data/lib/decidim/rest_full/test/definitions/shared.rb +22 -0
- data/lib/decidim/rest_full/test/definitions/space.rb +112 -0
- data/lib/decidim/rest_full/test/definitions/tags/attachments_tag.rb +26 -0
- data/lib/decidim/rest_full/test/definitions/tags/component_tag.rb +26 -0
- data/lib/decidim/rest_full/test/definitions/tags/jobs_tag.rb +20 -0
- data/lib/decidim/rest_full/test/definitions/tags/oauth_tag.rb +28 -0
- data/lib/decidim/rest_full/test/definitions/tags/organization_extended_data_tag.rb +22 -0
- data/lib/decidim/rest_full/test/definitions/tags/organizations_tag.rb +24 -0
- data/lib/decidim/rest_full/test/definitions/tags/roles_tag.rb +14 -0
- data/lib/decidim/rest_full/test/definitions/tags/space_tag.rb +22 -0
- data/lib/decidim/rest_full/test/definitions/tags/users_tag.rb +14 -0
- data/lib/decidim/rest_full/test/definitions/tags/webhooks_tag.rb +31 -0
- data/lib/decidim/rest_full/test/definitions/tags.rb +12 -0
- data/lib/decidim/rest_full/test/definitions/time_zone.rb +10 -0
- data/lib/decidim/rest_full/test/definitions/translated_prop.rb +11 -0
- data/lib/decidim/rest_full/test/definitions/user.rb +93 -0
- data/lib/decidim/rest_full/test/definitions/user_extended_data.rb +14 -0
- data/lib/decidim/rest_full/test/definitions/webhook.rb +95 -0
- data/lib/decidim/rest_full/test/definitions.rb +15 -0
- data/lib/decidim/rest_full/test/factories/api_client.rb +16 -0
- data/lib/decidim/rest_full/test/factories/magic_token.rb +13 -0
- data/lib/decidim/rest_full/test/factories/webhook_registration.rb +10 -0
- data/lib/decidim/rest_full/test/factories.rb +5 -0
- data/lib/decidim/rest_full/test/global_context.rb +11 -0
- data/lib/decidim/rest_full/test/on_api_endpoint_methods.rb +190 -0
- data/lib/decidim/rest_full/test/open_api_tag_registry.rb +25 -0
- data/lib/decidim/rest_full/test/openapi_specs.rb +107 -0
- data/lib/decidim/rest_full/test/shared_examples/has_extended_data.rb +41 -0
- data/lib/decidim/rest_full/test/shared_examples.rb +345 -0
- data/lib/decidim/rest_full/version.rb +27 -0
- data/lib/decidim/rest_full.rb +99 -0
- data/lib/decidim-restfull-core.rb +3 -0
- data/lib/tasks/decidim/rest_full/fire_webhook.rake +52 -0
- data/lib/tasks/decidim/rest_full/integrator_sandbox.rake +37 -0
- data/lib/tasks/decidim/rest_full/rest_full_upgrade.rake +64 -0
- data/lib/tasks/decidim/rest_full/rest_full_webpacker.rake +58 -0
- data/spec/commands/decidim/rest_full/core/impersonate_resource_owner_from_credentials_spec.rb +159 -0
- data/spec/commands/decidim/rest_full/core/syncronize_unconfirmed_host_spec.rb +117 -0
- data/spec/controllers/concerns/decidim/api/rest_full/collection_pagination_spec.rb +128 -0
- data/spec/decidim/rest_full/core/configuration_engine_disabled_routes_spec.rb +24 -0
- data/spec/forms/decidim/rest_full/core/magic_link_redirect_url_form_spec.rb +69 -0
- data/spec/forms/decidim/rest_full/core/webhook_registration_form_spec.rb +42 -0
- data/spec/jobs/decidim/rest_full/core/space_webhook_job_spec.rb +62 -0
- data/spec/jobs/decidim/rest_full/core/syncronize_unconfirmed_host_job_spec.rb +34 -0
- data/spec/jobs/decidim/rest_full/core/webhook_job_spec.rb +29 -0
- data/spec/jobs/decidim/rest_full/execute_api_job_job_spec.rb +45 -0
- data/spec/lib/decidim/rest_full/core/apartment_tenant_switch_spec.rb +26 -0
- data/spec/lib/decidim/rest_full/core/api_exception/bad_request_spec.rb +62 -0
- data/spec/lib/decidim/rest_full/core/definition_registry_add_resource_relationships_spec.rb +19 -0
- data/spec/lib/decidim/rest_full/core/gem_spec_paths_spec.rb +27 -0
- data/spec/lib/decidim/rest_full/core/http_cache/collection_fingerprint_spec.rb +59 -0
- data/spec/lib/decidim/rest_full/core/http_cache/fingerprint_contributor_registry_spec.rb +60 -0
- data/spec/lib/decidim/rest_full/core/http_cache/resource_show_fingerprint_spec.rb +33 -0
- data/spec/lib/decidim/rest_full/core/module_availability_spec.rb +130 -0
- data/spec/lib/decidim/rest_full/core/open_api_definition_paths_spec.rb +54 -0
- data/spec/lib/decidim/rest_full/core/optional_feature_gems_spec.rb +172 -0
- data/spec/lib/decidim/rest_full/core/roles/role_id_codec_spec.rb +62 -0
- data/spec/lib/decidim/rest_full/core/roles/roles_aggregator_spec.rb +53 -0
- data/spec/lib/decidim/rest_full/core/roles/roles_writer_spec.rb +38 -0
- data/spec/lib/decidim/rest_full/core/route_registry_spec.rb +90 -0
- data/spec/lib/decidim/rest_full/core/serializer_additions_registry_spec.rb +100 -0
- data/spec/lib/decidim/rest_full/core/settings_tab_spec.rb +15 -0
- data/spec/lib/decidim/rest_full/core/swagger_spec_paths_spec.rb +50 -0
- data/spec/lib/decidim/rest_full/core/webhook_dispatcher_proposal_published_spec.rb +63 -0
- data/spec/lib/decidim/rest_full/core/webhook_event_catalog_spec.rb +34 -0
- data/spec/lib/decidim/rest_full/routing_spec.rb +62 -0
- data/spec/lib/decidim/rest_full/test/apartment_spec.rb +17 -0
- data/spec/models/decidim/rest_full/api_job_spec.rb +51 -0
- data/spec/models/decidim/rest_full/application_mailer_override_spec.rb +60 -0
- data/spec/models/decidim/rest_full/core/has_extended_data_spec.rb +16 -0
- data/spec/models/decidim/rest_full/core/magic_token_spec.rb +68 -0
- data/spec/models/decidim/rest_full/core/webhook_registration_spec.rb +161 -0
- data/spec/models/decidim/rest_full/user_magic_token_spec.rb +51 -0
- data/spec/requests/decidim/api/rest_full/application_controller_introspect_spec.rb +85 -0
- data/spec/requests/decidim/api/rest_full/application_controller_ropc_spec.rb +299 -0
- data/spec/requests/decidim/api/rest_full/application_controller_token_spec.rb +57 -0
- data/spec/requests/decidim/api/rest_full/attachments/attachments_controller_direct_upload_spec.rb +39 -0
- data/spec/requests/decidim/api/rest_full/attachments/attachments_controller_index_spec.rb +41 -0
- data/spec/requests/decidim/api/rest_full/attachments/attachments_controller_show_spec.rb +34 -0
- data/spec/requests/decidim/api/rest_full/components/component_extended_data_controller_spec.rb +78 -0
- data/spec/requests/decidim/api/rest_full/components/components_controller_search_spec.rb +197 -0
- data/spec/requests/decidim/api/rest_full/components/components_controller_search_sql_spec.rb +48 -0
- data/spec/requests/decidim/api/rest_full/jobs/api_jobs_async_and_conditional_get_spec.rb +209 -0
- data/spec/requests/decidim/api/rest_full/jobs/jobs_controller_destroy_spec.rb +58 -0
- data/spec/requests/decidim/api/rest_full/jobs/jobs_controller_index_spec.rb +126 -0
- data/spec/requests/decidim/api/rest_full/jobs/jobs_controller_show_spec.rb +89 -0
- data/spec/requests/decidim/api/rest_full/module_availability_gate_spec.rb +77 -0
- data/spec/requests/decidim/api/rest_full/organizations/organization_extended_data_controller_index_spec.rb +88 -0
- data/spec/requests/decidim/api/rest_full/organizations/organization_extended_data_controller_update_spec.rb +194 -0
- data/spec/requests/decidim/api/rest_full/organizations/organizations_controller_index_spec.rb +57 -0
- data/spec/requests/decidim/api/rest_full/organizations/organizations_controller_show_spec.rb +66 -0
- data/spec/requests/decidim/api/rest_full/organizations/organizations_controller_update_spec.rb +120 -0
- data/spec/requests/decidim/api/rest_full/roles/roles_controller_create_spec.rb +230 -0
- data/spec/requests/decidim/api/rest_full/roles/roles_controller_destroy_spec.rb +52 -0
- data/spec/requests/decidim/api/rest_full/roles/roles_controller_index_spec.rb +127 -0
- data/spec/requests/decidim/api/rest_full/roles/roles_controller_show_spec.rb +215 -0
- data/spec/requests/decidim/api/rest_full/routes_boot_spec.rb +58 -0
- data/spec/requests/decidim/api/rest_full/scenarios/upsert_user_by_phone_and_set_locale_spec.rb +69 -0
- data/spec/requests/decidim/api/rest_full/spaces/space_extended_data_controller_spec.rb +78 -0
- data/spec/requests/decidim/api/rest_full/spaces/spaces_controller_index_spec.rb +167 -0
- data/spec/requests/decidim/api/rest_full/spaces/spaces_controller_search_spec.rb +280 -0
- data/spec/requests/decidim/api/rest_full/spaces/spaces_controller_show_spec.rb +98 -0
- data/spec/requests/decidim/api/rest_full/users/magic_links_controller_create_spec.rb +124 -0
- data/spec/requests/decidim/api/rest_full/users/magic_links_controller_show_spec.rb +84 -0
- data/spec/requests/decidim/api/rest_full/users/user_extended_data_controller_index_spec.rb +74 -0
- data/spec/requests/decidim/api/rest_full/users/user_extended_data_controller_update_spec.rb +192 -0
- data/spec/requests/decidim/api/rest_full/users/users_controller_index_spec.rb +182 -0
- data/spec/requests/decidim/api/rest_full/webhooks/webhook_events_controller_show_spec.rb +52 -0
- data/spec/requests/decidim/api/rest_full/webhooks/webhook_registrations_controller_create_spec.rb +95 -0
- data/spec/requests/decidim/api/rest_full/webhooks/webhook_registrations_controller_destroy_spec.rb +34 -0
- data/spec/requests/decidim/api/rest_full/webhooks/webhook_registrations_controller_index_spec.rb +43 -0
- data/spec/requests/decidim/api/rest_full/webhooks/webhook_registrations_controller_show_spec.rb +51 -0
- data/spec/requests/decidim/api/rest_full/webhooks/webhook_registrations_controller_update_spec.rb +57 -0
- data/spec/requests/decidim/rest_full/system/api_clients_controller_spec.rb +69 -0
- data/spec/serializers/decidim/api/rest_full/core/organization_webhook_serializer_spec.rb +18 -0
- data/spec/serializers/decidim/api/rest_full/core/space_webhook_serializer_spec.rb +44 -0
- data/spec/serializers/decidim/api/rest_full/core/user_webhook_serializer_spec.rb +19 -0
- data/spec/services/decidim/rest_full/core/api_system_operations_resource_extended_data_update_spec.rb +35 -0
- data/spec/services/decidim/rest_full/core/fire_webhook_spec.rb +59 -0
- data/spec/services/decidim/rest_full/core/participatory_process_step_activated_webhook_handler_spec.rb +57 -0
- metadata +482 -0
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module RestFull
|
|
5
|
+
module CLI
|
|
6
|
+
module Commands
|
|
7
|
+
## Add a permission to an API client
|
|
8
|
+
class Grant < Base
|
|
9
|
+
attr_reader :parser
|
|
10
|
+
|
|
11
|
+
def run(args)
|
|
12
|
+
parse_options(args)
|
|
13
|
+
execute
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def parse_options(args)
|
|
17
|
+
@parser = OptionParser.new do |opts|
|
|
18
|
+
opts.banner = <<~USAGE
|
|
19
|
+
Rest Full CLI for Decidim (V.#{::Decidim::RestFull.version})
|
|
20
|
+
Usage: api-client grant [options]
|
|
21
|
+
USAGE
|
|
22
|
+
|
|
23
|
+
default_options(opts)
|
|
24
|
+
|
|
25
|
+
opts.on("--id ID", "Client ID") do |id|
|
|
26
|
+
@options[:id] = id
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
opts.on("--perm PERMISSION", "--permission PERMISSION", "Permission") do |permission|
|
|
30
|
+
@options[:permissions] ||= []
|
|
31
|
+
@options[:permissions] << permission unless @options[:permissions].include?(permission)
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
parser.parse!(args)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def execute
|
|
38
|
+
puts_error("Error: --id is required for grant command") unless @options[:id]
|
|
39
|
+
|
|
40
|
+
api_client = Decidim::RestFull::Core::ApiClient.find_by(uid: @options[:id])
|
|
41
|
+
permissions = @options[:permissions]
|
|
42
|
+
# Check if permission are valid for granted scopes
|
|
43
|
+
|
|
44
|
+
validate_permissions(permissions, api_client.scopes)
|
|
45
|
+
new_permissions = permissions.select do |permission|
|
|
46
|
+
api_client.permissions.where(permission:).empty?
|
|
47
|
+
end
|
|
48
|
+
api_client.permissions << new_permissions.map do |permission|
|
|
49
|
+
api_client.permissions.build(permission:)
|
|
50
|
+
end
|
|
51
|
+
api_client.save!
|
|
52
|
+
puts_api_client api_client
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module RestFull
|
|
5
|
+
module CLI
|
|
6
|
+
module Commands
|
|
7
|
+
## Remove a permission from an API client
|
|
8
|
+
class Revoke < Base
|
|
9
|
+
attr_reader :parser
|
|
10
|
+
|
|
11
|
+
def run(args)
|
|
12
|
+
parse_options(args)
|
|
13
|
+
execute
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def parse_options(args)
|
|
17
|
+
@parser = OptionParser.new do |opts|
|
|
18
|
+
opts.banner = <<~USAGE
|
|
19
|
+
Rest Full CLI for Decidim (V.#{::Decidim::RestFull.version})
|
|
20
|
+
Usage: api-client revoke [options]
|
|
21
|
+
USAGE
|
|
22
|
+
|
|
23
|
+
default_options(opts)
|
|
24
|
+
|
|
25
|
+
opts.on("--id ID", "Client ID") do |id|
|
|
26
|
+
@options[:id] = id
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
opts.on("--perm PERMISSION", "--permission PERMISSION", "Permission") do |permission|
|
|
30
|
+
@options[:permissions] ||= []
|
|
31
|
+
@options[:permissions] << permission unless @options[:permissions].include?(permission)
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
parser.parse!(args)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def execute
|
|
38
|
+
puts_error("Error: --id is required for revoke command") unless @options[:id]
|
|
39
|
+
|
|
40
|
+
api_client = Decidim::RestFull::Core::ApiClient.find_by(uid: @options[:id])
|
|
41
|
+
permissions = @options[:permissions]
|
|
42
|
+
# Check if permission are valid for granted scopes
|
|
43
|
+
validate_permissions(permissions, api_client.scopes)
|
|
44
|
+
api_client.permissions.where(permission: permissions).destroy_all
|
|
45
|
+
|
|
46
|
+
api_client.save!
|
|
47
|
+
puts_api_client api_client
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
require "optparse"
|
|
5
|
+
require "devise"
|
|
6
|
+
require "decidim/core"
|
|
7
|
+
require "decidim/rest_full"
|
|
8
|
+
require_relative "cli/base"
|
|
9
|
+
require_relative "cli/create"
|
|
10
|
+
require_relative "cli/get"
|
|
11
|
+
require_relative "cli/grant"
|
|
12
|
+
require_relative "cli/revoke"
|
|
13
|
+
require_relative "cli/delete"
|
|
14
|
+
|
|
15
|
+
module Decidim
|
|
16
|
+
module RestFull
|
|
17
|
+
module CLI
|
|
18
|
+
class ApiClient
|
|
19
|
+
def initialize
|
|
20
|
+
@main_parser = OptionParser.new do |opts|
|
|
21
|
+
opts.banner = <<~USAGE
|
|
22
|
+
Manage Decidim's API Clients (v.#{::Decidim::RestFull.version})
|
|
23
|
+
Usage: api-client [command] [options]
|
|
24
|
+
Commands:
|
|
25
|
+
- create: Create a new API client
|
|
26
|
+
- get: Get an existing API client
|
|
27
|
+
- grant: Grant a permission to an existing API client
|
|
28
|
+
- revoke: Revoke a permission from an existing API client
|
|
29
|
+
- delete: Delete an API client and its tokens
|
|
30
|
+
USAGE
|
|
31
|
+
|
|
32
|
+
opts.on("-h", "--help", "Show this help message") do
|
|
33
|
+
puts opts # rubocop:disable Rails/Output
|
|
34
|
+
exit 0
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def run(args)
|
|
40
|
+
if ENV.fetch("DISABLE_REST_FULL_BIN", "false") == "true"
|
|
41
|
+
Rails.logger.debug "client-api is disabled. See DISABLE_REST_FULL_BIN environment variable"
|
|
42
|
+
exit 0
|
|
43
|
+
end
|
|
44
|
+
command = args.first
|
|
45
|
+
|
|
46
|
+
case command
|
|
47
|
+
when "create"
|
|
48
|
+
Commands::Create.new.run(args[1..])
|
|
49
|
+
when "get"
|
|
50
|
+
Commands::Get.new.run(args[1..])
|
|
51
|
+
when "grant"
|
|
52
|
+
Commands::Grant.new.run(args[1..])
|
|
53
|
+
when "revoke"
|
|
54
|
+
Commands::Revoke.new.run(args[1..])
|
|
55
|
+
when "delete"
|
|
56
|
+
Commands::Delete.new.run(args[1..])
|
|
57
|
+
when "help", nil
|
|
58
|
+
puts @main_parser # rubocop:disable Rails/Output
|
|
59
|
+
else
|
|
60
|
+
puts @main_parser # rubocop:disable Rails/Output
|
|
61
|
+
puts "Unknown command: #{command}" # rubocop:disable Rails/Output
|
|
62
|
+
exit 1
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
Decidim::RestFull::CLI::ApiClient.new.run(ARGV) if __FILE__ == $PROGRAM_NAME
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module RestFull
|
|
5
|
+
module Core
|
|
6
|
+
# Optional ros-apartment: switch tenant by host for collection queries.
|
|
7
|
+
# Request tenant is held by Decidim::Apartment::Elevator (insert_before CurrentOrganization).
|
|
8
|
+
# Missing tenant/org → nil (caller maps that to 404/empty). Never Organization.first.
|
|
9
|
+
module ApartmentTenantSwitch
|
|
10
|
+
module_function
|
|
11
|
+
|
|
12
|
+
def call(host)
|
|
13
|
+
return yield unless Gem.loaded_specs.has_key?("ros-apartment")
|
|
14
|
+
|
|
15
|
+
key = Decidim::Apartment::DistributionKey.for_host(host)
|
|
16
|
+
return unless key
|
|
17
|
+
|
|
18
|
+
key.switch { yield }
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module RestFull
|
|
5
|
+
module Core
|
|
6
|
+
# API exception types and HTTP status mapping.
|
|
7
|
+
# Handler is included in Doorkeeper::TokensController and adds rescue_from for each
|
|
8
|
+
# EXCEPTIONS entry so API errors return consistent JSON (error + error_description).
|
|
9
|
+
module ApiException
|
|
10
|
+
EXCEPTIONS = {
|
|
11
|
+
|
|
12
|
+
# ActiveRecord Exceptions
|
|
13
|
+
"ActiveRecord::RecordInvalid" => { status: 400, message: "Invalid request" },
|
|
14
|
+
"ActiveRecord::RecordNotSaved" => { status: 400, message: "Record could not be saved" },
|
|
15
|
+
"ActiveRecord::RecordNotFound" => { status: 404, message: "Record not found" },
|
|
16
|
+
|
|
17
|
+
# ActionController Exceptions
|
|
18
|
+
"ActionController::ParameterMissing" => { status: 400, message: "Required parameter missing" },
|
|
19
|
+
"ActionController::RoutingError" => { status: 404, message: "Route not found" },
|
|
20
|
+
"AbstractController::ActionNotFound" => { status: 404, message: "Action not found" },
|
|
21
|
+
"ActionController::InvalidAuthenticityToken" => { status: 403, message: "Invalid authenticity token" },
|
|
22
|
+
"ActionController::InvalidCrossOriginRequest" => { status: 403, message: "Invalid cross-origin request" },
|
|
23
|
+
|
|
24
|
+
# Parsing Errors
|
|
25
|
+
"ActionDispatch::Http::Parameters::ParseError" => { status: 400, message: "Malformed JSON request" },
|
|
26
|
+
|
|
27
|
+
# Authorization errors
|
|
28
|
+
"CanCan::AccessDenied" => { status: 403, message: "Forbidden" },
|
|
29
|
+
"Doorkeeper::Errors::TokenForbidden" => { status: 403, message: "Forbidden" },
|
|
30
|
+
"Doorkeeper::Errors::TokenRevoked" => { status: 403, message: "Forbidden" },
|
|
31
|
+
"Doorkeeper::Errors::TokenExpired" => { status: 403, message: "Forbidden" },
|
|
32
|
+
"Doorkeeper::Errors::InvalidToken" => { status: 401, message: "Unauthorized access" },
|
|
33
|
+
"Doorkeeper::Errors::InvalidTokenStrategy" => { status: 400, message: "Bad request" },
|
|
34
|
+
|
|
35
|
+
# Generic Application-Level Errors
|
|
36
|
+
"Decidim::RestFull::Core::ApiException::BadRequest" => { status: 400, message: "Bad request" },
|
|
37
|
+
"Decidim::RestFull::Core::ApiException::Unauthorized" => { status: 401, message: "Unauthorized access" },
|
|
38
|
+
"Decidim::RestFull::Core::ApiException::Forbidden" => { status: 403, message: "Forbidden" },
|
|
39
|
+
"Decidim::RestFull::Core::ApiException::NotFound" => { status: 404, message: "Resource not found" },
|
|
40
|
+
"Decidim::RestFull::Core::ApiException::NotImplemented" => { status: 501, message: "Not implemented" }
|
|
41
|
+
}.freeze
|
|
42
|
+
|
|
43
|
+
class BaseError < StandardError; end
|
|
44
|
+
|
|
45
|
+
# 400 with optional machine-readable error_details (ActiveModel types).
|
|
46
|
+
class BadRequest < StandardError
|
|
47
|
+
attr_reader :error_details
|
|
48
|
+
|
|
49
|
+
def initialize(message = nil, error_details: nil)
|
|
50
|
+
@error_details = error_details
|
|
51
|
+
super(message)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# Map ActiveModel::Error objects → BadRequest with error_details.
|
|
55
|
+
# Note: ActiveModel::Errors#to_a returns message strings — iterate with #each/#map instead.
|
|
56
|
+
def self.from_errors(errors)
|
|
57
|
+
list = if errors.is_a?(ActiveModel::Errors)
|
|
58
|
+
errors.map { |error| error }
|
|
59
|
+
else
|
|
60
|
+
Array(errors)
|
|
61
|
+
end
|
|
62
|
+
details = list.map do |error|
|
|
63
|
+
{
|
|
64
|
+
code: code_for(error),
|
|
65
|
+
field: error.attribute.to_s,
|
|
66
|
+
description: error.full_message
|
|
67
|
+
}
|
|
68
|
+
end
|
|
69
|
+
message = details.map { |d| d[:description] }.join(". ")
|
|
70
|
+
new(message, error_details: details)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# Convenience: form.errors, optionally limited to attribute names.
|
|
74
|
+
def self.from_form(form, only: nil)
|
|
75
|
+
errs = form.errors
|
|
76
|
+
if only
|
|
77
|
+
keys = Array(only).map(&:to_s)
|
|
78
|
+
errs = errs.select { |err| keys.include?(err.attribute.to_s) }
|
|
79
|
+
end
|
|
80
|
+
from_errors(errs)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# Prefer ActiveModel symbol types. Some hosts (e.g. DecidimAwesome
|
|
84
|
+
# etiquette override) add translated strings instead of symbols.
|
|
85
|
+
def self.code_for(error)
|
|
86
|
+
type = error.respond_to?(:raw_type) ? error.raw_type : error.type
|
|
87
|
+
return type.to_s if type.is_a?(Symbol)
|
|
88
|
+
|
|
89
|
+
infer_code_from_message(type.to_s.presence || error.message.to_s)
|
|
90
|
+
end
|
|
91
|
+
private_class_method :code_for
|
|
92
|
+
|
|
93
|
+
def self.infer_code_from_message(message)
|
|
94
|
+
msg = message.to_s.downcase
|
|
95
|
+
return "too_much_caps" if msg.include?("capital letter") && msg.include?("too many")
|
|
96
|
+
return "must_start_with_caps" if msg.include?("start with a capital")
|
|
97
|
+
return "too_many_marks" if msg.include?("punctuation")
|
|
98
|
+
return "too_short" if msg.include?("too short")
|
|
99
|
+
return "too_long" if msg.include?("too long")
|
|
100
|
+
return "blank" if msg.include?("blank")
|
|
101
|
+
return "cant_be_equal_to_template" if msg.include?("template")
|
|
102
|
+
|
|
103
|
+
"invalid"
|
|
104
|
+
end
|
|
105
|
+
private_class_method :infer_code_from_message
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
class NotImplemented < StandardError; end
|
|
109
|
+
|
|
110
|
+
class Unauthorized < StandardError; end
|
|
111
|
+
|
|
112
|
+
class Forbidden < StandardError; end
|
|
113
|
+
|
|
114
|
+
class NotFound < StandardError; end
|
|
115
|
+
|
|
116
|
+
module Handler
|
|
117
|
+
module_function
|
|
118
|
+
|
|
119
|
+
def self.included(klass)
|
|
120
|
+
klass.class_eval do
|
|
121
|
+
rescue_from StandardError do |exception|
|
|
122
|
+
render status: :internal_server_error,
|
|
123
|
+
json: Decidim::RestFull::Core::ApiException::Handler.server_error_payload(exception)
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
EXCEPTIONS.each do |exception_name, context|
|
|
127
|
+
rescue_from exception_name do |exception|
|
|
128
|
+
render status: context[:status],
|
|
129
|
+
json: Decidim::RestFull::Core::ApiException::Handler.mapped_error_payload(exception, context)
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def server_error_payload(exception)
|
|
136
|
+
{
|
|
137
|
+
error: "Server error",
|
|
138
|
+
error_description: server_error_description(exception)
|
|
139
|
+
}.compact
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def mapped_error_payload(exception, context)
|
|
143
|
+
{
|
|
144
|
+
error: "#{context[:status]}: #{context[:message]}",
|
|
145
|
+
error_description: mapped_error_description(exception, context),
|
|
146
|
+
error_details: error_details_for(exception)
|
|
147
|
+
}.compact
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
def server_error_description(exception)
|
|
151
|
+
if swagger_dry_run?
|
|
152
|
+
"Internal Server Error (#{Rails.env}: #{exception.message || "unknown"})"
|
|
153
|
+
else
|
|
154
|
+
"Internal Server Error"
|
|
155
|
+
end
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
def mapped_error_description(exception, context)
|
|
159
|
+
return exception.message if context[:status] == 400
|
|
160
|
+
return "#{Rails.env}: #{exception.message}" if swagger_dry_run?
|
|
161
|
+
|
|
162
|
+
context[:message].to_s
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
def error_details_for(exception)
|
|
166
|
+
return unless exception.respond_to?(:error_details)
|
|
167
|
+
return if exception.error_details.blank?
|
|
168
|
+
|
|
169
|
+
exception.error_details
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
def swagger_dry_run?
|
|
173
|
+
Rails.env.test? && ENV.fetch("SWAGGER_DRY_RUN", "1") == "1"
|
|
174
|
+
end
|
|
175
|
+
end
|
|
176
|
+
end
|
|
177
|
+
end
|
|
178
|
+
end
|
|
179
|
+
end
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module RestFull
|
|
5
|
+
module Core
|
|
6
|
+
class AttachmentsOperations
|
|
7
|
+
ATTACHED_TO_ALIASES = {
|
|
8
|
+
"organization" => "Decidim::Organization",
|
|
9
|
+
"Decidim::Organization" => "Decidim::Organization",
|
|
10
|
+
"participatory_processes" => "Decidim::ParticipatoryProcess",
|
|
11
|
+
"Decidim::ParticipatoryProcess" => "Decidim::ParticipatoryProcess"
|
|
12
|
+
}.freeze
|
|
13
|
+
|
|
14
|
+
OPTIONAL_ATTACHED_TO_ALIASES = [
|
|
15
|
+
["proposals", "Decidim::Proposals::Proposal"],
|
|
16
|
+
["proposal", "Decidim::Proposals::Proposal"],
|
|
17
|
+
["Decidim::Proposals::Proposal", "Decidim::Proposals::Proposal"],
|
|
18
|
+
["assemblies", "Decidim::Assembly"],
|
|
19
|
+
["Decidim::Assembly", "Decidim::Assembly"],
|
|
20
|
+
["conferences", "Decidim::Conference"],
|
|
21
|
+
["Decidim::Conference", "Decidim::Conference"],
|
|
22
|
+
["initiatives", "Decidim::Initiative"],
|
|
23
|
+
["Decidim::Initiative", "Decidim::Initiative"]
|
|
24
|
+
].freeze
|
|
25
|
+
|
|
26
|
+
def initialize(context, params)
|
|
27
|
+
@context = context
|
|
28
|
+
@params = params
|
|
29
|
+
@organization = context.organization
|
|
30
|
+
@current_user = context.act_as
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def index_scope
|
|
34
|
+
scope = Decidim::Attachment.all
|
|
35
|
+
scope = apply_filters(scope)
|
|
36
|
+
scope.select { |attachment| organization_matches?(attachment) }
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def find!(id)
|
|
40
|
+
attachment = Decidim::Attachment.find_by(id:)
|
|
41
|
+
raise Decidim::RestFull::Core::ApiException::NotFound, "Attachment not found" unless attachment
|
|
42
|
+
raise Decidim::RestFull::Core::ApiException::Forbidden unless organization_matches?(attachment)
|
|
43
|
+
|
|
44
|
+
attachment
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def create!
|
|
48
|
+
attached_to = resolve_attached_to!(attached_to_type_param, attached_to_id_param)
|
|
49
|
+
form = build_form(attached_to)
|
|
50
|
+
Decidim::Admin::CreateAttachment.call(form, attached_to) do
|
|
51
|
+
on(:ok) { |attachment| attachment }
|
|
52
|
+
on(:invalid) { raise Decidim::RestFull::Core::ApiException::BadRequest, form_errors(form) }
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def update!(attachment)
|
|
57
|
+
form = build_update_form(attachment)
|
|
58
|
+
Decidim::Admin::UpdateAttachment.call(attachment, form) do
|
|
59
|
+
on(:ok) { attachment.reload }
|
|
60
|
+
on(:invalid) { raise Decidim::RestFull::Core::ApiException::BadRequest, form_errors(form) }
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def destroy!(attachment)
|
|
65
|
+
Decidim.traceability.perform_action!("delete", attachment, @current_user) do
|
|
66
|
+
attachment.destroy!
|
|
67
|
+
end
|
|
68
|
+
true
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def direct_upload!
|
|
72
|
+
blob = ActiveStorage::Blob.create_before_direct_upload!(
|
|
73
|
+
filename: @params.require(:filename),
|
|
74
|
+
byte_size: @params.require(:byte_size).to_i,
|
|
75
|
+
checksum: @params.require(:checksum),
|
|
76
|
+
content_type: @params[:content_type].presence || "application/octet-stream"
|
|
77
|
+
)
|
|
78
|
+
{
|
|
79
|
+
signed_id: blob.signed_id,
|
|
80
|
+
filename: blob.filename.to_s,
|
|
81
|
+
content_type: blob.content_type,
|
|
82
|
+
byte_size: blob.byte_size
|
|
83
|
+
}
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
private
|
|
87
|
+
|
|
88
|
+
def apply_filters(scope)
|
|
89
|
+
filter = @params[:filter] || {}
|
|
90
|
+
if filter[:attached_to_type].present? && filter[:attached_to_id].present?
|
|
91
|
+
type = resolve_type_name(filter[:attached_to_type])
|
|
92
|
+
scope = scope.where(attached_to_type: type, attached_to_id: filter[:attached_to_id])
|
|
93
|
+
end
|
|
94
|
+
scope = scope.where(attachment_collection_id: filter[:attachment_collection_id]) if filter[:attachment_collection_id].present?
|
|
95
|
+
if filter[:file_type].present?
|
|
96
|
+
scope = scope.to_a.select { |a| file_type_matches?(a, filter[:file_type]) }
|
|
97
|
+
return scope
|
|
98
|
+
end
|
|
99
|
+
scope
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def file_type_matches?(attachment, file_type)
|
|
103
|
+
case file_type.to_s
|
|
104
|
+
when "link" then attachment.link?
|
|
105
|
+
when "image" then attachment.photo?
|
|
106
|
+
when "document" then attachment.document? && !attachment.photo?
|
|
107
|
+
else false
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def organization_matches?(attachment)
|
|
112
|
+
attachment.organization&.id == @organization.id
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def resolve_attached_to!(type_param, id_param)
|
|
116
|
+
type = resolve_type_name(type_param)
|
|
117
|
+
record = type.constantize.find_by(id: id_param)
|
|
118
|
+
raise Decidim::RestFull::Core::ApiException::NotFound, "Attached resource not found" unless record
|
|
119
|
+
raise Decidim::RestFull::Core::ApiException::Forbidden unless record_organization(record)&.id == @organization.id
|
|
120
|
+
|
|
121
|
+
record
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def record_organization(record)
|
|
125
|
+
return record if record.is_a?(Decidim::Organization)
|
|
126
|
+
return record.organization if record.respond_to?(:organization)
|
|
127
|
+
|
|
128
|
+
nil
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
def attached_to_aliases
|
|
132
|
+
@attached_to_aliases ||= begin
|
|
133
|
+
aliases = ATTACHED_TO_ALIASES.dup
|
|
134
|
+
OPTIONAL_ATTACHED_TO_ALIASES.each do |key, class_name|
|
|
135
|
+
aliases[key] = class_name if Object.const_defined?(class_name)
|
|
136
|
+
end
|
|
137
|
+
aliases.freeze
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
def resolve_type_name(value)
|
|
142
|
+
key = value.to_s
|
|
143
|
+
resolved = attached_to_aliases[key] || key
|
|
144
|
+
raise Decidim::RestFull::Core::ApiException::BadRequest, "Unknown attached_to_type: #{value}" unless Object.const_defined?(resolved)
|
|
145
|
+
|
|
146
|
+
resolved
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
def attached_to_type_param
|
|
150
|
+
@params[:attached_to_type] || @params.dig(:data, :attributes, :attached_to_type) ||
|
|
151
|
+
@params.dig(:data, "attributes", "attached_to_type")
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
def attached_to_id_param
|
|
155
|
+
@params[:attached_to_id] || @params.dig(:data, :attributes, :attached_to_id) ||
|
|
156
|
+
@params.dig(:data, "attributes", "attached_to_id")
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
def build_form(attached_to)
|
|
160
|
+
Decidim::Admin::AttachmentForm.from_params(form_params(attached_to)).with_context(form_context(attached_to))
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
def build_update_form(attachment)
|
|
164
|
+
Decidim::Admin::AttachmentForm.from_model(attachment).with_context(form_context(attachment.attached_to)).tap do |form|
|
|
165
|
+
attrs = update_form_params
|
|
166
|
+
form.title = attrs[:title] if attrs[:title]
|
|
167
|
+
form.description = attrs[:description] if attrs[:description]
|
|
168
|
+
form.weight = attrs[:weight] if attrs.has_key?(:weight)
|
|
169
|
+
form.attachment_collection_id = attrs[:attachment_collection_id] if attrs.has_key?(:attachment_collection_id)
|
|
170
|
+
end
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
def form_context(attached_to)
|
|
174
|
+
{
|
|
175
|
+
attached_to:,
|
|
176
|
+
current_organization: @organization,
|
|
177
|
+
current_user: @current_user
|
|
178
|
+
}
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
def form_params(_attached_to)
|
|
182
|
+
locale = @organization.default_locale
|
|
183
|
+
{
|
|
184
|
+
title: localized_hash(:title, locale),
|
|
185
|
+
description: localized_hash(:description, locale),
|
|
186
|
+
file: file_param,
|
|
187
|
+
weight: @params[:weight] || 0,
|
|
188
|
+
attachment_collection_id: @params[:attachment_collection_id]
|
|
189
|
+
}.compact
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
def update_form_params
|
|
193
|
+
locale = @organization.default_locale
|
|
194
|
+
attrs = json_attributes
|
|
195
|
+
{
|
|
196
|
+
title: localized_from_attrs(:title, attrs, locale),
|
|
197
|
+
description: localized_from_attrs(:description, attrs, locale),
|
|
198
|
+
weight: attrs["weight"] || attrs[:weight],
|
|
199
|
+
attachment_collection_id: attrs["attachment_collection_id"] || attrs[:attachment_collection_id]
|
|
200
|
+
}.compact
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
def localized_hash(field, locale)
|
|
204
|
+
value = @params[field] || @params[field.to_s]
|
|
205
|
+
{ locale => value.presence || @params.require(field) }
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
def localized_from_attrs(field, attrs, locale)
|
|
209
|
+
val = attrs[field.to_s] || attrs[field.to_sym]
|
|
210
|
+
return val if val.is_a?(Hash)
|
|
211
|
+
|
|
212
|
+
{ locale => val }
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
def file_param
|
|
216
|
+
@params[:file] || @params[:signed_id] || json_attributes["signed_id"] || json_attributes[:signed_id]
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
def json_attributes
|
|
220
|
+
data = @params[:data]
|
|
221
|
+
return {} unless data
|
|
222
|
+
|
|
223
|
+
data["attributes"] || data[:attributes] || {}
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
def form_errors(form)
|
|
227
|
+
form.errors.full_messages.join(", ")
|
|
228
|
+
end
|
|
229
|
+
end
|
|
230
|
+
end
|
|
231
|
+
end
|
|
232
|
+
end
|