我目前正在爲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分鐘?)徽章將由管理員在登錄安全(不是標準用戶)後編寫但我擔心安全性和受污染條目。
我錯過了什麼?有沒有更好的辦法?