handkerchief01

handkerchief01

Understanding authorize :always vs :by_default in a Domain with Calculations

Hello everyone,

I’m running into an issue with the load callback in a calculation module, specifically regarding domain-level authorization.

I have a resource set up like this:

calculations do
  calculate :total_seconds, :integer, Zoe.Calculations.TotalCourseSeconds, public?: true
end`

This resource belongs to the following domain:

domain: Zoe.Learning`

Inside the Zoe.Calculations.TotalCourseSeconds calculation module, I’m loading a relationship:

load(_, _, _), do: [:lessons]

When I try to run a query that loads this calculation:

courses = Zoe.Learning.read_courses!(load: [:total_seconds], scope: socket.assigns.scope)

I get the following error:

* The domain Zoe.Learning requires that authorization is run, but authorize?: false was given. (ash 3.5.43) lib/ash/error/forbidden/domain_requires_authorization.ex:4: Ash.Error.Forbidden.DomainRequiresAuthorization.exception/1 (ash 3.5.43) lib/ash/actions/helpers.ex:377: Ash.Actions.Helpers.add_authorize?/3 (ash 3.5.43) lib/ash/actions/helpers.ex:315: Ash.Actions.Helpers.set_opts/3

However, I’ve noticed that if I change the authorization block in the Learning domain from this:

authorization do
  authorize :always
end

to this:

authorization do
  authorize :by_default
end

…it works perfectly, and I can load the calculation without any errors.

This leads to my question: What are the differences between :always and :by_default in this context? Why does the internal query generated by the calculation’s load callback seem to fail with :always but succeed with :by_default?

Thanks in advance for any insights!

First Post!

frankdugan3

frankdugan3

:always ensures that you can’t set authorize?: false, so all actions in that domain must be authorized. :by_default sets authorize?: true by default, but allows you to explicitly set it to false if needed.

FWIW, by preference is to use:

authorization do
  authorize :by_default
  require_actor? true
end

I find that leads to the fewest surprises/debugging, and is safe by default.

Where Next?

Popular in Questions Top

New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 43487 311
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41989 114
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39467 209
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement