Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multitenancy without multiple schemas #577

Open
choallin opened this issue Oct 12, 2020 · 2 comments
Open

Multitenancy without multiple schemas #577

choallin opened this issue Oct 12, 2020 · 2 comments

Comments

@choallin
Copy link

I have an app where I need multitenancy without schemas.
I have a column in the users table which I use for that. How can I achieve this with pow? Is there a repository function that I can implement to use for a user lookup?

@danschultzer
Copy link
Collaborator

This is pretty application specific, but you would likely need something similar to the multitenancy guide: https://hexdocs.pm/pow/1.0.21/multitenancy.html#content

As you fetch the tenant in the conn, you would then pass that value on to the repo to use for any calls thereafter. I would use the :repo_opts for that, and implement a custom MyApp.MultitenancyRepo module. That repo module needs to implement get_by/3, delete/2, insert/2 and update/2 where you handle the table-based multitenancy by pulling the tenant from the :repo_opts, modify the query in get_by/2 or the changeset in the other functions, and then call MyApp.Repo.

@choallin
Copy link
Author

Sorry to come back to this again. :-/
I have a created this repo as you mentioned, also I am trying to fetch the data from the conn which is working for me.
But I don't know how to configure this custom "repo" to have access to this tenant_id field.
Here is what I have done so far:

Fetching the subdomain to access the tenant name:

defmodule AppointmentManagerWeb.Pow.TenantPlug do
  def init(config), do: config

  def call(conn, config) do
    subdomain = conn.host |> String.split(".") |> List.first
    config = Keyword.put(config, :repo_opts, [prefix: subdomain])

    Pow.Plug.Session.call(conn, config)
  end
end

endpoint.ex

plug Plug.Session,
    store: :cookie,
    key: "_my_app_key",
    signing_salt: "stuff"

  plug MyApp.Pow.TenantPlug
  plug Pow.Plug.Session,
    otp_app: :my_app,
    credentials_cache_store: {Pow.Store.CredentialsCache,
                              ttl: :timer.minutes(60*12),
                              namespace: "credentials"}

pow config:

config :my_app, :pow,
  user: MyApp.Users.User,
  repo: MyApp.MultitenancyRepo,
  extensions: [PowResetPassword],
  controller_callbacks: Pow.Extension.Phoenix.ControllerCallbacks,
  mailer_backend: MyApp.Mailer,
  web_module: MyAppWeb,
  users_context: MyApp.Users

What do I have to do, to get access to the configuration in my "Repo"?

@choallin choallin reopened this Dec 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants