Desive 로그인 후 처음으로 리소스에 액세스하려는 경우 Receive CanCan::AccessDenied 오류

12835 단어 cancan

묘사

애플리케이션에서 다음과 같은 기능을 사용했습니다.
Ruby 2.0.0
Rails 4.0.1
캄캄 1.6.10
디자인 3.2.0
Desive 로그인 후 처음으로 CanCan으로 보호된 리소스에 액세스하려고 하면 CanCan::AccessDenied 오류가 발생합니다.페이지를 다시 불러오면 정상적으로 작동할 수 있습니다.나의 테스트에서 나는 같은 행동을 보았다.CanCan으로 보호된 리소스에 액세스하려는 첫 번째 테스트는 항상 실패합니다.

토론 #1

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  check_authorization unless: :devise_controller?
  before_filter :authenticate_user!, unless: :devise_controller?

  # rescue_from CanCan::AccessDenied do |exception|
  #   redirect_to root_url, :alert => exception.message
  # end
end
다음은 나의 테스트이다.
require 'spec_helper'

describe UsersController do

  before (:each) do
    @request.env["devise.mapping"] = Devise.mappings[:admin_user]
    sign_in FactoryGirl.create(:admin_user)

    @user = FactoryGirl.create(:user)
  end

  def valid_session
    {"warden.user.user.key" => session["warden.user.user.key"]}
  end

  describe "#show" do
    context 'when logged in as admin and given a user' do

      it "displays the show page" do
        get :show, { :id => @user.id }, valid_session
        response.should be_success
      end

      it "finds the right user" do
        get :show, { :id => @user.id }, valid_session
        assigns(:user).should == @user
      end

    end
  end

end
여기 출력은 - 백트랙이 열었어요"
UsersController
  #show
    when logged in as admin and given a user
      displays the show page (FAILED - 1)
      finds the right user

Failures:

  1) UsersController#show when logged in as admin and given a user displays the show page
     Failure/Error: get :show, { :id => @user.id }, valid_session
     CanCan::AccessDenied:
       You are not authorized to access this page.
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/cancan-1.6.10/lib/cancan/ability.rb:208:in `authorize!'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/cancan-1.6.10/lib/cancan/controller_additions.rb:338:in `authorize!'
     # ./app/controllers/users_controller.rb:16:in `show'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/actionpack-4.0.1/lib/action_controller/metal/implicit_render.rb:4:in `send_action'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/actionpack-4.0.1/lib/abstract_controller/base.rb:189:in `process_action'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/actionpack-4.0.1/lib/action_controller/metal/rendering.rb:10:in `process_action'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/actionpack-4.0.1/lib/abstract_controller/callbacks.rb:18:in `block in process_action'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/activesupport-4.0.1/lib/active_support/callbacks.rb:423:in `_run__1383360963607712464__process_action__callbacks'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/activesupport-4.0.1/lib/active_support/callbacks.rb:80:in `run_callbacks'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/actionpack-4.0.1/lib/abstract_controller/callbacks.rb:17:in `process_action'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/actionpack-4.0.1/lib/action_controller/metal/rescue.rb:29:in `process_action'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/actionpack-4.0.1/lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/activesupport-4.0.1/lib/active_support/notifications.rb:159:in `block in instrument'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/activesupport-4.0.1/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/activesupport-4.0.1/lib/active_support/notifications.rb:159:in `instrument'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/actionpack-4.0.1/lib/action_controller/metal/instrumentation.rb:30:in `process_action'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/actionpack-4.0.1/lib/action_controller/metal/params_wrapper.rb:245:in `process_action'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/activerecord-4.0.1/lib/active_record/railties/controller_runtime.rb:18:in `process_action'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/actionpack-4.0.1/lib/abstract_controller/base.rb:136:in `process'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/actionpack-4.0.1/lib/abstract_controller/rendering.rb:44:in `process'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/actionpack-4.0.1/lib/action_controller/test_case.rb:569:in `process'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/actionpack-4.0.1/lib/action_controller/test_case.rb:64:in `process'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/devise-3.2.0/lib/devise/test_helpers.rb:19:in `block in process'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/devise-3.2.0/lib/devise/test_helpers.rb:72:in `catch'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/devise-3.2.0/lib/devise/test_helpers.rb:72:in `_catch_warden'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/devise-3.2.0/lib/devise/test_helpers.rb:19:in `process'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/actionpack-4.0.1/lib/action_controller/test_case.rb:469:in `get'
     # ./spec/controllers/users_controller_spec.rb:20:in `block (4 levels) in <top (required)>'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/rspec-core-2.14.7/lib/rspec/core/example.rb:114:in `instance_eval'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/rspec-core-2.14.7/lib/rspec/core/example.rb:114:in `block in run'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/rspec-core-2.14.7/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_exec'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/rspec-core-2.14.7/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_eval_with_args'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/rspec-core-2.14.7/lib/rspec/core/example.rb:247:in `instance_eval_with_args'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/rspec-core-2.14.7/lib/rspec/core/hooks.rb:106:in `block (2 levels) in run'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/rspec-core-2.14.7/lib/rspec/core/example.rb:179:in `call'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/rspec-core-2.14.7/lib/rspec/core/example.rb:179:in `run'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/rspec-core-2.14.7/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_exec'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/rspec-core-2.14.7/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_eval_with_args'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/rspec-core-2.14.7/lib/rspec/core/example.rb:247:in `instance_eval_with_args'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/rspec-core-2.14.7/lib/rspec/core/hooks.rb:106:in `block (2 levels) in run'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/rspec-core-2.14.7/lib/rspec/core/hooks.rb:108:in `call'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/rspec-core-2.14.7/lib/rspec/core/hooks.rb:108:in `run'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/rspec-core-2.14.7/lib/rspec/core/hooks.rb:446:in `run_hook'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/rspec-core-2.14.7/lib/rspec/core/example_group.rb:340:in `run_around_each_hooks'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/rspec-core-2.14.7/lib/rspec/core/example.rb:256:in `with_around_each_hooks'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/rspec-core-2.14.7/lib/rspec/core/example.rb:111:in `run'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/rspec-core-2.14.7/lib/rspec/core/example_group.rb:390:in `block in run_examples'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/rspec-core-2.14.7/lib/rspec/core/example_group.rb:386:in `map'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/rspec-core-2.14.7/lib/rspec/core/example_group.rb:386:in `run_examples'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/rspec-core-2.14.7/lib/rspec/core/example_group.rb:371:in `run'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/rspec-core-2.14.7/lib/rspec/core/example_group.rb:372:in `block in run'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/rspec-core-2.14.7/lib/rspec/core/example_group.rb:372:in `map'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/rspec-core-2.14.7/lib/rspec/core/example_group.rb:372:in `run'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/rspec-core-2.14.7/lib/rspec/core/example_group.rb:372:in `block in run'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/rspec-core-2.14.7/lib/rspec/core/example_group.rb:372:in `map'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/rspec-core-2.14.7/lib/rspec/core/example_group.rb:372:in `run'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/rspec-core-2.14.7/lib/rspec/core/command_line.rb:28:in `block (2 levels) in run'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/rspec-core-2.14.7/lib/rspec/core/command_line.rb:28:in `map'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/rspec-core-2.14.7/lib/rspec/core/command_line.rb:28:in `block in run'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/rspec-core-2.14.7/lib/rspec/core/reporter.rb:58:in `report'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/rspec-core-2.14.7/lib/rspec/core/command_line.rb:25:in `run'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/rspec-core-2.14.7/lib/rspec/core/runner.rb:80:in `run'
     # /Users/jeffb/.rvm/gems/ruby-2.0.0-p195@backoffice/gems/rspec-core-2.14.7/lib/rspec/core/runner.rb:17:in `block in autorun'

