提交 a8f6d5c6 编写于 作者: G Guillermo Iguaran

Integrate ActiveModel::ForbiddenAttributesProtection from StrongParameters gem

上级 88500546
......@@ -34,6 +34,7 @@ module ActiveModel
autoload :Conversion
autoload :Dirty
autoload :EachValidator, 'active_model/validator'
autoload :ForbiddenAttributesProtection
autoload :Lint
autoload :MassAssignmentSecurity
autoload :Model
......
module ActiveModel
class ForbiddenAttributes < StandardError
end
module ForbiddenAttributesProtection
def sanitize_for_mass_assignment(new_attributes, options = {})
if !new_attributes.respond_to?(:permitted?) || (new_attributes.respond_to?(:permitted?) && new_attributes.permitted?)
super
else
raise ActiveModel::ForbiddenAttributes
end
end
end
end
require 'cases/helper'
require 'models/mass_assignment_specific'
class ActiveModelMassUpdateProtectionTest < ActiveSupport::TestCase
test "forbidden attributes cannot be used for mass updating" do
params = { "a" => "b" }
class << params
define_method(:permitted?) { false }
end
assert_raises(ActiveModel::ForbiddenAttributes) do
SpecialPerson.new.sanitize_for_mass_assignment(params)
end
end
test "permitted attributes can be used for mass updating" do
params = { "a" => "b" }
class << params
define_method(:permitted?) { true }
end
assert_nothing_raised do
assert_equal({ "a" => "b" },
SpecialPerson.new.sanitize_for_mass_assignment(params))
end
end
test "regular attributes should still be allowed" do
assert_nothing_raised do
assert_equal({ a: "b" },
SpecialPerson.new.sanitize_for_mass_assignment(a: "b"))
end
end
end
......@@ -20,6 +20,13 @@ class Person
public :sanitize_for_mass_assignment
end
class SpecialPerson
include ActiveModel::MassAssignmentSecurity
include ActiveModel::ForbiddenAttributesProtection
public :sanitize_for_mass_assignment
end
class Account
include ActiveModel::MassAssignmentSecurity
attr_accessible :name, :email, :as => [:default, :admin]
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册