Require write access to a trade's transfer account
Merged
GF
2mo ago+109+109+012345678901234567890123456789-4-4-0123456789
Summary
Deposits and withdrawals create entries on both the investment account and a transfer account. This change ensures that users can only select transfer accounts they have write permission to, preventing unauthorized writes to accounts they can only read or cannot access.
Key Changes
- TradesController: Added permission check for transfer accounts before creating deposits/withdrawals, matching the pattern used in TransfersController
- API TradesController: Introduced
writable_transfer_account_idmethod that validates the transfer account is writable by the current user, raising RecordNotFound (404) if not - Trade form view: Updated transfer account picker to only display accounts the user can write to via
.writable_by(Current.user)scope - API documentation: Updated OpenAPI spec to clarify that transfer_account_id must be writable, returning 404 otherwise
- Tests: Added comprehensive test coverage for both web and API controllers:
- Rejection of read-only transfer accounts
- Rejection of inaccessible transfer accounts
- Successful operations with writable transfer accounts
- Verification that form only shows writable accounts
Implementation Details
The fix leverages existing permission infrastructure:
- Uses
accessible_accounts.find()in web controller to ensure account exists and is accessible - Uses
current_resource_owner.family.accounts.writable_by()in API to filter to writable accounts - Both approaches raise RecordNotFound when validation fails, providing consistent error responses (403 for web, 404 for API)
Summary by CodeRabbit
- Bug Fixes
- Deposit and withdrawal transfers now accept only accounts the current user can write to. Attempts to use read-only or inaccessible accounts return “Account not found” and do not create a trade.
- The transfer-account selector now shows only accounts the current user can write to.
- Documentation
- API documentation now describes the write-access requirement and the 404 response for accounts that do not meet it.