thojanssens1

thojanssens1

Sometimes you require foreign key ids to be included in the user form data.

Imagine a dropdown list where the user has to select a “location” when creating an “event”. His locations are saved and the dropdown shows a limited list of locations available.

Context function might typically look like:

def create_event(attrs) do
  %Event{}
  |> Event.changeset(attrs)
  |> Repo.insert()
end

where the event changeset accepts a location_id key.

However, the user could send an ID not available from the dropdown (bypassing the form input); even a location ID that doesn’t belong to him but to another user from the database.

There would need to be some sort of authorization for all those relations coming from external user data.

How do/would you manage that in your application? Where do you enforce authorization?

My take: the context functions should take the current user and the authorization logic belongs in the context. (this logic shouldn’t be in the controller/resolvers/etc. because it would be needed in multiple places and could be forgotten; implementing it in the contexts gives more guarantees).

But it means that most of my context functions will take some current-user struct and I’m not sure this is a commonly seen pattern.

Showing Posts 1 to 3

joseph-lozano

joseph-lozano

FWIW, passing a user (or in one of the cases, a team) into context functions is a pattern I have seen in at least 2 codebases/companies.

al2o3cr

al2o3cr

Depending on your domain, consider making create_event take an already-found Location:

def create_event(location, attrs) do
  %Event{location: location}
  |> Event.changeset(attrs)
  |> Repo.insert()
end

As a bonus, Event.changeset doesn’t need to cast location_id at all - useful if “reassigning location_id” isn’t a meaningful operation in your domain.

chungwong

chungwong

You can consider using validate_change/3

You can validate based on the user_id and location_id in the attrs

— All posts loaded —

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 92995 915
New
caslu
I want to open this thread for you all to discuss and help those who really like Ash but are still hesitant to use it in a real project. ...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
GES233
I’m posting this in response to Jose’s recent tweet (Cr. link) : People are sleeping on Elixir for a coding harness: Hot-code swappi...
New
_mfierro
Hello, I wrote Stop My Hand, a Scattergories-like web application using Phoenix/LiveView as my learning project for Elixir (after readin...
New
marciol
It would be helpful to have a list of companies worldwide that hire engineers without prior experience in Elixir. Often, it can be quite ...
New
durvia
Anyone running long-lived stateful processes on BEAM? We’re building an AI agent runtime and would love to compare notes. We’re a small ...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews