0

単一のユーザー分類にbefore_filterを使用する方法が十分に文書化されていることを考えると、複数のユーザータイプのアクションレベルの保護を取得するのに問題があります。説明させてください:

私はこのようなものを持っています...

class ApplicationController < ActionController::Base
  class << self 
    attr_accessor :standard_actions
  end
  @standard_actions = [:index, :show, :new, :edit, :create, :update, :destroy]

  def require_guardian
    unless current_user and current_user.is_a?(Guardian)
      store_location
      redirect_to home_url
      return false
    end
  end

  def require_admin
    unless current_user and current_user.is_a?(Administrator)
      store_location
      redirect_to register_url
      return false
    end
  end
end

また、GuardiansControllerでは、Administratorの標準アクションのみを許可したいのですが、他のすべてのアクションにはGuardianが必要です。だから私はこれを試しました...

class GuardiansController < ApplicationController
  before_filter :require_admin, :only => ApplicationController::standard_actions
  before_filter :require_guardian, :except => ApplicationController::standard_actions
  ...
end

これは、再帰的なリダイレクトを実行することになります。より良い方法があるに違いありませんか?

4

1 に答える 1

0

OK、これは注意深く見ずに何かを見逃している別のケースです。再帰的な方法でユーザーをリダイレクトするルートを誤って設定していました。上記の解決策は、ルートを適切に設定すると問題なく機能します。

def require_guardian
  unless current_user and current_user.is_a?(Guardian)
    store_location
    # this route (home_url) sent the user to another controller which ran a before_filter sending them back here again.
    # redirect_to home_url 
    # So I changed it to a neutral route and it works great!
    redirect_to register_url
    return false
  end
end
于 2010-02-13T00:31:25.823 に答える