djacobs7

djacobs7

I am building an app where users can belong to different organizations. I want each organization to be it’s own tenant ( Multitenancy — ash v3.29.3 ). When the user is logged in, they select a tenant, and can only have access to data from that tenant.

Is there a way call set_tenant in a router plug ( similar to Ash.PlugHelpers.set_actor(user) ) so that I can globally set the current tenant, without for any queries that might need it?

Showing Posts 1 to 9

jimsynz

jimsynz

Ash Core Team

Hi there :waving_hand:

There is Ash.PlugHelpers.set_tenant/2.

The thing to note is that the Ash plughelpers are there to provide a consistent interface across various extensions that interact with plugs - ie AshJsonApi, AshGraphQl and AshAuthentication. Note that these won’t do anything for your code - you would still need to call get_tenant/2 and pass it into your actions as an option.

There is also Ash.set_tenant/1 which sets it in the process dictionary but that feature is going away in 3.0 on favour of requiring it to be explicitly passed as above.

djacobs7

djacobs7 OP

Thank you! What you are saying about set_tenant totally makes sense; however, I don’t quite understand how I would call get_tenant and pass it in as an option. Like, if someone wanted to split a simple resource like the one below across tenants, what changes would you have to make?


defmodule App.API.Visit do

  use App.Resource,
    api: App.API,
    data_layer: AshPostgres.DataLayer,
    extensions: [AshGraphql.Resource],
    authorizers: [Ash.Policy.Authorizer]


   # OTHER STUFF .....

  actions do
    defaults [:read, :update]

    read :list_visits_paginated do
      pagination keyset?: true, default_limit: 100
    end

    update :update_visit do
      accept [:scheduled_for]
      require_attributes [:scheduled_for]
    end

    create :create_visit do
      accept [ :scheduled_for]
      change relate_actor(:user, allow_nil?: false)
   end

end
jimsynz

jimsynz

Ash Core Team

Right. Well the first think is to decide what sort of multitenancy you want and set the resource DSL appropriately. See the guide for a starting point.

Next when you call your actions you set the tenant on the changeset or query using their respective set_tenant functions. If you’re using the code interface you can pass a tenant: option in the options keyword list as the last argument in the call.

zachdaniel

zachdaniel

Creator of Ash

You can also use the tenant option to Ash.Query.for_read and Ash.Changeset.for_create/update/destroy :slight_smile:

djacobs7

djacobs7 OP

Ok. Thank you ( and thanks for your patience with me ). It sounds like I need to set the tenant as option when I call the actions in code, or when I do a query. That makes sense to me now

I think I am still confused about AshGraphQl / AshAdmin / AshAuthentication. If I use Ash.PlugHelpers.set_tenant/2 in the router, does that automatically set_tenant on those services and I just don’t need to worry about it? Or is there something I need to do inside of my router or Ash.Api to make it work?

zachdaniel

zachdaniel

Creator of Ash

Yes, AshGraphql and AshJsonApi will automatically use tenant/actor information set using Ash.PlugHelpers :slight_smile:

So you only need to manually thread the tenant through in things like LiveView or any custom calls to Ash.

alexslade

alexslade

There is also Ash.set_tenant/1 which sets it in the process dictionary but that feature is going away in 3.0

It looks like this is still possible in Ash 3, did something change since this comment?

zachdaniel

zachdaniel

Creator of Ash

It’s been removed. Where do you see that it’s possible?

alexslade

alexslade

I think I got mixed up, sorry. I was looking at PlugHelpers.set_tenant/2

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

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
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
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews