我目前正在为Ruby on Rails 5应用程序中的客户开发徽章系统。我不想将每个徽章硬编码到系统中,而我发现eval()
是一种具有危险后果的方法。在Rails 5中为徽章系统取得了eval的条目
这是我目前的策略:
应用程序/模型/ user.rb
class User
has_many :awards, dependent: :destroy
end
应用程序/模型/ badge.rb
class Badge
has_many :awards, dependent: :destroy
# name - string - Name of the badge
# icon - string - URL of badge icon
# description - text - Long text description of the badge
# criterion - text - boolean operation to eval() to earn badge
end
应用程序/模型/ award.rb
class Award
belongs_to :user
belongs_to :badge
def self.automatic_award
User.find_each do |user|
Badge.find_each do |badge|
# If the badge has not been awarded to the user
if Award.where(user: user, badge: badge).blank?
# If the badge criteria is met
if eval(badge.criterion.untaint)
Award.create(user: user, badge: badge)
# Add method to notify user of award.
end
end
end
end
end
end
我会写一个工作人员定期运行Award.automatic_award方法(每10分钟?)徽章将由管理员在登录安全(不是标准用户)后编写但我担心安全性和受污染条目。
我错过了什么?有没有更好的办法?