토론 #2

그래서 저는 더 좋은 오류gem을 추가했습니다. 제 응용 프로그램 컨트롤러에 CanCan에서 오류를 제거하는 방법을 설명했고 사용자 컨트롤러에 고장점을 추가했습니다. 아래와 같습니다.

  # GET /users/1
  # GET /users/1.json
  def show
    @user.unknown_method
  end
로그인한 후/users/1에 처음 접속했을 때 CanCan::AccessDenied 예외가 발생했습니다. 몇 가지 인스턴스 변수는 다음과 같습니다.
@_authorized =
true
@current_user = 
#<User id: 1, email: "[email protected]", encrypted_password: "$2a$10$X5A4y.cc9tD3kEfTdVkvge3ipN74JRhcQjafhLtj5oeg...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 24, current_sign_in_at: "2013-11-11 17:08:46", last_sign_in_at: "2013-11-11 16:40:07", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", created_at: "2013-11-06 16:50:11", updated_at: "2013-11-11 17:08:46">
@current_ability     =
#<Ability:0x007fc805150b88 @rules=[], @aliased_actions={:read=>[:index, :show], :create=>[:new], :update=>[:edit]}>
페이지를 다시 불러올 때 @user의 예상 위치에서 실패했습니다.알 수 없는\u 메서드이지만 현재\u ability 인스턴스 변수는 다음과 같습니다.
@current_ability =
#<Ability:0x007fc805fb2e10 @rules=[#<CanCan::Rule:0x007fc8071b2390 @match_all=false, @base_behavior=true, @actions=[:manage], @subjects=[:all], @conditions={}, @block=nil, @expanded_actions=[:manage]>, #<CanCan::Rule:0x007fc8071b1df0 @match_all=false, @base_behavior=true, @actions=[:read], @subjects=[:all], @conditions={}, @block=nil, @expanded_actions=[:read, :index, :show]>], @aliased_actions={:read=>[:index, :show], :create=>[:new], :update=>[:edit]}>
따라서 처음 액세스할 때 사용자의 현재 기능에 대한 정의가 없는 것 같습니다.이게 내가 뭘 잘못했는지 칸칸의 잘못인지 모르겠다.무슨 생각 있어요?나는 내가 바보 같다고 생각한다.보아하니 문제는 나의 능력 수업에서 나온 것 같다.

class Ability
  include CanCan::Ability

  def initialize(user)
    def initialize(user)
      user ||= User.new # guest user

      if user.role? :admin
        can :manage, :all
      end
      can :read, :all
    end  
  end
end
멍청이가 어떻게 이럴 수가 있어?

토론 #셋

저를 위해 일해 주세요.고맙습니다, 점원!

좋은 웹페이지 즐겨찾기