【发布时间】:2014-05-09 21:29:21
【问题描述】:
在 Railscasts Episode 388 - Multitenancy with Scopes 中,Ryan 正在添加默认范围以确保安全:
或者,我们可以使用 CanCan 等授权库来处理范围,但这不是为多租户应用程序设计的,它不能很好地解决这个问题。这是可以使用默认范围的一种情况,这就是我们要做的。
class Tenant < ActiveRecord::Base
attr_accessible :name, :subdomain
has_many :topics
end
class Topic < ActiveRecord::Base
attr_accessible :name, :content
belongs_to :user
has_many :posts
default_scope { where(tenant_id: Tenant.current_id) }
end
我的问题是:我想实现授权(例如使用 Cancan)并希望定义如下能力:
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new # guest user (not logged in)
if user.admin?
can :manage, Topic
else
can :read, Topic
end
end
end
用户是否有能力管理所有租户的主题或仅在租户范围内?
或者更一般的问题:多租户应用程序的正确授权方法是什么?
【问题讨论】:
标签: ruby-on-rails authorization cancan multi-tenant