The personal finance app for everyone (by everyone)
Ruby
Keep guests from changing family configuration through the API and imports (#4005)
GFRddab05c54665
diff --git a/app/controllers/api/v1/base_controller.rb b/app/controllers/api/v1/base_controller.rbindex f0285cb011..7fa36d56a1 100644--- a/app/controllers/api/v1/base_controller.rb+++ b/app/controllers/api/v1/base_controller.rb@@ -221,6 +221,15 @@ class Api::V1::BaseController < ApplicationControllerauthorize_scope!(:read)end+ # Guests are read-only by design, so API credentials they hold must not+ # change family-wide configuration either.+ def reject_guest_writes!+ return true unless current_resource_owner&.guest?++ render_json({ error: "forbidden", message: "Guests cannot modify family configuration" }, status: :forbidden)+ false+ end+# Consistent JSON response methoddef render_json(data, status: :ok)render json: data, status: statusdiff --git a/app/controllers/api/v1/categories_controller.rb b/app/controllers/api/v1/categories_controller.rbindex 302b972258..40ac1bf6cb 100644--- a/app/controllers/api/v1/categories_controller.rb+++ b/app/controllers/api/v1/categories_controller.rb@@ -5,6 +5,7 @@ class Api::V1::CategoriesController < Api::V1::BaseControllerbefore_action :ensure_read_scope, only: %i[index show]before_action :ensure_write_scope, only: :create+ before_action :reject_guest_writes!, only: :createbefore_action :set_category, only: :showdef indexdiff --git a/app/controllers/api/v1/import_sessions_controller.rb b/app/controllers/api/v1/import_sessions_controller.rbindex 212c97c2a1..8686e4a19b 100644--- a/app/controllers/api/v1/import_sessions_controller.rb+++ b/app/controllers/api/v1/import_sessions_controller.rb@@ -3,6 +3,8 @@class Api::V1::ImportSessionsController < Api::V1::BaseControllerbefore_action :ensure_read_scope, only: [ :show ]before_action :ensure_write_scope, only: [ :create, :create_chunk, :publish ]+ # Import sessions only carry Sure imports, which write family configuration.+ before_action :reject_guest_writes!, only: [ :create, :create_chunk, :publish ]before_action :set_import_session, only: [ :show, :create_chunk, :publish ]def creatediff --git a/app/controllers/api/v1/imports_controller.rb b/app/controllers/api/v1/imports_controller.rbindex 3c92bff208..84ea5c1149 100644--- a/app/controllers/api/v1/imports_controller.rb+++ b/app/controllers/api/v1/imports_controller.rb@@ -6,6 +6,7 @@ class Api::V1::ImportsController < Api::V1::BaseController# Ensure proper scope authorizationbefore_action :ensure_read_scope, only: [ :index, :show, :rows, :preflight ]before_action :ensure_write_scope, only: [ :create ]+ before_action :reject_guest_family_config_import!, only: [ :create ]before_action :set_import_with_rows, only: [ :show ]before_action :set_import, only: [ :rows ]@@ -119,6 +120,10 @@ class Api::V1::ImportsController < Api::V1::BaseController# For API simplicity, if enough info is provided, we might want to trigger processingif @import.configured? && params[:publish] == "true"+ if current_resource_owner.guest? && @import.creates_categories_or_tags?+ return render_json({ error: "forbidden", message: "Guests cannot create categories or tags", import_id: @import.id }, status: :forbidden)+ end+@import.publish_laterend@@ -162,6 +167,12 @@ class Api::V1::ImportsController < Api::V1::BaseControllerprivate+ def reject_guest_family_config_import!+ return true unless Import::FAMILY_CONFIG_TYPES.include?(params[:type].to_s)++ reject_guest_writes!+ end+def set_import@import = import_scope.find(params[:id])rescue ActiveRecord::RecordNotFounddiff --git a/app/controllers/api/v1/merchants_controller.rb b/app/controllers/api/v1/merchants_controller.rbindex cf190fc06f..acace64083 100644--- a/app/controllers/api/v1/merchants_controller.rb+++ b/app/controllers/api/v1/merchants_controller.rb@@ -5,6 +5,7 @@ module Apiclass MerchantsController < BaseControllerbefore_action -> { authorize_scope!(:read) }, only: [ :index, :show ]before_action -> { authorize_scope!(:write) }, only: [ :create ]+ before_action :reject_guest_writes!, only: [ :create ]def indexfamily = current_resource_owner.familydiff --git a/app/controllers/api/v1/tags_controller.rb b/app/controllers/api/v1/tags_controller.rbindex 42dfcf7dfd..79b6f07d21 100644--- a/app/controllers/api/v1/tags_controller.rb+++ b/app/controllers/api/v1/tags_controller.rb@@ -15,6 +15,7 @@ module Apiclass TagsController < BaseControllerbefore_action -> { authorize_scope!(:read) }, only: %i[index show]before_action -> { authorize_scope!(:read_write) }, only: %i[create update destroy]+ before_action :reject_guest_writes!, only: %i[create update destroy]before_action :set_tag, only: %i[show update destroy]# List all tags belonging to the familydiff --git a/app/controllers/concerns/import_guest_guardable.rb b/app/controllers/concerns/import_guest_guardable.rbnew file mode 100644index 0000000000..bb119a607b--- /dev/null+++ b/app/controllers/concerns/import_guest_guardable.rb@@ -0,0 +1,10 @@+# The import wizard's steps change an import before it is published. Guests+# may only change their own kind of import, and only before publishing.+module ImportGuestGuardable+ extend ActiveSupport::Concern++ private+ def require_import_editable!+ require_non_guest! unless @import.editable_by?(Current.user)+ end+enddiff --git a/app/controllers/import/configurations_controller.rb b/app/controllers/import/configurations_controller.rbindex 65c47fc681..5f764d0e21 100644--- a/app/controllers/import/configurations_controller.rb+++ b/app/controllers/import/configurations_controller.rb@@ -1,7 +1,10 @@class Import::ConfigurationsController < ApplicationController+ include ImportGuestGuardable+layout "imports"before_action :set_import+ before_action :require_import_editable!, only: :updatedef show# PDF imports are auto-configured from AI extraction, skip to clean stepdiff --git a/app/controllers/import/mappings_controller.rb b/app/controllers/import/mappings_controller.rbindex 098c401010..f7347e40cc 100644--- a/app/controllers/import/mappings_controller.rb+++ b/app/controllers/import/mappings_controller.rb@@ -1,5 +1,8 @@class Import::MappingsController < ApplicationController+ include ImportGuestGuardable+before_action :set_import+ before_action :require_import_editable!def updatemapping = @import.mappings.find(params[:id])diff --git a/app/controllers/import/qif_category_selections_controller.rb b/app/controllers/import/qif_category_selections_controller.rbindex b626aac3e0..19493dd6fa 100644--- a/app/controllers/import/qif_category_selections_controller.rb+++ b/app/controllers/import/qif_category_selections_controller.rb@@ -1,7 +1,10 @@class Import::QifCategorySelectionsController < ApplicationController+ include ImportGuestGuardable+layout "imports"before_action :set_import+ before_action :require_import_editable!, only: :updatedef showvalid_formats = @import.valid_date_formats_with_previewdiff --git a/app/controllers/import/rows_controller.rb b/app/controllers/import/rows_controller.rbindex ecab770bc1..81233e552f 100644--- a/app/controllers/import/rows_controller.rb+++ b/app/controllers/import/rows_controller.rb@@ -1,5 +1,8 @@class Import::RowsController < ApplicationController+ include ImportGuestGuardable+before_action :set_import_row+ before_action :require_import_editable!, only: :updatedef update@row.update_and_sync(row_params)diff --git a/app/controllers/import/uploads_controller.rb b/app/controllers/import/uploads_controller.rbindex d777718179..d3cad01b32 100644--- a/app/controllers/import/uploads_controller.rb+++ b/app/controllers/import/uploads_controller.rb@@ -1,7 +1,10 @@class Import::UploadsController < ApplicationController+ include ImportGuestGuardable+layout "imports"before_action :set_import+ before_action :require_import_editable!, only: :updatedef showenddiff --git a/app/controllers/imports_controller.rb b/app/controllers/imports_controller.rbindex 0314be0d41..0baa11f14f 100644--- a/app/controllers/imports_controller.rb+++ b/app/controllers/imports_controller.rb@@ -3,7 +3,10 @@ class ImportsController < ApplicationControllerbefore_action :set_import, only: %i[show update publish destroy revert apply_template cancel summary]before_action :require_statement_import_permission!, only: %i[update publish destroy revert apply_template cancel]- before_action :require_writable_import_accounts!, only: %i[publish revert]+ before_action :require_writable_import_accounts!, only: %i[publish revert destroy]+ before_action :reject_guest_family_config_import!, except: %i[index new show summary]+ before_action :require_guest_writable_created_accounts!, only: %i[revert destroy]+ before_action :reject_guest_new_categories_or_tags!, only: :publishdef update# Handle both pdf_import[account_id] and import[account_id] param formats@@ -181,6 +184,41 @@ class ImportsController < ApplicationControllerparams.require(:import).permit(:import_file)end+ def reject_guest_family_config_import!+ type = @import ? @import.type : params.dig(:import, :type).to_s+ require_non_guest! if Import::FAMILY_CONFIG_TYPES.include?(type)+ end++ # Reverting or deleting an import also destroys the accounts it created,+ # which require_writable_import_accounts! leaves out. Guests are read-only+ # by design, so they may only do that to accounts they can write.+ def require_guest_writable_created_accounts!+ return unless Current.user.guest?++ created_ids = @import.accounts.pluck(:id)+ writable_ids = Current.family.accounts.writable_by(Current.user).where(id: created_ids).pluck(:id)+ return if (created_ids - writable_ids).empty?++ deny_import_change(t("accounts.not_authorized"), fallback: imports_path)+ end++ # Categories and tags are family-wide configuration, so a guest's import+ # must map every category and tag to an existing one (or leave it empty).+ def reject_guest_new_categories_or_tags!+ return unless Current.user.guest? && @import.creates_categories_or_tags?++ deny_import_change(t("imports.publish.guest_new_categories_or_tags"), fallback: import_confirm_path(@import))+ end++ def deny_import_change(alert, fallback:)+ respond_to do |format|+ format.html { redirect_back_or_to fallback, alert: alert }+ format.turbo_stream { head :forbidden }+ format.json { head :forbidden }+ format.any { head :forbidden }+ end+ end+def require_statement_import_permission!return if @import.account_statement.blank? || @import.account_statement.manageable_by?(Current.user)@@ -189,8 +227,9 @@ class ImportsController < ApplicationControllerend# Imports are family-scoped, so a member can reach an import configured by- # someone else, or one whose target account was unshared since. Publishing- # or reverting writes into its accounts, so require write access to all.+ # someone else, or one whose target account was unshared since. Publishing,+ # reverting or deleting writes into its accounts, so require write access+ # to all.def require_writable_import_accounts!return if @import.accounts_writable_by?(Current.user)diff --git a/app/controllers/transaction_categories_controller.rb b/app/controllers/transaction_categories_controller.rbindex a0c1593560..b734af281f 100644--- a/app/controllers/transaction_categories_controller.rb+++ b/app/controllers/transaction_categories_controller.rb@@ -58,6 +58,7 @@ class TransactionCategoriesController < ApplicationControllerenddef needs_rule_notification?(transaction)+ return false if Current.user.guest?return false if Current.user.rule_prompts_disabledreturn false if transaction.category_id.blank?diff --git a/app/controllers/transactions_controller.rb b/app/controllers/transactions_controller.rbindex 3487845b94..3e1f590541 100644--- a/app/controllers/transactions_controller.rb+++ b/app/controllers/transactions_controller.rb@@ -610,6 +610,7 @@ class TransactionsController < ApplicationControllerenddef needs_rule_notification?(transaction)+ return false if Current.user.guest?return false if Current.user.rule_prompts_disabledif Current.user.rule_prompt_dismissed_at.present?diff --git a/app/models/assistant.rb b/app/models/assistant.rbindex f12c2f39f9..b8dae18dc7 100644--- a/app/models/assistant.rb+++ b/app/models/assistant.rb@@ -35,6 +35,15 @@ module AssistantFunction::RecordBillPayment].freeze+ # Guests may view family configuration but not change it, so these tools are+ # left out of a guest's registry (chat and MCP alike).+ FAMILY_CONFIG_FUNCTION_CLASSES = [+ Function::CreateTag,+ Function::UpdateTag,+ Function::CreateCategory,+ Function::UpdateCategory+ ].freeze+class << selfdef for_chat(chat)implementation_for(chat).for_chat(chat)@@ -87,6 +96,7 @@ module Assistant]classes += PREVIEW_FUNCTION_CLASSES if user&.preview_features_enabled?+ classes -= FAMILY_CONFIG_FUNCTION_CLASSES if user&.guest?classesenddiff --git a/app/models/import.rb b/app/models/import.rbindex 4ea1f277b3..0f05a6fcd3 100644--- a/app/models/import.rb+++ b/app/models/import.rb@@ -40,6 +40,9 @@ class Import < ApplicationRecordDOCUMENT_TYPES = %w[bank_statement credit_card_statement investment_statement financial_document contract other].freezeTYPES = %w[TransactionImport TradeImport AccountImport MintImport ActualImport YnabImport CategoryImport RuleImport MerchantImport PdfImport QifImport SureImport].freeze+ # Imports that write family-wide configuration (categories, tags, rules,+ # merchants), which guests may view but not change.+ FAMILY_CONFIG_TYPES = %w[CategoryImport MerchantImport RuleImport SureImport].freezeSIGNAGE_CONVENTIONS = %w[inflows_positive inflows_negative]SEPARATORS = [ [ "Comma (,)", "," ], [ "Semicolon (;)", ";" ] ].freeze@@ -472,6 +475,24 @@ class Import < ApplicationRecordcomplete? || revert_failed?end+ def family_config?+ FAMILY_CONFIG_TYPES.include?(type)+ end++ # Guests may view family configuration but not change it, so they may only+ # edit an import that doesn't write family configuration and isn't being+ # (or already) published.+ def editable_by?(user)+ return true unless user&.guest?++ (pending? || failed?) && !family_config?+ end++ # Whether publishing would add new categories or tags to the family.+ def creates_categories_or_tags?+ mappings.creational.where(type: %w[Import::CategoryMapping Import::TagMapping]).where.not(key: [ nil, "" ]).exists?+ end+# Whether the user may write to every pre-existing account this import# targets (its own account, account mappings) or has already written into# (entries). Accounts the import created itself are excluded.diff --git a/app/views/budget_categories/_no_categories.html.erb b/app/views/budget_categories/_no_categories.html.erbindex ed949d2d92..f785222891 100644--- a/app/views/budget_categories/_no_categories.html.erb+++ b/app/views/budget_categories/_no_categories.html.erb@@ -5,19 +5,21 @@<%= t(".no_categories_message") %></p>- <div class="flex items-center gap-2">- <%= render DS::Button.new(- text: t(".use_defaults"),- href: bootstrap_categories_path,- ) %>+ <% unless Current.user&.guest? %>+ <div class="flex items-center gap-2">+ <%= render DS::Button.new(+ text: t(".use_defaults"),+ href: bootstrap_categories_path,+ ) %>- <%= render DS::Link.new(- text: t(".new_category"),- variant: "outline",- icon: "plus",- href: new_category_path,- frame: :modal,- ) %>- </div>+ <%= render DS::Link.new(+ text: t(".new_category"),+ variant: "outline",+ icon: "plus",+ href: new_category_path,+ frame: :modal,+ ) %>+ </div>+ <% end %></div></div>diff --git a/app/views/categories/_category.html.erb b/app/views/categories/_category.html.erbindex e110bfd722..7fe8ac32e2 100644--- a/app/views/categories/_category.html.erb+++ b/app/views/categories/_category.html.erb@@ -11,15 +11,17 @@<%= render partial: "categories/badge", locals: { category: category } %></div>- <div class="shrink-0" data-testid="category-actions">- <%= render DS::Menu.new do |menu| %>- <% menu.with_item(variant: "link", text: t(".edit"), icon: "pencil", href: edit_category_path(category), data: { turbo_frame: :modal }) %>+ <% unless Current.user&.guest? %>+ <div class="shrink-0" data-testid="category-actions">+ <%= render DS::Menu.new do |menu| %>+ <% menu.with_item(variant: "link", text: t(".edit"), icon: "pencil", href: edit_category_path(category), data: { turbo_frame: :modal }) %>- <% if has_transactions %>- <% menu.with_item(variant: "link", text: t(".delete"), icon: "trash-2", href: new_category_deletion_path(category), destructive: true, data: { turbo_frame: :modal }) %>- <% else %>- <% menu.with_item(variant: "button", text: t(".delete"), icon: "trash-2", href: category_path(category), method: :delete) %>+ <% if has_transactions %>+ <% menu.with_item(variant: "link", text: t(".delete"), icon: "trash-2", href: new_category_deletion_path(category), destructive: true, data: { turbo_frame: :modal }) %>+ <% else %>+ <% menu.with_item(variant: "button", text: t(".delete"), icon: "trash-2", href: category_path(category), method: :delete) %>+ <% end %><% end %>- <% end %>- </div>+ </div>+ <% end %></div>diff --git a/app/views/categories/index.html.erb b/app/views/categories/index.html.erbindex e9dc57d16e..222f95e445 100644--- a/app/views/categories/index.html.erb+++ b/app/views/categories/index.html.erb@@ -1,28 +1,30 @@<%= content_for :page_title, t(".categories") %><%= content_for :page_actions do %>- <%= render DS::Menu.new do |menu| %>- <% menu.with_item(- variant: "link",- text: t(".merge"),- href: merge_categories_path,- frame: :modal,- icon: "combine") %>- <% menu.with_item(- variant: "button",- text: t(".delete_all"),- href: destroy_all_categories_path,- method: :delete,- icon: "trash-2",- confirm: CustomConfirm.for_resource_deletion("all categories", high_severity: true)) %>- <% end %>+ <% unless Current.user&.guest? %>+ <%= render DS::Menu.new do |menu| %>+ <% menu.with_item(+ variant: "link",+ text: t(".merge"),+ href: merge_categories_path,+ frame: :modal,+ icon: "combine") %>+ <% menu.with_item(+ variant: "button",+ text: t(".delete_all"),+ href: destroy_all_categories_path,+ method: :delete,+ icon: "trash-2",+ confirm: CustomConfirm.for_resource_deletion("all categories", high_severity: true)) %>+ <% end %>- <%= render DS::Link.new(- text: t(".new"),- variant: "primary",- icon: "plus",- href: new_category_path,- frame: :modal- ) %>+ <%= render DS::Link.new(+ text: t(".new"),+ variant: "primary",+ icon: "plus",+ href: new_category_path,+ frame: :modal+ ) %>+ <% end %><% end %><div class="bg-container rounded-xl shadow-border-xs p-4">@@ -39,20 +41,22 @@<div class="flex justify-center items-center py-20"><div class="text-center flex flex-col items-center max-w-[500px]"><p class="text-sm text-secondary mb-4"><%= t(".empty") %></p>- <div class="flex items-center gap-2">- <%= render DS::Button.new(- text: t(".bootstrap"),- href: bootstrap_categories_path,- ) %>+ <% unless Current.user&.guest? %>+ <div class="flex items-center gap-2">+ <%= render DS::Button.new(+ text: t(".bootstrap"),+ href: bootstrap_categories_path,+ ) %>- <%= render DS::Link.new(- text: t(".new"),- variant: "outline",- icon: "plus",- href: new_category_path,- frame: :modal- ) %>- </div>+ <%= render DS::Link.new(+ text: t(".new"),+ variant: "outline",+ icon: "plus",+ href: new_category_path,+ frame: :modal+ ) %>+ </div>+ <% end %></div></div><% end %>diff --git a/app/views/category/dropdowns/_row.html.erb b/app/views/category/dropdowns/_row.html.erbindex 0a0f41e540..a0ae6171f7 100644--- a/app/views/category/dropdowns/_row.html.erb+++ b/app/views/category/dropdowns/_row.html.erb@@ -31,8 +31,10 @@<%= render partial: "categories/badge", locals: { category: category } %><% end %>- <%= render DS::Menu.new(variant: :icon_sm) do |menu| %>- <% menu.with_item(variant: "link", text: t(".edit"), icon: "pencil-line", href: edit_category_path(category), data: { turbo_frame: :modal }) %>- <% menu.with_item(variant: "link", text: t(".delete"), icon: "trash-2", href: new_category_deletion_path(category), data: { turbo_frame: :modal }, destructive: true) %>+ <% unless Current.user&.guest? %>+ <%= render DS::Menu.new(variant: :icon_sm) do |menu| %>+ <% menu.with_item(variant: "link", text: t(".edit"), icon: "pencil-line", href: edit_category_path(category), data: { turbo_frame: :modal }) %>+ <% menu.with_item(variant: "link", text: t(".delete"), icon: "trash-2", href: new_category_deletion_path(category), data: { turbo_frame: :modal }, destructive: true) %>+ <% end %><% end %><% end %>diff --git a/app/views/category/dropdowns/show.html.erb b/app/views/category/dropdowns/show.html.erbindex 6973ac52d6..b02cf0cb23 100644--- a/app/views/category/dropdowns/show.html.erb+++ b/app/views/category/dropdowns/show.html.erb@@ -1,7 +1,7 @@<%= turbo_frame_tag "category_dropdown" do %><%# Creating is only offered where the category can then be assigned: a user who %><%# can't annotate this account would get a new category the assignment then rejects. %>- <% can_create = can_annotate_account?(@transaction.entry.account) %>+ <% can_create = can_annotate_account?(@transaction.entry.account) && !Current.user&.guest? %><div class="flex flex-col relative"data-controller="list-filter category-quick-create"data-category-quick-create-create-url-value="<%= categories_path(format: :json) %>"@@ -84,12 +84,14 @@<div class="text-center flex flex-col items-center max-w-[500px]"><p class="text-sm text-secondary font-normal mb-4"><%= t(".empty") %></p>- <%= render DS::Button.new(- text: t(".bootstrap"),- variant: "outline",- href: bootstrap_categories_path,- method: :post,- data: { turbo_frame: :_top }) %>+ <% unless Current.user&.guest? %>+ <%= render DS::Button.new(+ text: t(".bootstrap"),+ variant: "outline",+ href: bootstrap_categories_path,+ method: :post,+ data: { turbo_frame: :_top }) %>+ <% end %></div></div><% end %>diff --git a/app/views/family_merchants/_family_merchant.html.erb b/app/views/family_merchants/_family_merchant.html.erbindex d1c6f8be38..bea7b683a7 100644--- a/app/views/family_merchants/_family_merchant.html.erb+++ b/app/views/family_merchants/_family_merchant.html.erb@@ -16,16 +16,18 @@</p></div></td>- <td class="py-3 px-4 text-sm text-right align-middle">- <%= render DS::Menu.new do |menu| %>- <% menu.with_item(variant: "link", text: t(".edit"), href: edit_family_merchant_path(family_merchant), icon: "pencil", data: { turbo_frame: "modal" }) %>- <% menu.with_item(- variant: "button",- text: t(".delete"),- href: family_merchant_path(family_merchant),- icon: "trash-2",- method: :delete,- confirm: CustomConfirm.for_resource_deletion(family_merchant.name)) %>- <% end %>- </td>+ <% unless Current.user&.guest? %>+ <td class="py-3 px-4 text-sm text-right align-middle">+ <%= render DS::Menu.new do |menu| %>+ <% menu.with_item(variant: "link", text: t(".edit"), href: edit_family_merchant_path(family_merchant), icon: "pencil", data: { turbo_frame: "modal" }) %>+ <% menu.with_item(+ variant: "button",+ text: t(".delete"),+ href: family_merchant_path(family_merchant),+ icon: "trash-2",+ method: :delete,+ confirm: CustomConfirm.for_resource_deletion(family_merchant.name)) %>+ <% end %>+ </td>+ <% end %></tr>diff --git a/app/views/family_merchants/_provider_merchant.html.erb b/app/views/family_merchants/_provider_merchant.html.erbindex 1f33c03174..53773ce58d 100644--- a/app/views/family_merchants/_provider_merchant.html.erb+++ b/app/views/family_merchants/_provider_merchant.html.erb@@ -21,21 +21,23 @@<td class="py-3 px-4 text-sm text-secondary align-middle"><%= provider_merchant.source&.titleize %></td>- <td class="py-3 px-4 text-sm text-right align-middle">- <%= render DS::Menu.new do |menu| %>- <% menu.with_item(variant: "link", text: t(".edit"), href: edit_family_merchant_path(provider_merchant), icon: "pencil", data: { turbo_frame: "modal" }) %>- <% menu.with_item(- variant: "button",- text: t(".remove"),- href: family_merchant_path(provider_merchant),- icon: "trash-2",- method: :delete,- confirm: CustomConfirm.new(- destructive: true,- title: t(".remove_confirm_title"),- body: t(".remove_confirm_body", name: provider_merchant.name),- btn_text: t(".remove")- )) %>- <% end %>- </td>+ <% unless Current.user&.guest? %>+ <td class="py-3 px-4 text-sm text-right align-middle">+ <%= render DS::Menu.new do |menu| %>+ <% menu.with_item(variant: "link", text: t(".edit"), href: edit_family_merchant_path(provider_merchant), icon: "pencil", data: { turbo_frame: "modal" }) %>+ <% menu.with_item(+ variant: "button",+ text: t(".remove"),+ href: family_merchant_path(provider_merchant),+ icon: "trash-2",+ method: :delete,+ confirm: CustomConfirm.new(+ destructive: true,+ title: t(".remove_confirm_title"),+ body: t(".remove_confirm_body", name: provider_merchant.name),+ btn_text: t(".remove")+ )) %>+ <% end %>+ </td>+ <% end %></tr>diff --git a/app/views/family_merchants/index.html.erb b/app/views/family_merchants/index.html.erbindex 0eb163a406..1301cbef18 100644--- a/app/views/family_merchants/index.html.erb+++ b/app/views/family_merchants/index.html.erb@@ -1,26 +1,28 @@<%= content_for :page_title, t(".title") %><%= content_for :page_actions do %>- <%= render DS::Menu.new do |menu| %>- <% menu.with_item(- variant: "link",- text: t(".merge"),- href: merge_family_merchants_path,- frame: :modal,- icon: "combine") %>- <% menu.with_item(- variant: "link",- text: t(".import"),- href: new_import_path(type: "MerchantImport"),- frame: :modal,- icon: "upload") %>+ <% unless Current.user&.guest? %>+ <%= render DS::Menu.new do |menu| %>+ <% menu.with_item(+ variant: "link",+ text: t(".merge"),+ href: merge_family_merchants_path,+ frame: :modal,+ icon: "combine") %>+ <% menu.with_item(+ variant: "link",+ text: t(".import"),+ href: new_import_path(type: "MerchantImport"),+ frame: :modal,+ icon: "upload") %>+ <% end %>+ <%= render DS::Link.new(+ text: t(".new"),+ variant: "primary",+ icon: "plus",+ href: new_family_merchant_path,+ frame: :modal+ ) %><% end %>- <%= render DS::Link.new(- text: t(".new"),- variant: "primary",- icon: "plus",- href: new_family_merchant_path,- frame: :modal- ) %><% end %><div class="bg-container rounded-xl shadow-border-xs p-4 space-y-6">@@ -55,7 +57,9 @@<thead><tr class="text-xs uppercase font-medium text-secondary border-b border-divider"><th scope="col" class="text-left py-3 px-4"><%= t(".table.merchant") %></th>- <th scope="col" class="text-right py-3 px-4"><%= t(".table.actions") %></th>+ <% unless Current.user&.guest? %>+ <th scope="col" class="text-right py-3 px-4"><%= t(".table.actions") %></th>+ <% end %></tr></thead><tbody>@@ -79,12 +83,14 @@<div class="text-center flex flex-col items-center max-w-[300px]"><p class="text-primary mb-1 font-medium text-sm"><%= t(".family_empty", moniker: family_moniker_downcase) %></p>- <%= render DS::Link.new(- text: t(".new"),- icon: "plus",- href: new_family_merchant_path,- frame: :modal- ) %>+ <% unless Current.user&.guest? %>+ <%= render DS::Link.new(+ text: t(".new"),+ icon: "plus",+ href: new_family_merchant_path,+ frame: :modal+ ) %>+ <% end %></div></div><% end %>@@ -104,7 +110,7 @@</div></div>- <% if @enhanceable_count > 0 && @llm_available %>+ <% if @enhanceable_count > 0 && @llm_available && !Current.user&.guest? %><div class="flex items-center justify-between p-4 bg-container-inset border border-secondary rounded-lg"><div class="flex items-start gap-2"><%= icon "sparkles", class: "w-5 h-5 text-link mt-0.5 flex-shrink-0" %>@@ -145,7 +151,9 @@<tr class="text-xs uppercase font-medium text-secondary border-b border-divider"><th scope="col" class="text-left py-3 px-4"><%= t(".table.merchant") %></th><th scope="col" class="text-left py-3 px-4"><%= t(".table.source") %></th>- <th scope="col" class="text-right py-3 px-4"><%= t(".table.actions") %></th>+ <% unless Current.user&.guest? %>+ <th scope="col" class="text-right py-3 px-4"><%= t(".table.actions") %></th>+ <% end %></tr></thead><tbody>@@ -193,7 +201,9 @@<tr class="text-xs uppercase font-medium text-secondary border-b border-divider"><th scope="col" class="text-left py-3 px-4"><%= t(".table.merchant") %></th><th scope="col" class="text-left py-3 px-4"><%= t(".table.source") %></th>- <th scope="col" class="text-right py-3 px-4"><%= t(".table.actions") %></th>+ <% unless Current.user&.guest? %>+ <th scope="col" class="text-right py-3 px-4"><%= t(".table.actions") %></th>+ <% end %></tr></thead><tbody>diff --git a/app/views/imports/_revert_failure.html.erb b/app/views/imports/_revert_failure.html.erbindex 67468c4dc4..e4f12dfa84 100644--- a/app/views/imports/_revert_failure.html.erb+++ b/app/views/imports/_revert_failure.html.erb@@ -11,11 +11,13 @@<p class="text-sm text-secondary"><%= t(".description") %></p></div>- <%= render DS::Button.new(- text: t(".try_again"),- full_width: true,- href: revert_import_path(import),- method: :put- ) %>+ <% unless Current.user&.guest? && import.family_config? %>+ <%= render DS::Button.new(+ text: t(".try_again"),+ full_width: true,+ href: revert_import_path(import),+ method: :put+ ) %>+ <% end %></div></div>diff --git a/app/views/imports/index.html.erb b/app/views/imports/index.html.erbindex a8f6c6e70a..7ae48c95d2 100644--- a/app/views/imports/index.html.erb+++ b/app/views/imports/index.html.erb@@ -74,7 +74,9 @@</td><td class="px-2 py-3 text-right"><div class="flex items-center justify-end gap-2">- <% if import.complete? || import.revert_failed? %>+ <% if Current.user&.guest? && import.family_config? %>+ <%# Guests may view family configuration imports but not undo or delete them. %>+ <% elsif import.complete? || import.revert_failed? %><%= button_to revert_import_path(import),method: :put,class: "flex items-center gap-2",diff --git a/app/views/imports/new.html.erb b/app/views/imports/new.html.erbindex 495e107218..121760437a 100644--- a/app/views/imports/new.html.erb+++ b/app/views/imports/new.html.erb@@ -63,7 +63,7 @@</li><% end %>- <% if params[:type].nil? || params[:type] == "SureImport" %>+ <% if (params[:type].nil? || params[:type] == "SureImport") && !Current.user&.guest? %><li><%= styled_form_with url: imports_path, scope: :import, multipart: true, class: "w-full", data: { turbo_frame: "_top" } do |form| %><%= form.hidden_field :type, value: "SureImport" %>@@ -222,7 +222,8 @@icon_bg_class: "bg-blue-500/5",icon_text_class: "text-blue-500",label: t(".import_categories"),- enabled: true %>+ enabled: !Current.user&.guest?,+ disabled_message: t("shared.require_non_guest") %><% end %><% if params[:type].nil? || params[:type] == "MerchantImport" %>@@ -232,7 +233,8 @@icon_bg_class: "bg-orange-500/5",icon_text_class: "text-orange-500",label: t(".import_merchants"),- enabled: true %>+ enabled: !Current.user&.guest?,+ disabled_message: t("shared.require_non_guest") %><% end %><% if params[:type].nil? || params[:type] == "RuleImport" %>@@ -242,7 +244,8 @@icon_bg_class: "bg-green-500/5",icon_text_class: "text-green-500",label: t(".import_rules"),- enabled: true %>+ enabled: !Current.user&.guest?,+ disabled_message: t("shared.require_non_guest") %><% end %></ul></div>diff --git a/app/views/rules/_rule.html.erb b/app/views/rules/_rule.html.erbindex ff645fb351..7cbb7773f9 100644--- a/app/views/rules/_rule.html.erb+++ b/app/views/rules/_rule.html.erb@@ -71,18 +71,20 @@</div><div class="flex items-center gap-4"><%= styled_form_with model: rule, namespace: "rule_#{rule.id}", data: { controller: "auto-submit-form" } do |f| %>- <%= f.toggle :active, { data: { auto_submit_form_target: "auto" } } %>+ <%= f.toggle :active, { disabled: Current.user&.guest?, data: { auto_submit_form_target: "auto" } } %><% end %>- <%= render DS::Menu.new do |menu| %>- <% menu.with_item(variant: "link", text: t(".edit"), href: edit_rule_path(rule), icon: "pencil", data: { turbo_frame: "modal" }) %>- <% menu.with_item(variant: "link", text: t(".re_apply_rule"), href: confirm_rule_path(rule), icon: "refresh-cw", data: { turbo_frame: "modal" }) %>- <% menu.with_item(- variant: "button",- text: t(".delete"),- href: rule_path(rule),- icon: "trash-2",- method: :delete,- confirm: CustomConfirm.for_resource_deletion("rule")) %>+ <% unless Current.user&.guest? %>+ <%= render DS::Menu.new do |menu| %>+ <% menu.with_item(variant: "link", text: t(".edit"), href: edit_rule_path(rule), icon: "pencil", data: { turbo_frame: "modal" }) %>+ <% menu.with_item(variant: "link", text: t(".re_apply_rule"), href: confirm_rule_path(rule), icon: "refresh-cw", data: { turbo_frame: "modal" }) %>+ <% menu.with_item(+ variant: "button",+ text: t(".delete"),+ href: rule_path(rule),+ icon: "trash-2",+ method: :delete,+ confirm: CustomConfirm.for_resource_deletion("rule")) %>+ <% end %><% end %></div></div>diff --git a/app/views/rules/index.html.erb b/app/views/rules/index.html.erbindex 235a6812e0..0593aafca2 100644--- a/app/views/rules/index.html.erb+++ b/app/views/rules/index.html.erb@@ -1,41 +1,43 @@<%= content_for :page_title, t(".page_title") %><%= content_for :page_actions do %>- <% if @rules.any? %>- <%= render DS::Menu.new do |menu| %>- <% menu.with_item(- variant: "button",- text: t("rules.clear_ai_cache.button"),- href: clear_ai_cache_rules_path,- icon: "refresh-cw",- method: :post,- confirm: CustomConfirm.new(- title: t("rules.clear_ai_cache.confirm_title"),- body: t("rules.clear_ai_cache.confirm_body"),- btn_text: t("rules.clear_ai_cache.confirm_button")- )) %>- <% menu.with_item(- variant: "button",- text: t(".delete_all_rules"),- href: destroy_all_rules_path,- icon: "trash-2",- method: :delete,- confirm: CustomConfirm.for_resource_deletion("all rules", high_severity: true)) %>+ <% unless Current.user&.guest? %>+ <% if @rules.any? %>+ <%= render DS::Menu.new do |menu| %>+ <% menu.with_item(+ variant: "button",+ text: t("rules.clear_ai_cache.button"),+ href: clear_ai_cache_rules_path,+ icon: "refresh-cw",+ method: :post,+ confirm: CustomConfirm.new(+ title: t("rules.clear_ai_cache.confirm_title"),+ body: t("rules.clear_ai_cache.confirm_body"),+ btn_text: t("rules.clear_ai_cache.confirm_button")+ )) %>+ <% menu.with_item(+ variant: "button",+ text: t(".delete_all_rules"),+ href: destroy_all_rules_path,+ icon: "trash-2",+ method: :delete,+ confirm: CustomConfirm.for_resource_deletion("all rules", high_severity: true)) %>+ <% end %>+ <%= render DS::Link.new(+ text: t("rules.apply_all.button"),+ variant: "secondary",+ href: confirm_all_rules_path,+ icon: "play",+ frame: :modal+ ) %><% end %><%= render DS::Link.new(- text: t("rules.apply_all.button"),- variant: "secondary",- href: confirm_all_rules_path,- icon: "play",+ text: t(".new_rule"),+ variant: "primary",+ href: new_rule_path(resource_type: "transaction"),+ icon: "plus",frame: :modal) %><% end %>- <%= render DS::Link.new(- text: t(".new_rule"),- variant: "primary",- href: new_rule_path(resource_type: "transaction"),- icon: "plus",- frame: :modal- ) %><% end %><% if self_hosted? %>@@ -94,15 +96,17 @@<div class="text-center flex flex-col items-center max-w-[500px]"><p class="text-sm text-primary font-medium mb-1"><%= t(".no_rules_title") %></p><p class="text-sm text-secondary mb-4"><%= t(".no_rules_description") %></p>- <div class="flex items-center gap-2">- <%= render DS::Link.new(- text: t(".new_rule"),- variant: "primary",- href: new_rule_path(resource_type: "transaction"),- icon: "plus",- frame: :modal- ) %>- </div>+ <% unless Current.user&.guest? %>+ <div class="flex items-center gap-2">+ <%= render DS::Link.new(+ text: t(".new_rule"),+ variant: "primary",+ href: new_rule_path(resource_type: "transaction"),+ icon: "plus",+ frame: :modal+ ) %>+ </div>+ <% end %></div></div><% end %>diff --git a/app/views/tags/_tag.html.erb b/app/views/tags/_tag.html.erbindex 6c1a0a560e..65b280445a 100644--- a/app/views/tags/_tag.html.erb+++ b/app/views/tags/_tag.html.erb@@ -6,28 +6,30 @@<%= render partial: "tags/badge", locals: { tag: tag } %></p></div>- <div class="justify-self-end">- <%= render DS::Menu.new do |menu| %>- <% menu.with_item(variant: "link", text: t(".edit"), href: edit_tag_path(tag), icon: "pencil", data: { turbo_frame: "modal" }) %>+ <% unless Current.user&.guest? %>+ <div class="justify-self-end">+ <%= render DS::Menu.new do |menu| %>+ <% menu.with_item(variant: "link", text: t(".edit"), href: edit_tag_path(tag), icon: "pencil", data: { turbo_frame: "modal" }) %>- <% if tag.transactions.any? %>- <% menu.with_item(- variant: "link",- text: t(".delete"),- href: new_tag_deletion_path(tag),- icon: "trash-2",- frame: :modal- ) %>- <% else %>- <% menu.with_item(- variant: "button",- text: t(".delete"),- href: tag_path(tag),- icon: "trash-2",- method: :delete,- confirm: CustomConfirm.for_resource_deletion(tag.name)- ) %>+ <% if tag.transactions.any? %>+ <% menu.with_item(+ variant: "link",+ text: t(".delete"),+ href: new_tag_deletion_path(tag),+ icon: "trash-2",+ frame: :modal+ ) %>+ <% else %>+ <% menu.with_item(+ variant: "button",+ text: t(".delete"),+ href: tag_path(tag),+ icon: "trash-2",+ method: :delete,+ confirm: CustomConfirm.for_resource_deletion(tag.name)+ ) %>+ <% end %><% end %>- <% end %>- </div>+ </div>+ <% end %></div>diff --git a/app/views/tags/index.html.erb b/app/views/tags/index.html.erbindex e2067dba75..ba30078ac6 100644--- a/app/views/tags/index.html.erb+++ b/app/views/tags/index.html.erb@@ -1,22 +1,24 @@<%= content_for :page_title, t(".tags") %><%= content_for :page_actions do %>- <%= render DS::Menu.new do |menu| %>- <% menu.with_item(- variant: "button",- text: t(".delete_all"),- href: destroy_all_tags_path,- method: :delete,- icon: "trash-2",- confirm: CustomConfirm.for_resource_deletion("all tags", high_severity: true)) %>- <% end %>+ <% unless Current.user&.guest? %>+ <%= render DS::Menu.new do |menu| %>+ <% menu.with_item(+ variant: "button",+ text: t(".delete_all"),+ href: destroy_all_tags_path,+ method: :delete,+ icon: "trash-2",+ confirm: CustomConfirm.for_resource_deletion("all tags", high_severity: true)) %>+ <% end %>- <%= render DS::Link.new(- text: t(".new"),- variant: "primary",- href: new_tag_path,- icon: "plus",- frame: :modal- ) %>+ <%= render DS::Link.new(+ text: t(".new"),+ variant: "primary",+ href: new_tag_path,+ icon: "plus",+ frame: :modal+ ) %>+ <% end %><% end %><div class="bg-container rounded-xl shadow-border-xs p-4">@@ -39,12 +41,14 @@<div class="text-center flex flex-col items-center max-w-[300px]"><p class="text-primary mb-1 font-medium text-sm"><%= t(".empty") %></p>- <%= render DS::Link.new(- text: t(".new"),- icon: "plus",- href: new_tag_path,- frame: :modal- ) %>+ <% unless Current.user&.guest? %>+ <%= render DS::Link.new(+ text: t(".new"),+ icon: "plus",+ href: new_tag_path,+ frame: :modal+ ) %>+ <% end %></div></div><% end %>diff --git a/app/views/transactions/index.html.erb b/app/views/transactions/index.html.erbindex c00cbef9f9..49f68c131a 100644--- a/app/views/transactions/index.html.erb+++ b/app/views/transactions/index.html.erb@@ -4,7 +4,9 @@<div class="flex items-center gap-5"><div class="flex items-center gap-2"><%= render DS::Menu.new do |menu| %>- <% menu.with_item(variant: "link", text: t(".new_rule"), href: new_rule_path(resource_type: "transaction"), icon: "plus", data: { turbo_frame: :modal }) %>+ <% unless Current.user&.guest? %>+ <% menu.with_item(variant: "link", text: t(".new_rule"), href: new_rule_path(resource_type: "transaction"), icon: "plus", data: { turbo_frame: :modal }) %>+ <% end %><% menu.with_item(variant: "link", text: t(".edit_rules"), href: rules_path, icon: "git-branch", data: { turbo_frame: :_top }) %><% menu.with_item(variant: "link", text: t(".edit_categories"), href: categories_path, icon: "shapes", data: { turbo_frame: :_top }) %><% menu.with_item(variant: "link", text: t(".edit_tags"), href: tags_path, icon: "tags", data: { turbo_frame: :_top }) %>diff --git a/config/locales/views/imports/de.yml b/config/locales/views/imports/de.ymlindex 526190b731..e9307a08bf 100644--- a/config/locales/views/imports/de.yml+++ b/config/locales/views/imports/de.yml@@ -447,6 +447,7 @@ de:category: Kategorietag: Tagpublish:+ guest_new_categories_or_tags: Gäste können keine Kategorien oder Tags anlegen. Ordne sie bestehenden zu oder lass sie leer.max_rows_exceeded: Dein Import überschreitet die Höchstzahl von %{max} Zeilen.started: Dein Import läuft jetzt im Hintergrund.revert:diff --git a/config/locales/views/imports/en.yml b/config/locales/views/imports/en.ymlindex 2225926b8a..e160120c13 100644--- a/config/locales/views/imports/en.yml+++ b/config/locales/views/imports/en.yml@@ -310,6 +310,7 @@ en:publish:started: "Your import has started in the background."max_rows_exceeded: "Your import exceeds the maximum row count of %{max}."+ guest_new_categories_or_tags: "Guests can't create categories or tags. Map them to existing ones or leave them empty."revert:started: "Import is reverting in the background."cancel:diff --git a/config/locales/views/shared/de.yml b/config/locales/views/shared/de.ymlindex 5ef4b33415..7da19c0706 100644--- a/config/locales/views/shared/de.yml+++ b/config/locales/views/shared/de.yml@@ -31,6 +31,7 @@ de:preview: Vorschaudot_separator: "·"require_admin: Das dürfen nur Admins+ require_non_guest: Gäste können diese Aktion nicht ausführensync_toast:message: Neue Daten verfügbarrefresh: Aktualisierendiff --git a/docs/api/openapi.yaml b/docs/api/openapi.yamlindex 1c81880528..7f79de449e 100644--- a/docs/api/openapi.yaml+++ b/docs/api/openapi.yaml@@ -5692,7 +5692,7 @@ paths:schema:"$ref": "#/components/schemas/ErrorResponse"'403':- description: forbidden - api key missing read_write scope+ description: forbidden - api key missing read_write scope, or guest usercontent:application/json:schema:@@ -7207,7 +7207,7 @@ paths:schema:"$ref": "#/components/schemas/ErrorResponse"'403':- description: insufficient scope+ description: insufficient scope or guest usercontent:application/json:schema:@@ -7519,6 +7519,14 @@ paths:application/json:schema:"$ref": "#/components/schemas/ImportResponse"+ '403':+ description: guest users cannot import family configuration+ content:+ application/json:+ schema:+ oneOf:+ - "$ref": "#/components/schemas/ErrorResponse"+ - "$ref": "#/components/schemas/ErrorResponseWithImportId"'422':description: validation error or publish rejectioncontent:@@ -8170,6 +8178,12 @@ paths:application/json:schema:"$ref": "#/components/schemas/ErrorResponse"+ '403':+ description: guest users cannot import merchants+ content:+ application/json:+ schema:+ "$ref": "#/components/schemas/ErrorResponse"'422':description: missing file or invalid CSVcontent:@@ -9393,6 +9407,12 @@ paths:application/json:schema:"$ref": "#/components/schemas/TagDetail"+ '403':+ description: guest users cannot modify tags+ content:+ application/json:+ schema:+ "$ref": "#/components/schemas/ErrorResponse"'422':description: validation error - missing namecontent:diff --git a/spec/requests/api/v1/categories_spec.rb b/spec/requests/api/v1/categories_spec.rbindex d74e76b564..e67802dc35 100644--- a/spec/requests/api/v1/categories_spec.rb+++ b/spec/requests/api/v1/categories_spec.rb@@ -147,7 +147,7 @@ RSpec.describe 'API V1 Categories', type: :request dorun_test!end- response '403', 'forbidden - api key missing read_write scope' do+ response '403', 'forbidden - api key missing read_write scope, or guest user' doschema '$ref' => '#/components/schemas/ErrorResponse'let(:read_only_api_key) dodiff --git a/spec/requests/api/v1/import_sessions_spec.rb b/spec/requests/api/v1/import_sessions_spec.rbindex 02c9af9f94..96ff3e95a0 100644--- a/spec/requests/api/v1/import_sessions_spec.rb+++ b/spec/requests/api/v1/import_sessions_spec.rb@@ -92,7 +92,7 @@ RSpec.describe 'API V1 Import Sessions', type: :request dorun_test!end- response '403', 'insufficient scope' do+ response '403', 'insufficient scope or guest user' doschema '$ref' => '#/components/schemas/ErrorResponse'let(:'X-Api-Key') { api_key_without_write_scope.plain_key }diff --git a/spec/requests/api/v1/imports_spec.rb b/spec/requests/api/v1/imports_spec.rbindex 490af3da1d..abb4bab8d1 100644--- a/spec/requests/api/v1/imports_spec.rb+++ b/spec/requests/api/v1/imports_spec.rb@@ -250,6 +250,25 @@ RSpec.describe 'API V1 Imports', type: :request dorun_test!end+ response '403', 'guest users cannot import family configuration' do+ schema oneOf: [+ { '$ref' => '#/components/schemas/ErrorResponse' },+ { '$ref' => '#/components/schemas/ErrorResponseWithImportId' }+ ]++ let(:user) do+ family.users.create!(+ email: '[email protected]',+ password: 'password123',+ password_confirmation: 'password123',+ role: 'guest'+ )+ end+ let(:body) { { raw_file_content: "name\nGroceries", type: 'CategoryImport' } }++ run_test!+ end+response '422', 'validation error or publish rejection' doschema oneOf: [{ '$ref' => '#/components/schemas/ErrorResponse' },diff --git a/spec/requests/api/v1/merchants_spec.rb b/spec/requests/api/v1/merchants_spec.rbindex 76ebcded41..f9ca005133 100644--- a/spec/requests/api/v1/merchants_spec.rb+++ b/spec/requests/api/v1/merchants_spec.rb@@ -85,6 +85,24 @@ RSpec.describe 'API V1 Merchants', type: :request dorun_test!end+ response '403', 'guest users cannot import merchants' do+ schema '$ref' => '#/components/schemas/ErrorResponse'++ let(:user) do+ family.users.create!(+ email: '[email protected]',+ password: 'password123',+ password_confirmation: 'password123',+ role: 'guest'+ )+ end+ let(:file) do+ Rack::Test::UploadedFile.new(StringIO.new("name\nCoffee Shop"), 'text/csv', true, original_filename: 'merchants.csv')+ end++ run_test!+ end+response '422', 'missing file or invalid CSV' doschema '$ref' => '#/components/schemas/ErrorResponse'let(:file) { nil }diff --git a/spec/requests/api/v1/tags_spec.rb b/spec/requests/api/v1/tags_spec.rbindex 947a67b6a5..021e636640 100644--- a/spec/requests/api/v1/tags_spec.rb+++ b/spec/requests/api/v1/tags_spec.rb@@ -107,6 +107,22 @@ RSpec.describe 'API V1 Tags', type: :request dorun_test!end+ response '403', 'guest users cannot modify tags' do+ schema '$ref' => '#/components/schemas/ErrorResponse'++ let(:user) do+ family.users.create!(+ email: '[email protected]',+ password: 'password123',+ password_confirmation: 'password123',+ role: 'guest'+ )+ end+ let(:body) { { tag: { name: 'Guest tag' } } }++ run_test!+ end+response '422', 'validation error - missing name' doschema '$ref' => '#/components/schemas/ErrorResponse'diff --git a/test/controllers/api/v1/guest_config_writes_test.rb b/test/controllers/api/v1/guest_config_writes_test.rbnew file mode 100644index 0000000000..79f1a664db--- /dev/null+++ b/test/controllers/api/v1/guest_config_writes_test.rb@@ -0,0 +1,83 @@+# frozen_string_literal: true++require "test_helper"++class Api::V1::GuestConfigWritesTest < ActionDispatch::IntegrationTest+ setup do+ @guest = family_guest+ @headers = { "X-Api-Key" => guest_api_key.plain_key }+ end++ test "guest api keys can still read tags" do+ get api_v1_tags_url, headers: @headers++ assert_response :success+ end++ test "guest api keys cannot create, update or delete tags" do+ tag = @guest.family.tags.create!(name: "Guest-proof #{SecureRandom.hex(4)}", color: "#3b82f6")++ assert_no_difference("Tag.count") do+ post api_v1_tags_url, params: { tag: { name: "Guest tag" } }, headers: @headers+ assert_response :forbidden++ patch api_v1_tag_url(tag), params: { tag: { name: "Renamed" } }, headers: @headers+ assert_response :forbidden++ delete api_v1_tag_url(tag), headers: @headers+ assert_response :forbidden+ end++ assert_not_equal "Renamed", tag.reload.name+ end++ test "guest api keys cannot create categories" do+ assert_no_difference("Category.count") do+ post api_v1_categories_url, params: { category: { name: "Guest category" } }, headers: @headers+ end++ assert_response :forbidden+ end++ test "guest api keys cannot import merchants" do+ file = Rack::Test::UploadedFile.new(StringIO.new("name\nGuest Shop"), "text/csv", true, original_filename: "merchants.csv")++ assert_no_difference("FamilyMerchant.count") do+ post api_v1_merchants_url, params: { file: file }, headers: @headers+ end++ assert_response :forbidden+ end++ test "guest api keys cannot create family configuration imports or import sessions" do+ assert_no_difference([ "Import.count", "ImportSession.count" ]) do+ %w[CategoryImport MerchantImport RuleImport SureImport].each do |type|+ post api_v1_imports_url, params: { type: type, raw_file_content: "name\nGuest" }, headers: @headers+ assert_response :forbidden, "#{type} should be rejected for guests"+ end++ post api_v1_import_sessions_url, params: { type: "SureImport" }, headers: @headers+ assert_response :forbidden+ end+ end++ test "guest api keys can still create transaction imports" do+ assert_difference("Import.count", 1) do+ post api_v1_imports_url, params: { type: "TransactionImport", raw_file_content: "date,amount,name\n01/15/2024,5.00,Coffee" }, headers: @headers+ end++ assert_response :created+ end++ private++ def guest_api_key+ ApiKey.create!(+ user: @guest,+ name: "Guest Key #{SecureRandom.hex(4)}",+ key: ApiKey.generate_secure_key,+ scopes: %w[read_write],+ source: "web"+ )+ end+enddiff --git a/test/controllers/imports_controller_test.rb b/test/controllers/imports_controller_test.rbindex 3b7f11661e..422fbfedb0 100644--- a/test/controllers/imports_controller_test.rb+++ b/test/controllers/imports_controller_test.rb@@ -503,6 +503,20 @@ class ImportsControllerTest < ActionDispatch::IntegrationTestassert_equal I18n.t("accounts.not_authorized"), flash[:alert]end+ test "member cannot delete import whose entries are in a read only shared account" do+ import = imports(:transaction)+ import.update!(status: :failed)+ entries(:transaction).update!(import: import, account: accounts(:credit_card))++ sign_in users(:family_member)+ assert_no_difference([ "Import.count", "Entry.count" ]) do+ delete import_url(import)+ end++ assert_redirected_to import_path(import)+ assert_equal I18n.t("accounts.not_authorized"), flash[:alert]+ end+test "member cannot publish QIF import whose embedded account matches a read only shared account" doimport = qif_import_with_embedded_account(accounts(:credit_card).name)QifImport.any_instance.expects(:publish_later).neverdiff --git a/test/controllers/transaction_categories_controller_test.rb b/test/controllers/transaction_categories_controller_test.rbindex 526c4de336..2e60292262 100644--- a/test/controllers/transaction_categories_controller_test.rb+++ b/test/controllers/transaction_categories_controller_test.rb@@ -64,6 +64,25 @@ class TransactionCategoriesControllerTest < ActionDispatch::IntegrationTestassert_not_includes response.body, "action_type=set_transaction_category"end+ test "does not offer guests to create a rule they cannot save" do+ category = categories(:income)+ @entry.update!(name: "AMZN Mktp UK")+ rule = @entry.account.family.rules.build(name: "Amazon", resource_type: "transaction", active: true)+ rule.conditions.build(condition_type: "transaction_name", operator: "like", value: "AMAZON.CO.UK")+ rule.actions.build(action_type: "set_transaction_category", value: category.id.to_s)+ rule.save!+ @entry.account.update!(owner: family_guest)+ sign_in family_guest++ patch transaction_category_url(@entry),+ params: { entry: { entryable_type: "Transaction", entryable_attributes: { id: @transaction.id, category_id: category.id } } },+ as: :turbo_stream++ assert_response :success+ assert_equal category.id, @transaction.reload.category_id+ assert_not_includes response.body, "action_type=set_transaction_category"+ end+test "a matched transfer's inflow leg does not take a category" dooutflow, inflow = create_contribution_transfercategory = categories(:income)diff --git a/test/controllers/transactions_controller_test.rb b/test/controllers/transactions_controller_test.rbindex 3559cfb063..90fb214848 100644--- a/test/controllers/transactions_controller_test.rb+++ b/test/controllers/transactions_controller_test.rb@@ -1952,6 +1952,28 @@ endassert_match I18n.t("transactions.show.category_set_on_outflow"), response.bodyend+ test "does not offer guests to create a rule through a transaction update" do+ category = categories(:income)+ @entry.update!(name: "AMZN Mktp UK")+ rule = @entry.account.family.rules.build(name: "Amazon", resource_type: "transaction", active: true)+ rule.conditions.build(condition_type: "transaction_name", operator: "like", value: "AMAZON.CO.UK")+ rule.actions.build(action_type: "set_transaction_category", value: category.id.to_s)+ rule.save!+ @entry.account.update!(owner: family_guest)+ sign_in family_guest++ patch transaction_url(@entry), params: {+ entry: {+ entryable_type: @entry.entryable_type,+ entryable_attributes: { id: @entry.entryable_id, category_id: category.id }+ }+ }, as: :turbo_stream++ assert_response :success+ assert_equal category.id, @entry.reload.entryable.category_id+ assert_not_includes response.body, "action_type=set_transaction_category"+ end+privatedef rendered_entry_idscss_select("turbo-frame[id^='entry_']").map { |node| node["id"].delete_prefix("entry_") }diff --git a/test/integration/guest_read_only_config_test.rb b/test/integration/guest_read_only_config_test.rbnew file mode 100644index 0000000000..b343ae1b98--- /dev/null+++ b/test/integration/guest_read_only_config_test.rb@@ -0,0 +1,222 @@+require "test_helper"++class GuestReadOnlyConfigTest < ActionDispatch::IntegrationTest+ setup do+ sign_in family_guest+ end++ test "guest can view family configuration" do+ [ categories_path, tags_path, rules_path, family_merchants_path ].each do |path|+ get path++ assert_response :success, "#{path} should stay readable for guests"+ end+ end++ test "guest cannot create categories, tags or merchants" do+ assert_no_difference([ "Category.count", "Tag.count", "FamilyMerchant.count" ]) do+ post categories_path, params: { category: { name: "Guest category", color: "#000000" } }+ post tags_path, params: { tag: { name: "Guest tag" } }+ post family_merchants_path, params: { family_merchant: { name: "Guest merchant" } }+ end+ end++ test "guest cannot update or delete existing configuration" do+ category = categories(:food_and_drink)+ tag = tags(:one)+ merchant = merchants(:netflix)++ patch category_path(category), params: { category: { name: "Renamed by guest" } }+ patch tag_path(tag), params: { tag: { name: "Renamed by guest" } }+ patch family_merchant_path(merchant), params: { family_merchant: { name: "Renamed by guest" } }++ assert_not_equal "Renamed by guest", category.reload.name+ assert_not_equal "Renamed by guest", tag.reload.name+ assert_not_equal "Renamed by guest", merchant.reload.name++ assert_no_difference([ "Category.count", "Tag.count", "FamilyMerchant.count", "Rule.count" ]) do+ delete category_path(category)+ delete tag_path(tag)+ delete family_merchant_path(merchant)+ delete destroy_all_categories_path+ delete destroy_all_tags_path+ delete destroy_all_rules_path+ post category_deletions_path(category)+ post tag_deletions_path(tag)+ end+ end++ test "guest cannot apply rules to family transactions" do+ Rule.any_instance.expects(:apply_later).never++ post apply_rule_path(rules(:one))+ post apply_all_rules_path++ assert_redirected_to accounts_path+ assert_equal I18n.t("shared.require_non_guest"), flash[:alert]+ end++ test "guest cannot import family configuration" do+ assert_no_difference("Import.count") do+ %w[CategoryImport MerchantImport RuleImport SureImport].each do |type|+ post imports_path, params: { import: { type: type } }+ assert_equal I18n.t("shared.require_non_guest"), flash[:alert]+ end+ end++ import = family_guest.family.imports.create!(type: "MerchantImport")+ Import.any_instance.expects(:publish_later).never+ post publish_import_path(import)+ assert_equal I18n.t("shared.require_non_guest"), flash[:alert]+ end++ test "guest cannot revert, cancel or delete a family configuration import" do+ import = family_guest.family.imports.create!(type: "SureImport", status: :complete)+ Import.any_instance.expects(:revert_later).never+ Import.any_instance.expects(:force_fail!).never++ put revert_import_path(import)+ assert_equal I18n.t("shared.require_non_guest"), flash[:alert]++ post cancel_import_path(import)+ assert_equal I18n.t("shared.require_non_guest"), flash[:alert]++ assert_no_difference("Import.count") do+ delete import_path(import)+ end+ assert_equal I18n.t("shared.require_non_guest"), flash[:alert]+ end++ test "guest cannot revert or delete an import whose created accounts they cannot write" do+ import = family_guest.family.imports.create!(type: "TransactionImport", status: :complete)+ account = family_guest.family.accounts.create!(+ name: "Imported by admin", balance: 0, currency: "USD", accountable: Depository.new,+ owner: users(:family_admin), import: import+ )+ Import.any_instance.expects(:revert_later).never++ put revert_import_path(import)+ assert_equal I18n.t("accounts.not_authorized"), flash[:alert]++ assert_no_difference([ "Import.count", "Account.count" ]) do+ delete import_path(import)+ end+ assert_equal I18n.t("accounts.not_authorized"), flash[:alert]+ assert account.reload.persisted?+ end++ test "guest cannot publish an import that would create categories or tags" do+ import = family_guest.family.imports.create!(type: "TransactionImport")+ import.mappings.create!(type: "Import::CategoryMapping", key: "Guest category", create_when_empty: true)+ Import.any_instance.expects(:publish_later).never++ post publish_import_path(import)++ assert_equal I18n.t("imports.publish.guest_new_categories_or_tags"), flash[:alert]+ end++ test "guest gets 403 instead of a redirect when a non-HTML revert is not allowed" do+ import = family_guest.family.imports.create!(type: "TransactionImport", status: :complete)+ family_guest.family.accounts.create!(+ name: "Imported by admin", balance: 0, currency: "USD", accountable: Depository.new,+ owner: users(:family_admin), import: import+ )+ Import.any_instance.expects(:revert_later).never++ put revert_import_path(import), as: :turbo_stream++ assert_response :forbidden+ end++ test "guest cannot change a family configuration import in the import steps" do+ import = family_guest.family.imports.create!(type: "RuleImport", raw_file_str: "name\nOriginal")++ patch import_upload_path(import), params: { import: { raw_file_str: "name\nReplaced" } }++ assert_equal I18n.t("shared.require_non_guest"), flash[:alert]+ assert_equal "name\nOriginal", import.reload.raw_file_str+ end++ test "guest cannot change an import's mappings once it is publishing" do+ import = imports(:transaction)+ import.update!(status: :importing)+ mapping = import_mappings(:one)++ patch import_mapping_path(import, mapping), params: {+ import_mapping: { type: "Import::CategoryMapping", mappable_type: "Category", mappable_id: Import::CategoryMapping::CREATE_NEW_KEY, key: mapping.key }+ }++ assert_equal I18n.t("shared.require_non_guest"), flash[:alert]+ assert_equal mapping.attributes, mapping.reload.attributes+ end++ test "guest can still map a pending transaction import to existing categories" do+ import = imports(:transaction)+ mapping = import_mappings(:one)++ patch import_mapping_path(import, mapping), params: {+ import_mapping: { mappable_type: "Category", mappable_id: categories(:income).id, key: mapping.key }+ }++ assert_redirected_to import_confirm_path(import)+ assert_equal categories(:income), mapping.reload.mappable+ end++ test "guest does not see revert or delete for family configuration imports" do+ complete = family_guest.family.imports.create!(type: "RuleImport", status: :complete)+ pending = family_guest.family.imports.create!(type: "CategoryImport")++ get imports_path++ assert_response :success+ assert_select "form[action=?]", revert_import_path(complete), count: 0+ assert_select "form[action=?]", import_path(pending), count: 0+ end++ test "guest does not see controls to change family configuration" do+ get categories_path+ assert_select "a[href=?]", new_category_path, count: 0+ assert_select "form[action=?]", destroy_all_categories_path, count: 0+ assert_select "[data-testid=category-actions]", count: 0++ get tags_path+ assert_select "a[href=?]", new_tag_path, count: 0+ assert_select "a[href=?]", edit_tag_path(tags(:one)), count: 0++ get rules_path+ assert_select "a[href=?]", new_rule_path(resource_type: "transaction"), count: 0+ assert_select "a[href=?]", confirm_all_rules_path, count: 0+ assert_select "a[href=?]", edit_rule_path(rules(:one)), count: 0+ assert_select "input[type=checkbox][name=?][disabled]", "rule[active]"++ get family_merchants_path+ assert_select "a[href=?]", new_family_merchant_path, count: 0+ assert_select "a[href=?]", edit_family_merchant_path(merchants(:netflix)), count: 0+ end++ test "members still see controls to change family configuration" do+ sign_in users(:family_member)++ get categories_path+ assert_select "a[href=?]", new_category_path+ assert_select "[data-testid=category-actions]"++ get tags_path+ assert_select "a[href=?]", edit_tag_path(tags(:one))++ get rules_path+ assert_select "a[href=?]", edit_rule_path(rules(:one))+ assert_select "input[type=checkbox][name=?]:not([disabled])", "rule[active]"++ get family_merchants_path+ assert_select "a[href=?]", edit_family_merchant_path(merchants(:netflix))+ end++ test "members can still manage family configuration" do+ sign_in users(:family_member)++ assert_difference("Tag.count", 1) do+ post tags_path, params: { tag: { name: "Member tag" } }+ end+ end+enddiff --git a/test/models/assistant_test.rb b/test/models/assistant_test.rbindex f368cb43ed..973abfbd8e 100644--- a/test/models/assistant_test.rb+++ b/test/models/assistant_test.rb@@ -19,6 +19,16 @@ class AssistantTest < ActiveSupport::TestCaseassert_includes preview_classes, Assistant::Function::GetValuationsend+ test "guest registry leaves out the tools that change categories and tags" do+ guest_classes = Assistant.function_classes(family_guest)++ Assistant::FAMILY_CONFIG_FUNCTION_CLASSES.each do |fn_class|+ assert_not_includes guest_classes, fn_class+ assert_includes Assistant.function_classes(users(:family_admin)), fn_class+ end+ assert_includes guest_classes, Assistant::Function::GetCategories+ end+setup do@chat = chats(:two)@message = @chat.messages.create!(