0
愚蠢的问题时间:在下面的请求规范中,我尝试确保我的分区中的第一个用户不能被编辑(除了第一个用户以外的任何人)。本规范中put和update_attributes之间的区别
# user is not logged in during these tests
# variant 1 - this passes
describe "first user" do
let(:first_user){ FactoryGirl.create(:admin) }
# use put to modify the user
before { put user_path(first_user, email: '[email protected]') }
# this passes, the response is a redirect
specify { response.should redirect_to(root_path) }
end
# variant 2 - this test fails
describe "first user" do
let(:first_user){ FactoryGirl.create(:admin) }
# this fails, email is updated
it "can't be updated or edited" do
expect do
first_user.update_attributes(email: '[email protected]')
end.not_to change(first_user.reload, :email)
end
end
这两个测试似乎做同样的事情,但一个失败,一个通过。我想我的理解很糟糕。应该update_attributes方法,如称为失败的测试,调用我的控制器的过滤器之前:
# users_controller.rb
before_filter correct_user, only: [:edit, :update]
private
# pretty messy, but ensures that ordinary users can only
# edit their own accounts, that admin users can
# edit all accounts, except for the first one.
# I believe it also ensures that the first_user
# can only be edited by the owner of the first account, i.e. me
# due to the fact that the first condition of the `unless` clause will pass
# if the current_user is the first_user. The complexity is necessary to prevent
# other admins, from being able to edit the first_user.
def correct_user
@user=User.find(params[:id])
redirect_to(root_path, only_path: true) unless current_user?(@user) || (current_user.admin? && !first_user?(@user))
end
def first_user?(user)
user==User.first
end
是否忽略的update_attributes我的before_filter?为什么不放?
谢谢,我现在看到。我想我担心攻击者进入我的应用程序并运行update_attributes。那么我必须在模型中找到防止这种情况的方法。稍后当我回到PC时,会接受这一点。再次感谢。 – stephenmurdoch
@stephenmurdoch“Inside”你的应用程序? –
@Dave_Newton我在这里复杂的东西。我担心有人可以通过命令行访问我的应用程序(比如在我运行heroku控制台时黑客入侵了我的电脑),可以修改第一个用户(我的帐户),并且可以通过禁用'update_attributes在那个记录上。试图做到这一点有什么意义吗?可以这样做吗?或者是否会让具有这种访问权限的攻击者无法阻止? – stephenmurdoch