Users
이 있고 각 사용자가 Projects
을 만들 수 있다고 가정합니다.Pundit 범위 지정 사용 빈 결과
Show
내 rails 컨트롤러의 동작을 관리자 또는 프로젝트 소유자 만이 Show
작업을 수행 할 수 있도록 제한하려고합니다.
제가 직면 한 문제는 아마도 Pundit에서 Scopes를 사용하는 방법에 대한 오해입니다.
내 Show
조치는 다음과 같습니다
def show
project = policy_scope(Project).find_by({id: project_params[:id]})
if project
render json: project
else
render json: { error: "Not found" }, status: :not_found
end
end
내 학자 범위 클래스는 다음과 같습니다 : 내 레일 테스트에서
class Scope < Scope
def resolve
if @user.admin?
scope.all
else
# obviously, if non-matching user id, an ActiveRelation of
# empty array would be returned and subsequent find_by(...)
# would fail causing my controller's 'else' to execute
# returning 404 instead of 403
scope.where(user_id: @user.id)
end
end
end
, 내가 받아야하는 비 프로젝트 소유자를 주장하는 것을 시도하고있다 a 403 금지됨 :
test "show project should return forbidden if non admin viewing other user's project" do
# "rex" here is not the owner of the project
get project_path(@project.id), headers: @rex_authorization_header
assert_response :forbidden
end
내 테스트에 실패했습니다. 오류가 나타납니다 :
Failure:
ProjectsControllerTest#test_show_project_should_return_forbidden_if_non_admin_viewing_other_user's_project [/Users/zhang/App_Projects/LanceKit/Rails_Project/LanceKit/test/controllers/projects_controller_test.rb:40]:
Expected response to be a <403: forbidden>, but was a <404: Not Found>.
Expected: 403
Actual: 404
나는 올바르게 Pundit을 사용하고있는 것 같지 않습니다.
동작에 policy_scope(Project)...
을 사용하는 대신 Pundit의 authorize project
을 사용해야합니까?
scope.where(...)
은 잘못된 사용자 ID를 감지하고 결과를 반환하지 않고 '이 리소스를 볼 수있는 권한이 없습니다.'라는 오류를 반환 할 것으로 예상했습니다.
또한이 Stackoverflow 게시물을 찾았습니다 : http://stackoverflow.com/questions/21172620/why-are-scope-oriented-actions-particularly-index-actions-treated-differen Rob의 대답은 범위를 사용하여 제안하는 것 같습니다. 색인/표시 작업. – Zhang