5

我正在构建多租户应用程序。

所有数据的隔离都是由每个表中的 TenantID 列完成的。

为所有租户模型自动处理多租户的最佳方法是什么。

例子:

Contacts.new({.....}) should automatically add :tenant => curret_user.tenant
Contacts.where({....}) should also add :tenant => curret_user.tenant

目前我在 CanCan gem 中看到了类似的东西,它可以获取特定用户参数的记录。但它没有为插入和更新操作提供任何东西。或者可能是我不明白该怎么做。

问候,阿列克谢·扎哈罗夫。

4

3 回答 3

1

如果您将通过租户对象处理所有集合,这是可能的。

这是使用 Mongoid 的示例:

#Find all products with price > 500 in current tenant scope

current_tenant.products.where(:price.gt => 500) 

#It also work for create and save operations

current_tenant.products.create :name => "apple", :price => 200
于 2010-09-30T14:40:34.840 回答
1

我建议查看多租户红宝石宝石。确保执行的所有查询都尊重当前租户变得很简单。 http://blog.codecrate.com/2011/03/multitenant-locking-down-your-app-and.html

前任:

Multitenant.with_tenant current_tenant do
  # queries within this block are automatically
  # scoped to the current tenant
  User.all

  # records created within this block are
  # automatically assigned to the current tenant
  User.create :name => 'Bob'
end
于 2011-05-23T13:49:45.190 回答
1

我使用Act As Tenant gem 进行多租户。它是非常好的宝石,非常易于使用。这是此 gem充当租户的文档

于 2014-12-01T10:31:16.357 回答