gpt4 book ai didi

ruby - 根据账户的 'right'打开/关闭应用功能的 'type'数据结构是什么?

转载 作者:数据小太阳 更新时间:2023-10-29 08:10:28 24 4
gpt4 key购买 nike

我的应用有 10 项功能,这些功能的启用/禁用取决于用户拥有的 3 种“类型”帐户中的哪一种。

目前,我有 10 种方法(每个特征一种),大致如下:

def is_FEATURENAME_enabled
case currentuser.accounttype
when "A", "C" # account types allow to see that feature
return true
else
return false
end
end

然后,在我可能会禁用某项功能的每个地方,我都会这样做

if foo.is_SOMEFEATURE_enable
do stuff to enable that feature
end

它有效。维护起来并不难。但是应该有更好的方法。我怀疑正确的解决方案是在一个地方定义某种结构(散列?我不知道),将启用的功能映射到帐户类型,然后有一个我称之为类似的方法:

if foo.is_feature_enabled(:FEATURENAME)
do stuff to enable feature
end

方法 is_feature_enabled 查看 currentuser.accountype 并检查映射结构以查看是否启用了已识别的功能。

而且我怀疑定义该映射的 DRY 方法(假设我拥有比帐户类型更多的功能)是一次列出所有功能,然后为每个功能列出有权访问该功能的帐户类型(而不是相反) ).这样,当我添加一项新功能时,我只需要在映射中编辑一行。像这样的东西:

FeatureA: usertype1
FeatureB: usertype1, usertype3
FeatureC: usertype2
...

似乎比以下更合乎逻辑且更易于维护:

usertype1: FeatureA, FeatureB, FeatureD, FeatureG
usertype2: FeatureC, FeatureD
usertype3: FeatureB, FeatureD, FeatureG, FeatureH

任何建议都将不胜感激,并且对学习使用 ruby​​ 做事的正确方法具有指导意义。

最佳答案

我认为您几乎已经找到了自己完成这项工作的最佳方法——您的建议是明智的。只需使用特征名称作为哈希的查找键,然后获取结果列表并检查该列表是否包含当前用户的帐户类型。

例如,

# For example...
$AllowedUserCastes = {
:CanLogin => ["admin", "paiduser", "crazyuser", "anonymous"],
:CanDrink => ["admin", "21yearolduser", "crazyuser"],
:CanArrest => ["admin", "police"]
}

def featureAllowed?( whichFeature )
$AllowedUserCastes[whichFeature].include? currentUserCaste()
end

关于ruby - 根据账户的 'right'打开/关闭应用功能的 'type'数据结构是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6566232/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com