The personal finance app for everyone (by everyone)
Ruby
Prevent guests from mutating family configuration (#4004)
Febryand Nur Hidayatb6338c2401ab
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rbindex 06645dbd40..416d95fcd2 100644--- a/app/controllers/application_controller.rb+++ b/app/controllers/application_controller.rb@@ -54,6 +54,19 @@ class ApplicationController < ActionController::Baseendend+ # Guests are read-only by design: they may view family configuration but+ # never change it.+ def require_non_guest!+ return if Current.user && !Current.user.guest?++ respond_to do |format|+ format.html { redirect_to accounts_path, alert: t("shared.require_non_guest") }+ format.turbo_stream { head :forbidden }+ format.json { head :forbidden }+ format.any { head :forbidden }+ end+ end+# Provider panels post from the page (connection row or drawer), so Turbo# asks for a stream without sending a Turbo-Frame header.def turbo_panel_request?diff --git a/app/controllers/categories_controller.rb b/app/controllers/categories_controller.rbindex 53895c6414..57cac63a36 100644--- a/app/controllers/categories_controller.rb+++ b/app/controllers/categories_controller.rb@@ -1,4 +1,5 @@class CategoriesController < ApplicationController+ before_action :require_non_guest!, except: %i[index]before_action :set_category, only: %i[edit update destroy]before_action :set_categories, only: %i[update edit]before_action :set_transaction, only: :creatediff --git a/app/controllers/category/deletions_controller.rb b/app/controllers/category/deletions_controller.rbindex e11777abbd..3ba0fb6ac0 100644--- a/app/controllers/category/deletions_controller.rb+++ b/app/controllers/category/deletions_controller.rb@@ -1,4 +1,5 @@class Category::DeletionsController < ApplicationController+ before_action :require_non_guest!before_action :set_categorybefore_action :set_replacement_category, only: :creatediff --git a/app/controllers/family_merchants_controller.rb b/app/controllers/family_merchants_controller.rbindex a8bb4580dc..b1bcb88115 100644--- a/app/controllers/family_merchants_controller.rb+++ b/app/controllers/family_merchants_controller.rb@@ -1,4 +1,5 @@class FamilyMerchantsController < ApplicationController+ before_action :require_non_guest!, except: %i[index]before_action :set_merchant, only: %i[edit update destroy]def indexdiff --git a/app/controllers/rules_controller.rb b/app/controllers/rules_controller.rbindex cdadd21230..1a320a809a 100644--- a/app/controllers/rules_controller.rb+++ b/app/controllers/rules_controller.rb@@ -1,6 +1,7 @@class RulesController < ApplicationControllerinclude StreamExtensions+ before_action :require_non_guest!, except: %i[index]before_action :set_rule, only: [ :edit, :update, :destroy, :apply, :confirm ]def indexdiff --git a/app/controllers/tag/deletions_controller.rb b/app/controllers/tag/deletions_controller.rbindex c4b8d889bb..f46b90a908 100644--- a/app/controllers/tag/deletions_controller.rb+++ b/app/controllers/tag/deletions_controller.rb@@ -1,4 +1,5 @@class Tag::DeletionsController < ApplicationController+ before_action :require_non_guest!before_action :set_tagbefore_action :set_replacement_tag, only: :creatediff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rbindex 315b71093e..a4b48a772d 100644--- a/app/controllers/tags_controller.rb+++ b/app/controllers/tags_controller.rb@@ -1,4 +1,5 @@class TagsController < ApplicationController+ before_action :require_non_guest!, except: %i[index]before_action :set_tag, only: %i[edit update destroy]def indexdiff --git a/config/locales/views/shared/en.yml b/config/locales/views/shared/en.ymlindex 9bfbfa10af..86c69c89c0 100644--- a/config/locales/views/shared/en.yml+++ b/config/locales/views/shared/en.yml@@ -25,6 +25,7 @@ en:syncing_notice:syncing: Syncing accounts data...require_admin: "Only admins can perform this action"+ require_non_guest: "Guests cannot perform this action"require_connector_owner: "You can only manage connections you added"custom_confirm:default_title: "Are you sure?"diff --git a/test/controllers/categories_controller_test.rb b/test/controllers/categories_controller_test.rbindex 17f4af0230..e309ce0733 100644--- a/test/controllers/categories_controller_test.rb+++ b/test/controllers/categories_controller_test.rb@@ -158,6 +158,42 @@ class CategoriesControllerTest < ActionDispatch::IntegrationTestassert_redirected_to categories_urlend+ test "guest cannot create category" do+ sign_in family_guest++ assert_no_difference "Category.count" do+ post categories_url, params: {+ category: {+ name: "Guest Category",+ color: Category::COLORS.sample } }+ end++ assert_redirected_to accounts_url+ end++ test "guest cannot destroy category" do+ sign_in family_guest++ assert_no_difference "Category.count" do+ delete category_url(categories(:food_and_drink))+ end++ assert_redirected_to accounts_url+ end++ test "member can create category" do+ sign_in users(:family_member)++ assert_difference "Category.count", 1 do+ post categories_url, params: {+ category: {+ name: "Member Category",+ color: Category::COLORS.sample } }+ end++ assert_redirected_to categories_url+ end+test "bootstrap" do# 22 default categories minus 2 that already exist in fixtures (Income, Food & Drink)assert_difference "Category.count", 20 dodiff --git a/test/controllers/family_merchants_controller_test.rb b/test/controllers/family_merchants_controller_test.rbindex b3fb6a7f5f..e7715d2467 100644--- a/test/controllers/family_merchants_controller_test.rb+++ b/test/controllers/family_merchants_controller_test.rb@@ -40,6 +40,36 @@ class FamilyMerchantsControllerTest < ActionDispatch::IntegrationTestassert_redirected_to family_merchants_pathend+ test "guest cannot create merchant" do+ sign_in family_guest++ assert_no_difference("FamilyMerchant.count") do+ post family_merchants_url, params: { family_merchant: { name: "Guest Merchant" } }+ end++ assert_redirected_to accounts_url+ end++ test "guest cannot destroy merchant" do+ sign_in family_guest++ assert_no_difference("FamilyMerchant.count") do+ delete family_merchant_url(@merchant)+ end++ assert_redirected_to accounts_url+ end++ test "member can create merchant" do+ sign_in users(:family_member)++ assert_difference("FamilyMerchant.count") do+ post family_merchants_url, params: { family_merchant: { name: "Member Merchant" } }+ end++ assert_redirected_to family_merchants_path+ end+test "should create merchant as json" doassert_difference("FamilyMerchant.count") dopost family_merchants_url(format: :json), params: { family_merchant: { name: "Quick Merchant" } }diff --git a/test/controllers/rules_controller_test.rb b/test/controllers/rules_controller_test.rbindex 7e4b1d720a..b1a45555e0 100644--- a/test/controllers/rules_controller_test.rb+++ b/test/controllers/rules_controller_test.rb@@ -273,6 +273,45 @@ class RulesControllerTest < ActionDispatch::IntegrationTestassert_redirected_to rules_urlend+ test "guest cannot create rule" do+ sign_in family_guest++ assert_no_difference "Rule.count" do+ post rules_url, params: { rule: { name: "Guest Rule", resource_type: "transaction" } }+ end++ assert_redirected_to accounts_url+ end++ test "guest cannot apply rule" do+ sign_in family_guest+ rule = rules(:one)++ assert_no_enqueued_jobs do+ post apply_rule_url(rule)+ end++ assert_redirected_to accounts_url+ assert_not rule.reload.active?+ end++ test "member can create rule" do+ sign_in users(:family_member)++ assert_difference "Rule.count", 1 do+ post rules_url, params: {+ rule: {+ name: "Member Rule",+ resource_type: "transaction",+ actions_attributes: {+ "0" => {+ action_type: "set_transaction_category",+ value: categories(:food_and_drink).id } } } }+ end++ assert_redirected_to confirm_rule_path(Rule.order(:created_at).last, reload_on_close: true)+ end+test "clear_ai_cache enqueues job and records the request in the debug log" doassert_enqueued_with(job: ClearAiCacheJob, args: [ @user.family ]) dopost clear_ai_cache_rules_urldiff --git a/test/controllers/tags_controller_test.rb b/test/controllers/tags_controller_test.rbindex 9bf86c4e9e..d0cd4a761e 100644--- a/test/controllers/tags_controller_test.rb+++ b/test/controllers/tags_controller_test.rb@@ -33,6 +33,37 @@ class TagsControllerTest < ActionDispatch::IntegrationTestassert_equal "Tag created", flash[:notice]end+ test "guest cannot create tag" do+ sign_in family_guest++ assert_no_difference("Tag.count") do+ post tags_url, params: { tag: { name: "Guest Tag" } }+ end++ assert_redirected_to accounts_url+ end++ test "guest cannot destroy tag" do+ sign_in family_guest+ tag = tags(:one)++ assert_no_difference("Tag.count") do+ delete tag_url(tag)+ end++ assert_redirected_to accounts_url+ end++ test "member can create tag" do+ sign_in users(:family_member)++ assert_difference("Tag.count") do+ post tags_url, params: { tag: { name: "Member Tag" } }+ end++ assert_redirected_to tags_url+ end+test "should create tag as json" doassert_difference("Tag.count") dopost tags_url(format: :json), params: { tag: { name: "Quick Tag", color: "#e99537" } }