This is a best practice / idiomatic phoenix question. I’m building a demo app but I’m looking to understand good practice for more rigorous apps that control personal data and financial information.
Most of the actions in my contexts require a user. I.e. creating a transaction
, updating it, etc. Either the record is going to be created belonging to the user, or permissions will be checked / logged. I know I can use a library like Canary to simplify the role management, but at this point I’m purely thinking about how to pass these user records around.
Approach A — Let Ecto handle it implicitly — treat the user
as a required part of the transaction
schema. Inject the user into the transactions attributes in my controllers before passing to MyApp.Ledger.create_transaction(attrs)
. Use put_assoc
in the schema and use Ecto to validate the users presence.
Approach B — Handle it in my contexts explicitly — alter the interface of my context functions, make them all require a user too. Do any checks I need to before handing over to Ecto.
It feels like options A is less work, but is more “magic” and hidden logic. B feels like a lot of repetitive boilerplate - but a more explicit way of doing things.
I see this as a boundary issue - I want the knowledge and handling of users to live in my core application, not mixed up into the controllers of my web wrapper.
Does anyone have a strong opinion on either way? Or something else?
Have you done anything like this yourself? My goal is to have robust checks on which user is requesting which actions.