curiosum-dev / permit_phoenix

Phoenix, Plug and LiveView integrations for the Permit authorization library.
MIT License
19 stars 1 forks source link

Guide/Help #19

Open kraegpoeth opened 2 months ago

kraegpoeth commented 2 months ago

How to use this when i need prefix on my queries? I have setup multitenancy with prefix, but i cannot get this to work in liveview. I keep on getting ERROR 42P01 (undefined_table) relation "users" does not exist which makes sense because i need to look in the tenant_prefix.users table instead. I'm new to elixir so sorry for any noob questions!

kraegpoeth commented 2 months ago

Well i figured out how to do it via finalize_query, but now i think that this evolves into a change request. Because i need to return user from fetch_subject, but how can i modify the socket in fetch_subject?

it works now with this, but that is only thanks to Process.dict, which it what Vnet.Tenant.set_tenant stores tenant in.


      """
      Adds the tenant prefix to the query
      """

      @impl true
      def finalize_query(query, resolution_context) do
        prefix = Vnet.Tenant.prefix()
        query |> Ecto.Query.put_query_prefix(prefix)
      end

      @doc
      """
      Fetches the current user from the session token.
      Also sets the tenant in Process dictionary for finalize_query to use
      """

      @impl true
      def fetch_subject(socket, session) do
        Vnet.Tenant.set_tenant(session["subdomain"])
        Vnet.Users.get_user_by_session_token(session["user_token"])
      end```