0
멍청한 질문 시간 : 다음 요청 사양에서는 db의 첫 번째 사용자를 (첫 번째 사용자 외의 다른 사용자가) 편집 할 수 없도록합니다.이 사양의 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
내 before_filter를 무시 update_attributes합니까? 왜 안 넣을까요?
감사합니다. 나는 내 앱에 들어가서 update_attributes를 실행하는 공격자에 대해 걱정하고 있다고 생각한다. 나는 모델에서 이것을 방지하는 방법을 찾아야 만한다. 내가 나중에 PC에서 돌아 왔을 때 이것을 받아 들일 것입니다. 다시 한번 감사드립니다. – stephenmurdoch
@stephenmurdoch "내부"앱? –
@Dave_Newton 여기서 복잡한 일을했습니다. 나는 내 앱에 커맨드 라인 액세스 권한을 가진 사람이 (내가 heroku 콘솔을 실행하는 동안 내 PC를 해킹했다고) 첫 번째 사용자 (내 계정)를 수정할 수 있으며, 어떻게해서든지 update_attributes '그 기록에. 이 일을 시도 할 때 어떤 점이 있습니까? 이 작업을 수행 할 수 있습니까 아니면 그러한 종류의 액세스 권한을 가진 공격자가 막을 수 있습니까? – stephenmurdoch