Skip to content

armgit / sure

Public Imported from GitHub
0

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.rb
index 06645dbd40..416d95fcd2 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -54,6 +54,19 @@ class ApplicationController < ActionController::Base
end
end
+ # 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.rb
index 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: :create
diff --git a/app/controllers/category/deletions_controller.rb b/app/controllers/category/deletions_controller.rb
index 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_category
before_action :set_replacement_category, only: :create
diff --git a/app/controllers/family_merchants_controller.rb b/app/controllers/family_merchants_controller.rb
index 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 index
diff --git a/app/controllers/rules_controller.rb b/app/controllers/rules_controller.rb
index cdadd21230..1a320a809a 100644
--- a/app/controllers/rules_controller.rb
+++ b/app/controllers/rules_controller.rb
@@ -1,6 +1,7 @@
class RulesController < ApplicationController
include StreamExtensions
+ before_action :require_non_guest!, except: %i[index]
before_action :set_rule, only: [ :edit, :update, :destroy, :apply, :confirm ]
def index
diff --git a/app/controllers/tag/deletions_controller.rb b/app/controllers/tag/deletions_controller.rb
index 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_tag
before_action :set_replacement_tag, only: :create
diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb
index 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 index
diff --git a/config/locales/views/shared/en.yml b/config/locales/views/shared/en.yml
index 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.rb
index 17f4af0230..e309ce0733 100644
--- a/test/controllers/categories_controller_test.rb
+++ b/test/controllers/categories_controller_test.rb
@@ -158,6 +158,42 @@ class CategoriesControllerTest < ActionDispatch::IntegrationTest
assert_redirected_to categories_url
end
+ 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 do
diff --git a/test/controllers/family_merchants_controller_test.rb b/test/controllers/family_merchants_controller_test.rb
index 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::IntegrationTest
assert_redirected_to family_merchants_path
end
+ 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" do
assert_difference("FamilyMerchant.count") do
post 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.rb
index 7e4b1d720a..b1a45555e0 100644
--- a/test/controllers/rules_controller_test.rb
+++ b/test/controllers/rules_controller_test.rb
@@ -273,6 +273,45 @@ class RulesControllerTest < ActionDispatch::IntegrationTest
assert_redirected_to rules_url
end
+ 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" do
assert_enqueued_with(job: ClearAiCacheJob, args: [ @user.family ]) do
post clear_ai_cache_rules_url
diff --git a/test/controllers/tags_controller_test.rb b/test/controllers/tags_controller_test.rb
index 9bf86c4e9e..d0cd4a761e 100644
--- a/test/controllers/tags_controller_test.rb
+++ b/test/controllers/tags_controller_test.rb
@@ -33,6 +33,37 @@ class TagsControllerTest < ActionDispatch::IntegrationTest
assert_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" do
assert_difference("Tag.count") do
post tags_url(format: :json), params: { tag: { name: "Quick Tag", color: "#e99537" } }