Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Require write access to a trade's transfer account (#3816)

GFR8dd71b1f0a7e
diff --git a/app/controllers/api/v1/trades_controller.rb b/app/controllers/api/v1/trades_controller.rb
index 43014cf45e..036c3f57c3 100644
--- a/app/controllers/api/v1/trades_controller.rb
+++ b/app/controllers/api/v1/trades_controller.rb
@@ -248,7 +248,7 @@ class Api::V1::TradesController < Api::V1::BaseController
amount: parse_positive_amount!(trade_params[:amount], context: "deposit/withdrawal"),
currency: trade_params[:currency].presence || account.currency,
type: type,
- transfer_account_id: trade_params[:transfer_account_id]
+ transfer_account_id: writable_transfer_account_id
}.compact
when "interest", "fee"
@@ -440,6 +440,16 @@ class Api::V1::TradesController < Api::V1::BaseController
"#{security.ticker}|#{security.exchange_operating_mic}"
end
+ # A deposit or withdrawal also books an entry on the other account, so it
+ # has to be one the key's user may write to. Raises RecordNotFound, which
+ # #create answers with "Account not found".
+ def writable_transfer_account_id
+ return if trade_params[:transfer_account_id].blank?
+
+ current_resource_owner.family.accounts.writable_by(current_resource_owner)
+ .find(trade_params[:transfer_account_id]).id
+ end
+
def render_validation_error(message, errors)
render json: {
error: "validation_failed",
diff --git a/app/controllers/trades_controller.rb b/app/controllers/trades_controller.rb
index e3aed40dbb..0ac5661aea 100644
--- a/app/controllers/trades_controller.rb
+++ b/app/controllers/trades_controller.rb
@@ -19,6 +19,13 @@ class TradesController < ApplicationController
return unless require_account_permission!(@account)
+ # A deposit or withdrawal also books an entry on the other account, so that
+ # account needs the same write permission, as in TransfersController#create.
+ if create_params[:transfer_account_id].present?
+ transfer_account = accessible_accounts.find(create_params[:transfer_account_id])
+ return unless require_account_permission!(transfer_account)
+ end
+
@model = Trade::CreateForm.new(create_params.merge(account: @account)).create
if @model.persisted?
diff --git a/app/views/trades/_form.html.erb b/app/views/trades/_form.html.erb
index 85c307e112..b74846a5fa 100644
--- a/app/views/trades/_form.html.erb
+++ b/app/views/trades/_form.html.erb
@@ -55,7 +55,7 @@
<% end %>
<% if %w[deposit withdrawal].include?(type) %>
- <%= form.collection_select :transfer_account_id, accessible_accounts.visible.alphabetically, :id, :name, { prompt: t(".account_prompt"), label: t(".account") } %>
+ <%= form.collection_select :transfer_account_id, accessible_accounts.writable_by(Current.user).visible.alphabetically, :id, :name, { prompt: t(".account_prompt"), label: t(".account") } %>
<% end %>
<% if %w[buy sell].include?(type) %>
diff --git a/docs/api/openapi.yaml b/docs/api/openapi.yaml
index c4ed338e81..3fc01d16bf 100644
--- a/docs/api/openapi.yaml
+++ b/docs/api/openapi.yaml
@@ -9483,7 +9483,9 @@ paths:
transfer_account_id:
type: string
format: uuid
- description: Destination/source account ID for linked transfers
+ description: Destination/source account ID for linked transfers.
+ Must be an account the user can write to; otherwise the request
+ returns 404.
required:
- account_id
- date
diff --git a/spec/requests/api/v1/trades_spec.rb b/spec/requests/api/v1/trades_spec.rb
index a1a730e972..b7af019310 100644
--- a/spec/requests/api/v1/trades_spec.rb
+++ b/spec/requests/api/v1/trades_spec.rb
@@ -181,7 +181,7 @@ RSpec.describe 'API V1 Trades', type: :request do
currency: { type: :string, description: 'Currency (defaults to account currency)' },
investment_activity_label: { type: :string, description: 'Activity label (e.g. Buy, Sell)' },
category_id: { type: :string, format: :uuid, description: 'Category ID' },
- transfer_account_id: { type: :string, format: :uuid, description: 'Destination/source account ID for linked transfers' }
+ transfer_account_id: { type: :string, format: :uuid, description: 'Destination/source account ID for linked transfers. Must be an account the user can write to; otherwise the request returns 404.' }
},
required: %w[account_id date type]
}
diff --git a/test/controllers/api/v1/trades_controller_test.rb b/test/controllers/api/v1/trades_controller_test.rb
index 60d6c22507..924fd20119 100644
--- a/test/controllers/api/v1/trades_controller_test.rb
+++ b/test/controllers/api/v1/trades_controller_test.rb
@@ -313,6 +313,32 @@ class Api::V1::TradesControllerTest < ActionDispatch::IntegrationTest
assert_equal @investment_account.id, transfer.outflow_transaction.entry.account_id, "Outflow should come from investment"
end
+ # The transfer books an entry on the other account too, so the key's user
+ # needs write permission there. family_member can write to the investment
+ # account here, only read credit_card and not see connected at all.
+ test "create withdrawal rejects a transfer account the user may only read or not access" do
+ @user = users(:family_member)
+ @investment_account.account_shares.create!(user: @user, permission: "full_control")
+
+ [ accounts(:credit_card), accounts(:connected) ].each do |transfer_account|
+ assert_no_difference [ "Entry.count", "Transfer.count" ] do
+ post "/api/v1/trades",
+ params: { trade: {
+ account_id: @investment_account.id,
+ type: "withdrawal",
+ date: Date.current,
+ amount: 500.00,
+ currency: "USD",
+ transfer_account_id: transfer_account.id
+ } },
+ headers: api_headers(read_write_api_key)
+ end
+
+ assert_response :not_found
+ assert_equal "Account not found", JSON.parse(response.body)["message"]
+ end
+ end
+
test "create deposit without amount returns 422" do
post "/api/v1/trades",
params: { trade: {
diff --git a/test/controllers/trades_controller_test.rb b/test/controllers/trades_controller_test.rb
index e2f5e2c86b..fbbb3056be 100644
--- a/test/controllers/trades_controller_test.rb
+++ b/test/controllers/trades_controller_test.rb
@@ -128,6 +128,56 @@ class TradesControllerTest < ActionDispatch::IntegrationTest
assert_redirected_to @entry.account
end
+ # A deposit or withdrawal books an entry on the other account too, so that
+ # account needs write permission, not just the investment account.
+ test "a withdrawal cannot book into an account the member may only read" do
+ sign_in_as_member_with_writable_investment
+
+ assert_no_difference [ "Entry.count", "Transfer.count" ] do
+ post trades_url(account_id: @investment.id), params: {
+ model: { type: "withdrawal", date: Date.current, amount: 10, currency: "USD", transfer_account_id: accounts(:credit_card).id }
+ }
+ end
+
+ assert_equal I18n.t("accounts.not_authorized"), flash[:alert]
+ end
+
+ test "a deposit cannot draw from another member's private account" do
+ sign_in_as_member_with_writable_investment
+
+ assert_no_difference [ "Entry.count", "Transfer.count" ] do
+ post trades_url(account_id: @investment.id), params: {
+ model: { type: "deposit", date: Date.current, amount: 10, currency: "USD", transfer_account_id: accounts(:connected).id }
+ }
+ end
+
+ assert_response :not_found
+ end
+
+ test "a member can still book a withdrawal into an account shared with full control" do
+ sign_in_as_member_with_writable_investment
+
+ assert_difference -> { Transfer.count } => 1 do
+ post trades_url(account_id: @investment.id), params: {
+ model: { type: "withdrawal", date: Date.current, amount: 10, currency: "USD", transfer_account_id: accounts(:depository).id }
+ }
+ end
+
+ assert_redirected_to @investment
+ end
+
+ test "the transfer account picker only offers accounts the member may write to" do
+ sign_in_as_member_with_writable_investment
+
+ get new_trade_url(account_id: @investment.id, type: "withdrawal")
+
+ assert_response :success
+ assert_select "input[type=hidden][name='model[transfer_account_id]']"
+ assert_select "[role=option][data-value='#{accounts(:depository).id}']"
+ assert_select "[role=option][data-value='#{accounts(:credit_card).id}']", count: 0
+ assert_select "[role=option][data-value='#{accounts(:connected).id}']", count: 0
+ end
+
test "creates withdrawal entry" do
to_account = accounts(:depository) # Account the withdrawal is going to
@@ -485,4 +535,14 @@ class TradesControllerTest < ActionDispatch::IntegrationTest
assert @entry.trade.locked_attributes.key?("investment_activity_label"), "investment_activity_label should be locked"
assert @entry.protected_from_sync?, "Entry should be protected from sync"
end
+
+ private
+ # family_member gets the investment account with full control; fixtures
+ # already share depository with full control and credit_card read-only,
+ # and leave connected private to family_admin.
+ def sign_in_as_member_with_writable_investment
+ @investment = accounts(:investment)
+ @investment.account_shares.create!(user: users(:family_member), permission: "full_control")
+ sign_in users(:family_member)
+ end
end