Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Scope the rebuilt manual accounts list to the viewer (#4052)

jaysbeekay68a041cb302a
diff --git a/app/controllers/binance_items_controller.rb b/app/controllers/binance_items_controller.rb
index aaf71a0166..9dbc8d5bb1 100644
--- a/app/controllers/binance_items_controller.rb
+++ b/app/controllers/binance_items_controller.rb
@@ -160,7 +160,7 @@ class BinanceItemsController < ApplicationController
if turbo_frame_request?
item = binance_account.binance_item.reload
@binance_items = Current.family.binance_items.ordered.includes(:syncs)
- @manual_accounts = Account.uncached { Current.family.accounts.visible_manual.order(:name).to_a }
+ @manual_accounts = Account.uncached { Current.family.accounts.visible_manual_for(Current.user).order(:name).to_a }
flash.now[:notice] = t(".success")
@account.reload
diff --git a/app/controllers/brex_items/account_setups_controller.rb b/app/controllers/brex_items/account_setups_controller.rb
index d1d074b7a0..e681da62d2 100644
--- a/app/controllers/brex_items/account_setups_controller.rb
+++ b/app/controllers/brex_items/account_setups_controller.rb
@@ -44,7 +44,7 @@ class BrexItems::AccountSetupsController < ApplicationController
end
def render_accounts_update_after_setup
- @manual_accounts = Account.uncached { Current.family.accounts.visible_manual.order(:name).to_a }
+ @manual_accounts = Account.uncached { Current.family.accounts.visible_manual_for(Current.user).order(:name).to_a }
@brex_items = Current.family.brex_items.ordered
manual_accounts_stream = if @manual_accounts.any?
diff --git a/app/controllers/coinbase_items_controller.rb b/app/controllers/coinbase_items_controller.rb
index 5089ad7f9b..75002f591a 100644
--- a/app/controllers/coinbase_items_controller.rb
+++ b/app/controllers/coinbase_items_controller.rb
@@ -187,7 +187,7 @@ class CoinbaseItemsController < ApplicationController
@manual_accounts = Account.uncached {
Current.family.accounts
- .visible_manual
+ .visible_manual_for(Current.user)
.order(:name)
.to_a
}
diff --git a/app/controllers/enable_banking_items_controller.rb b/app/controllers/enable_banking_items_controller.rb
index 3ccea35087..217ee7bc3c 100644
--- a/app/controllers/enable_banking_items_controller.rb
+++ b/app/controllers/enable_banking_items_controller.rb
@@ -486,7 +486,7 @@ class EnableBankingItemsController < ApplicationController
# Recompute data needed by Accounts#index partials
@manual_accounts = Account.uncached {
Current.family.accounts
- .visible_manual
+ .visible_manual_for(Current.user)
.order(:name)
.to_a
}
diff --git a/app/controllers/lunchflow_items_controller.rb b/app/controllers/lunchflow_items_controller.rb
index 88533a13d3..c80add7ef9 100644
--- a/app/controllers/lunchflow_items_controller.rb
+++ b/app/controllers/lunchflow_items_controller.rb
@@ -619,7 +619,7 @@ class LunchflowItemsController < ApplicationController
# Recompute data needed by Accounts#index partials
@manual_accounts = Account.uncached {
Current.family.accounts
- .visible_manual
+ .visible_manual_for(Current.user)
.order(:name)
.to_a
}
diff --git a/app/controllers/mercury_items_controller.rb b/app/controllers/mercury_items_controller.rb
index f8b8dc7ff6..e5979afb20 100644
--- a/app/controllers/mercury_items_controller.rb
+++ b/app/controllers/mercury_items_controller.rb
@@ -617,7 +617,7 @@ class MercuryItemsController < ApplicationController
# Recompute data needed by Accounts#index partials
@manual_accounts = Account.uncached {
Current.family.accounts
- .visible_manual
+ .visible_manual_for(Current.user)
.order(:name)
.to_a
}
diff --git a/app/controllers/simplefin_items_controller.rb b/app/controllers/simplefin_items_controller.rb
index f2a8a79e03..26db18302f 100644
--- a/app/controllers/simplefin_items_controller.rb
+++ b/app/controllers/simplefin_items_controller.rb
@@ -316,7 +316,7 @@ class SimplefinItemsController < ApplicationController
# Recompute data needed by Accounts#index partials
@manual_accounts = Account.uncached {
Current.family.accounts
- .visible_manual
+ .visible_manual_for(Current.user)
.order(:name)
.to_a
}
@@ -473,7 +473,7 @@ class SimplefinItemsController < ApplicationController
# Recompute data needed by Accounts#index partials
@manual_accounts = Account.uncached {
Current.family.accounts
- .visible_manual
+ .visible_manual_for(Current.user)
.order(:name)
.to_a
}
diff --git a/app/controllers/sophtron_items_controller.rb b/app/controllers/sophtron_items_controller.rb
index 69d9ec22da..c76d6b7e8d 100644
--- a/app/controllers/sophtron_items_controller.rb
+++ b/app/controllers/sophtron_items_controller.rb
@@ -611,7 +611,7 @@ class SophtronItemsController < ApplicationController
if turbo_frame_request?
@manual_accounts = Account.uncached {
- Current.family.accounts.visible_manual.order(:name).to_a
+ Current.family.accounts.visible_manual_for(Current.user).order(:name).to_a
}
@sophtron_items = Current.family.sophtron_items.ordered
manual_accounts_stream = if @manual_accounts.any?
diff --git a/app/models/account.rb b/app/models/account.rb
index 9e081a980c..5b8d1dbe3f 100644
--- a/app/models/account.rb
+++ b/app/models/account.rb
@@ -62,6 +62,12 @@ class Account < ApplicationRecord
visible.manual
}
+ # The manual accounts +user+ may see, as /accounts lists them. Admins are
+ # not exempt: an admin sees what they own or were shared, as on /accounts.
+ scope :visible_manual_for, ->(user) {
+ visible_manual.where(id: user.accessible_accounts.select(:id))
+ }
+
scope :listable_manual, -> {
manual.where.not(status: :pending_deletion)
}
diff --git a/test/controllers/lunchflow_items_controller_test.rb b/test/controllers/lunchflow_items_controller_test.rb
index 597563cb53..0b19db8370 100644
--- a/test/controllers/lunchflow_items_controller_test.rb
+++ b/test/controllers/lunchflow_items_controller_test.rb
@@ -190,4 +190,35 @@ class LunchflowItemsControllerTest < ActionDispatch::IntegrationTest
assert_redirected_to settings_providers_path
assert_match "Api key can't be blank", flash[:alert]
end
+
+ test "complete_account_setup's stream lists only the manual accounts the viewer can access" do
+ own, shared, private_account = create_manual_accounts_for_scope_test
+
+ post complete_account_setup_lunchflow_item_url(lunchflow_items(:one)),
+ params: {},
+ headers: { "Turbo-Frame" => "modal" }
+
+ assert_turbo_stream action: "update", target: "manual-accounts"
+ assert_includes response.body, own.name
+ assert_includes response.body, shared.name
+ assert_not_includes response.body, private_account.name
+ end
+
+ private
+ # The #manual-accounts list rebuilt in this response must match /accounts
+ # for the viewer: their own manual accounts and those shared with them,
+ # never a member's account that was not shared.
+ def create_manual_accounts_for_scope_test
+ admin = users(:family_admin)
+ member = users(:family_member)
+ build = ->(name, owner) {
+ Account.create!(family: families(:dylan_family), owner: owner, name: name, currency: "USD", balance: 0,
+ accountable: Depository.create!(subtype: "checking"))
+ }
+ own = build.call("Admin Own Manual Marker", admin)
+ shared = build.call("Member Shared Manual Marker", member)
+ shared.account_shares.create!(user: admin, permission: "read_only")
+ private_account = build.call("Member Private Manual Marker", member)
+ [ own, shared, private_account ]
+ end
end
diff --git a/test/controllers/simplefin_items_controller_test.rb b/test/controllers/simplefin_items_controller_test.rb
index ec1606ea07..5835a52582 100644
--- a/test/controllers/simplefin_items_controller_test.rb
+++ b/test/controllers/simplefin_items_controller_test.rb
@@ -1120,4 +1120,35 @@ class SimplefinItemsControllerTest < ActionDispatch::IntegrationTest
assert Account.exists?(stale_account.id)
assert SimplefinAccount.exists?(stale_sfa.id)
end
+
+ test "complete_account_setup's stream lists only the manual accounts the viewer can access" do
+ own, shared, private_account = create_manual_accounts_for_scope_test
+
+ post complete_account_setup_simplefin_item_url(@simplefin_item),
+ params: { sync_start_date: Date.today.to_s },
+ headers: { "Turbo-Frame" => "modal" }
+
+ assert_turbo_stream action: "update", target: "manual-accounts"
+ assert_includes response.body, own.name
+ assert_includes response.body, shared.name
+ assert_not_includes response.body, private_account.name
+ end
+
+ private
+ # The #manual-accounts list rebuilt in this response must match /accounts
+ # for the viewer: their own manual accounts and those shared with them,
+ # never a member's account that was not shared.
+ def create_manual_accounts_for_scope_test
+ admin = users(:family_admin)
+ member = users(:family_member)
+ build = ->(name, owner) {
+ Account.create!(family: families(:dylan_family), owner: owner, name: name, currency: "USD", balance: 0,
+ accountable: Depository.create!(subtype: "checking"))
+ }
+ own = build.call("Admin Own Manual Marker", admin)
+ shared = build.call("Member Shared Manual Marker", member)
+ shared.account_shares.create!(user: admin, permission: "read_only")
+ private_account = build.call("Member Private Manual Marker", member)
+ [ own, shared, private_account ]
+ end
end