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

index_name based apartment current tenant #320

Open
paulomcnally opened this issue Nov 13, 2018 · 5 comments
Open

index_name based apartment current tenant #320

paulomcnally opened this issue Nov 13, 2018 · 5 comments

Comments

@paulomcnally
Copy link

paulomcnally commented Nov 13, 2018

I am using https://github.com/influitive/apartment and I would expect index names to be generated based on the schema name as a prefix or suffix.

Example:
tenant_model or model_tenant.

Apartment::Tenant.current

It always returns public even when its value is different.

@simonireilly
Copy link
Contributor

Hi, also using an Apartment like implementation of Algolia. Consider storing:
tenant: Apartment::tenant.current on the index.

See #138 for the full explanation.

You can then generate a secure search only API key on the back end which filters searches to that tenant only. It cannot be used for other tenants data. See https://www.algolia.com/doc/guides/security/api-keys/?language=php#generating-secured-api-keys

I think this is the best way to do it, otherwise you will need to manage the API keys for your indices which will be N+1 for every tenant. I also think there is a limit to the total indices you are allowed on certain packages.

@paulomcnally
Copy link
Author

I was thinking of one application for each tenant.

captura de pantalla 2018-11-15 a la s 8 30 56 p m

In this way you could relate the data from algolia to tenant and separate the information.

@julienbourdeau
Copy link
Contributor

There are 2 solutions:

  • one index per tenant and secured API key with restriction on the index name
  • one index with all tenant and secured api keys with restriction on the tenant filter (solution by @simonireilly

In both case, make sure you use the Secured API Keys, not "standard" API keys.

One tenant per App won't be possible because there are no API to create apps.

I never used Aparment, can you share how you define the Index Name? I think it must be as string and can't be a method call.

@simonireilly
Copy link
Contributor

@julienbourdeau Apartment is a middleware for adding subdomains e.g. www.awesomeshop.commerce-platform.com. Similar to devise current_user you get an accessor on the thread that tells you the current tenant (returns the subdomain).

Apartment.current_tenant # awesomeshop

This is basically what this would look like for apartment, as suggested in #78 (comment)

Easily implemented as my fork shows https://github.com/simonireilly/algoliasearch-rails/pull/1/files.

#341 might fix this anyway?

The main issue is that you have the string being specific to the thread - so it needs to be a Proc to be dynamic 👍

Per-tenant indices

You can affix the index name with the current tenant from your application using:

class Contact < ActiveRecord::Base
 include AlgoliaSearch
  algoliasearch per_tenant: Proc.new { Apartment.current_tenant } do
 # index name will be "awesomeshop_Contact"
   attribute :first_name, :last_name, :email
 end
end

@Spone
Copy link
Contributor

Spone commented Mar 21, 2019

This can probably be handled at the same time as #341

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

4 participants