NTS*_*NTS 7 postgresql ruby-on-rails
我需要帮助在Postgres数据库中设计复杂的用户权限.在我的Rails应用程序中,每个用户都可以访问一组独特的功能.换句话说,没有预定义的"角色"来确定用户可以访问哪些功能.
在几乎每个控制器/视图中,应用程序都会检查当前用户是否可以访问不同的功能.理想情况下,该应用程序将提供约100种不同的功能,并将支持500k +用户.
目前,我正在考虑三种不同的选择(但欢迎替代品!),并想知道哪种选择提供最佳性能.提前感谢您的任何帮助/建议.
通过在User表和Feature表之间构建多对多关系,应用程序可以通过查询连接表来检查用户是否可以访问给定功能.
例如,如果连接表中有连接user1和feature1的记录,则user1可以访问feature1.
该应用程序可以将每个功能表示为User表上的布尔列.这样可以避免查询多个表来检查权限.
例如,如果user1.has_feature1为true,则user1可以访问feature1.
该应用程序可以将功能存储为User表格中(GIN索引?)数组列中的字符串.然后,要检查用户是否可以访问某个功能,它将在数组列中搜索给定功能.
例如,如果user1.features.include? 'feature1'为true,则user1可以访问feature1.
多对多关系是这里唯一可行的选择。他们将其称为关系数据库是有原因的。
为什么?
class Feature
has_many :user_features
has_many :users, through: :user_features
end
class UserFeature
belongs_to :user
belongs_to :feature
end
class User
has_many :user_features
has_many :features, through: :user_features
def has_feature?(name)
features.exist?(name: name)
end
end
Run Code Online (Sandbox Code Playgroud)